Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Issues with references to subscripted variables
On 2025-11-30 13:03, Philippe Altherr wrote:
> *Issue 2: reference to subscripted **reference*
>
> When a reference A references a subscripted referent B that is itself a
> reference, expansions of A wrongly apply the subscript to the content of B
> (i.e., the name of the variable referred by B) instead of applying it to
> the content of the variable referred by B:
>
> $ var=XYZ; local -n ref1=var ref2=ref1[2]; echo ref2=$ref2
> ref2=a
> $ arr=(X Y Z); local -n ref1=arr ref2=ref1[2]; echo ref2=$ref2
> ref2=r
>
> Assignments to A wrongly modify the content of B instead of modifying the
> content of the variable referred by B:
>
> $ var=XYZ; local -n ref1=var ref2=ref1[2]; ref2=abc; typeset -p var ref1
> typeset -g var=XYZ
> typeset -n ref1=vabcr
> $ arr=(X Y Z); local -n ref1=arr ref2=ref1[2]; ref2=abc; typeset -p arr ref1
> typeset -g -a arr=( X Y Z )
> typeset -n ref1=aabcr
>
> In both cases, the reason is, as Bart commented in workers/54076
> <https://zsh.org/workers/54076>, because resolve_nameref()
> <https://github.com/zsh-users/zsh/blob/5539bc3fd59a2577bf1f951430c20e2b1e7b4dce/Src/params.c#L6353>(wrongly)
> assumes that a subscripted referent must be the stopping point, and so
> returns its input.
>
> *Discussion*
>
> The examples above are unlikely to occur as such in any real script.
> However, it's not difficult to come up with more real life scenarios that
> trigger the same issues; references to subscripted variables look very
> brittle to me. It's not far-fetched to think that many users who try to use
> them would be affected by at least one of these issues.
It's a problem. Regular nameref users would definitely hit this eventually. Not
to mention badly clashing with the historical design.
That said, there's also the total lack of a mechanism for doing controlled
descent into the nameref chain that has always been a problem. It would be
really nice to also have a non-terrible way to introspect if not manipulate the
non-leaf variables (without changes to the normal expected functioning.)
Messages sorted by:
Reverse Date,
Date,
Thread,
Author