Skip to content

Commit 6e37307

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

12 files changed

Lines changed: 48 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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ compile-flags: --emit=link
2+
#![feature(min_generic_const_args)]
3+
#![expect(incomplete_features)]
4+
5+
type const CONST: usize = 1_i32;
6+
//~^ ERROR mismatched types
7+
8+
fn main() {
9+
CONST;
10+
}
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:5: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
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//! Regression test for <https://github.com/rust-lang/rust/issues/133966>
22
pub struct Data([[&'static str]; 5_i32]);
3-
//~^ ERROR the constant `5` is not of type `usize`
4-
//~| ERROR the size for values of type `[&'static str]` cannot be known at compilation time
5-
//~| ERROR mismatched types
3+
//~^ ERROR mismatched types
64
const _: &'static Data = unsafe { &*(&[] as *const Data) };
7-
//~^ ERROR the type `[[&str]; 5]` has an unknown layout
85
fn main() {}
Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,3 @@
1-
error: the constant `5` is not of type `usize`
2-
--> $DIR/array-len-mismatch-type.rs:2:17
3-
|
4-
LL | pub struct Data([[&'static str]; 5_i32]);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `i32`
6-
|
7-
= note: the length of array `[[&'static str]; 5]` must be type `usize`
8-
9-
error[E0277]: the size for values of type `[&'static str]` cannot be known at compilation time
10-
--> $DIR/array-len-mismatch-type.rs:2:17
11-
|
12-
LL | pub struct Data([[&'static str]; 5_i32]);
13-
| ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
14-
|
15-
= help: the trait `Sized` is not implemented for `[&'static str]`
16-
= note: slice and array elements must have `Sized` type
17-
18-
error[E0080]: the type `[[&str]; 5]` has an unknown layout
19-
--> $DIR/array-len-mismatch-type.rs:6:39
20-
|
21-
LL | const _: &'static Data = unsafe { &*(&[] as *const Data) };
22-
| ^^ evaluation of `_` failed here
23-
241
error[E0308]: mismatched types
252
--> $DIR/array-len-mismatch-type.rs:2:34
263
|
@@ -33,7 +10,6 @@ LL - pub struct Data([[&'static str]; 5_i32]);
3310
LL + pub struct Data([[&'static str]; 5_usize]);
3411
|
3512

36-
error: aborting due to 4 previous errors
13+
error: aborting due to 1 previous error
3714

38-
Some errors have detailed explanations: E0080, E0277, E0308.
39-
For more information about an error, try `rustc --explain E0080`.
15+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)