From ddf0e4a7bb9887b2b6b26ebcf3bfd67aa8425834 Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Tue, 19 Sep 2023 17:36:04 +0800 Subject: [PATCH 1/3] don't overrite arg's type if it's annotated explicitly --- crates/nu-command/tests/commands/def.rs | 16 +++++++++++++++ crates/nu-parser/src/parser.rs | 27 +++++++++++++++++-------- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/crates/nu-command/tests/commands/def.rs b/crates/nu-command/tests/commands/def.rs index 65d5d58db3f9..89a910b8b95b 100644 --- a/crates/nu-command/tests/commands/def.rs +++ b/crates/nu-command/tests/commands/def.rs @@ -164,3 +164,19 @@ fn extern_with_block() { assert_eq!(actual.out, "--bar,baz,--,-q,-u,-x"); } + +#[test] +fn def_default_value_shouldnt_restrict_explicit_type() { + let actual = nu!("def foo [x: any = null] { $x }; foo 1"); + assert_eq!(actual.out, "1"); + let actual2 = nu!("def foo [--x: any = null] { $x }; foo --x 1"); + assert_eq!(actual2.out, "1"); +} + +#[test] +fn def_defualt_value_should_restrict_implicit_type() { + let actual = nu!("def foo [x = 3] { $x }; foo 'a'"); + assert!(actual.err.contains("expected int")); + let actual2 = nu!("def foo2 [--x = 'a'] { $x }; foo2 --x 3"); + assert!(actual2.err.contains("expected string")); +} diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index b56c60022996..507c61269441 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -3374,6 +3374,7 @@ pub fn parse_signature_helper(working_set: &mut StateWorkingSet, span: Span) -> let mut args: Vec = vec![]; let mut parse_mode = ParseMode::ArgMode; + let mut arg_explicit_type = false; for token in &output { match token { @@ -3428,6 +3429,7 @@ pub fn parse_signature_helper(working_set: &mut StateWorkingSet, span: Span) -> working_set.error(ParseError::Expected("default value", span)); } } + arg_explicit_type = false; } else { match parse_mode { ParseMode::ArgMode | ParseMode::AfterCommaArgMode => { @@ -3710,6 +3712,7 @@ pub fn parse_signature_helper(working_set: &mut StateWorkingSet, span: Span) -> } } } + arg_explicit_type = true; } parse_mode = ParseMode::ArgMode; } @@ -3732,10 +3735,12 @@ pub fn parse_signature_helper(working_set: &mut StateWorkingSet, span: Span) -> let var_type = &working_set.get_variable(var_id).ty; match var_type { Type::Any => { - working_set.set_variable_type( - var_id, - expression.ty.clone(), - ); + if !arg_explicit_type { + working_set.set_variable_type( + var_id, + expression.ty.clone(), + ); + } } _ => { if !type_compatible(var_type, &expression.ty) { @@ -3763,7 +3768,9 @@ pub fn parse_signature_helper(working_set: &mut StateWorkingSet, span: Span) -> None }; - *shape = expression.ty.to_shape(); + if !arg_explicit_type { + *shape = expression.ty.to_shape(); + } *required = false; } Arg::RestPositional(..) => { @@ -3800,9 +3807,13 @@ pub fn parse_signature_helper(working_set: &mut StateWorkingSet, span: Span) -> if var_type != &Type::Bool { match var_type { Type::Any => { - *arg = Some(expression_ty.to_shape()); - working_set - .set_variable_type(var_id, expression_ty); + if !arg_explicit_type { + *arg = Some(expression_ty.to_shape()); + working_set.set_variable_type( + var_id, + expression_ty, + ); + } } t => { if t != &expression_ty { From 17fcc32c9ff319e8f7c9f55e01772198287dd881 Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Tue, 19 Sep 2023 17:58:33 +0800 Subject: [PATCH 2/3] fix typo --- crates/nu-command/tests/commands/def.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/nu-command/tests/commands/def.rs b/crates/nu-command/tests/commands/def.rs index 89a910b8b95b..4232d373b2af 100644 --- a/crates/nu-command/tests/commands/def.rs +++ b/crates/nu-command/tests/commands/def.rs @@ -174,7 +174,7 @@ fn def_default_value_shouldnt_restrict_explicit_type() { } #[test] -fn def_defualt_value_should_restrict_implicit_type() { +fn def_default_value_should_restrict_implicit_type() { let actual = nu!("def foo [x = 3] { $x }; foo 'a'"); assert!(actual.err.contains("expected int")); let actual2 = nu!("def foo2 [--x = 'a'] { $x }; foo2 --x 3"); From 94b7cd631ab19c408192da07a3334184a68ebc4f Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Tue, 19 Sep 2023 23:01:17 +0800 Subject: [PATCH 3/3] adjust test --- crates/nu-command/tests/commands/def.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/tests/commands/def.rs b/crates/nu-command/tests/commands/def.rs index 4232d373b2af..e149c159fca4 100644 --- a/crates/nu-command/tests/commands/def.rs +++ b/crates/nu-command/tests/commands/def.rs @@ -175,8 +175,8 @@ fn def_default_value_shouldnt_restrict_explicit_type() { #[test] fn def_default_value_should_restrict_implicit_type() { - let actual = nu!("def foo [x = 3] { $x }; foo 'a'"); + let actual = nu!("def foo [x = 3] { $x }; foo 3.0"); assert!(actual.err.contains("expected int")); - let actual2 = nu!("def foo2 [--x = 'a'] { $x }; foo2 --x 3"); - assert!(actual2.err.contains("expected string")); + let actual2 = nu!("def foo2 [--x = 3] { $x }; foo2 --x 3.0"); + assert!(actual2.err.contains("expected int")); }