Subject: Re: bug: suffix alias and tabcompletion
From: Peter Stephenson <[email-masked]>
To: Zsh hackers list <[email-masked]>
Date: Wed, 22 Jun 2016 10:01:59 +0100
List-Id: Zsh Workers List <zsh-workers.zsh.org>

On Tue, 21 Jun 2016 13:18:21 -0700
Bart Schaefer <[email-masked]> wrote:
> On Tue, Jun 21, 2016 at 12:39 PM, Michael Gebhard
> <[email-masked]> wrote:
> >
> > running the following commands and trying to complete the last line
> > by pressing tab produces a zsh taking up 100% cpu.
> >
> > zsh -f
> > touch foo.bar
> > alias -s bar='echo a &'
> 
> It's not (just) completion; attempting to run the command "foo.bar"
> produces a similar infinite loop.  The alias has expanded to "echo a &
> foo.bar" which then recursively expands "foo.bar" (because after the
> "&" it is in command position again) and off we go.  The "touch
> foo.bar" isn't required.

This is different from a normal alias because in that case the original
text is completely removed; you can only encounter the (normal or
global) alias again while expanding the alias, which we detect, or
after, which is a completely different set of input triggering the
expansion so there's no recursion.

In this case, the argument foo.bar appears again *after* the alias has
been expanded fully because the alias is inserted before it.  By the
time we get to foo.bar again as the argument, the trace of alias
expansion has been removed --- we've just got the resulting fully
expanded text "echo a &" and no more alias.

The nearest parallel to this case with normal aliases is this:

alias foo='echo a &'
% foo foo
[1] 3423
a
[2] 3424
% 
[1]  - done       echo a
% a

[2]  + done       echo a

You'll see the underlying expansion behaviour is the same --- both
aliases get expanded.  This is long-standing and I assume is regarded as
OK.

I suppose we'd need some way of marking something as having had a suffix
alias expanded before it, and then we'd need to reset that flag if we
encountered something not at the start of that input that was in command
position so we could expand a new suffix alias.

However, I don't think it's quite as simple as the two separate tests
- when expanding a suffix alias, set a lexical flag to say no more
suffix aliases;
- reset that flag any time you reach a new command position

because in the problem case we have reached a new command position and
don't want to reset the flag.  So there's some subtle ordering to sort
out.

pws