Skip to content

Commit 3b9b5c0

Browse files
committed
Add ConstItemRhs and revert to using BodyId for non type_consts
1 parent 597a1c0 commit 3b9b5c0

File tree

26 files changed

+206
-133
lines changed

26 files changed

+206
-133
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3754,10 +3754,29 @@ pub struct ConstItem {
37543754
pub ident: Ident,
37553755
pub generics: Generics,
37563756
pub ty: Box<Ty>,
3757-
pub body: Option<Box<AnonConst>>,
3757+
pub body: Option<ConstItemRhs>,
37583758
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
37593759
}
37603760

3761+
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
3762+
pub enum ConstItemRhs {
3763+
TypeConst(AnonConst),
3764+
Body(Box<Expr>),
3765+
}
3766+
3767+
impl ConstItemRhs {
3768+
pub fn span(&self) -> Span {
3769+
self.expr().span
3770+
}
3771+
3772+
pub fn expr(&self) -> &Expr {
3773+
match self {
3774+
ConstItemRhs::TypeConst(anon_const) => &anon_const.value,
3775+
ConstItemRhs::Body(expr) => expr,
3776+
}
3777+
}
3778+
}
3779+
37613780
// Adding a new variant? Please update `test_item` in `tests/ui/macros/stringify.rs`.
37623781
#[derive(Clone, Encodable, Decodable, Debug)]
37633782
pub enum ItemKind {

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ macro_rules! common_visitor_and_walkers {
422422
Closure,
423423
Const,
424424
ConstItem,
425+
ConstItemRhs,
425426
Defaultness,
426427
Delegation,
427428
DelegationMac,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
201201
|this| {
202202
let ty = this
203203
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
204-
let body = this.lower_item_body_to_const_arg(body.as_deref());
204+
let body = this.lower_const_item_rhs(attrs, body.as_ref(), span);
205205
(ty, body)
206206
},
207207
);
@@ -813,8 +813,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
813813
let ty = this
814814
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
815815
let body = body
816-
.as_deref()
817-
.map(|body| this.lower_item_body_to_const_arg(Some(body)));
816+
.as_ref()
817+
.map(|body| this.lower_const_item_rhs(attrs, Some(body), i.span));
818818
hir::TraitItemKind::Const(ty, body)
819819
},
820820
);
@@ -1028,7 +1028,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10281028
let ty = this
10291029
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
10301030
this.lower_define_opaque(hir_id, &define_opaque);
1031-
let body = this.lower_item_body_to_const_arg(body.as_deref());
1031+
let body = this.lower_const_item_rhs(attrs, body.as_ref(), i.span);
10321032
hir::ImplItemKind::Const(ty, body)
10331033
},
10341034
),

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23032303
self.arena.alloc(hir::ConstArg { hir_id: self.next_id(), kind: ct_kind })
23042304
}
23052305

2306+
fn lower_const_item_rhs(
2307+
&mut self,
2308+
attrs: &[hir::Attribute],
2309+
rhs: Option<&ConstItemRhs>,
2310+
span: Span,
2311+
) -> hir::ConstItemRhs<'hir> {
2312+
match rhs {
2313+
Some(ConstItemRhs::TypeConst(anon)) => {
2314+
hir::ConstItemRhs::TypeConst(self.lower_item_body_to_const_arg(Some(anon)))
2315+
}
2316+
Some(ConstItemRhs::Body(body)) => {
2317+
hir::ConstItemRhs::Body(self.lower_const_body(span, Some(body)))
2318+
}
2319+
None if attr::contains_name(attrs, sym::type_const) => {
2320+
hir::ConstItemRhs::TypeConst(self.lower_item_body_to_const_arg(None))
2321+
}
2322+
None => hir::ConstItemRhs::Body(self.lower_const_body(span, None)),
2323+
}
2324+
}
2325+
23062326
/// See [`hir::ConstArg`] for when to use this function vs
23072327
/// [`Self::lower_anon_const_to_anon_const`].
23082328
fn lower_item_body_to_const_arg(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl<'a> State<'a> {
221221
None,
222222
generics,
223223
ty,
224-
body.as_deref().map(|ct| &*ct.value),
224+
body.as_ref().map(|ct| ct.expr()),
225225
&item.vis,
226226
ast::Safety::Default,
227227
*defaultness,
@@ -574,7 +574,7 @@ impl<'a> State<'a> {
574574
None,
575575
generics,
576576
ty,
577-
body.as_deref().map(|ct| &*ct.value),
577+
body.as_ref().map(|ct| ct.expr()),
578578
vis,
579579
ast::Safety::Default,
580580
*defaultness,

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) fn expand(
4242

4343
// Generate anonymous constant serving as container for the allocator methods.
4444
let const_ty = ecx.ty(sig_span, TyKind::Tup(ThinVec::new()));
45-
let const_body = ecx.anon_const_block(ecx.block(span, stmts));
45+
let const_body = ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts)));
4646
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
4747
let const_item = if is_stmt {
4848
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))

compiler/rustc_builtin_macros/src/global_allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub(crate) fn expand(
4747

4848
// Generate anonymous constant serving as container for the allocator methods.
4949
let const_ty = ecx.ty(ty_span, TyKind::Tup(ThinVec::new()));
50-
let const_body = ecx.anon_const_block(ecx.block(span, stmts));
50+
let const_body = ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts)));
5151
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
5252
let const_item = if is_stmt {
5353
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,9 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> Box<ast::Item> {
385385
cx.attr_nested_word(sym::allow, sym::deprecated, span),
386386
]);
387387

388-
let block = cx.anon_const_block(
388+
let block = ast::ConstItemRhs::Body(cx.expr_block(
389389
cx.block(span, thin_vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),
390-
);
390+
));
391391

392392
let anon_constant = cx.item_const(
393393
span,

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,8 @@ pub(crate) fn expand_test_or_bench(
289289
ty: cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
290290
define_opaque: None,
291291
// test::TestDescAndFn {
292-
body: Some(Box::new(ast::AnonConst {
293-
id: ast::DUMMY_NODE_ID,
294-
value: cx.expr_struct(
292+
body: Some(ast::ConstItemRhs::Body(
293+
cx.expr_struct(
295294
sp,
296295
test_path("TestDescAndFn"),
297296
thin_vec![
@@ -372,7 +371,7 @@ pub(crate) fn expand_test_or_bench(
372371
field("testfn", test_fn), // }
373372
],
374373
), // }
375-
})),
374+
)),
376375
}
377376
.into(),
378377
),

compiler/rustc_expand/src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ impl<'a> ExtCtxt<'a> {
726726
span: Span,
727727
ident: Ident,
728728
ty: Box<ast::Ty>,
729-
body: Box<ast::AnonConst>,
729+
body: ast::ConstItemRhs,
730730
) -> Box<ast::Item> {
731731
let defaultness = ast::Defaultness::Final;
732732
self.item(

0 commit comments

Comments
 (0)