Skip to content

Commit b4cf9dd

Browse files
committed
Fix ICE when a suffixed literal's type doesn't match the expected const arg type
1 parent 59fd4ef commit b4cf9dd

14 files changed

Lines changed: 88 additions & 99 deletions

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,6 +2851,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
28512851
if const_lit_matches_ty(tcx, &l.lit, l.ty, l.neg) {
28522852
tcx.at(expr.span)
28532853
.lit_to_const(l)
2854+
.filter(|value| value.ty == l.ty)
28542855
.map(|value| ty::Const::new_value(tcx, value.valtree, value.ty))
28552856
} else {
28562857
None
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ compile-flags: -Zvalidate-mir
2+
3+
// Regression test for https://github.com/rust-lang/rust/issues/152962
4+
5+
#![feature(min_generic_const_args)]
6+
#![allow(incomplete_features)]
7+
8+
pub struct A;
9+
10+
pub trait Array {
11+
type const LEN: usize;
12+
fn arr() -> [u8; Self::LEN];
13+
}
14+
15+
impl Array for A {
16+
type const LEN: usize = 0u8;
17+
//~^ ERROR mismatched types
18+
19+
fn arr() -> [u8; const { Self::LEN }] {}
20+
}
21+
22+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/type_const-assoc-mismatched-literal-suffix.rs:16:29
3+
|
4+
LL | type const LEN: usize = 0u8;
5+
| ^^^ expected `usize`, found `u8`
6+
|
7+
help: change the type of the numeric literal from `u8` to `usize`
8+
|
9+
LL - type const LEN: usize = 0u8;
10+
LL + type const LEN: usize = 0usize;
11+
|
12+
13+
error: aborting due to 1 previous error
14+
15+
For more information about this error, try `rustc --explain E0308`.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ compile-flags: --emit=link
2+
3+
// Regression test for https://github.com/rust-lang/rust/issues/152653
4+
5+
#![feature(min_generic_const_args)]
6+
#![expect(incomplete_features)]
7+
8+
type const CONST: usize = 1_i32;
9+
//~^ ERROR mismatched types
10+
11+
fn main() {
12+
CONST;
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/type_const-mismatched-literal-suffix.rs:8:27
3+
|
4+
LL | type const CONST: usize = 1_i32;
5+
| ^^^^^ expected `usize`, found `i32`
6+
|
7+
help: change the type of the numeric literal from `i32` to `usize`
8+
|
9+
LL - type const CONST: usize = 1_i32;
10+
LL + type const CONST: usize = 1_usize;
11+
|
12+
13+
error: aborting due to 1 previous error
14+
15+
For more information about this error, try `rustc --explain E0308`.

tests/ui/const-generics/mgca/type_const-mismatched-types.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
#![feature(min_generic_const_args)]
33

44
type const FREE: u32 = 5_usize;
5-
//~^ ERROR the constant `5` is not of type `u32`
6-
//~| ERROR mismatched types
5+
//~^ ERROR mismatched types
76

87
type const FREE2: isize = FREE;
9-
//~^ ERROR the constant `5` is not of type `isize`
108

119
trait Tr {
1210
type const N: usize;
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
error: the constant `5` is not of type `u32`
2-
--> $DIR/type_const-mismatched-types.rs:4:1
3-
|
4-
LL | type const FREE: u32 = 5_usize;
5-
| ^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `usize`
6-
7-
error: the constant `5` is not of type `isize`
8-
--> $DIR/type_const-mismatched-types.rs:8:1
9-
|
10-
LL | type const FREE2: isize = FREE;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `usize`
12-
13-
error[E0308]: mismatched types
14-
--> $DIR/type_const-mismatched-types.rs:16:27
15-
|
16-
LL | type const N: usize = false;
17-
| ^^^^^ expected `usize`, found `bool`
18-
191
error[E0308]: mismatched types
202
--> $DIR/type_const-mismatched-types.rs:4:24
213
|
@@ -28,6 +10,12 @@ LL - type const FREE: u32 = 5_usize;
2810
LL + type const FREE: u32 = 5_u32;
2911
|
3012

31-
error: aborting due to 4 previous errors
13+
error[E0308]: mismatched types
14+
--> $DIR/type_const-mismatched-types.rs:14:27
15+
|
16+
LL | type const N: usize = false;
17+
| ^^^^^ expected `usize`, found `bool`
18+
19+
error: aborting due to 2 previous errors
3220

3321
For more information about this error, try `rustc --explain E0308`.
Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
error: the constant `1` is not of type `u8`
2-
--> $DIR/type-mismatch.rs:8:27
3-
|
4-
LL | assert_eq!(R.method::<1u16>(), 1);
5-
| ^^^^ expected `u8`, found `u16`
6-
|
7-
note: required by a const generic parameter in `R::method`
8-
--> $DIR/type-mismatch.rs:5:15
9-
|
10-
LL | fn method<const N: u8>(&self) -> u8 { N }
11-
| ^^^^^^^^^^^ required by this const generic parameter in `R::method`
12-
131
error[E0308]: mismatched types
142
--> $DIR/type-mismatch.rs:8:27
153
|
@@ -22,6 +10,6 @@ LL - assert_eq!(R.method::<1u16>(), 1);
2210
LL + assert_eq!(R.method::<1u8>(), 1);
2311
|
2412

25-
error: aborting due to 2 previous errors
13+
error: aborting due to 1 previous error
2614

2715
For more information about this error, try `rustc --explain E0308`.
Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
error: the constant `1` is not of type `u8`
2-
--> $DIR/type-mismatch.rs:8:27
3-
|
4-
LL | assert_eq!(R.method::<1u16>(), 1);
5-
| ^^^^ expected `u8`, found `u16`
6-
|
7-
note: required by a const generic parameter in `R::method`
8-
--> $DIR/type-mismatch.rs:5:15
9-
|
10-
LL | fn method<const N: u8>(&self) -> u8 { N }
11-
| ^^^^^^^^^^^ required by this const generic parameter in `R::method`
12-
131
error[E0308]: mismatched types
142
--> $DIR/type-mismatch.rs:8:27
153
|
@@ -22,6 +10,6 @@ LL - assert_eq!(R.method::<1u16>(), 1);
2210
LL + assert_eq!(R.method::<1u8>(), 1);
2311
|
2412

25-
error: aborting due to 2 previous errors
13+
error: aborting due to 1 previous error
2614

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

tests/ui/const-generics/type-dependent/type-mismatch.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ impl R {
66
}
77
fn main() {
88
assert_eq!(R.method::<1u16>(), 1);
9-
//~^ ERROR the constant `1` is not of type `u8`
10-
//~| ERROR mismatched types
9+
//~^ ERROR mismatched types
1110
}

0 commit comments

Comments
 (0)