From f8d325dbfef5fec7ee109e37c624236998de8843 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Thu, 27 Jul 2023 06:22:08 +1200 Subject: [PATCH] Set the rest variable to the correct type (#9816) # Description This fixes the type of `$rest` to be a `List<...>` so that it properly is checked in function bodies. fixes https://github.com/nushell/nushell/issues/9809 --- crates/nu-parser/src/parser.rs | 2 +- src/tests/test_parser.rs | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 1aee66d786..c592b4e479 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -3695,7 +3695,7 @@ pub fn parse_signature_helper(working_set: &mut StateWorkingSet, span: Span) -> Arg::RestPositional(PositionalArg { shape, var_id, .. }) => { - working_set.set_variable_type(var_id.expect("internal error: all custom parameters must have var_ids"), syntax_shape.to_type()); + working_set.set_variable_type(var_id.expect("internal error: all custom parameters must have var_ids"), Type::List(Box::new(syntax_shape.to_type()))); *shape = syntax_shape; } Arg::Flag(Flag { arg, var_id, .. }) => { diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index 24ce15b001..7d0b678fd0 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -665,3 +665,11 @@ fn properly_nest_captures() -> TestResult { fn properly_nest_captures_call_first() -> TestResult { run_test(r#"do { let b = 3; c; def c [] { $b }; c }"#, "3") } + +#[test] +fn properly_typecheck_rest_param() -> TestResult { + run_test( + r#"def foo [...rest: string] { $rest | length }; foo "a" "b" "c""#, + "3", + ) +}