Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: BUG: $_ empty on null function call



Peter Stephenson wrote on Mon, Feb 09, 2015 at 15:13:40 +0000:
> On Mon, 9 Feb 2015 14:25:07 +0000
> Peter Stephenson <p.stephenson@xxxxxxxxxxx> wrote:
> > On Mon, 9 Feb 2015 14:10:26 +0000
> > Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
> > > There are some other differences between anonymous functions, e.g., they
> > > don't honor PRINT_EXIT_VALUE:
> > >
> > > Engineering-wise, the ideal solution would be for anonymous and named
> > > functions to share code... though I realize that may be a somewhat
> > > invasive code change.
> > 
> > They already do everywhere that doesn't deal with the special argument
> > syntax (Micah's problem) or with immediate execution after a definition.
> > I suspect this may have to do with a different path owing to an
> > optimisation later in the execution path where we make certain
> > assumptions if code is regarded as "simple".
> 
> Sigh.  It's a combination of that *and* execution immediately after
> definition.
> 
> When the code is parsed, we don't know if PRINTEXITVALUE is going to be
> set when it's run.  At this point I think we declare "simple" code
> execution for anonymous functions dead in the water.  The effect is
> probably small anyway.
> 

I was looking at making [[ honor PRINT_EXIT_VALUE; right now it doesn't,
because it uses execlist->execsimple->execcond (and so never passes
through execcmd).  I'm mentioning that since it may be relevant, as it
also concerns a simple command wanting to honor PRINT_EXIT_VALUE.

¹ The use-case: I use [[ ]] as a standalone command (not as part of an
if or while) in interactive shells to test its syntax when writing scripts.

> It looks like we can make some code in the lowest level of general
> command execution, execcmd(), run in a few more cases, at least the
> following attempt to move them out of an if block doesn't cause any test
> failures.
> 
> This doesn't help with Micah's problem which is due to the *third*
> difference.
> 

I'll just point out for anyone who needs $_ with anonymous functions
working "yesterday" that a quick and dirty way to achieve that is via
the attached patch.  It might not be a good general solution since it
duplicates code, but it does make invocation of anonymous functions set
$_ and I think it has no harmful effects.

I suppose a better fix would involve extracting the arguments of an
anonymous function up in execcmd() rather than down in execfuncdef(), so
they can reuse the existing setunderscore() call in execcmd()?

> +      () { false; }
> +1:PRINT_EXIT_VALUE option for anonymous function
> +?zsh: exit 1

Thanks for fixing this :)

Daniel
diff --git a/Src/exec.c b/Src/exec.c
index 3b0e936..719345e 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -4482,6 +4482,11 @@ execfuncdef(Estate state, Eprog redir_prog)
 	    shf->node.nam = "(anon)";
 	    pushnode(args, shf->node.nam);
 
+	    /* Set up special parameter $_ */
+	    setunderscore((args && nonempty(args))
+			  ? ((char *) getdata(lastnode(args)))
+			  : "");
+
 	    execshfunc(shf, args);
 	    ret = lastval;
 


Messages sorted by: Reverse Date, Date, Thread, Author