Skip to content

Commit 841f864

Browse files
committed
separate ast item for const block items
1 parent 92ca7eb commit 841f864

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
@@ -3564,6 +3564,7 @@ impl Item {
35643564
pub fn opt_generics(&self) -> Option<&Generics> {
35653565
match &self.kind {
35663566
ItemKind::ExternCrate(..)
3567+
| ItemKind::ConstBlock(_)
35673568
| ItemKind::Use(_)
35683569
| ItemKind::Mod(..)
35693570
| ItemKind::ForeignMod(_)
@@ -3805,6 +3806,15 @@ impl ConstItemRhs {
38053806
}
38063807
}
38073808

3809+
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
3810+
pub struct ConstBlockItem {
3811+
pub body: Box<Expr>,
3812+
}
3813+
3814+
impl ConstBlockItem {
3815+
pub const IDENT: Ident = Ident { name: kw::Underscore, span: DUMMY_SP };
3816+
}
3817+
38083818
// Adding a new variant? Please update `test_item` in `tests/ui/macros/stringify.rs`.
38093819
#[derive(Clone, Encodable, Decodable, Debug)]
38103820
pub enum ItemKind {
@@ -3824,6 +3834,11 @@ pub enum ItemKind {
38243834
///
38253835
/// E.g., `const FOO: i32 = 42;`.
38263836
Const(Box<ConstItem>),
3837+
/// A module-level const block.
3838+
/// Equivalent to `const _: () = const { ... }`.
3839+
///
3840+
/// E.g., `const { assert!(true) }`.
3841+
ConstBlock(ConstBlockItem),
38273842
/// A function declaration (`fn`).
38283843
///
38293844
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
@@ -3900,6 +3915,8 @@ impl ItemKind {
39003915
| ItemKind::MacroDef(ident, _)
39013916
| ItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
39023917

3918+
ItemKind::ConstBlock(_) => Some(ConstBlockItem::IDENT),
3919+
39033920
ItemKind::Use(_)
39043921
| ItemKind::ForeignMod(_)
39053922
| ItemKind::GlobalAsm(_)
@@ -3913,9 +3930,9 @@ impl ItemKind {
39133930
pub fn article(&self) -> &'static str {
39143931
use ItemKind::*;
39153932
match self {
3916-
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
3917-
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..)
3918-
| Delegation(..) | DelegationMac(..) => "a",
3933+
Use(..) | Static(..) | Const(..) | ConstBlock(..) | Fn(..) | Mod(..)
3934+
| GlobalAsm(..) | TyAlias(..) | Struct(..) | Union(..) | Trait(..) | TraitAlias(..)
3935+
| MacroDef(..) | Delegation(..) | DelegationMac(..) => "a",
39193936
ExternCrate(..) | ForeignMod(..) | MacCall(..) | Enum(..) | Impl { .. } => "an",
39203937
}
39213938
}
@@ -3926,6 +3943,7 @@ impl ItemKind {
39263943
ItemKind::Use(..) => "`use` import",
39273944
ItemKind::Static(..) => "static item",
39283945
ItemKind::Const(..) => "constant item",
3946+
ItemKind::ConstBlock(..) => "const block",
39293947
ItemKind::Fn(..) => "function",
39303948
ItemKind::Mod(..) => "module",
39313949
ItemKind::ForeignMod(..) => "extern block",
@@ -3955,7 +3973,18 @@ impl ItemKind {
39553973
| Self::Trait(box Trait { generics, .. })
39563974
| Self::TraitAlias(box TraitAlias { generics, .. })
39573975
| Self::Impl(Impl { generics, .. }) => Some(generics),
3958-
_ => None,
3976+
3977+
Self::ExternCrate(..)
3978+
| Self::Use(..)
3979+
| Self::Static(..)
3980+
| Self::ConstBlock(..)
3981+
| Self::Mod(..)
3982+
| Self::ForeignMod(..)
3983+
| Self::GlobalAsm(..)
3984+
| Self::MacCall(..)
3985+
| Self::MacroDef(..)
3986+
| Self::Delegation(..)
3987+
| Self::DelegationMac(..) => None,
39593988
}
39603989
}
39613990
}

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
@@ -960,7 +960,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
960960
}
961961

962962
// These items do not add names to modules.
963-
ItemKind::Impl { .. } | ItemKind::ForeignMod(..) | ItemKind::GlobalAsm(..) => {}
963+
ItemKind::Impl { .. }
964+
| ItemKind::ForeignMod(..)
965+
| ItemKind::GlobalAsm(..)
966+
| ItemKind::ConstBlock(..) => {}
964967

965968
ItemKind::MacroDef(..) | ItemKind::MacCall(_) | ItemKind::DelegationMac(..) => {
966969
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)