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

Range pattern does not perform type inference #88074

Closed
nbdd0121 opened this issue Aug 16, 2021 · 3 comments · Fixed by #88090
Closed

Range pattern does not perform type inference #88074

nbdd0121 opened this issue Aug 16, 2021 · 3 comments · Fixed by #88090
Assignees
Labels
A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nbdd0121
Copy link
Contributor

nbdd0121 commented Aug 16, 2021

trait Zero {
    const ZERO: Self;
}

impl Zero for i32 {
    const ZERO: Self = 0;
}

fn foo() {
    match 1 {
        Zero::ZERO ..= 1 => {},
        _ => {},
    }
}

currently throws E0029:

error[E0029]: only `char` and numeric types are allowed in range patterns
  --> src/lib.rs:11:9
   |
11 |         Zero::ZERO ..= 1 => {},
   |         ^^^^^^^^^^     - this is of type `{integer}`
   |         |
   |         this is of type `_` but it should be `char` or numeric

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

while it should infer the constant to be <i32 as Zero>::ZERO.

@rustbot modify labels: +T-compiler +A-inference
@rustbot claim

@nbdd0121 nbdd0121 added the C-bug Category: This is a bug. label Aug 16, 2021
@rustbot rustbot added A-inference Area: Type inference T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 16, 2021
@jyn514
Copy link
Member

jyn514 commented Aug 16, 2021

I don't think this is incorrect. Zero::ZERO doesn't refer to a specific constant, it's just an abstract associated constant. i32::ZERO works fine.

@jyn514
Copy link
Member

jyn514 commented Aug 16, 2021

Hmm, let x: i32 = Zero::ZERO does work, though. But let x = Zero::ZERO does not. It does seem inconsistent that let x: i32 = Zero::ZERO works and Zero::ZERO ..= 1_i32 doesn't.

@nbdd0121
Copy link
Contributor Author

Zero::ZERO is just <_ as Zero>::ZERO, so type inference should kick in here. In fact you can write

match 1 {
    Zero::ZERO => {},
    _ => {},
}

and inference works just fine. The issue only happens when used in range patterns.

I am prototyping a fix right now and it seems that the issue is just that E0029 check happens a little bit early (before unification is performed).

Manishearth added a commit to Manishearth/rust that referenced this issue Oct 5, 2021
Manishearth added a commit to Manishearth/rust that referenced this issue Oct 5, 2021
Manishearth added a commit to Manishearth/rust that referenced this issue Oct 5, 2021
@bors bors closed this as completed in 4f6afee Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference C-bug Category: This is a bug. 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.

3 participants