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

Const RangeInclusive<T> used within match case raises error E0308 #105576

Open
bvanseg opened this issue Dec 11, 2022 · 1 comment
Open

Const RangeInclusive<T> used within match case raises error E0308 #105576

bvanseg opened this issue Dec 11, 2022 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-patterns Relating to patterns and pattern matching D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bvanseg
Copy link

bvanseg commented Dec 11, 2022

I tried this code:

use std::ops::RangeInclusive;

const MEMORY_RANGE: RangeInclusive<u16> = 0x00FF..=0x3FFF;

fn main() {
    let address: u16 = 0x0FFF;

    match address {
        // This is valid.
        0x00FF..=0x3FFF => { /* ... */ },
        // This is not valid.
        MEMORY_RANGE => { /* ... */ }
    }
}

I expected to see this happen: MEMORY_RANGE to compile without error.

Instead, this happened: MEMORY_RANGE as a case within the match raises a compiler error error[E0308]: mismatched types.

Meta

rustc --version --verbose:

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-unknown-linux-gnu
release: 1.65.0
LLVM version: 15.0.0
Backtrace

error[E0308]: mismatched types
  --> src/main.rs:12:9
   |
3  | const MEMORY_RANGE: RangeInclusive<u16> = 0x00FF..=0x3FFF;
   | --------------------------------------- constant defined here
...
8  |     match address {
   |           ------- this expression has type `u16`
...
12 |         MEMORY_RANGE => { /* ... */ }
   |         ^^^^^^^^^^^^
   |         |
   |         expected `u16`, found struct `RangeInclusive`
   |         `MEMORY_RANGE` is interpreted as a constant, not a new binding
   |
   = note: expected type `u16`
            found struct `RangeInclusive<u16>`
help: you may want to move the range into the match block
   |
12 |         0x00FF..=0x3FFF => { /* ... */ }
   |         ~~~~~~~~~~~~~~~

For more information about this error, try `rustc --explain E0308`.

@bvanseg bvanseg added the C-bug Category: This is a bug. label Dec 11, 2022
@Noratrieb
Copy link
Member

This error is actually correct (but it could and should be improved to mention this). The constant being used is a "constant pattern", which has to be of the same type. The range is a "range pattern", which does what you expect.

For more info, see https://doc.rust-lang.org/reference/patterns.html

@fmease fmease added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. A-patterns Relating to patterns and pattern matching and removed C-bug Category: This is a bug. needs-triage-legacy labels Jan 26, 2024
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 D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants