Fix bad operator (#3479)

This commit is contained in:
JT 2021-05-24 17:27:10 +12:00 committed by GitHub
parent 3bcc2aad80
commit 5fcc7f2328
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 4 deletions

View File

@ -59,7 +59,11 @@ pub fn evaluate_baseline_expr(
)),
}
}
_ => unreachable!(),
_ => Err(ShellError::labeled_error(
"Unknown operator",
"unknown operator",
binary.op.span,
)),
}
}
Expression::Range(range) => {

View File

@ -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;
}

View File

@ -59,7 +59,7 @@ pub fn expression_to_flat_shape(e: &SpannedExpression) -> Vec<Spanned<FlatShape>
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
}

View File

@ -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!(