Skip to content

Commit 7dfc407

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

14 files changed

+98
-103
lines changed

compiler/rustc_middle/src/ty/consts/lit.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::LitKind;
1+
use rustc_ast::{LitFloatType, LitIntType, LitKind};
22
use rustc_hir;
33
use rustc_macros::HashStable;
44

@@ -44,10 +44,17 @@ pub fn const_lit_matches_ty<'tcx>(
4444
{
4545
true
4646
}
47-
(LitKind::Int(..), ty::Uint(_)) if !neg => true,
48-
(LitKind::Int(..), ty::Int(_)) => true,
47+
(LitKind::Int(_, LitIntType::Unsigned(lit_ty)), ty::Uint(expect_ty)) if !neg => {
48+
lit_ty == *expect_ty
49+
}
50+
(LitKind::Int(_, LitIntType::Signed(lit_ty)), ty::Int(expect_ty)) => lit_ty == *expect_ty,
51+
(LitKind::Int(_, LitIntType::Unsuffixed), ty::Uint(_)) if !neg => true,
52+
(LitKind::Int(_, LitIntType::Unsuffixed), ty::Int(_)) => true,
4953
(LitKind::Bool(..), ty::Bool) => true,
50-
(LitKind::Float(..), ty::Float(_)) => true,
54+
(LitKind::Float(_, LitFloatType::Suffixed(lit_ty)), ty::Float(expect_ty)) => {
55+
lit_ty == *expect_ty
56+
}
57+
(LitKind::Float(_, LitFloatType::Unsuffixed), ty::Float(_)) => true,
5158
(LitKind::Char(..), ty::Char) => true,
5259
(LitKind::Err(..), _) => true,
5360
_ => false,
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)