diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 3f82375308927..49974c4ede7be 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -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 diff --git a/tests/ui/const-generics/mgca/type_const-assoc-mismatched-literal-suffix.rs b/tests/ui/const-generics/mgca/type_const-assoc-mismatched-literal-suffix.rs new file mode 100644 index 0000000000000..923bcca441ccf --- /dev/null +++ b/tests/ui/const-generics/mgca/type_const-assoc-mismatched-literal-suffix.rs @@ -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() {} diff --git a/tests/ui/const-generics/mgca/type_const-assoc-mismatched-literal-suffix.stderr b/tests/ui/const-generics/mgca/type_const-assoc-mismatched-literal-suffix.stderr new file mode 100644 index 0000000000000..b1f1e51f59fc2 --- /dev/null +++ b/tests/ui/const-generics/mgca/type_const-assoc-mismatched-literal-suffix.stderr @@ -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`. diff --git a/tests/ui/const-generics/mgca/type_const-mismatched-literal-suffix.rs b/tests/ui/const-generics/mgca/type_const-mismatched-literal-suffix.rs new file mode 100644 index 0000000000000..c88fdd191a5de --- /dev/null +++ b/tests/ui/const-generics/mgca/type_const-mismatched-literal-suffix.rs @@ -0,0 +1,13 @@ +//@ compile-flags: --emit=link + +// 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; +} diff --git a/tests/ui/const-generics/mgca/type_const-mismatched-literal-suffix.stderr b/tests/ui/const-generics/mgca/type_const-mismatched-literal-suffix.stderr new file mode 100644 index 0000000000000..cbf966cded0a5 --- /dev/null +++ b/tests/ui/const-generics/mgca/type_const-mismatched-literal-suffix.stderr @@ -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`. diff --git a/tests/ui/const-generics/mgca/type_const-mismatched-types.rs b/tests/ui/const-generics/mgca/type_const-mismatched-types.rs index c73785f9a3e38..deac97383b04a 100644 --- a/tests/ui/const-generics/mgca/type_const-mismatched-types.rs +++ b/tests/ui/const-generics/mgca/type_const-mismatched-types.rs @@ -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; diff --git a/tests/ui/const-generics/mgca/type_const-mismatched-types.stderr b/tests/ui/const-generics/mgca/type_const-mismatched-types.stderr index f7f64c535f602..245105e95b5ed 100644 --- a/tests/ui/const-generics/mgca/type_const-mismatched-types.stderr +++ b/tests/ui/const-generics/mgca/type_const-mismatched-types.stderr @@ -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` - error[E0308]: mismatched types --> $DIR/type_const-mismatched-types.rs:4:24 | @@ -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`. diff --git a/tests/ui/const-generics/type-dependent/type-mismatch.full.stderr b/tests/ui/const-generics/type-dependent/type-mismatch.full.stderr index 9de140dab9eb6..95d20de1b432e 100644 --- a/tests/ui/const-generics/type-dependent/type-mismatch.full.stderr +++ b/tests/ui/const-generics/type-dependent/type-mismatch.full.stderr @@ -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(&self) -> u8 { N } - | ^^^^^^^^^^^ required by this const generic parameter in `R::method` - error[E0308]: mismatched types --> $DIR/type-mismatch.rs:8:27 | @@ -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`. diff --git a/tests/ui/const-generics/type-dependent/type-mismatch.min.stderr b/tests/ui/const-generics/type-dependent/type-mismatch.min.stderr index 9de140dab9eb6..95d20de1b432e 100644 --- a/tests/ui/const-generics/type-dependent/type-mismatch.min.stderr +++ b/tests/ui/const-generics/type-dependent/type-mismatch.min.stderr @@ -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(&self) -> u8 { N } - | ^^^^^^^^^^^ required by this const generic parameter in `R::method` - error[E0308]: mismatched types --> $DIR/type-mismatch.rs:8:27 | @@ -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`. diff --git a/tests/ui/const-generics/type-dependent/type-mismatch.rs b/tests/ui/const-generics/type-dependent/type-mismatch.rs index fc7ae994184be..6ed5fdca30ae3 100644 --- a/tests/ui/const-generics/type-dependent/type-mismatch.rs +++ b/tests/ui/const-generics/type-dependent/type-mismatch.rs @@ -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 } diff --git a/tests/ui/consts/const-eval/array-len-mismatch-type.rs b/tests/ui/consts/const-eval/array-len-mismatch-type.rs index 463572c13e104..6e08a05b1062a 100644 --- a/tests/ui/consts/const-eval/array-len-mismatch-type.rs +++ b/tests/ui/consts/const-eval/array-len-mismatch-type.rs @@ -1,8 +1,5 @@ //! Regression test for 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() {} diff --git a/tests/ui/consts/const-eval/array-len-mismatch-type.stderr b/tests/ui/consts/const-eval/array-len-mismatch-type.stderr index 0f03006f00326..ce641f4a74b9c 100644 --- a/tests/ui/consts/const-eval/array-len-mismatch-type.stderr +++ b/tests/ui/consts/const-eval/array-len-mismatch-type.stderr @@ -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 | @@ -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`. diff --git a/tests/ui/repeat-expr/repeat_count.rs b/tests/ui/repeat-expr/repeat_count.rs index b1e3a9d8cb3b6..f4d07cc2de03d 100644 --- a/tests/ui/repeat-expr/repeat_count.rs +++ b/tests/ui/repeat-expr/repeat_count.rs @@ -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: (), diff --git a/tests/ui/repeat-expr/repeat_count.stderr b/tests/ui/repeat-expr/repeat_count.stderr index 5da9dbe032098..eb9581b8f7ae4 100644 --- a/tests/ui/repeat-expr/repeat_count.stderr +++ b/tests/ui/repeat-expr/repeat_count.stderr @@ -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 | @@ -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`.