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 28556 invoked from network); 7 Dec 2022 15:06:14 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 7 Dec 2022 15:06:14 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1670425574; b=ISesxG88oQUKjukZeJC6b30OdYYGATEGA8R7fETq6runaxaH1w7x3xbkuKCxthohrl6M4wv/dA 7w+b2kKy0WWYkZbE0uCGPj7rsOpj7Wf8a2rLkXcHiD4XUXocdK0SdrvU0rER6YqDmG8vKHzxGV CTyABi2ywD+f5M2o/p7MwdUCOKMAD8gdGJSB4IBkju71wbPAZuLdWkQr1bVvGge/hpurN4TXzs Z7Ts8av9UjvGExHpHAn2sTN6DPG/4tPWMLlriz55V7imZPwFC6M4WgzV5e055DKbcroeVbJ3a4 lp4JaisgpZ6sBbAPT2w2c6fDMr4BJRWkjJ0VDJ0LidNX2w==; 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=1670425574; bh=RaC7uM/8uQqMsrPt3yRUtUuztre6KlUOTQMZyfRh/KU=; 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=qZ5n7zb+U+s7jvLeIu63FnJNqVeft0SRuQnPMxmjpOfSn5JEALEWebJ+oHJJty/WaVvNSjqG0f Zk2rUc3kPIcKaY5Wl5EYfz5aNMqiw/pfZfsUMFwlQd8+m4n9DfpCOiTQ8647MHUHlUpgi6bh69 348lbP4pymOC6IveJiPB8bippDpCUPBBeYjtKlu3uNNcoJbVBmy9qftId7pkbHNMM7WIEKfCxo nbgh+JEUtbMkJSUqLmdTdCVJIksIF9Iyra528w47ebQS6he0mA/qlo2LPzNa9sJyi2aUODQTfQ BEzR5cQwW1d6l0lqfoO2euWFRJb2yAWRqfDt8eRE5LrJ1g==; 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=PN+qiWsEMaRH0q46lrOMWNWRbpAwsy+HC09TgbyJ8cc=; b=JwKeFUGeZVJ7AJHCyFdy4aZ2/x jtBcfNGdm4RkbKee8kM4hN4WY+Lnf1LH7NNaybwri/v1pVTvmF23NPNXNT1Hm/SvSIlC6e46TxmsP MXsgUGRgS3/iNV6cxyZp45toXwxO69Xf5bAIFG98oCb2ghxlU2d0Cgv6Zok8BsnTYvC1bSlK7o7ee OKY84z+auoggGxkyRMicPalL0FTv9pL3WtHHeql2rA97uxMRrBI4ZCKjl0etb0gsyAcW71p1KYfUV B5KIr7l+1zhAG1MAswWiTjW/UUEU+GPktGfX3P2Yri655dVMDFoSqYCnXMU0IXm6pTb1gTr4bXNgr naTYdmQA==; Received: by zero.zsh.org with local id 1p2vzx-000KIh-Qz; Wed, 07 Dec 2022 15:06:13 +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]:56931) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1p2vzM-000K0V-GM; Wed, 07 Dec 2022 15:05:39 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 3EFC75C0162; Wed, 7 Dec 2022 10:05:35 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Wed, 07 Dec 2022 10:05:35 -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=1670425535; x= 1670511935; bh=PN+qiWsEMaRH0q46lrOMWNWRbpAwsy+HC09TgbyJ8cc=; b=s kDSGwgUmCsujKaVWVYGT/rkwQ4XaZH84BmWJBY2gt6UqCHf7CXfjPl7ten0OiAZN tcqP+SRPFcTCnPsYh5HW82o+LGHX9jGXozUpE15751LKs3z5UVQPCIPja1KYckJM 45r0iH94+M62I5pwG+r7U43ks2uD9iJvO/Yho1KCYKWH6/EKNycn2wOF/cRopeaP 1UaPbb9DB0IJy25OsTUX7bNVqrQYvGJcLrfffhw9jbN2nliwFpWs2+xwmPIS8/p1 FgfSEGQozVX2H7GuRX4SPDboWrGvX4FZcqCAJpF1EfAJXO3sWLjs4OsjLmIqrAf7 Q+gI+uNZ9C9em8mHFxy7w== 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=1670425535; x=1670511935; bh=PN+qiWsEMaRH0q46lrOMWNWRbpAw sy+HC09TgbyJ8cc=; b=bLplzhU0iLO3p+g4PC72aGxIjmbktGdtFrgCqW2Z+mP3 xPnIX7BbLDLh5eFcJQzqvHHc8C4ANaPLW6Iw/6YUpCTSPdbstaAc3YPXu+iHDRH4 vOTXRtRWII6wCfw031321+Pak+v0t4hQD51yGIbry5VMldvWuFLfQOTY6rDI/l0D jx9EbmANmX63fF1alLkkSNkFeFr8tFLWlFsqQgjzPMmfiKQtFzVuQbA2b+VuwADg zDC2iipPRgYvm/B0JhiaSl56I9otQvWCqY/Mr8PyWqbXAlBP7tGd77g35aPlsvPw OvDX4f+h5MVsH+tLBGGYm8NQdc4lsNoV8H526k1/4w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudekgdejvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvfevufgjfhffkfggtgesthdtredttddttdenucfhrhhomhepofhitghhrggv lhcuifhrvggvnhgsvghrghcuoehmihgthhgrvghlsehgrhgvvghnsggvrhhgrdhstghivg hntggvqeenucggtffrrghtthgvrhhnpeejvdehkeeuueffleeivdejffdtgfelheffffdu gedvgedvheegveeilefgkeetgeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpehmihgthhgrvghlsehgrhgvvghnsggvrhhgrdhstghivghntggv X-ME-Proxy: Feedback-ID: ib2794684:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 7 Dec 2022 10:05:34 -0500 (EST) From: Michael Greenberg To: Stephane Chazelas , Bart Schaefer Cc: zsh-workers@zsh.org Subject: Re: `return` does not behave properly under `!` In-Reply-To: <20221207084255.ykkg6cgzn4nqc73f@chazelas.org> References: <20221207084255.ykkg6cgzn4nqc73f@chazelas.org> Date: Wed, 07 Dec 2022 10:05:34 -0500 Message-ID: MIME-Version: 1.0 Content-Type: text/plain X-Seq: 51112 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: Hi Stephane and Bart (reply inline below, as it's relevant to Stephane), On 2022-12-07 at 08:42:55 AM, Stephane Chazelas wrote: > 2022-12-06 13:18:59 -0500, Michael Greenberg: > [...] >> i() { >> ! return 9 >> echo fail not >> } >> i >> echo $? >> ``` > [...] >> 0 > [...] > > That: > > ! return n > > Behaves like return !n (and ! return behave like return !$? (or > return "$(( ! $? ))" in POSIX syntax) doesn't sound totally off > to me. As written, I would say the POSIX spec may even be > requiring it. Here's what the POSIX spec says about `return`: The return utility shall cause the shell to stop executing the current function or dot script. If the shell is not currently executing a function or dot script, the results are unspecified. ... EXIT STATUS The value of the special parameter '?' shall be set to n, an unsigned decimal integer, or to the exit status of the last command executed if n is not specified. If n is not an unsigned decimal integer, or is greater than 255, the results are unspecified. When return is executed in a trap action, the last command is considered to be the command that executed immediately preceding the trap action. And here's what it says about `!` on pipelines: If the pipeline does not begin with the ! reserved word, the exit status shall be the exit status of the last command specified in the pipeline. Otherwise, the exit status shall be the logical NOT of the exit status of the last command. That is, if the last command returns zero, the exit status shall be 1; if the last command returns greater than zero, the exit status shall be zero. 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. 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. 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... On 2022-12-06 at 08:53:25 PM, Bart Schaefer wrote: > On Tue, Dec 6, 2022 at 10:19 AM Michael Greenberg > wrote: >> >> Following up an example from Harald van Dijk on the dash mailing list, I >> discovered that zsh does not `return` properly under a `!`. > > This appears to have been fixed by Philippe Altherr's series of > patches for ERR_EXIT et al, zsh-workers 51001, 51071, 51076. 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. > That's consistent with ! break causing the while/until loop it's > in returning with failure which I've seen code rely upon (AFAIK, > that's the only way to get those loops to return with failure; > doesn't work in pdksh and derivatives though). That seems hardly relevant here: a return explicitly sets $?, unlike break/continue (which both have exit statuses, unlike return). Cheers, Michael