Fix parser scope leak in where $cond (#17976)

Try to fix #17975
This commit is contained in:
Justin Ma
2026-04-03 23:02:27 +08:00
committed by GitHub
parent 06b0c6e017
commit 679b2e2aad
3 changed files with 63 additions and 0 deletions
+3
View File
@@ -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);
}
+33
View File
@@ -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| {
+27
View File
@@ -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 = &[