Rollup merge of #151055 - mgca-arr, r=BoxyUwU

Emit error instead of delayed bug when meeting mismatch type for const array

r? BoxyUwU

resolve: rust-lang/rust#151024

cc: https://github.com/rust-lang/rust/pull/150869
This commit is contained in:
Guillaume Gomez 2026-01-13 23:39:13 +01:00 committed by GitHub
commit 3adbd3ae4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 5 deletions

View file

@ -2415,11 +2415,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
};
let ty::Array(elem_ty, _) = ty.kind() else {
return Const::new_error_with_message(
tcx,
array_expr.span,
"const array must have an array type",
);
let e = tcx
.dcx()
.span_err(array_expr.span, format!("expected `{}`, found const array", ty));
return Const::new_error(tcx, e);
};
let elems = array_expr

View file

@ -0,0 +1,21 @@
//! regression test for <https://github.com/rust-lang/rust/issues/151024>
#![feature(min_generic_const_args)]
#![feature(adt_const_params)]
#![expect(incomplete_features)]
trait Trait1<const N: usize> {}
trait Trait2<const N: [u8; 3]> {}
fn foo<T>()
where
T: Trait1<{ [] }>, //~ ERROR: expected `usize`, found const array
{
}
fn bar<T>()
where
T: Trait2<3>, //~ ERROR: mismatched types
{
}
fn main() {}

View file

@ -0,0 +1,15 @@
error: expected `usize`, found const array
--> $DIR/array-expr-type-mismatch-in-where-bound.rs:11:17
|
LL | T: Trait1<{ [] }>,
| ^^
error[E0308]: mismatched types
--> $DIR/array-expr-type-mismatch-in-where-bound.rs:17:15
|
LL | T: Trait2<3>,
| ^ expected `[u8; 3]`, found integer
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`.