Rollup merge of #88090 - nbdd0121:inference, r=nikomatsakis

Perform type inference in range pattern

Fix #88074
This commit is contained in:
Jubilee 2021-10-04 21:12:33 -07:00 committed by GitHub
commit 4f6afee4e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 109 additions and 28 deletions

View file

@ -0,0 +1,28 @@
trait Zero {
const ZERO: Self;
}
impl Zero for String {
const ZERO: Self = String::new();
}
fn foo() {
match String::new() {
Zero::ZERO ..= Zero::ZERO => {},
//~^ ERROR only `char` and numeric types are allowed in range patterns
_ => {},
}
}
fn bar() {
match Zero::ZERO {
Zero::ZERO ..= Zero::ZERO => {},
//~^ ERROR type annotations needed [E0282]
_ => {},
}
}
fn main() {
foo();
bar();
}

View file

@ -0,0 +1,21 @@
error[E0029]: only `char` and numeric types are allowed in range patterns
--> $DIR/issue-88074-pat-range-type-inference-err.rs:11:9
|
LL | Zero::ZERO ..= Zero::ZERO => {},
| ----------^^^^^----------
| | |
| | this is of type `String` but it should be `char` or numeric
| this is of type `String` but it should be `char` or numeric
error[E0282]: type annotations needed
--> $DIR/issue-88074-pat-range-type-inference-err.rs:19:9
|
LL | Zero::ZERO ..= Zero::ZERO => {},
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
|
= note: type must be known at this point
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0029, E0282.
For more information about an error, try `rustc --explain E0029`.

View file

@ -0,0 +1,16 @@
// check-pass
trait Zero {
const ZERO: Self;
}
impl Zero for i32 {
const ZERO: Self = 0;
}
fn main() {
match 1 {
Zero::ZERO ..= 1 => {},
_ => {},
}
}

View file

@ -19,7 +19,6 @@ enum_number!(Change {
Neg = -1,
Arith = 1 + 1, //~ ERROR arbitrary expressions aren't allowed in patterns
//~| ERROR arbitrary expressions aren't allowed in patterns
//~| ERROR only `char` and numeric types are allowed in range patterns
});
fn main() {}

View file

@ -10,15 +10,5 @@ error: arbitrary expressions aren't allowed in patterns
LL | Arith = 1 + 1,
| ^^^^^
error[E0029]: only `char` and numeric types are allowed in range patterns
--> $DIR/patkind-litrange-no-expr.rs:20:13
|
LL | $( $value ..= 42 => Some($name::$variant), )* // PatKind::Range
| -- this is of type `{integer}`
...
LL | Arith = 1 + 1,
| ^^^^^ this is of type `_` but it should be `char` or numeric
error: aborting due to 2 previous errors
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0029`.