2020-02-09 00:45:22 +08:00
|
|
|
#RUN: %fish -C 'set -g fish %fish' %s
|
2019-01-14 08:57:38 +08:00
|
|
|
|
|
|
|
function never_runs
|
2020-03-10 02:36:12 +08:00
|
|
|
while false
|
|
|
|
end
|
2019-01-14 08:57:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function early_return
|
2020-03-10 02:36:12 +08:00
|
|
|
while true
|
|
|
|
return 2
|
|
|
|
end
|
2019-01-14 08:57:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function runs_once
|
2020-03-10 02:36:12 +08:00
|
|
|
set -l i 1
|
|
|
|
while test $i -ne 0 && set i (math $i - 1)
|
|
|
|
end
|
2019-01-14 08:57:38 +08:00
|
|
|
end
|
|
|
|
|
2020-02-08 22:21:22 +08:00
|
|
|
# this should return 0
|
2020-03-10 02:36:12 +08:00
|
|
|
never_runs
|
|
|
|
echo "Empty Loop in Function: $status"
|
2020-02-08 22:21:22 +08:00
|
|
|
#CHECK: Empty Loop in Function: 0
|
2019-01-14 08:57:38 +08:00
|
|
|
|
|
|
|
# this should return 0
|
2020-03-10 02:36:12 +08:00
|
|
|
runs_once
|
|
|
|
echo "Runs Once: $status"
|
2020-02-08 22:21:22 +08:00
|
|
|
#CHECK: Runs Once: 0
|
2019-01-14 08:57:38 +08:00
|
|
|
|
|
|
|
# this should return 2
|
2020-03-10 02:36:12 +08:00
|
|
|
early_return
|
|
|
|
echo "Early Return: $status"
|
2020-02-08 22:21:22 +08:00
|
|
|
#CHECK: Early Return: 2
|
2019-01-21 08:37:20 +08:00
|
|
|
|
2020-03-10 02:36:12 +08:00
|
|
|
function set_status
|
|
|
|
return $argv[1]
|
|
|
|
end
|
2019-01-21 08:37:20 +08:00
|
|
|
|
|
|
|
# The previous status is visible in the loop condition.
|
|
|
|
# This includes both the incoming status, and the last command in the
|
|
|
|
# loop body.
|
|
|
|
set_status 36
|
|
|
|
while begin
|
2020-03-10 02:36:12 +08:00
|
|
|
set -l saved $status
|
|
|
|
echo "Condition Status: $status"
|
|
|
|
set_status $saved
|
|
|
|
end
|
|
|
|
true
|
2019-01-21 08:37:20 +08:00
|
|
|
end
|
2020-02-08 22:21:22 +08:00
|
|
|
#CHECK: Condition Status: 36
|
2019-01-21 08:37:20 +08:00
|
|
|
|
|
|
|
# The condition status IS visible in the loop body.
|
|
|
|
set_status 55
|
|
|
|
while true
|
2020-03-10 02:36:12 +08:00
|
|
|
echo "Body Status: $status"
|
|
|
|
break
|
2019-01-21 08:37:20 +08:00
|
|
|
end
|
2020-02-08 22:21:22 +08:00
|
|
|
#CHECK: Body Status: 0
|
2019-01-21 08:37:20 +08:00
|
|
|
|
|
|
|
# The status of the last command is visible in the loop condition
|
|
|
|
set_status 13
|
|
|
|
while begin
|
2020-03-10 02:36:12 +08:00
|
|
|
set -l saved $status
|
|
|
|
echo "Condition 2 Status: $saved"
|
|
|
|
test $saved -ne 5
|
|
|
|
end
|
|
|
|
set_status 5
|
2019-01-21 08:37:20 +08:00
|
|
|
end
|
2020-02-08 22:21:22 +08:00
|
|
|
#CHECK: Condition 2 Status: 13
|
|
|
|
#CHECK: Condition 2 Status: 5
|
2019-01-21 08:37:20 +08:00
|
|
|
|
|
|
|
# The status of the last command is visible outside the loop
|
|
|
|
set rem 5 7 11
|
|
|
|
while [ (count $rem) -gt 0 ]
|
2020-03-10 02:36:12 +08:00
|
|
|
set_status $rem[1]
|
|
|
|
set rem $rem[2..-1]
|
2019-01-21 08:37:20 +08:00
|
|
|
end
|
|
|
|
echo "Loop Exit Status: $status"
|
2020-02-08 22:21:22 +08:00
|
|
|
#CHECK: Loop Exit Status: 11
|
2019-01-21 08:37:20 +08:00
|
|
|
|
|
|
|
# Empty loops succeed.
|
|
|
|
false
|
2020-03-10 02:36:12 +08:00
|
|
|
while false
|
|
|
|
end
|
2019-01-21 08:37:20 +08:00
|
|
|
echo "Empty Loop Status: $status"
|
2020-02-08 22:21:22 +08:00
|
|
|
#CHECK: Empty Loop Status: 0
|
2020-02-08 22:37:10 +08:00
|
|
|
|
|
|
|
# Loop control in conditions, should have no output.
|
|
|
|
for i in 1 2 3
|
2020-03-10 02:36:12 +08:00
|
|
|
while break
|
|
|
|
end
|
2020-02-09 00:45:22 +08:00
|
|
|
echo $i
|
2020-02-08 22:37:10 +08:00
|
|
|
end
|
|
|
|
for i in 1 2 3
|
2020-03-10 02:36:12 +08:00
|
|
|
while continue
|
|
|
|
end
|
2020-02-09 00:45:22 +08:00
|
|
|
echo $i
|
2020-02-08 22:37:10 +08:00
|
|
|
end
|
|
|
|
|
2020-03-10 02:36:12 +08:00
|
|
|
if false; or --help
|
|
|
|
end
|
2020-02-09 00:45:22 +08:00
|
|
|
|
|
|
|
# Make sure while loops don't run forever with no-exec (#1543)
|
|
|
|
echo "while true; end" | $fish --no-execute
|
|
|
|
|
|
|
|
# For loops with read-only vars is an error (#4342)
|
|
|
|
for status in a b c
|
|
|
|
echo $status
|
|
|
|
end
|
|
|
|
#CHECKERR: {{.*}}loops.fish (line {{\d+}}): You cannot use read-only variable 'status' in a for loop
|
|
|
|
#CHECKERR: for status in a b c
|
|
|
|
#CHECKERR: ^
|
|
|
|
|
|
|
|
# "That goes for non-electric ones as well (#5548)"
|
|
|
|
for hostname in a b c
|
|
|
|
echo $hostname
|
|
|
|
end
|
|
|
|
#CHECKERR: {{.*}}loops.fish (line {{\d+}}): You cannot use read-only variable 'hostname' in a for loop
|
|
|
|
#CHECKERR: for hostname in a b c
|
|
|
|
#CHECKERR: ^
|
|
|
|
|
|
|
|
# For loop control vars available outside the for block
|
|
|
|
begin
|
|
|
|
set -l loop_var initial-value
|
|
|
|
for loop_var in a b c
|
|
|
|
# do nothing
|
|
|
|
end
|
|
|
|
set --show loop_var
|
|
|
|
end
|
|
|
|
|
|
|
|
set -g loop_var global_val
|
|
|
|
function loop_test
|
|
|
|
for loop_var in a b c
|
|
|
|
if test $loop_var = b
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
set --show loop_var
|
|
|
|
end
|
|
|
|
loop_test
|
|
|
|
set --show loop_var
|
|
|
|
|
|
|
|
begin
|
|
|
|
set -l loop_var
|
|
|
|
for loop_var in aa bb cc
|
|
|
|
end
|
|
|
|
set --show loop_var
|
|
|
|
end
|
|
|
|
set --show loop_var
|
|
|
|
#CHECK: $loop_var: set in local scope, unexported, with 1 elements
|
|
|
|
#CHECK: $loop_var[1]: length=1 value=|c|
|
|
|
|
#CHECK: $loop_var: not set in global scope
|
|
|
|
#CHECK: $loop_var: not set in universal scope
|
|
|
|
#CHECK:
|
|
|
|
#CHECK: $loop_var: set in local scope, unexported, with 1 elements
|
|
|
|
#CHECK: $loop_var[1]: length=1 value=|b|
|
|
|
|
#CHECK: $loop_var: set in global scope, unexported, with 1 elements
|
|
|
|
#CHECK: $loop_var[1]: length=10 value=|global_val|
|
|
|
|
#CHECK: $loop_var: not set in universal scope
|
|
|
|
#CHECK:
|
|
|
|
#CHECK: $loop_var: not set in local scope
|
|
|
|
#CHECK: $loop_var: set in global scope, unexported, with 1 elements
|
|
|
|
#CHECK: $loop_var[1]: length=10 value=|global_val|
|
|
|
|
#CHECK: $loop_var: not set in universal scope
|
|
|
|
#CHECK:
|
|
|
|
#CHECK: $loop_var: set in local scope, unexported, with 1 elements
|
|
|
|
#CHECK: $loop_var[1]: length=2 value=|cc|
|
|
|
|
#CHECK: $loop_var: set in global scope, unexported, with 1 elements
|
|
|
|
#CHECK: $loop_var[1]: length=10 value=|global_val|
|
|
|
|
#CHECK: $loop_var: not set in universal scope
|
|
|
|
#CHECK:
|
|
|
|
#CHECK: $loop_var: not set in local scope
|
|
|
|
#CHECK: $loop_var: set in global scope, unexported, with 1 elements
|
|
|
|
#CHECK: $loop_var[1]: length=10 value=|global_val|
|
|
|
|
#CHECK: $loop_var: not set in universal scope
|