elves / elvish

Powerful scripting language & versatile interactive shell
https://elv.sh/
BSD 2-Clause "Simplified" License
5.73k stars 303 forks source link

Scoping of the right hand side of a var assignment #1829

Closed hanche closed 3 months ago

hanche commented 3 months ago

What happened, and what did you expect to happen?

What happens:

⬥ var a = x
⬥ var a = [$a $a] ##
⬥ put $a
⮕ [$nil $nil]

Expected result should be [x x].

Clearly, the problem is that the right hand side of the assignment (marked ## in the above code) is within the scope of the variable being declared.

I think the correct behaviour (for some value of “correct” is to first evaluate the right hand side of the assignment, then create the new variable(s) and perform the assignment.

Others may disagree, and some may argue that this is a feature request rather than a bug. But if so, it is at least a documentation bug, since the exact behaviour is not documented.

Output of "elvish -version"

0.21.0-dev.0.20240724150116-9597a25e366b-dirty

Code of Conduct

hanche commented 3 months ago

This appears to have been introduced with commit 9597a25e where with was introduced.

xiaq commented 3 months ago

The original behavior is the intended one and was broken accidentally. A regression test should prevent this from happening in future!