Skip to content

Comparison operators have higher precedence than range operator .. #22877

Open
@cjsut

Description

@cjsut

This has the effect of causing parse errors and other weirdness when trying to use a range literal in a comparison.

1..2 == 1..2

gives us

<input>:1:10: 1:12 error: expected one of `.`, `;`, `<eof>`, or an operator, found `..`
<input>:1 1..2 == 1..2

Huh? Okay, what about this?

1..2 == 1
<anon>:13:20: 13:31 error: start and end of range have incompatible types: expected `_`, found `bool` (expected integral variable, found bool) [E0308]
<anon>:13 println!("{:?}", { 1..2 == 1 });
                             ^~~~~~~~~~~

So it looks as though the comparison is being reduced before the range.

More fun:

1..2 < (3..4)
<anon>:13:27: 13:35 error: mismatched types:
expected `_`,
   found `core::ops::Range<_>`
(expected integral variable,
   found struct `core::ops::Range`) [E0308]
<anon>:13 println!("{:?}", { 1..2 < (3..4) });
                                   ^~~~~~~~

Never mind the fact that ranges don't implement (Partial)Ord -- the precedence is still wrong here, too. Placing the parentheses on the first range instead of the second properly tells us

<anon>:13:20: 13:30 error: binary operation `<` cannot be applied to type `core::ops::Range<_>`
<anon>:13 println!("{:?}", { (1..2) < 3..4 });
                            ^~~~~~~~~~

(although i'd argue that it should also complain about mismatched types here, as well. ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserArea: The lexing & parsing of Rust source code to an ASTC-feature-requestCategory: A feature request, i.e: not implemented / a PR.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.I-needs-decisionIssue: In need of a decision.P-lowLow priorityT-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions