mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-05-27 16:51:15 -03:00
Don't overwrite unrelated variables with for-loop-variables
for-loops that were not inside a function could overwrite global
and universal variables with the loop variable. Avoid this by making
for-loop-variables local variables in their enclosing scope.
This means that if someone does:
set a global
for a in local; end
echo $a
The local $a will shadow the global one (but not be visible in child
scopes). Which is surprising, but less dangerous than the previous
behavior.
The detection whether the loop is running inside a function was failing
inside command substitutions. Remove this special handling of functions
alltogether, it's not needed anymore.
Fixes #6480
This commit is contained in:
34
tests/checks/for.fish
Normal file
34
tests/checks/for.fish
Normal file
@@ -0,0 +1,34 @@
|
||||
# RUN: %fish %s
|
||||
|
||||
# A for-loop-variable is a local variable in the enclosing scope.
|
||||
set -g i global
|
||||
# implicit set -l i $i
|
||||
for i in local
|
||||
end
|
||||
set -ql i && echo $i
|
||||
# CHECK: local
|
||||
|
||||
# The loop variable is initialized with any previous value.
|
||||
set -g j global
|
||||
for j in
|
||||
end
|
||||
set -ql j && echo $j
|
||||
# CHECK: global
|
||||
|
||||
# Loop variables exist only locally in the enclosing local scope.
|
||||
# They do not modify other local/global/universal variables.
|
||||
set -g k global
|
||||
begin
|
||||
for k in local1
|
||||
echo $k
|
||||
# CHECK: local1
|
||||
for k in local2
|
||||
end
|
||||
echo $k
|
||||
# CHECK: local2
|
||||
end
|
||||
echo $k
|
||||
# CHECK: local1
|
||||
end
|
||||
echo $k
|
||||
# CHECK: global
|
||||
Reference in New Issue
Block a user