Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm
Precedence: bulk
X-No-Archive: yes
List-Id: Zsh Workers List <zsh-workers.zsh.org>
List-Post: <mailto:zsh-workers@zsh.org>
List-Help: <mailto:zsh-workers-help@zsh.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au
X-Spam-Level: 
X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_00,TO_NO_BRKTS_PCNT
	autolearn=no autolearn_force=no version=3.4.0
Date: Thu, 14 Jan 2016 00:13:36 +0000
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: zsh-workers@zsh.org
Subject: [PATCH] typeset: set $? on incidental error
Message-ID: <20160114001336.GA11173@tarsus.local2>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
User-Agent: Mutt/1.5.23 (2014-03-12)
X-Seq: zsh-workers 37609

The 'typeset' family of builtins doesn't set $? when one would expect it
to do so:

    % x=$(true) y=$(exit 42); echo $?
    42
    % local x=$(true) y=$(exit 42); echo $?
    0

This patch makes 'typeset' behave as ordiary assignment does.

It's a one-liner but still, I'd appreciate a second pair of eyes over it.

Cheers,

Daniel

diff --git a/Src/builtin.c b/Src/builtin.c
index d8974eb..c4af66f 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -475,9 +475,13 @@ execbuiltin(LinkList args, LinkList assigns, Builtin bn)
 	{
 	    /*
 	     * Takes two sets of arguments.
+	     *
+	     * Return assignfunc's return value, or if it succeeded return any
+	     * preexisting error.
 	     */
 	    HandlerFuncAssign assignfunc = (HandlerFuncAssign)bn->handlerfunc;
-	    return (*(assignfunc)) (name, argv, assigns, &ops, bn->funcid);
+	    int retval = (*(assignfunc)) (name, argv, assigns, &ops, bn->funcid);
+	    return retval ? retval : lastval;
 	}
 	else
 	{
diff --git a/Test/B02typeset.ztst b/Test/B02typeset.ztst
index 7d65cc8..a87ecfb 100644
--- a/Test/B02typeset.ztst
+++ b/Test/B02typeset.ztst
@@ -706,3 +706,13 @@
 >typeset -a array=( '' two '' four )
 >typeset -a array=( one '' three )
 >no really nothing here
+
+  s='local x=$(exit 42); echo $?'
+  eval $s
+  (
+    disable -r local
+    eval $s
+  )
+0:typeset reports errors like parameter assignment does
+>42
+>42

