Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure error in else is forwarded appropriately #7411

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 18 additions & 10 deletions crates/nu-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,25 +645,33 @@ pub fn parse_multispan_value(
(arg, error)
}
SyntaxShape::OneOf(shapes) => {
let mut err = None;
for shape in shapes.iter() {
if let (s, None) = parse_multispan_value(
let (s, option_err) = parse_multispan_value(
working_set,
spans,
spans_idx,
shape,
expand_aliases_denylist,
) {
return (s, None);
);
match option_err {
None => return (s, None),
e => err = err.or(e),
}
}
let span = spans[*spans_idx];
(
Expression::garbage(span),
Some(ParseError::Expected(
format!("one of a list of accepted shapes: {:?}", shapes),
span,
)),
)

if err.is_some() {
(Expression::garbage(span), err)
} else {
(
Expression::garbage(span),
Some(ParseError::Expected(
format!("one of a list of accepted shapes: {:?}", shapes),
span,
)),
)
}
}
SyntaxShape::Expression => {
trace!("parsing: expression");
Expand Down
19 changes: 19 additions & 0 deletions crates/nu-parser/tests/test_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,4 +1402,23 @@ mod input_types {
assert!(block.len() == 2, "testing: {}", input);
}
}

#[test]
fn else_errors_correctly() {
let mut engine_state = EngineState::new();
add_declations(&mut engine_state);

let mut working_set = StateWorkingSet::new(&engine_state);
let (_, err) = parse(
&mut working_set,
None,
b"if false { 'a' } else { $foo }",
true,
&[],
);

let err = err.unwrap();

assert!(matches!(err, ParseError::VariableNotFound(_)));
}
}