Skip to content

Commit 059e616

Browse files
committed
Lower hir::ConstArgKind::Array to a ValTree
1 parent e638b24 commit 059e616

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,15 +2383,30 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23832383
hir::ConstArgKind::TupleCall(qpath, args) => {
23842384
self.lower_const_arg_tuple_call(hir_id, qpath, args, const_arg.span())
23852385
}
2386-
hir::ConstArgKind::Array(_array_expr) => {
2387-
span_bug!(const_arg.span(), "lowering `{:?}` is not yet implemented", const_arg)
2388-
}
2386+
hir::ConstArgKind::Array(array_expr) => self.lower_const_arg_array(array_expr),
23892387
hir::ConstArgKind::Anon(anon) => self.lower_const_arg_anon(anon),
23902388
hir::ConstArgKind::Infer(span, ()) => self.ct_infer(None, span),
23912389
hir::ConstArgKind::Error(_, e) => ty::Const::new_error(tcx, e),
23922390
}
23932391
}
23942392

2393+
fn lower_const_arg_array(&self, array_expr: &'tcx hir::ConstArgArrayExpr<'tcx>) -> Const<'tcx> {
2394+
let tcx = self.tcx();
2395+
2396+
let array_len = array_expr.elems.len() as u64;
2397+
let elem_ty = self.ty_infer(None, array_expr.span);
2398+
let elems = array_expr
2399+
.elems
2400+
.iter()
2401+
.map(|elem| self.lower_const_arg(elem, FeedConstTy::No))
2402+
.collect::<Vec<_>>();
2403+
2404+
let array_ty = Ty::new_array(tcx, elem_ty, array_len);
2405+
let valtree = ty::ValTree::from_branches(tcx, elems);
2406+
2407+
ty::Const::new_value(tcx, valtree, array_ty)
2408+
}
2409+
23952410
fn lower_const_arg_tuple_call(
23962411
&self,
23972412
hir_id: HirId,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![expect(incomplete_features)]
2+
#![feature(min_generic_const_args, adt_const_params)]
3+
4+
fn takes_array<const A: [u32; 3]>() {}
5+
6+
fn generic_caller<const N: u32, const N2: u32>() {
7+
takes_array::<{ [N, N2, N] }>(); // ok
8+
9+
takes_array::<{ [N, N2, 1] }>(); // not implemented
10+
//~^ ERROR complex const arguments must be placed inside of a `const` block
11+
12+
takes_array::<{ [N, N2, 1 + 1] }>(); // not implemented
13+
//~^ ERROR complex const arguments must be placed inside of a `const` block
14+
15+
takes_array::<{ [N; 3] }>(); // not implemented
16+
//~^ ERROR complex const arguments must be placed inside of a `const` block
17+
}
18+
19+
fn main() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: complex const arguments must be placed inside of a `const` block
2+
--> $DIR/array-expr.rs:9:29
3+
|
4+
LL | takes_array::<{ [N, N2, 1] }>(); // not implemented
5+
| ^
6+
7+
error: complex const arguments must be placed inside of a `const` block
8+
--> $DIR/array-expr.rs:12:29
9+
|
10+
LL | takes_array::<{ [N, N2, 1 + 1] }>(); // not implemented
11+
| ^^^^^
12+
13+
error: complex const arguments must be placed inside of a `const` block
14+
--> $DIR/array-expr.rs:15:19
15+
|
16+
LL | takes_array::<{ [N; 3] }>(); // not implemented
17+
| ^^^^^^^^^^
18+
19+
error: aborting due to 3 previous errors
20+

0 commit comments

Comments
 (0)