Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2851,6 +2851,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
if const_lit_matches_ty(tcx, &l.lit, l.ty, l.neg) {
tcx.at(expr.span)
.lit_to_const(l)
.filter(|value| value.ty == l.ty)
.map(|value| ty::Const::new_value(tcx, value.valtree, value.ty))
} else {
None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//@ compile-flags: -Zvalidate-mir

// Regression test for https://github.com/rust-lang/rust/issues/152962

#![feature(min_generic_const_args)]
#![allow(incomplete_features)]

pub struct A;

pub trait Array {
type const LEN: usize;
fn arr() -> [u8; Self::LEN];
}

impl Array for A {
type const LEN: usize = 0u8;
//~^ ERROR mismatched types

fn arr() -> [u8; const { Self::LEN }] {}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0308]: mismatched types
--> $DIR/type_const-assoc-mismatched-literal-suffix.rs:16:29
|
LL | type const LEN: usize = 0u8;
| ^^^ expected `usize`, found `u8`
|
help: change the type of the numeric literal from `u8` to `usize`
|
LL - type const LEN: usize = 0u8;
LL + type const LEN: usize = 0usize;
|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ compile-flags: --emit=link
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was required to perform MIR analysis.


// Regression test for https://github.com/rust-lang/rust/issues/152653

#![feature(min_generic_const_args)]
#![expect(incomplete_features)]

type const CONST: usize = 1_i32;
//~^ ERROR mismatched types

fn main() {
CONST;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0308]: mismatched types
--> $DIR/type_const-mismatched-literal-suffix.rs:8:27
|
LL | type const CONST: usize = 1_i32;
| ^^^^^ expected `usize`, found `i32`
|
help: change the type of the numeric literal from `i32` to `usize`
|
LL - type const CONST: usize = 1_i32;
LL + type const CONST: usize = 1_usize;
|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
4 changes: 1 addition & 3 deletions tests/ui/const-generics/mgca/type_const-mismatched-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
#![feature(min_generic_const_args)]

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

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

trait Tr {
type const N: usize;
Expand Down
26 changes: 7 additions & 19 deletions tests/ui/const-generics/mgca/type_const-mismatched-types.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
error: the constant `5` is not of type `u32`
--> $DIR/type_const-mismatched-types.rs:4:1
|
LL | type const FREE: u32 = 5_usize;
| ^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `usize`

error: the constant `5` is not of type `isize`
--> $DIR/type_const-mismatched-types.rs:8:1
|
LL | type const FREE2: isize = FREE;
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `usize`

error[E0308]: mismatched types
--> $DIR/type_const-mismatched-types.rs:16:27
|
LL | type const N: usize = false;
| ^^^^^ expected `usize`, found `bool`

Comment on lines -1 to -18
Copy link
Contributor Author

@lapla-cogito lapla-cogito Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix, as mentioned above, fixes the ICE while simultaneously reverting the seemingly redundant error output introduced in #152001 (in fact, this ICE was identified as originating from this PR in the original issue).

error[E0308]: mismatched types
--> $DIR/type_const-mismatched-types.rs:4:24
|
Expand All @@ -28,6 +10,12 @@ LL - type const FREE: u32 = 5_usize;
LL + type const FREE: u32 = 5_u32;
|

error: aborting due to 4 previous errors
error[E0308]: mismatched types
--> $DIR/type_const-mismatched-types.rs:14:27
|
LL | type const N: usize = false;
| ^^^^^ expected `usize`, found `bool`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
14 changes: 1 addition & 13 deletions tests/ui/const-generics/type-dependent/type-mismatch.full.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
error: the constant `1` is not of type `u8`
--> $DIR/type-mismatch.rs:8:27
|
LL | assert_eq!(R.method::<1u16>(), 1);
| ^^^^ expected `u8`, found `u16`
|
note: required by a const generic parameter in `R::method`
--> $DIR/type-mismatch.rs:5:15
|
LL | fn method<const N: u8>(&self) -> u8 { N }
| ^^^^^^^^^^^ required by this const generic parameter in `R::method`

error[E0308]: mismatched types
--> $DIR/type-mismatch.rs:8:27
|
Expand All @@ -22,6 +10,6 @@ LL - assert_eq!(R.method::<1u16>(), 1);
LL + assert_eq!(R.method::<1u8>(), 1);
|

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
14 changes: 1 addition & 13 deletions tests/ui/const-generics/type-dependent/type-mismatch.min.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
error: the constant `1` is not of type `u8`
--> $DIR/type-mismatch.rs:8:27
|
LL | assert_eq!(R.method::<1u16>(), 1);
| ^^^^ expected `u8`, found `u16`
|
note: required by a const generic parameter in `R::method`
--> $DIR/type-mismatch.rs:5:15
|
LL | fn method<const N: u8>(&self) -> u8 { N }
| ^^^^^^^^^^^ required by this const generic parameter in `R::method`

error[E0308]: mismatched types
--> $DIR/type-mismatch.rs:8:27
|
Expand All @@ -22,6 +10,6 @@ LL - assert_eq!(R.method::<1u16>(), 1);
LL + assert_eq!(R.method::<1u8>(), 1);
|

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
3 changes: 1 addition & 2 deletions tests/ui/const-generics/type-dependent/type-mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ impl R {
}
fn main() {
assert_eq!(R.method::<1u16>(), 1);
//~^ ERROR the constant `1` is not of type `u8`
//~| ERROR mismatched types
//~^ ERROR mismatched types
}
5 changes: 1 addition & 4 deletions tests/ui/consts/const-eval/array-len-mismatch-type.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! Regression test for <https://github.com/rust-lang/rust/issues/133966>
pub struct Data([[&'static str]; 5_i32]);
//~^ ERROR the constant `5` is not of type `usize`
//~| ERROR the size for values of type `[&'static str]` cannot be known at compilation time
//~| ERROR mismatched types
//~^ ERROR mismatched types
const _: &'static Data = unsafe { &*(&[] as *const Data) };
//~^ ERROR the type `[[&str]; 5]` has an unknown layout
fn main() {}
28 changes: 2 additions & 26 deletions tests/ui/consts/const-eval/array-len-mismatch-type.stderr
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
error: the constant `5` is not of type `usize`
--> $DIR/array-len-mismatch-type.rs:2:17
|
LL | pub struct Data([[&'static str]; 5_i32]);
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `i32`
|
= note: the length of array `[[&'static str]; 5]` must be type `usize`

error[E0277]: the size for values of type `[&'static str]` cannot be known at compilation time
--> $DIR/array-len-mismatch-type.rs:2:17
|
LL | pub struct Data([[&'static str]; 5_i32]);
| ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[&'static str]`
= note: slice and array elements must have `Sized` type

error[E0080]: the type `[[&str]; 5]` has an unknown layout
--> $DIR/array-len-mismatch-type.rs:6:39
|
LL | const _: &'static Data = unsafe { &*(&[] as *const Data) };
| ^^ evaluation of `_` failed here

error[E0308]: mismatched types
--> $DIR/array-len-mismatch-type.rs:2:34
|
Expand All @@ -33,7 +10,6 @@ LL - pub struct Data([[&'static str]; 5_i32]);
LL + pub struct Data([[&'static str]; 5_usize]);
|

error: aborting due to 4 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0080, E0277, E0308.
For more information about an error, try `rustc --explain E0080`.
For more information about this error, try `rustc --explain E0308`.
5 changes: 1 addition & 4 deletions tests/ui/repeat-expr/repeat_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ fn main() {
//~| NOTE expected `usize`, found `isize`
//~| NOTE `-1_isize` cannot fit into type `usize`
let h = [0; 4u8];
//~^ ERROR the constant `4` is not of type `usize`
//~| NOTE expected `usize`, found `u8`
//~| NOTE the length of array `[{integer}; 4]` must be type `usize`
//~| ERROR mismatched types
//~^ ERROR mismatched types
//~| NOTE expected `usize`, found `u8`
struct I {
i: (),
Expand Down
22 changes: 7 additions & 15 deletions tests/ui/repeat-expr/repeat_count.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,6 @@ LL | let g = [0_usize; -1_isize];
|
= note: `-1_isize` cannot fit into type `usize`

error: the constant `4` is not of type `usize`
--> $DIR/repeat_count.rs:29:13
|
LL | let h = [0; 4u8];
| ^^^^^^^^ expected `usize`, found `u8`
|
= note: the length of array `[{integer}; 4]` must be type `usize`

error[E0308]: mismatched types
--> $DIR/repeat_count.rs:38:17
|
LL | let i = [0; I { i: () }];
| ^^^^^^^^^^^ expected `usize`, found `I`

error[E0308]: mismatched types
--> $DIR/repeat_count.rs:29:17
|
Expand All @@ -76,7 +62,13 @@ LL - let h = [0; 4u8];
LL + let h = [0; 4usize];
|

error: aborting due to 10 previous errors
error[E0308]: mismatched types
--> $DIR/repeat_count.rs:35:17
|
LL | let i = [0; I { i: () }];
| ^^^^^^^^^^^ expected `usize`, found `I`

error: aborting due to 9 previous errors

Some errors have detailed explanations: E0308, E0435.
For more information about an error, try `rustc --explain E0308`.
Loading