Skip to content

Commit 8971bb3

Browse files
committed
separate ast item for const block items
1 parent 31cf103 commit 8971bb3

File tree

13 files changed

+107
-63
lines changed

13 files changed

+107
-63
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3551,6 +3551,7 @@ impl Item {
35513551
pub fn opt_generics(&self) -> Option<&Generics> {
35523552
match &self.kind {
35533553
ItemKind::ExternCrate(..)
3554+
| ItemKind::ConstBlock(_)
35543555
| ItemKind::Use(_)
35553556
| ItemKind::Mod(..)
35563557
| ItemKind::ForeignMod(_)
@@ -3792,6 +3793,15 @@ impl ConstItemRhs {
37923793
}
37933794
}
37943795

3796+
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
3797+
pub struct ConstBlockItem {
3798+
pub body: Box<Expr>,
3799+
}
3800+
3801+
impl ConstBlockItem {
3802+
pub const IDENT: Ident = Ident { name: kw::Underscore, span: DUMMY_SP };
3803+
}
3804+
37953805
// Adding a new variant? Please update `test_item` in `tests/ui/macros/stringify.rs`.
37963806
#[derive(Clone, Encodable, Decodable, Debug)]
37973807
pub enum ItemKind {
@@ -3811,6 +3821,11 @@ pub enum ItemKind {
38113821
///
38123822
/// E.g., `const FOO: i32 = 42;`.
38133823
Const(Box<ConstItem>),
3824+
/// A module-level const block.
3825+
/// Equivalent to `const _: () = const { ... }`.
3826+
///
3827+
/// E.g., `const { assert!(true) }`.
3828+
ConstBlock(ConstBlockItem),
38143829
/// A function declaration (`fn`).
38153830
///
38163831
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
@@ -3887,6 +3902,8 @@ impl ItemKind {
38873902
| ItemKind::MacroDef(ident, _)
38883903
| ItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
38893904

3905+
ItemKind::ConstBlock(_) => Some(ConstBlockItem::IDENT),
3906+
38903907
ItemKind::Use(_)
38913908
| ItemKind::ForeignMod(_)
38923909
| ItemKind::GlobalAsm(_)
@@ -3900,9 +3917,9 @@ impl ItemKind {
39003917
pub fn article(&self) -> &'static str {
39013918
use ItemKind::*;
39023919
match self {
3903-
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
3904-
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..)
3905-
| Delegation(..) | DelegationMac(..) => "a",
3920+
Use(..) | Static(..) | Const(..) | ConstBlock(..) | Fn(..) | Mod(..)
3921+
| GlobalAsm(..) | TyAlias(..) | Struct(..) | Union(..) | Trait(..) | TraitAlias(..)
3922+
| MacroDef(..) | Delegation(..) | DelegationMac(..) => "a",
39063923
ExternCrate(..) | ForeignMod(..) | MacCall(..) | Enum(..) | Impl { .. } => "an",
39073924
}
39083925
}
@@ -3913,6 +3930,7 @@ impl ItemKind {
39133930
ItemKind::Use(..) => "`use` import",
39143931
ItemKind::Static(..) => "static item",
39153932
ItemKind::Const(..) => "constant item",
3933+
ItemKind::ConstBlock(..) => "const block",
39163934
ItemKind::Fn(..) => "function",
39173935
ItemKind::Mod(..) => "module",
39183936
ItemKind::ForeignMod(..) => "extern block",
@@ -3942,7 +3960,18 @@ impl ItemKind {
39423960
| Self::Trait(box Trait { generics, .. })
39433961
| Self::TraitAlias(box TraitAlias { generics, .. })
39443962
| Self::Impl(Impl { generics, .. }) => Some(generics),
3945-
_ => None,
3963+
3964+
Self::ExternCrate(..)
3965+
| Self::Use(..)
3966+
| Self::Static(..)
3967+
| Self::ConstBlock(..)
3968+
| Self::Mod(..)
3969+
| Self::ForeignMod(..)
3970+
| Self::GlobalAsm(..)
3971+
| Self::MacCall(..)
3972+
| Self::MacroDef(..)
3973+
| Self::Delegation(..)
3974+
| Self::DelegationMac(..) => None,
39463975
}
39473976
}
39483977
}

compiler/rustc_ast/src/visit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ macro_rules! common_visitor_and_walkers {
422422
ByRef,
423423
Closure,
424424
Const,
425+
ConstBlockItem,
425426
ConstItem,
426427
ConstItemRhs,
427428
Defaultness,
@@ -819,6 +820,8 @@ macro_rules! common_visitor_and_walkers {
819820
visit_visitable!($($mut)? vis, use_tree),
820821
ItemKind::Static(item) =>
821822
visit_visitable!($($mut)? vis, item),
823+
ItemKind::ConstBlock(item) =>
824+
visit_visitable!($($mut)? vis, item),
822825
ItemKind::Const(item) =>
823826
visit_visitable!($($mut)? vis, item),
824827
ItemKind::Mod(safety, ident, mod_kind) =>

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
183183
self.lower_define_opaque(hir_id, define_opaque);
184184
hir::ItemKind::Static(*m, ident, ty, body_id)
185185
}
186-
ItemKind::Const(box ast::ConstItem {
187-
ident, generics, ty, rhs, define_opaque, ..
186+
ItemKind::Const(box ConstItem {
187+
defaultness: _,
188+
ident,
189+
generics,
190+
ty,
191+
rhs,
192+
define_opaque,
188193
}) => {
189194
let ident = self.lower_ident(*ident);
190195
let (generics, (ty, rhs)) = self.lower_generics(
@@ -201,6 +206,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
201206
self.lower_define_opaque(hir_id, &define_opaque);
202207
hir::ItemKind::Const(ident, generics, ty, rhs)
203208
}
209+
ItemKind::ConstBlock(ConstBlockItem { body }) => hir::ItemKind::Const(
210+
self.lower_ident(ConstBlockItem::IDENT),
211+
hir::Generics::empty(),
212+
self.arena.alloc(self.ty_tup(DUMMY_SP, &[])),
213+
hir::ConstItemRhs::Body({
214+
let body = self.lower_expr_mut(body);
215+
self.record_body(&[], body)
216+
}),
217+
),
204218
ItemKind::Fn(box Fn {
205219
sig: FnSig { decl, header, span: fn_sig_span },
206220
ident,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ 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())
210+
}
208211
ast::ItemKind::Const(box ast::ConstItem {
209212
defaultness,
210213
ident,

compiler/rustc_hir/src/target.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ impl Target {
171171
ast::ItemKind::Use(..) => Target::Use,
172172
ast::ItemKind::Static { .. } => Target::Static,
173173
ast::ItemKind::Const(..) => Target::Const,
174+
ast::ItemKind::ConstBlock(..) => Target::Const,
174175
ast::ItemKind::Fn { .. } => Target::Fn,
175176
ast::ItemKind::Mod(..) => Target::Mod,
176177
ast::ItemKind::ForeignMod { .. } => Target::ForeignMod,

compiler/rustc_parse/src/parser/item.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -273,28 +273,7 @@ impl<'a> Parser<'a> {
273273
{
274274
// CONST BLOCK ITEM
275275
self.psess.gated_spans.gate(sym::const_block_items, self.token.span);
276-
let block = self.parse_const_block(DUMMY_SP, false)?;
277-
ItemKind::Const(Box::new(ConstItem {
278-
defaultness: Defaultness::Final,
279-
ident: Ident { name: kw::Underscore, span: DUMMY_SP },
280-
generics: Generics {
281-
params: thin_vec![],
282-
where_clause: WhereClause {
283-
has_where_token: false,
284-
predicates: thin_vec![],
285-
span: DUMMY_SP,
286-
},
287-
span: DUMMY_SP,
288-
},
289-
ty: Box::new(Ty {
290-
id: DUMMY_NODE_ID,
291-
kind: TyKind::Tup(thin_vec![]),
292-
span: DUMMY_SP,
293-
tokens: None,
294-
}),
295-
rhs: Some(ConstItemRhs::Body(block)),
296-
define_opaque: None,
297-
}))
276+
ItemKind::ConstBlock(ConstBlockItem { body: self.parse_expr()? })
298277
} else if let Const::Yes(const_span) = self.parse_constness(case) {
299278
// CONST ITEM
300279
self.recover_const_mut(const_span);

compiler/rustc_passes/src/input_stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
572572
Use,
573573
Static,
574574
Const,
575+
ConstBlock,
575576
Fn,
576577
Mod,
577578
ForeignMod,

compiler/rustc_passes/src/lang_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl<'ast, 'tcx> visit::Visitor<'ast> for LanguageItemCollector<'ast, 'tcx> {
276276
ast::ItemKind::ExternCrate(..) => Target::ExternCrate,
277277
ast::ItemKind::Use(_) => Target::Use,
278278
ast::ItemKind::Static(_) => Target::Static,
279-
ast::ItemKind::Const(_) => Target::Const,
279+
ast::ItemKind::Const(_) | ast::ItemKind::ConstBlock(_) => Target::Const,
280280
ast::ItemKind::Fn(_) | ast::ItemKind::Delegation(..) => Target::Fn,
281281
ast::ItemKind::Mod(..) => Target::Mod,
282282
ast::ItemKind::ForeignMod(_) => Target::ForeignFn,

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
925925
}
926926

927927
// These items do not add names to modules.
928-
ItemKind::Impl { .. } | ItemKind::ForeignMod(..) | ItemKind::GlobalAsm(..) => {}
928+
ItemKind::Impl { .. }
929+
| ItemKind::ForeignMod(..)
930+
| ItemKind::GlobalAsm(..)
931+
| ItemKind::ConstBlock(..) => {}
929932

930933
ItemKind::MacroDef(..) | ItemKind::MacCall(_) | ItemKind::DelegationMac(..) => {
931934
unreachable!()

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
121121
mutability: s.mutability,
122122
nested: false,
123123
},
124-
ItemKind::Const(..) => DefKind::Const,
124+
ItemKind::Const(..) | ItemKind::ConstBlock(..) => DefKind::Const,
125125
ItemKind::Fn(..) | ItemKind::Delegation(..) => DefKind::Fn,
126126
ItemKind::MacroDef(ident, def) => {
127127
let edition = i.span.edition();

0 commit comments

Comments
 (0)