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::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");
 | 
			
		||||
 | 
			
		||||
@ -181,7 +181,7 @@ fn word(i: &mut Input<'_>) -> PResult<Token> {
 | 
			
		||||
 | 
			
		||||
fn operator(i: &mut Input<'_>) -> PResult<Token> {
 | 
			
		||||
    let (value, range) = alt((
 | 
			
		||||
        ">=", "<=", "==", "=>", "!=", "|>", "*", "+", "-", "/", "%", "=", "<", ">", r"\", "^", "|", "&",
 | 
			
		||||
        ">=", "<=", "==", "=>", "!=", "|>", "*", "+", "-", "/", "%", "=", "<", ">", r"\", "^", "||", "&&", "|", "&",
 | 
			
		||||
    ))
 | 
			
		||||
    .with_span()
 | 
			
		||||
    .parse_next(i)?;
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user