zsh-workers
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: Radon Rosborough <radon.neon@gmail.com>, zsh-workers@zsh.org
Subject: Re: Question about err_return
Date: Mon, 21 Aug 2017 06:27:12 +0000	[thread overview]
Message-ID: <1503296832.2707476.1079657368.3BDF7CBD@webmail.messagingengine.com> (raw)
In-Reply-To: <CADB4rJG_ajfBdeC+Cd4YHDhi2pXRr3j7tCXKrV_AvT6+Y7_U7A@mail.gmail.com>

Radon Rosborough wrote on Sun, 20 Aug 2017 22:28 -0700:
> why does the following:
> 
>     function { setopt err_return; false; echo 'oh no' }
> 
> print nothing, while
> 
>     function { setopt err_return; false; echo 'oh no' } && true
> 
> prints 'oh no'? This seems very inconsistent to me, as I would expect
> the result of 'x && true' to be the same as 'x' in all circumstances.
> 
> Now I found an old thread [1] about this, and the resolution was that
> this behavior was unsurprising since err_return emulates err_exit. The
> idea is that err_exit is automatically disabled for all child code
> whenever a function is invoked as part of a conditional expression,
> and so err_return does the same.

I'm having trouble seeing how this is not simply a bug.  Consider:
.
    % f() { setopt err_return; false; echo 'this runs' }
    % f && :
    this runs

The command "false" had a non-zero exit status, and the enclosing function
continued to execute.  That's exactly what the documentation promises would
not happen:
.
       ERR_RETURN
              If a command has a non-zero exit status, return immediately from
              the enclosing function.

I don't see how the fact that f's callsite is the left-hand side of a '&&'
is relevant here.  It would be relevant to deciding whether f's non-zero
return value should cause the _caller_ to return/exit immediately as well,
but that has to do with the ERR_EXIT / ERR_RETURN settings in the caller
scope, and shouldn't change f's behaviour.  (The invariant you proposed —
«foo» ≡ «foo && true» — is a good one.)

The analogy with ERR_EXIT's treatment of '&&'/'||' should kick in when those
oprators are used _inside_ the function:
.
    % f() { setopt err_return; false || true; echo 'this should be printed' }

The callee function behaves the same way if err_return is set in the caller, as
Bart suggested in the original thread.

> But we have an err_return in Zsh. Is it worth breaking backward
> compatibility to improve the usability of err_return?

Well, I'm certainly not suggesting to change this before 5.4.2 (which is
expected soon).  But we could change this after 5.4.2 and let it soak in master
until 5.5.  Thoughts?

> If not, I would like to know how I can otherwise implement error
> checking in my scripts. I need for any unexpected error to cause an
> immediate return from the enclosing function, which is how err_return
> is advertised in the Zsh manual [3], but not how it actually works at
> present.

Yes, the documentation doesn't match the implementation, so we need to fix one
or the other.

Thanks for the report, and for digging up those old threads.

Cheers,

Daniel


  reply	other threads:[~2017-08-21  6:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-21  5:28 Radon Rosborough
2017-08-21  6:27 ` Daniel Shahaf [this message]
2017-08-23 16:56 ` Peter Stephenson
2017-08-23 20:09   ` Peter Stephenson
2017-08-24 10:33     ` Peter Stephenson

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=1503296832.2707476.1079657368.3BDF7CBD@webmail.messagingengine.com \
    --to=d.s@daniel.shahaf.name \
    --cc=radon.neon@gmail.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).