From 744a28b31dde95ff0aea7e7190f5b8f39427ead3 Mon Sep 17 00:00:00 2001 From: mike <98623181+1Kinoti@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:57:18 +0300 Subject: [PATCH] type-check default values of list annotations (#8600) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description @fdncred noticed an [issue](https://github.com/nushell/nushell/pull/8529#issuecomment-1482770636) with list annotations that while i was trying to find a fix found another issue. innitially, this was accepted by the parser ```nu def err [list: list = ['a' 'b' 'c']] {} ``` but now an error is raised ```nu Error: nu::parser::assignment_mismatch × Default value wrong type ╭─[entry #1:1:1] 1 │ def err [list: list = ['a' 'b' 'c']] {} · ──────┬──── · ╰── expected default value to be `list` ╰──── ``` # User-Facing Changes none # Tests + Formatting done --- crates/nu-parser/src/parser.rs | 34 +++++++++++++++++++++++++--------- src/tests/test_signatures.rs | 14 ++++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 788bafbcf0..1a30d875f6 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -3995,19 +3995,35 @@ pub fn parse_signature_helper( expression.ty.clone(), ); } - Type::List(_) => { - if var_type.is_list() && expression.ty.is_list() { - working_set.set_variable_type( - var_id, - expression.ty.clone(), - ); + Type::List(param_ty) => { + if let Type::List(expr_ty) = &expression.ty { + if param_ty == expr_ty + || **param_ty == Type::Any + { + working_set.set_variable_type( + var_id, + expression.ty.clone(), + ); + } else { + error = error.or_else(|| { + Some( + ParseError::AssignmentMismatch( + "Default value wrong type" + .into(), + format!( + "expected default value to be `{var_type}`", + ), + expression.span, + ), + ) + }) + } } else { error = error.or_else(|| { Some(ParseError::AssignmentMismatch( "Default value wrong type".into(), format!( - "default value not {0}", - expression.ty + "expected default value to be `{var_type}`", ), expression.span, )) @@ -4019,7 +4035,7 @@ pub fn parse_signature_helper( error = error.or_else(|| { Some(ParseError::AssignmentMismatch( "Default value wrong type".into(), - format!("default value not {t}"), + format!("expected default value to be `{t}`"), expression.span, )) }) diff --git a/src/tests/test_signatures.rs b/src/tests/test_signatures.rs index 68fd31b80e..dd06f40d63 100644 --- a/src/tests/test_signatures.rs +++ b/src/tests/test_signatures.rs @@ -111,3 +111,17 @@ fn list_annotations_unknown_separators() -> TestResult { let expected = "unknown type"; fail_test(input, expected) } + +#[test] +fn list_annotations_with_default_val_1() -> TestResult { + let input = "def run [list: list = [2 5 4]] {$list | length}; run"; + let expected = "3"; + run_test(input, expected) +} + +#[test] +fn list_annotations_with_default_val_2() -> TestResult { + let input = "def run [list: list = [2 5 4]] {$list | length}; run"; + let expected = "Default value wrong type"; + fail_test(input, expected) +}