From 57d96c09fa78f6231093cb241da4ddbd545f84bf Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Sat, 15 Jul 2023 19:41:48 +1200 Subject: [PATCH] fix input signature of let/mut (#9695) # Description This updates `let` and `mut` to allow for any input. This lets them typecheck any collection they do. For example, this now compiles: ``` def foo []: [int -> int, string -> int] { let x = $in if ($x | describe) == "int" { 3 } else { 4 } } 100 | foo ``` # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-cmd-lang/src/core_commands/let_.rs | 2 +- crates/nu-cmd-lang/src/core_commands/mut_.rs | 2 +- src/tests/test_parser.rs | 32 ++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/crates/nu-cmd-lang/src/core_commands/let_.rs b/crates/nu-cmd-lang/src/core_commands/let_.rs index cd6ae2338e..649035c8dd 100644 --- a/crates/nu-cmd-lang/src/core_commands/let_.rs +++ b/crates/nu-cmd-lang/src/core_commands/let_.rs @@ -17,7 +17,7 @@ impl Command for Let { fn signature(&self) -> nu_protocol::Signature { Signature::build("let") - .input_output_types(vec![(Type::Nothing, Type::Nothing)]) + .input_output_types(vec![(Type::Any, Type::Nothing)]) .allow_variants_without_examples(true) .required("var_name", SyntaxShape::VarWithOptType, "variable name") .required( diff --git a/crates/nu-cmd-lang/src/core_commands/mut_.rs b/crates/nu-cmd-lang/src/core_commands/mut_.rs index 3a93d85bb7..35e54c43c4 100644 --- a/crates/nu-cmd-lang/src/core_commands/mut_.rs +++ b/crates/nu-cmd-lang/src/core_commands/mut_.rs @@ -17,7 +17,7 @@ impl Command for Mut { fn signature(&self) -> nu_protocol::Signature { Signature::build("mut") - .input_output_types(vec![(Type::Nothing, Type::Nothing)]) + .input_output_types(vec![(Type::Any, Type::Nothing)]) .allow_variants_without_examples(true) .required("var_name", SyntaxShape::VarWithOptType, "variable name") .required( diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index b4242a9779..6aec8c8517 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -623,3 +623,35 @@ fn def_with_input_output_broken_1() -> TestResult { fn def_with_input_output_broken_2() -> TestResult { fail_test(r#"def foo []: int -> { 3 }"#, "expected type") } + +#[test] +fn def_with_in_var_let_1() -> TestResult { + run_test( + r#"def foo []: [int -> int, string -> int] { let x = $in; if ($x | describe) == "int" { 3 } else { 4 } }; "100" | foo"#, + "4", + ) +} + +#[test] +fn def_with_in_var_let_2() -> TestResult { + run_test( + r#"def foo []: [int -> int, string -> int] { let x = $in; if ($x | describe) == "int" { 3 } else { 4 } }; 100 | foo"#, + "3", + ) +} + +#[test] +fn def_with_in_var_mut_1() -> TestResult { + run_test( + r#"def foo []: [int -> int, string -> int] { mut x = $in; if ($x | describe) == "int" { 3 } else { 4 } }; "100" | foo"#, + "4", + ) +} + +#[test] +fn def_with_in_var_mut_2() -> TestResult { + run_test( + r#"def foo []: [int -> int, string -> int] { mut x = $in; if ($x | describe) == "int" { 3 } else { 4 } }; 100 | foo"#, + "3", + ) +}