zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane@chazelas.org>
To: Michael Greenberg <michael@greenberg.science>
Cc: Bart Schaefer <schaefer@brasslantern.com>, zsh-workers@zsh.org
Subject: Re: `return` does not behave properly under `!`
Date: Wed, 7 Dec 2022 16:01:13 +0000	[thread overview]
Message-ID: <20221207160113.ol3lcbm6p7xu3rru@chazelas.org> (raw)
In-Reply-To: <m2o7sfp83l.fsf@greenberg.science>

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..

[...]
> 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/

And search in there.

$ rsync rsync.zsh.org::
========================================================
ZSH FTP primary site (rsync.zsh.org)

Example usage:
rsync -av --delete rsync://rsync.zsh.org/pub /local/path/to/zsh/pub

Always do a dry run (-n) first or --delete may bite you!
========================================================
Modules:
========================================================


pub             Zsh FTP Pub directory (~280MB as of Feb 2010)
mla             Zsh Mailing List Archive directory (~333MB as of Feb 2010)


> > 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).
[...]

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.

-- 
Stephane


  reply	other threads:[~2022-12-07 16:01 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-06 18:18 Michael Greenberg
2022-12-07  4:53 ` Bart Schaefer
2022-12-07  8:42 ` Stephane Chazelas
2022-12-07 15:05   ` Michael Greenberg
2022-12-07 16:01     ` Stephane Chazelas [this message]
2022-12-07 16:16       ` Peter Stephenson
2022-12-07 17:06         ` Bart Schaefer
2022-12-07 17:21           ` Peter Stephenson
2022-12-07 18:51             ` Bart Schaefer
2022-12-07 17:10       ` Michael Greenberg
2022-12-09 15:22         ` Michael Greenberg
2022-12-09 16:21           ` Bart Schaefer
2022-12-09 19:20             ` Michael Greenberg
2022-12-07 16:08     ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221207160113.ol3lcbm6p7xu3rru@chazelas.org \
    --to=stephane@chazelas.org \
    --cc=michael@greenberg.science \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).