Skip to content

Commit d616e6c

Browse files
committed
Emit error instead of delayed bug when meeting mismatch type for const
array
1 parent 2f1bd3f commit d616e6c

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,11 +2415,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24152415
};
24162416

24172417
let ty::Array(elem_ty, _) = ty.kind() else {
2418-
return Const::new_error_with_message(
2419-
tcx,
2420-
array_expr.span,
2421-
"const array must have an array type",
2422-
);
2418+
let e = tcx
2419+
.dcx()
2420+
.span_err(array_expr.span, format!("expected `{}`, found const array", ty));
2421+
return Const::new_error(tcx, e);
24232422
};
24242423

24252424
let elems = array_expr
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! regression test for <https://github.com/rust-lang/rust/issues/151024>
2+
#![feature(min_generic_const_args)]
3+
#![feature(adt_const_params)]
4+
#![expect(incomplete_features)]
5+
6+
trait Trait1<const N: usize> {}
7+
trait Trait2<const N: [u8; 3]> {}
8+
9+
fn foo<T>()
10+
where
11+
T: Trait1<{ [] }>, //~ ERROR: expected `usize`, found const array
12+
{
13+
}
14+
15+
fn bar<T>()
16+
where
17+
T: Trait2<3>, //~ ERROR: mismatched types
18+
{
19+
}
20+
21+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: expected `usize`, found const array
2+
--> $DIR/array-expr-type-mismatch-in-where-bound.rs:11:17
3+
|
4+
LL | T: Trait1<{ [] }>,
5+
| ^^
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/array-expr-type-mismatch-in-where-bound.rs:17:15
9+
|
10+
LL | T: Trait2<3>,
11+
| ^ expected `[u8; 3]`, found integer
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)