diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index ef72e8a104..185142d139 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -2057,6 +2057,10 @@ pub fn parse_brace_expr( } else if matches!(second_token_contents, Some(TokenContents::Pipe)) || matches!(second_token_contents, Some(TokenContents::PipePipe)) { + if matches!(shape, SyntaxShape::Block) { + working_set.error(ParseError::Mismatch("block".into(), "closure".into(), span)); + return Expression::garbage(working_set, span); + } parse_closure_expression(working_set, shape, span) } else if matches!(third_token, Some(b":")) { parse_full_cell_path(working_set, None, span) diff --git a/crates/nu-parser/tests/test_parser.rs b/crates/nu-parser/tests/test_parser.rs index 4dd7715067..8b4fd5e95b 100644 --- a/crates/nu-parser/tests/test_parser.rs +++ b/crates/nu-parser/tests/test_parser.rs @@ -2796,6 +2796,26 @@ mod input_types { } } + #[test] + fn closure_in_block_position_errors_correctly() { + let mut engine_state = EngineState::new(); + add_declarations(&mut engine_state); + + let mut working_set = StateWorkingSet::new(&engine_state); + let inputs = [r#"if true { || print hi }"#, r#"if true { |x| $x }"#]; + + for input in inputs { + parse(&mut working_set, None, input.as_bytes(), true); + assert!( + matches!( + working_set.parse_errors.first(), + Some(ParseError::Mismatch(_, _, _)) + ), + "testing: {input}" + ); + } + } + #[test] fn else_errors_correctly() { let mut engine_state = EngineState::new();