Skip to content

Commit 0654a7a

Browse files
committed
const_block_items: do not create an AnonConst
1 parent 4841313 commit 0654a7a

File tree

15 files changed

+197
-188
lines changed

15 files changed

+197
-188
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3808,8 +3808,9 @@ impl ConstItemRhs {
38083808

38093809
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
38103810
pub struct ConstBlockItem {
3811-
// FIXME(const_block_items): current invariant is body.kind == InlineConst
3812-
pub body: Box<Expr>,
3811+
pub id: NodeId,
3812+
pub span: Span,
3813+
pub block: Box<Block>,
38133814
}
38143815

38153816
impl ConstBlockItem {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
206206
self.lower_define_opaque(hir_id, &define_opaque);
207207
hir::ItemKind::Const(ident, generics, ty, rhs)
208208
}
209-
ItemKind::ConstBlock(ConstBlockItem { body }) => hir::ItemKind::Const(
209+
ItemKind::ConstBlock(ConstBlockItem { span, id, block }) => hir::ItemKind::Const(
210210
self.lower_ident(ConstBlockItem::IDENT),
211211
hir::Generics::empty(),
212212
self.arena.alloc(self.ty_tup(DUMMY_SP, &[])),
213213
hir::ConstItemRhs::Body({
214-
let body = self.lower_expr_mut(body);
214+
let body = hir::Expr {
215+
hir_id: self.lower_node_id(*id),
216+
kind: hir::ExprKind::Block(self.lower_block(block, false), None),
217+
span: self.lower_span(*span),
218+
};
215219
self.record_body(&[], body)
216220
}),
217221
),

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,16 @@ impl<'a> State<'a> {
205205
define_opaque.as_deref(),
206206
);
207207
}
208-
ast::ItemKind::ConstBlock(ast::ConstBlockItem { body }) => {
209-
self.print_expr(body, FixupContext::default())
208+
ast::ItemKind::ConstBlock(ast::ConstBlockItem { id: _, span: _, block }) => {
209+
let ib = self.ibox(INDENT_UNIT);
210+
self.word("const");
211+
self.nbsp();
212+
{
213+
let cb = self.cbox(0);
214+
let ib = self.ibox(0);
215+
self.print_block_with_attrs(block, &[], cb, ib);
216+
}
217+
self.end(ib);
210218
}
211219
ast::ItemKind::Const(box ast::ConstItem {
212220
defaultness,

compiler/rustc_parse/src/parser/item.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,13 @@ impl<'a> Parser<'a> {
269269
self.parse_item_impl(attrs, def_())?
270270
} else if let AllowConstBlockItems::Yes | AllowConstBlockItems::DoesNotMatter =
271271
allow_const_block_items
272-
&& self.token.is_keyword(kw::Const)
273-
&& self.look_ahead(1, |t| *t == token::OpenBrace || t.is_metavar_block())
272+
&& self.check_inline_const(0)
274273
{
275274
// CONST BLOCK ITEM
276-
self.psess.gated_spans.gate(sym::const_block_items, self.token.span);
277275
if let AllowConstBlockItems::DoesNotMatter = allow_const_block_items {
278276
debug!("Parsing a const block item that does not matter: {:?}", self.token.span);
279277
};
280-
ItemKind::ConstBlock(ConstBlockItem { body: self.parse_expr()? })
278+
ItemKind::ConstBlock(self.parse_const_block_item()?)
281279
} else if let Const::Yes(const_span) = self.parse_constness(case) {
282280
// CONST ITEM
283281
self.recover_const_mut(const_span);
@@ -1395,6 +1393,14 @@ impl<'a> Parser<'a> {
13951393
}
13961394
}
13971395

1396+
fn parse_const_block_item(&mut self) -> PResult<'a, ConstBlockItem> {
1397+
self.expect_keyword(exp!(Const))?;
1398+
let const_span = self.prev_token.span;
1399+
self.psess.gated_spans.gate(sym::const_block_items, const_span);
1400+
let block = self.parse_block()?;
1401+
Ok(ConstBlockItem { id: DUMMY_NODE_ID, span: const_span.to(block.span), block })
1402+
}
1403+
13981404
/// Parse a static item with the prefix `"static" "mut"?` already parsed and stored in
13991405
/// `mutability`.
14001406
///

compiler/rustc_resolve/src/late.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,23 +2857,27 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28572857
);
28582858
self.resolve_define_opaques(define_opaque);
28592859
}
2860-
ItemKind::ConstBlock(ConstBlockItem { body }) => self.with_generic_param_rib(
2861-
&[],
2862-
RibKind::Item(HasGenericParams::No, def_kind),
2863-
item.id,
2864-
LifetimeBinderKind::ConstItem,
2865-
DUMMY_SP,
2866-
|this| {
2867-
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
2868-
this.with_constant_rib(
2869-
IsRepeatExpr::No,
2870-
ConstantHasGenerics::Yes,
2871-
Some((ConstBlockItem::IDENT, ConstantItemKind::Const)),
2872-
|this| this.visit_expr(body),
2860+
ItemKind::ConstBlock(ConstBlockItem { id: _, span: _, block }) => self
2861+
.with_generic_param_rib(
2862+
&[],
2863+
RibKind::Item(HasGenericParams::No, def_kind),
2864+
item.id,
2865+
LifetimeBinderKind::ConstItem,
2866+
DUMMY_SP,
2867+
|this| {
2868+
this.with_lifetime_rib(
2869+
LifetimeRibKind::Elided(LifetimeRes::Infer),
2870+
|this| {
2871+
this.with_constant_rib(
2872+
IsRepeatExpr::No,
2873+
ConstantHasGenerics::Yes,
2874+
Some((ConstBlockItem::IDENT, ConstantItemKind::Const)),
2875+
|this| this.resolve_labeled_block(None, block.id, block),
2876+
)
2877+
},
28732878
);
2874-
})
2875-
},
2876-
),
2879+
},
2880+
),
28772881

28782882
ItemKind::Use(use_tree) => {
28792883
let maybe_exported = match use_tree.kind {

0 commit comments

Comments
 (0)