Operators in patterns have incorrect priorities

12a8392
Opened by Vadim Petrochenkov at 2021-07-30 13:11:01

(Or at least unnatural priorities.)

Binary range operators have higher priority than unary operators like & or box.

#![feature(exclusive_range_pattern)]

fn main() {
    // Interpreted as (&0) .. (10), ERROR mismatched types
    let x = &0 .. 10;

    match &0 {
        &0 .. 10 => {} // OK?!
        _ => {}
    }
}

We can change the priorities for all the unstable kinds of ranges and come up with some deprecation story for stable BEGIN ... END.

  1. (Or at least unnatural priorities.)

    Binary range operators have higher priority than unary operators like & or box.

    #![feature(exclusive_range_pattern)]
    
    fn main() {
        // Interpreted as (&0) .. (10), ERROR mismatched types
        let x = &0 .. 10;
    
        match &0 {
            &0 .. 10 => {} // OK?!
            _ => {}
        }
    }
    

    We can change the priorities for all the unstable kinds of ranges and come up with some deprecation story for stable BEGIN ... END.

    <!-- TRIAGEBOT_START --> <!-- TRIAGEBOT_ASSIGN_START --> <!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":null}$$TRIAGEBOT_ASSIGN_DATA_END --> <!-- TRIAGEBOT_ASSIGN_END --> <!-- TRIAGEBOT_END -->

    rustbot at 2020-11-03 00:07:08

  2. I agree bianary operation is key at this point my friend.

    On Feb 24, 2018 7:47 AM, "Vadim Petrochenkov" notifications@github.com wrote:

    (Or at least unnatural priorities.)

    Binary range operators have higher priority than unary operators like & or box.

    #![feature(exclusive_range_pattern)] fn main() { // Interpreted as (&0) .. (10), ERROR mismatched types let x = &0 .. 10;

    match &0 {
        &0 .. 10 => {} // OK?!
        _ => {}
    }
    

    }

    We can change the priorities for all the unstable kinds of ranges and come up with some deprecation story for stable BEGIN ... END.

    — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rust-lang/rust/issues/48501, or mute the thread https://github.com/notifications/unsubscribe-auth/AhXLZqePgg1g85TIKYBMonnVrsrJZFf5ks5tYATegaJpZM4SR4T8 .

    William L. Hodge at 2018-02-24 12:50:52

  3. This is weird. The behavior in expressions is the one I would expect. However, in patterns you aren't allowed to use parentheses for grouping (why not?) so there's no way to override the precedence.

    Alex Burka at 2018-02-25 02:44:47

  4. However, in patterns you aren't allowed to use parentheses for grouping (why not?) so there's no way to override the precedence.

    That's why I submitted https://github.com/rust-lang/rust/pull/48500 before fixing this :)

    Vadim Petrochenkov at 2018-02-25 08:33:06

  5. Do you have any ideas for how to do that deprecation? If it's smooth enough we can stabilize ..= in patterns anyway and change them at the same time.

    Alex Burka at 2018-02-27 00:29:55

  6. @durka

    Do you have any ideas for how to do that deprecation?

    Compatibility warning, as usual? I plan to prepare the fix this weekend, so it shouldn't delay stabilization of ..= in patterns too much.

    Vadim Petrochenkov at 2018-02-27 08:08:02

  7. Nominating because of @workingjubilee's comment in https://github.com/rust-lang/rust/issues/67264#issuecomment-718166790

    I would be happy to draft the PR to stabilize this but first I want to know if #48501 is something that needs action on it still.

    scottmcm at 2020-10-28 19:51:30

  8. @rustbot claim

    Jubilee at 2020-11-03 00:07:06

  9. Whoops, wrong spot. @rustbot release-assignment

    Jubilee at 2020-11-03 00:21:43