From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 12334 invoked from network); 7 Dec 2022 17:23:53 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 7 Dec 2022 17:23:53 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1670433833; b=na4mWmkSv6ZhMhieqdT/ECeZqkOgpWSkK8yh9xbdddVYgLlWK1q6qhS7WPB9XcfiTXU21Wwnhk 7kaeEe3neczaw7CT0AlG6eO2/vuxSfZ7Yr5lsYFoKQBHSZoLMClWWj1o6XEktbo12bGQSrJGCZ ivApCREfoDZg9H7Q1Qg1WCtVd7zYuhGuGSW7rHjnoPnE8myYDdmjO3mEQCBbYzA4CyiabFLWq0 tYDV1DSvNW9tBUB1RSBSjqCsXJE+xw1jmE4Q9UURlGy49Y4bZu5hwhsDgJGlXIATAbhMdYxT+w wOmXOj3iLip0lUTWDpEEiSxDV0qI0XijooeyY8YbpB6Mwg==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (out4-smtp.messagingengine.com) smtp.remote-ip=66.111.4.28; dkim=pass header.d=greenberg.science header.s=fm2 header.a=rsa-sha256; dkim=pass header.d=messagingengine.com header.s=fm1 header.a=rsa-sha256; dmarc=none header.from=greenberg.science; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1670433833; bh=87NW8nSpvjlGTNOjbPJ87yKXU6bBtk1MEu4K8lw7py8=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:MIME-Version:Message-ID:Date:References: In-Reply-To:Subject:Cc:To:From:DKIM-Signature:DKIM-Signature:DKIM-Signature; b=EKbq2typcN5rVcpPw779wfhDTPlUJKb4cU0Qq3H+i/uB6OQQJhX/USngmZnmMXFyZFLQCJ+mBC amZTLuMy0NScPamsQCyO6RIhAFt4/UL6jpfHcBxrrWw2F9oR2M3ru3IZh3VLYHVKQtEZtMoh/9 2C8hvKbI9tqIdmlZKecTUZjqT/Ej14Uxo5SpiV8YxvezM+0R77bvvcqiEO06zmsnNwut3dvcV3 OGW9R+d4UyXR3BS9ytRXK+AhNcOcLduQ9U5F4G8T5MAFeMNTm4+fhYmtj3igoz4lJGRWz7jmdT XvO50xuX4Fv+vi/To2iUTlvqAQ4m/sRlhr45t2LwvZ+hdg==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:MIME-Version:Message-ID: Date:References:In-Reply-To:Subject:Cc:To:From:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=0H2UpVulDFPjZkj3wyWUHCdpdmh0M6k8vaXYUEGkkLU=; b=aQOOWELOjIRgesYoIesLLe3RSC DV8Dii01oJhHWw3F5Bk0bUZ0cDYWmlp0BfG1/e61NQXef1Bw18s5sm73meUD2nA4d3I8VylwtlW6T wb4EPMab8eZuF3cH1z3Fb7OO+5B2KWpekVVymhxCk4FRL+CQsxRgM3H5EA7wa1k39RpTTTSBuBBSW BOE44pTDmfXnw1ydn/pCjGZCl/F9ZZ5dSjWnBiaUfNOaTrPjnfmWdYcfPnHBiqkBBx8cA1kDcK7+1 tEvUHhAMhdFwKbJr2iYO9GZlE6S4O7VELAbXh6/1WXkMuEmTXxwMYRDeyL+aF2mO08yfma7ukFRH0 iECm0npg==; Received: by zero.zsh.org with local id 1p2y9A-0004XU-UU; Wed, 07 Dec 2022 17:23:52 +0000 Authentication-Results: zsh.org; iprev=pass (out4-smtp.messagingengine.com) smtp.remote-ip=66.111.4.28; dkim=pass header.d=greenberg.science header.s=fm2 header.a=rsa-sha256; dkim=pass header.d=messagingengine.com header.s=fm1 header.a=rsa-sha256; dmarc=none header.from=greenberg.science; arc=none Received: from out4-smtp.messagingengine.com ([66.111.4.28]:55491) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1p2xwf-0003en-VT; Wed, 07 Dec 2022 17:11:01 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id D00BD5C007B; Wed, 7 Dec 2022 12:10:56 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Wed, 07 Dec 2022 12:10:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= greenberg.science; h=cc:cc:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1670433056; x= 1670519456; bh=0H2UpVulDFPjZkj3wyWUHCdpdmh0M6k8vaXYUEGkkLU=; b=t 44b+AsKt47gr0vKzIuhTxNWaRiCOWIWFo1FkSoRBY97G8UP8TivuNuaBPyUhWYfB u9dhuyCER+AOIoeOCeYfq0EJILx8Zlbat7+N3pSlLQ34hGuAmUgXqCq7J5nadvj0 qpoHzH03tiia+giPHdAaKu7MUJCMkzTXE5rWOo/uN4XGNcOVQLEQa1am7oRkfnS7 u9gUfMjJhHleuIFYD0euFbeLhursmbHMqwSYirI00VqcYOsdxz6PFlxm98pa85uj /vu9RwZm4wSQBC5v8baJZ1iZV+ezLIFABQnc937fAIZTPWoSqC5Td+4aWndEsFgn pcghxZERgkURwpYm86pOA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:date:date:feedback-id :feedback-id:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1670433056; x=1670519456; bh=0H2UpVulDFPjZkj3wyWUHCdpdmh0 M6k8vaXYUEGkkLU=; b=asuQezgnEQCmFMXz7Ho1ICmjWx+37TFQLTb8hUKKnt5X yEP4+hkQoxRb8idjnLp/ABRJByq8BYax11KjfHjRpRwjWyb5R8WQYVxS0APabeyR vt8iAbnmbAl0NzsYLZ7x4yxxjCtbTsXkiEcPCBw8Onw0Pjg7LW+yvdiGR49lZ+E0 Uk5Ap86EwKW0qLm3z1n3xPApjsUHhEYoNCTWvlYPrBIWzmAl8G2puRafNPAPgGbR SJInkBZUUXQSovrMTPZ6N6edu8CZmvQGRnbgCEU3L4OdMZMaY0RE08gsSuhiMNwd UoqXO1SLe54UGtpNieqOOou3WfDQXr31V5q5PRqGdg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudekgdelkecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenog fuuhhsphgvtghtffhomhgrihhnucdlgeelmdenucfjughrpefhvfevufgjfhffkfggtges thdtredttddttdenucfhrhhomhepofhitghhrggvlhcuifhrvggvnhgsvghrghcuoehmih gthhgrvghlsehgrhgvvghnsggvrhhgrdhstghivghntggvqeenucggtffrrghtthgvrhhn peeuieejlefhvdegleehtedvfeetvdekjeegvefhueehvdeuiefhueeiieelkeeltdenuc ffohhmrghinhepiihshhdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgr mhepmhgrihhlfhhrohhmpehmihgthhgrvghlsehgrhgvvghnsggvrhhgrdhstghivghntg gv X-ME-Proxy: Feedback-ID: ib2794684:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 7 Dec 2022 12:10:56 -0500 (EST) From: Michael Greenberg To: Stephane Chazelas Cc: Bart Schaefer , zsh-workers@zsh.org Subject: Re: `return` does not behave properly under `!` In-Reply-To: <20221207160113.ol3lcbm6p7xu3rru@chazelas.org> References: <20221207084255.ykkg6cgzn4nqc73f@chazelas.org> <20221207160113.ol3lcbm6p7xu3rru@chazelas.org> Date: Wed, 07 Dec 2022 12:10:55 -0500 Message-ID: MIME-Version: 1.0 Content-Type: text/plain X-Seq: 51124 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: On 2022-12-07 at 04:01:13 PM, Stephane Chazelas wrote: > 2022-12-07 10:05:34 -0500, Michael Greenberg: >> Hi Stephane and Bart (reply inline below, as it's relevant to Stephane), > [...] >> So my reading is the opposite: `return` doesn't have an exit status, >> because it causes the currently executing function (or sourced script) >> to terminate; it does, however, set $?. So `!` shouldn't affect it. > [...] > > Agreed. > >> But even without a close reading of the spec, it's just not how early >> return works in any language. Having the evaluation context beyond >> `return` affect the returned value subverts the early and immediate >> return---and, in so doing, invites bugs. > [...] > > Agreed. > >> So far, dash, yash, and zsh are the only shells I've found with this >> behavior, and the dash folks seem to agree that it's an undesirable >> bug. And also... > > Also bosh. > > I can't see why one would call ! return if not to expect that to > have an effect on the exit status. > > $ zsh -c 'f() { ! { false || return; }; }; f; echo "$?"' > 0 > > Is more of a problem though and I agree would be an undesirable bug.. Like in your example, I think the pernicious case is when the ! and the return are far apart. The scenario I imagine is something like: f() { while ! { SOME_CONDITION_FOR_EARLY_EXIT && return 1 SOME_CONDITION_FOR_TERMINATION_WITH_POSTPROCESSING } do ... done ... } In process of running the loop's predicate, the script might realize that it's just time to exit the function early (without postprocessing). The outer negation in the predicate is meant to negate the whole predicate, not the return. It would be surprising to have that distant `!` affect the return (and having it negate the status would make it impossible to return statuses other than 0 and 1 in this form). > > [...] >> Ah---thank you! I wish there were a straightforward fulltext search of >> the archives (though I don't know I would have found this anyway, as it >> doesn't explicitly mention `return`). I'll try to test these patches to >> confirm. > > You can download and rsync the full archive from > rsync://rsync.zsh.org/mla/zsh-workers/ Oh---I didn't realize this rsync interface existed, thank you! Might be good to document it on https://www.zsh.org/mla/. >> That seems hardly relevant here: a return explicitly sets $?, unlike >> break/continue (which both have exit statuses, unlike return). > [...] > > They're still special builtins that break the flow, so I would > still consider it relevant. I've not checked zsh recently but > there are funny things you can do with some shells by calling > break/continue in a function where the corresponding "enclosing" > loop is in one of the callers. IIRC there was some POSIX > discussion about that and the text tightened. Yes, non-lexical break is a mess, with varying treatment in popular shells (e.g., bash and dash disagree). Having ! affect return from a distance amounts to a form of non-lexical return munging. As someone who studies programming languages for a living, I would very much advise against non-lexical control if we can avoid it. :) I think there's a good opportunity to have shells in alignment here (and maybe make POSIX more explicit): if zsh folks agree that return should not be affected by an enclosing !, we can align bash/dash/zsh. (I'm hopeful yash folks will agree, too.) Cheers, Michael