From 2e4900f08504c96365b0c85aa5076dff864ce2e6 Mon Sep 17 00:00:00 2001 From: 132ikl <132@ikl.sh> Date: Mon, 28 Jul 2025 22:24:12 -0400 Subject: [PATCH] Check type of row conditions at parse-time (#16175) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description As a bonus to #16174, I realized it would be trivial to add a similar check to where. Before: ```nushell 1..100 | where 1 # => no output... ``` After: ```nushell 1..100 | where 1 # => Error: nu::parser::type_mismatch # => # => × Type mismatch. # => ╭─[entry #3:1:16] # => 1 │ 1..100 | where 1 # => · ┬ # => · ╰── expected bool, found int # => ╰──── ``` # User-Facing Changes * `where` should now error on row condition expressions which are not booleans # Tests + Formatting Added test # After Submitting N/A --- crates/nu-parser/src/parser.rs | 12 +++++++++++- tests/repl/test_parser.rs | 5 +++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index f02cd0857a..b1ca82106e 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -3742,7 +3742,17 @@ pub fn parse_row_condition(working_set: &mut StateWorkingSet, spans: &[Span]) -> return expression; } _ => { - // We have an expression, so let's convert this into a block. + // We have an expression, check that it's compatible with bool + if !type_compatible(&Type::Bool, &expression.ty) { + working_set.error(ParseError::TypeMismatch( + Type::Bool, + expression.ty.clone(), + expression.span, + )); + return Expression::garbage(working_set, expression.span); + } + + // Convert this expression into a block. let mut block = Block::new(); let mut pipeline = Pipeline::new(); pipeline.elements.push(PipelineElement { diff --git a/tests/repl/test_parser.rs b/tests/repl/test_parser.rs index 45b890653c..0cc4bc4cfe 100644 --- a/tests/repl/test_parser.rs +++ b/tests/repl/test_parser.rs @@ -618,6 +618,11 @@ fn single_value_row_condition() -> TestResult { ) } +#[test] +fn row_condition_non_boolean() -> TestResult { + fail_test(r#"[1 2 3] | where 1"#, "expected bool") +} + #[test] fn performance_nested_lists() -> TestResult { // Parser used to be exponential on deeply nested lists