Subject: [PATCH] typeset: set $? on incidental error
From: Daniel Shahaf <[email-masked]>
To: [email-masked]
Date: Thu, 14 Jan 2016 00:13:36 +0000
List-Id: Zsh Workers List <zsh-workers.zsh.org>

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