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

Re: PATCH: Fix nameref errors exposed by Philippe's tests



On Mon, May 5, 2025 at 3:59 PM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>>
>> Hm.  It's not looking one level above the current scope, it's looking
>> one level above the parameter currently in scope.
>
> Which parameter? The one being initialized?

No, the one being referenced.

local s # declared in h
rs=s # assigned in i

$s is from scope h but is visible in scope i
$rs looks one scope above the visible $s, finds the $s from g

I can look at this separately but I'm not sure how important it is,
because the intention of -u is to "protect" locals declared in the
current scope; that is, the intention for -u is to always declare both
the named reference and the locals in the same scope.  It's meant for
this case:

function use_ref() {
  local -nu from_caller=$1
  local my_var=deeper
  print $from_caller
}
function the_caller() {
  local my_var=shallower
  use_ref my_var
}
the_caller
(output is "shallower" not "deeper")

Deliberately propagating an uninitialized -u reference from some
higher scope was never really discussed and I'm not sure what the
real-world use-case is, but it may not be too hard to fix.




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