From 679b2e2aadd7fcfdd60e140c271ec7964409a545 Mon Sep 17 00:00:00 2001 From: Justin Ma Date: Fri, 3 Apr 2026 23:02:27 +0800 Subject: [PATCH] Fix parser scope leak in where $cond (#17976) Try to fix #17975 --- crates/nu-parser/src/parser.rs | 3 +++ tests/modules/mod.rs | 33 +++++++++++++++++++++++++++++++++ tests/overlays/mod.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 9d30dd8de6..72132befc0 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -4035,11 +4035,13 @@ pub fn parse_row_condition(working_set: &mut StateWorkingSet, spans: &[Span]) -> Expr::FullCellPath(ref box_fcp) if box_fcp.head.as_var().is_some_and(|id| id != var_id) => { let mut expression = expression; expression.ty = Type::Any; + working_set.exit_scope(); return expression; } Expr::Var(arg_var_id) if arg_var_id != var_id => { let mut expression = expression; expression.ty = Type::Any; + working_set.exit_scope(); return expression; } _ => { @@ -4050,6 +4052,7 @@ pub fn parse_row_condition(working_set: &mut StateWorkingSet, spans: &[Span]) -> expression.ty.clone(), expression.span, )); + working_set.exit_scope(); return Expression::garbage(working_set, expression.span); } diff --git a/tests/modules/mod.rs b/tests/modules/mod.rs index 0733d82226..97cd474095 100644 --- a/tests/modules/mod.rs +++ b/tests/modules/mod.rs @@ -1,5 +1,6 @@ use nu_test_support::fs::Stub::{FileWithContent, FileWithContentToBeTrimmed}; use nu_test_support::playground::Playground; +use nu_test_support::prelude::{Result, TestResultExt, test}; use nu_test_support::{nu, nu_repl_code}; use pretty_assertions::assert_eq; use rstest::rstest; @@ -136,6 +137,38 @@ fn module_public_import_alias() { }) } +#[test] +fn module_public_import_decl_with_stored_where_condition() -> Result { + Playground::setup( + "module_public_import_decl_with_stored_where_condition", + |dirs, sandbox| -> Result { + sandbox.with_files(&[FileWithContentToBeTrimmed( + "main.nu", + " + export use mod.nu helper + ", + )]); + + sandbox.with_files(&[FileWithContentToBeTrimmed( + "mod.nu", + r#" + export def helper [] { + let cond = {|x| true } + [{a: 1}] | where $cond + } + + export def main [] { "ok" } + "#, + )]); + + test() + .cwd(dirs.test()) + .run("use main.nu helper; helper | to nuon --raw") + .expect_value_eq("[[a];[1]]") + }, + ) +} + #[test] fn module_nested_imports() { Playground::setup("module_nested_imports", |dirs, sandbox| { diff --git a/tests/overlays/mod.rs b/tests/overlays/mod.rs index 089c89cea6..5ca024e123 100644 --- a/tests/overlays/mod.rs +++ b/tests/overlays/mod.rs @@ -1,5 +1,6 @@ use nu_test_support::fs::Stub::{FileWithContent, FileWithContentToBeTrimmed}; use nu_test_support::playground::Playground; +use nu_test_support::prelude::{Result, TestResultExt, test}; use nu_test_support::{nu, nu_repl_code}; use pretty_assertions::assert_eq; @@ -230,6 +231,32 @@ fn add_overlay_from_const_module_name_decl() { assert_eq!(actual.out, "foo"); } +#[test] +fn add_overlay_from_file_with_stored_where_condition() -> Result { + Playground::setup( + "add_overlay_from_file_with_stored_where_condition", + |dirs, sandbox| -> Result { + sandbox.with_files(&[FileWithContentToBeTrimmed( + "mod.nu", + r#" + export def helper [] { + let cond = {|x| true } + [{a: 1}] | where $cond + } + + export def main [] { "ok" } + "#, + )]); + + let inp = &["overlay use mod.nu", "helper | to nuon --raw"]; + + let mut tester = test().cwd(dirs.test()).add_nu_to_path(); + tester.run(inp.join("; ")).expect_value_eq("[[a];[1]]")?; + tester.run(nu_repl_code(inp)).expect_value_eq("[[a];[1]]") + }, + ) +} + #[test] fn new_overlay_from_const_name() { let inp = &[