Non-fatal error on using && or || (#6845)
Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
@ -582,6 +582,26 @@ fn binary_operator(i: &mut TokenSlice) -> PResult<BinaryOperator> {
|
|||||||
"<=" => BinaryOperator::Lte,
|
"<=" => BinaryOperator::Lte,
|
||||||
"|" => BinaryOperator::Or,
|
"|" => BinaryOperator::Or,
|
||||||
"&" => BinaryOperator::And,
|
"&" => 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(
|
return Err(CompilationError::fatal(
|
||||||
token.as_source_range(),
|
token.as_source_range(),
|
||||||
@ -4511,6 +4531,13 @@ export fn cos(num: number(rad)): number(_) {}"#;
|
|||||||
assert_eq!(errs.len(), 3, "found: {errs:#?}");
|
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]
|
#[test]
|
||||||
fn error_type_ascription() {
|
fn error_type_ascription() {
|
||||||
let (_, errs) = assert_no_fatal("a + b: number");
|
let (_, errs) = assert_no_fatal("a + b: number");
|
||||||
|
@ -181,7 +181,7 @@ fn word(i: &mut Input<'_>) -> PResult<Token> {
|
|||||||
|
|
||||||
fn operator(i: &mut Input<'_>) -> PResult<Token> {
|
fn operator(i: &mut Input<'_>) -> PResult<Token> {
|
||||||
let (value, range) = alt((
|
let (value, range) = alt((
|
||||||
">=", "<=", "==", "=>", "!=", "|>", "*", "+", "-", "/", "%", "=", "<", ">", r"\", "^", "|", "&",
|
">=", "<=", "==", "=>", "!=", "|>", "*", "+", "-", "/", "%", "=", "<", ">", r"\", "^", "||", "&&", "|", "&",
|
||||||
))
|
))
|
||||||
.with_span()
|
.with_span()
|
||||||
.parse_next(i)?;
|
.parse_next(i)?;
|
||||||
|
Reference in New Issue
Block a user