From bf9e5583ba93cc628e222c415da36437df9ac428 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Sat, 25 May 2024 13:17:35 +0200 Subject: [PATCH] Push and pop for-block every run through the loop We do the same in while loops. This clears the local variables every time. Fixes #10525 --- src/parse_execution.rs | 6 ++++-- tests/checks/for.fish | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/parse_execution.rs b/src/parse_execution.rs index 688a45641..06da5979d 100644 --- a/src/parse_execution.rs +++ b/src/parse_execution.rs @@ -990,7 +990,6 @@ fn run_for_statement( assert!(retval == EnvStackSetResult::ENV_OK); trace_if_enabled_with_args(ctx.parser(), L!("for"), &arguments); - let fb = ctx.parser().push_block(Block::for_block()); // We fire the same event over and over again, just construct it once. let evt = Event::variable_set(for_var_name.clone()); @@ -1014,7 +1013,11 @@ fn run_for_statement( event::fire(ctx.parser(), evt.clone()); ctx.parser().libdata_mut().pods.loop_status = LoopStatus::normals; + + // Push and pop the block again and again to clear variables + let fb = ctx.parser().push_block(Block::for_block()); self.run_job_list(ctx, block_contents, Some(fb)); + ctx.parser().pop_block(fb); if self.check_end_execution(ctx) == Some(EndExecutionReason::control_flow) { // Handle break or continue. @@ -1026,7 +1029,6 @@ fn run_for_statement( } } - ctx.parser().pop_block(fb); trace_if_enabled(ctx.parser(), L!("end for")); ret } diff --git a/tests/checks/for.fish b/tests/checks/for.fish index 139ab1ff9..935a80491 100644 --- a/tests/checks/for.fish +++ b/tests/checks/for.fish @@ -43,3 +43,13 @@ end # CHECK: foo set # CHECK: foo set # CHECK: foo set + +for x in 1 2 3 + test $x -eq 2 && set -l foo bar + echo foo value is $foo +end +# We keep the old value from outside the loop +# CHECK: foo value is 3 +# CHECK: foo set +# CHECK: foo value is bar +# CHECK: foo value is 3