Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matching a const Range #76191

Closed
Typas opened this issue Sep 1, 2020 · 4 comments · Fixed by #76222
Closed

Matching a const Range #76191

Typas opened this issue Sep 1, 2020 · 4 comments · Fixed by #76222
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-patterns Relating to patterns and pattern matching C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Typas
Copy link

Typas commented Sep 1, 2020

When I was doing rustlings, I found out it is possible to match with a range which is coded like this:

fn color(n: i32) -> Option<u8> {
    match n {
        0..=255 => Some(n as u8),
        _ => None,
    }
}

but when I trying to replace the range with a const,

use std::ops::RangeInclusive;

const RGB_RANGE: RangeInclusive<i32> = 0..=255;

fn color(n: i32) -> Option<u8> {
    match n {
        RGB_RANGE => Some(n as u8),
        _ => None,
    }
}

the compiler gives error:

error[E0308]: mismatched types
 --> src/main.rs:7:9
  |
3 | const RGB: RangeInclusive<i32> = 0..=255;
  | ----------------------------------------- constant defined here
...
6 |     match n {
  |           - this expression has type `i32`
7 |         RGB => Some(n as u8),
  |         ^^^
  |         |
  |         expected `i32`, found struct `std::ops::RangeInclusive`
  |         `RGB` is interpreted as a constant, not a new binding
  |         help: introduce a new binding instead: `other_rgb`
  |
  = note: expected type `i32`
           found struct `std::ops::RangeInclusive<i32>`

I have no idea why matching a literal range is possible but not matching a constant which type is RangeInclusive.

@matprec
Copy link
Contributor

matprec commented Sep 1, 2020

This is probably #76001

@workingjubilee
Copy link
Contributor

workingjubilee commented Sep 1, 2020

Sort of, integrating consts properly into pattern matching has been a long story. #74446 has been the latest chapter.

@jonas-schievink
Copy link
Contributor

This is currently the intended behavior of const patterns – they just compare the matched expression with the constant. The diagnostic could be improved though.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-patterns Relating to patterns and pattern matching C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 1, 2020
@guswynn
Copy link
Contributor

guswynn commented Sep 1, 2020

I might take a look at improving this diagnostic (fair warning, would be my first contribution)

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Sep 12, 2020
Give better suggestion when const Range*'s are used as patterns

Fixes rust-lang#76191

let me know if there is more/different information you want to show in this case
@bors bors closed this as completed in 2e2e7de Sep 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-patterns Relating to patterns and pattern matching C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants