From a4809d2f0862248ddc72e2bc961a5ada77964453 Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Thu, 25 Jan 2024 14:16:49 +0800 Subject: [PATCH] Remove `--flag: bool` support (#11541) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description This is a follow up to: #11365 After this pr, `--flag: bool` is no longer allowed. I think `ParseWarning::Deprecated` is useful when we want to deprecated something at syntax level, so I just leave it there for now. # User-Facing Changes ## Before ``` ❯ def foo [--b: bool] {} Error: × Deprecated: --flag: bool ╭─[entry #15:1:1] 1 │ def foo [--b: bool] {} · ──┬─ · ╰── `--flag: bool` is deprecated and will be removed in 0.90. Please use `--flag` instead, more info: https://www.nushell.sh/book/custom_commands.html ╰──── ``` ## After ``` ❯ def foo [--b: bool] {} Error: × Type annotations are not allowed for boolean switches. ╭─[entry #2:1:1] 1 │ def foo [--b: bool] {} · ──┬─ · ╰── Remove the `: bool` type annotation. ╰──── ``` # Tests + Formatting Done --- crates/nu-command/tests/commands/def.rs | 13 ------------- crates/nu-parser/src/parser.rs | 10 +++++----- src/tests/test_custom_commands.rs | 3 +-- 3 files changed, 6 insertions(+), 20 deletions(-) diff --git a/crates/nu-command/tests/commands/def.rs b/crates/nu-command/tests/commands/def.rs index c39e89ada2..0ec13c0511 100644 --- a/crates/nu-command/tests/commands/def.rs +++ b/crates/nu-command/tests/commands/def.rs @@ -172,19 +172,6 @@ fn def_default_value_should_restrict_implicit_type() { assert!(actual2.err.contains("expected int")); } -#[test] -fn def_boolean_flags() { - let actual = nu!("def foo [--x: bool] { $x }; foo --x"); - assert!(actual.err.contains("flag missing bool argument")); - let actual = nu!("def foo [--x: bool = false] { $x }; foo"); - assert_eq!(actual.out, "false"); - let actual = nu!("def foo [--x: bool = false] { $x }; foo --x"); - assert!(actual.err.contains("flag missing bool argument")); - // boolean flags' default value should be null - let actual = nu!("def foo [--x: bool] { $x == null }; foo"); - assert_eq!(actual.out, "true"); -} - #[test] fn def_wrapped_with_block() { let actual = nu!( diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 6e1ab7fc6f..a89b0dfdaa 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -18,8 +18,8 @@ use nu_protocol::{ }, engine::StateWorkingSet, eval_const::eval_constant, - span, BlockId, DidYouMean, Flag, ParseError, ParseWarning, PositionalArg, Signature, Span, - Spanned, SyntaxShape, Type, Unit, VarId, ENV_VARIABLE_ID, IN_VARIABLE_ID, + span, BlockId, DidYouMean, Flag, ParseError, PositionalArg, Signature, Span, Spanned, + SyntaxShape, Type, Unit, VarId, ENV_VARIABLE_ID, IN_VARIABLE_ID, }; use crate::parse_keywords::{ @@ -3603,9 +3603,9 @@ pub fn parse_signature_helper(working_set: &mut StateWorkingSet, span: Span) -> } => { working_set.set_variable_type(var_id.expect("internal error: all custom parameters must have var_ids"), syntax_shape.to_type()); if syntax_shape == SyntaxShape::Boolean { - working_set.warning(ParseWarning::DeprecatedWarning( - "--flag: bool".to_string(), - "--flag".to_string(), + working_set.error(ParseError::LabeledError( + "Type annotations are not allowed for boolean switches.".to_string(), + "Remove the `: bool` type annotation.".to_string(), span, )); } diff --git a/src/tests/test_custom_commands.rs b/src/tests/test_custom_commands.rs index 48e575a14b..fe6f8dfa76 100644 --- a/src/tests/test_custom_commands.rs +++ b/src/tests/test_custom_commands.rs @@ -149,8 +149,7 @@ fn custom_flag2() -> TestResult { #[test] fn deprecated_boolean_flag() { let actual = nu!(r#"def florb [--dry-run: bool, --another-flag] { "aaa" }; florb"#); - assert!(actual.err.contains("Deprecated")); - assert_eq!(actual.out, "aaa"); + assert!(actual.err.contains("not allowed")); } #[test]