zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: "break" and functions
Date: Fri, 29 Mar 2024 11:51:03 -0700	[thread overview]
Message-ID: <CAH+w=7acsf9YYrOeS6GPWSffm4p4=-rU6YONsBAhJg20=S1b7Q@mail.gmail.com> (raw)
In-Reply-To: <43539-1711733926.063834@011r.oEGd.QZp_>

On Fri, Mar 29, 2024 at 10:38 AM Oliver Kiddle <opk@zsh.org> wrote:
>
> My expectation would have been for either an error message saying
>   break: not in while, until, select, or repeat loop`
> or that because the break is run before the builtin that the builtin
> would never get to run.

The latter was my expectation; I think the break is inside the loop.

Skipping ahead:
> It also rather surprises me that the break here even works:
>
>   br() { break }
>   while : ; do
>     br
>   done

% setopt localloops
% br() { break }
% repeat 3 br
zsh: `break' active at end of function scope
zsh: `break' active at end of function scope
zsh: `break' active at end of function scope
% unsetopt localloops
% repeat 3 br
%

> But the implementation of break is just putting
> a value in the breaks integer variable so I guess that variable isn't
> checked until after the builtin is run.

Right; since the break occurs in prefork() we're already deeper in the
execXXX() call stack than anywhere the variable is checked.  The
function call re-enters that stack closer to the top, and finds the
setting.

> The anonymous function is
> perhaps a two-stage process for defining and then running. Note:
>   while :; do; () { echo hello } > ${|REPLY=x;break}; done
> creates an empty file named x.

Again, prefork() ... the redirections also occur before breaks is
checked.  Although this:
  echo xx > ${|REPLY=x;exit}
does bail out before completing the redirect, whereas this:
  echo xx > ${|REPLY=x;return}
does not.

> When suggesting the test, the exact behaviour was not something I cared
> strongly about provided it wasn't seg fault, infinite loop or similar.

Yes, I understand.  I considered testing this way for future-proofing:

repeat 3 V=${|REPLY=x; break}
print $V

I ended up deciding that it was reasonable to leave the "echo ${|...}"
as an ad-hoc regression test in the event builtins do start
"break"-ing.

> In the case of
>   repeat 3 print c*(oe:'break':)
> it might be a useful feature if break applied to the internal loop over
> globbed files causing print to run but only with those files that were
> matched so far.

IIRC the entire glob loop finishes and then any (e) actions are
applied to the resulting list.  So "matched so far" is not well
defined.  I believe the only way to stop the internal loop "early" is
the (Y) qualifier.


  reply	other threads:[~2024-03-29 18:51 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-29  1:54 Bart Schaefer
2024-03-29  9:30 ` Mikael Magnusson
2024-03-29 16:28   ` Bart Schaefer
2024-03-29 16:56     ` Bart Schaefer
2024-03-29 17:38 ` Oliver Kiddle
2024-03-29 18:51   ` Bart Schaefer [this message]
2024-03-29 21:14     ` Mikael Magnusson
2024-03-29 22:02       ` 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='CAH+w=7acsf9YYrOeS6GPWSffm4p4=-rU6YONsBAhJg20=S1b7Q@mail.gmail.com' \
    --to=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).