Skip to content

Commit 4a0cca3

Browse files
committed
wip on lowering as const blocks
1 parent 0cea2c1 commit 4a0cca3

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -500,16 +500,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
500500
span: Span,
501501
body: Option<&Expr>,
502502
) -> (hir::BodyId, Option<&'hir hir::ConstArg<'hir>>) {
503-
let ct_arg = if self.tcx.features().min_generic_const_args()
504-
&& let Some(expr) = body
505-
{
506-
self.try_lower_as_const_path(expr)
507-
} else {
508-
None
509-
};
510-
let body_id = if body.is_some() && ct_arg.is_none() {
511-
// TODO: lower as const block instead
512-
self.lower_const_body(span, body)
503+
let mgca = self.tcx.features().min_generic_const_args();
504+
let ct_arg =
505+
if mgca && let Some(expr) = body { self.try_lower_as_const_path(expr) } else { None };
506+
let body_id = if mgca && ct_arg.is_none() {
507+
self.lower_const_body_with_const_block(span, body)
513508
} else {
514509
self.lower_const_body(span, body)
515510
};
@@ -1292,6 +1287,39 @@ impl<'hir> LoweringContext<'_, 'hir> {
12921287
self.lower_fn_body(decl, contract, |this| this.lower_block_expr(body))
12931288
}
12941289

1290+
/// HACK(mgca): lower the body of the const item as a const block
1291+
/// we need this later to be able to control generics in the body
1292+
/// separately from the const's type, etc.
1293+
pub(super) fn lower_const_body_with_const_block(
1294+
&mut self,
1295+
span: Span,
1296+
expr: Option<&Expr>,
1297+
) -> hir::BodyId {
1298+
self.lower_body(|this| {
1299+
(
1300+
&[],
1301+
match expr {
1302+
Some(expr) => {
1303+
let def_id = this.local_def_id(expr.id);
1304+
// TODO: somehow avoid reusing the same nodeid for the const block and the body expr
1305+
let hir_id = this.lower_node_id(expr.id);
1306+
let block = hir::ConstBlock {
1307+
def_id,
1308+
hir_id,
1309+
body: this.lower_const_body(expr.span, Some(expr)),
1310+
};
1311+
hir::Expr {
1312+
hir_id,
1313+
span: this.lower_span(expr.span),
1314+
kind: hir::ExprKind::ConstBlock(block),
1315+
}
1316+
}
1317+
None => this.expr_err(span, this.dcx().span_delayed_bug(span, "no block")),
1318+
},
1319+
)
1320+
})
1321+
}
1322+
12951323
pub(super) fn lower_const_body(&mut self, span: Span, expr: Option<&Expr>) -> hir::BodyId {
12961324
self.lower_body(|this| {
12971325
(

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
174174
);
175175
}
176176
}
177+
// HACK(mgca): see lower_const_body_with_const_block in ast_lowering
178+
ItemKind::Const(box ConstItem { expr: Some(ref expr), .. })
179+
if this.resolver.tcx.features().min_generic_const_args() =>
180+
{
181+
this.create_def(expr.id, None, DefKind::InlineConst, i.span);
182+
}
177183
_ => {}
178184
}
179185
visit::walk_item(this, i);
@@ -334,7 +340,15 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
334340
};
335341

336342
let def = self.create_def(i.id, Some(ident.name), def_kind, i.span);
337-
self.with_parent(def, |this| visit::walk_assoc_item(this, i, ctxt));
343+
self.with_parent(def, |this| {
344+
// HACK(mgca): see lower_const_body_with_const_block in ast_lowering
345+
if let AssocItemKind::Const(box ConstItem { expr: Some(expr), .. }) = &i.kind
346+
&& this.resolver.tcx.features().min_generic_const_args()
347+
{
348+
this.create_def(expr.id, None, DefKind::InlineConst, i.span);
349+
}
350+
visit::walk_assoc_item(this, i, ctxt)
351+
});
338352
}
339353

340354
fn visit_pat(&mut self, pat: &'a Pat) {

0 commit comments

Comments
 (0)