zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH] Re: Inconsistent behavior of ERR_EXIT with conditionals
Date: Sun, 6 Nov 2022 21:35:05 -0800	[thread overview]
Message-ID: <CAH+w=7avPaVau=cOc5Xy1HH4UkoeQmOuuAzh0s--Hh0KLrEHwQ@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7apH5_YnJtWT4x=mUOFhpHH1xMN9EkjjB0FJAi=8JQHdQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1075 bytes --]

On Sun, Nov 6, 2022 at 7:50 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> More questionable are the looping constructs.  I can't come up with a
> way to have the loop end in an error state without the whole shell
> ERREXITing before reaching the end of the loop body.

Found the way (and it should have been obvious):

repeat 1; do false && true; done

The statement `false && true` doesn't itself cause an exit, but does
become $? of the loop.

Now the weird bit is that the attached patch DOES NOT cause a slew of
test failures in C03traps.  E.g. if I run this test standalone:

Src/zsh -f =(<<<"(setopt err_exit
  if true; then
    false
  fi
  print OK
  )")

I get no output and $? = 1.  The exact same code in C03traps.ztst
prints OK, which it should not.  The only difference I can see is that
ZTST_execchunk fiddles with localloops, but I've tried doing that too.

There is the question of why ignoring a false status at the end of a
complex command has so far been considered correct for ERR_EXIT,
according to C03.  This is a disagreement with e.g. bash.

[-- Attachment #2: complexerrexit.txt --]
[-- Type: text/plain, Size: 1839 bytes --]

diff --git a/Src/exec.c b/Src/exec.c
index c8bcf4ee5..2422dae91 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -451,7 +451,7 @@ execcursh(Estate state, int do_exec)
     cmdpop();
 
     state->pc = end;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
 
     return lastval;
 }
diff --git a/Src/loop.c b/Src/loop.c
index db5b3e097..be5261369 100644
--- a/Src/loop.c
+++ b/Src/loop.c
@@ -208,7 +208,7 @@ execfor(Estate state, int do_exec)
     loops--;
     simple_pline = old_simple_pline;
     state->pc = end;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
     return lastval;
 }
 
@@ -336,7 +336,7 @@ execselect(Estate state, UNUSED(int do_exec))
     loops--;
     simple_pline = old_simple_pline;
     state->pc = end;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
     return lastval;
 }
 
@@ -478,7 +478,7 @@ execwhile(Estate state, UNUSED(int do_exec))
     popheap();
     loops--;
     state->pc = end;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
     return lastval;
 }
 
@@ -532,7 +532,7 @@ execrepeat(Estate state, UNUSED(int do_exec))
     loops--;
     simple_pline = old_simple_pline;
     state->pc = end;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
     return lastval;
 }
 
@@ -587,7 +587,7 @@ execif(Estate state, int do_exec)
 	    lastval = 0;
     }
     state->pc = end;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
 
     return lastval;
 }
@@ -701,7 +701,7 @@ execcase(Estate state, int do_exec)
 
     if (!anypatok)
 	lastval = 0;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
 
     return lastval;
 }

  reply	other threads:[~2022-11-07  5:35 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-04 16:37 Philippe Altherr
2022-11-06 20:45 ` Bart Schaefer
2022-11-07  3:50   ` Bart Schaefer
2022-11-07  5:35     ` Bart Schaefer [this message]
2022-11-07  9:44       ` [PATCH] " Peter Stephenson
2022-11-08  1:20         ` Bart Schaefer
2022-11-08  4:58     ` Philippe Altherr
2022-11-08  5:36       ` Bart Schaefer
2022-11-08  8:04         ` Lawrence Velázquez
2022-11-08 18:51           ` Philippe Altherr
2022-11-08 19:20             ` Lawrence Velázquez
2022-11-08 23:28             ` Bart Schaefer
2022-11-09  4:11               ` Philippe Altherr
2022-11-09  6:00                 ` Bart Schaefer
2022-11-09 14:22                   ` Philippe Altherr
2022-11-10  1:00                     ` Bart Schaefer
2022-11-10  5:09                       ` Bart Schaefer
2022-11-11  3:04                         ` Philippe Altherr
2022-11-11  4:06                           ` Lawrence Velázquez
2022-11-11  4:09                           ` Eric Cook
2022-11-08 23:11           ` 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=7avPaVau=cOc5Xy1HH4UkoeQmOuuAzh0s--Hh0KLrEHwQ@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).