From 5fcc7f23285b1abe6eb9b3a4985b9c76b0b9be80 Mon Sep 17 00:00:00 2001 From: JT Date: Mon, 24 May 2021 17:27:10 +1200 Subject: [PATCH] Fix bad operator (#3479) --- crates/nu-engine/src/evaluate/evaluator.rs | 6 +++++- crates/nu-parser/src/parse.rs | 7 +++++-- crates/nu-parser/src/shapes.rs | 2 +- tests/shell/pipeline/commands/internal.rs | 12 ++++++++++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/crates/nu-engine/src/evaluate/evaluator.rs b/crates/nu-engine/src/evaluate/evaluator.rs index 5533d47ccf..fea3bd9271 100644 --- a/crates/nu-engine/src/evaluate/evaluator.rs +++ b/crates/nu-engine/src/evaluate/evaluator.rs @@ -59,7 +59,11 @@ pub fn evaluate_baseline_expr( )), } } - _ => unreachable!(), + _ => Err(ShellError::labeled_error( + "Unknown operator", + "unknown operator", + binary.op.span, + )), } } Expression::Range(range) => { diff --git a/crates/nu-parser/src/parse.rs b/crates/nu-parser/src/parse.rs index 4aa18e0a00..bcf88b4999 100644 --- a/crates/nu-parser/src/parse.rs +++ b/crates/nu-parser/src/parse.rs @@ -907,7 +907,10 @@ fn parse_arg( } SyntaxShape::Range => parse_range(&lite_arg, scope), - SyntaxShape::Operator => parse_operator(&lite_arg), + SyntaxShape::Operator => ( + garbage(lite_arg.span), + Some(ParseError::mismatch("operator", lite_arg.clone())), + ), SyntaxShape::Filesize => parse_filesize(&lite_arg), SyntaxShape::Duration => parse_duration(&lite_arg), SyntaxShape::FilePath => { @@ -1230,7 +1233,7 @@ pub fn parse_math_expression( prec.push(0); while idx < lite_args.len() { - let (op, err) = parse_arg(SyntaxShape::Operator, scope, &lite_args[idx]); + let (op, err) = parse_operator(&lite_args[idx]); if error.is_none() { error = err; } diff --git a/crates/nu-parser/src/shapes.rs b/crates/nu-parser/src/shapes.rs index 5a8b2c8fa6..0065c1afde 100644 --- a/crates/nu-parser/src/shapes.rs +++ b/crates/nu-parser/src/shapes.rs @@ -59,7 +59,7 @@ pub fn expression_to_flat_shape(e: &SpannedExpression) -> Vec Expression::Binary(binary) => { let mut output = vec![]; output.append(&mut expression_to_flat_shape(&binary.left)); - output.push(FlatShape::Operator.spanned(binary.op.span)); + output.append(&mut expression_to_flat_shape(&binary.op)); output.append(&mut expression_to_flat_shape(&binary.right)); output } diff --git a/tests/shell/pipeline/commands/internal.rs b/tests/shell/pipeline/commands/internal.rs index 830a5c9145..dd78059ada 100644 --- a/tests/shell/pipeline/commands/internal.rs +++ b/tests/shell/pipeline/commands/internal.rs @@ -433,6 +433,18 @@ fn can_process_one_row_from_internal_and_pipes_it_to_stdin_of_external() { assert_eq!(actual.out, "nushell"); } +#[test] +fn bad_operator() { + let actual = nu!( + cwd: ".", + r#" + 2 $ 2 + "# + ); + + assert!(actual.err.contains("operator")); +} + #[test] fn index_out_of_bounds() { let actual = nu!(