Non-fatal error on using && or || (#6845)

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-05-12 13:46:17 +12:00
committed by GitHub
parent 17b1120a27
commit dfc4b7d0c5
2 changed files with 28 additions and 1 deletions

View File

@ -582,6 +582,26 @@ fn binary_operator(i: &mut TokenSlice) -> PResult<BinaryOperator> {
"<=" => BinaryOperator::Lte,
"|" => BinaryOperator::Or,
"&" => BinaryOperator::And,
"||" => {
ParseContext::err(
CompilationError::err(
token.as_source_range(),
"`||` is not an operator, did you mean to use `|`?",
)
.with_suggestion("Replace `||` with `|`", "|", None, Tag::None),
);
BinaryOperator::Or
}
"&&" => {
ParseContext::err(
CompilationError::err(
token.as_source_range(),
"`&&` is not an operator, did you mean to use `&`?",
)
.with_suggestion("Replace `&&` with `&`", "&", None, Tag::None),
);
BinaryOperator::And
}
_ => {
return Err(CompilationError::fatal(
token.as_source_range(),
@ -4511,6 +4531,13 @@ export fn cos(num: number(rad)): number(_) {}"#;
assert_eq!(errs.len(), 3, "found: {errs:#?}");
}
#[test]
fn error_double_and() {
let (_, errs) = assert_no_fatal("foo = true && false");
assert_eq!(errs.len(), 1, "found: {errs:#?}");
assert!(errs[0].message.contains("`&&`") && errs[0].message.contains("`&`") && errs[0].suggestion.is_some());
}
#[test]
fn error_type_ascription() {
let (_, errs) = assert_no_fatal("a + b: number");