Skip to content

Improve error message when incorrectly using the || operator instead of or #22200

@MaddyGuthridge

Description

@MaddyGuthridge

Zig Version

0.13.0

Steps to Reproduce and Observed Output

In the following code, I incorrectly used || instead of or (likely a common beginner mistake).

fn example(x: i32, y: i32) bool {
    return
        x < y
        || x > y + 10
        || y - x == 5;
}

The compiler gives an error about chaining comparison operators, likely because || has a higher precedence than comparison operators.

main.zig:12:24: error: comparison operators cannot be chained
            || pos.col < 0
                       ^

Expected Output

Instead, it would be much more helpful to give an error suggesting the or keyword instead of the || operator. This could be detected by using the fact that || is not defined for anything except for the Error Set Type, meaning that even without the comparison operators, it would not be allowed to compile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    error messageThis issue points out an error message that is unhelpful and should be improved.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions