Skip to content

Commit 123f00e

Browse files
committed
Use AnonConst as RHS of const items instead of side channel hack
1 parent fb0bc69 commit 123f00e

File tree

17 files changed

+87
-121
lines changed

17 files changed

+87
-121
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3617,12 +3617,7 @@ pub struct ConstItem {
36173617
pub ident: Ident,
36183618
pub generics: Generics,
36193619
pub ty: P<Ty>,
3620-
/// A [`NodeId`] that can be used for the body of the const, independently of the ID
3621-
/// of the body's root expression.
3622-
// HACK(mgca): this is potentially temporary, tbd, in order to create defs for const bodies.
3623-
// FIXME(mgca): maybe merge this with expr since their Options should be in sync
3624-
pub body_id: Option<NodeId>,
3625-
pub expr: Option<P<Expr>>,
3620+
pub body: Option<P<AnonConst>>,
36263621
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
36273622
}
36283623

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,13 +1331,12 @@ impl WalkItemKind for AssocItemKind {
13311331
}
13321332

13331333
fn walk_const_item<T: MutVisitor>(vis: &mut T, item: &mut ConstItem) {
1334-
let ConstItem { defaultness, ident, generics, ty, body_id, expr, define_opaque } = item;
1334+
let ConstItem { defaultness, ident, generics, ty, body, define_opaque } = item;
13351335
visit_defaultness(vis, defaultness);
13361336
vis.visit_ident(ident);
13371337
vis.visit_generics(generics);
13381338
vis.visit_ty(ty);
1339-
visit_opt(body_id, |body_id| vis.visit_id(body_id));
1340-
visit_opt(expr, |expr| vis.visit_expr(expr));
1339+
visit_opt(body, |body| vis.visit_anon_const(body));
13411340
walk_define_opaques(vis, define_opaque);
13421341
}
13431342

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,13 @@ impl WalkItemKind for ItemKind {
388388
ident,
389389
generics,
390390
ty,
391-
body_id: _,
392-
expr,
391+
body,
393392
define_opaque,
394393
}) => {
395394
try_visit!(visitor.visit_ident(ident));
396395
try_visit!(visitor.visit_generics(generics));
397396
try_visit!(visitor.visit_ty(ty));
398-
visit_opt!(visitor, visit_expr, expr);
397+
visit_opt!(visitor, visit_anon_const, body);
399398
try_visit!(walk_define_opaques(visitor, define_opaque));
400399
}
401400
ItemKind::Fn(func) => {
@@ -991,14 +990,13 @@ impl WalkItemKind for AssocItemKind {
991990
ident,
992991
generics,
993992
ty,
994-
body_id: _,
995-
expr,
993+
body,
996994
define_opaque,
997995
}) => {
998996
try_visit!(visitor.visit_ident(ident));
999997
try_visit!(visitor.visit_generics(generics));
1000998
try_visit!(visitor.visit_ty(ty));
1001-
visit_opt!(visitor, visit_expr, expr);
999+
visit_opt!(visitor, visit_anon_const, body);
10021000
try_visit!(walk_define_opaques(visitor, define_opaque));
10031001
}
10041002
AssocItemKind::Fn(func) => {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
199199
ident,
200200
generics,
201201
ty,
202-
body_id,
203-
expr,
202+
body,
204203
define_opaque,
205204
..
206205
}) => {
@@ -212,8 +211,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
212211
|this| {
213212
let ty = this
214213
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
215-
let body =
216-
this.lower_const_item(span, body_id.unwrap(), expr.as_deref().unwrap());
214+
let body = this.lower_const_item(body.as_deref().unwrap());
217215
(ty, body)
218216
},
219217
);
@@ -498,25 +496,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
498496
}
499497
}
500498

501-
fn lower_const_item(
502-
&mut self,
503-
span: Span,
504-
body_id: NodeId,
505-
body_expr: &Expr,
506-
) -> &'hir hir::ConstArg<'hir> {
499+
fn lower_const_item(&mut self, body: &AnonConst) -> &'hir hir::ConstArg<'hir> {
507500
let mgca = self.tcx.features().min_generic_const_args();
508-
if mgca && let Some(ct_arg) = self.try_lower_as_const_path(body_expr) {
501+
if mgca && let Some(ct_arg) = self.try_lower_as_const_path(body) {
509502
return ct_arg;
510503
}
511-
let anon = self.arena.alloc(self.with_new_scopes(span, |this| {
512-
let body = this.lower_const_body(span, Some(body_expr));
513-
hir::AnonConst {
514-
hir_id: this.lower_node_id(body_id),
515-
def_id: this.local_def_id(body_id),
516-
body,
517-
span,
518-
}
519-
}));
504+
let anon = self.lower_anon_const_to_anon_const(body);
520505
self.arena
521506
.alloc(hir::ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Anon(anon) })
522507
}
@@ -816,8 +801,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
816801
ident,
817802
generics,
818803
ty,
819-
body_id,
820-
expr,
804+
body,
821805
define_opaque,
822806
..
823807
}) => {
@@ -828,15 +812,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
828812
|this| {
829813
let ty = this
830814
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
831-
let body = body_id
832-
.zip(expr.as_deref())
833-
.map(|(b_id, b_ex)| this.lower_const_item(i.span, b_id, b_ex));
815+
let body = body.as_deref().map(|body| this.lower_const_item(body));
834816
hir::TraitItemKind::Const(ty, body)
835817
},
836818
);
837819

838820
if define_opaque.is_some() {
839-
if expr.is_some() {
821+
if body.is_some() {
840822
self.lower_define_opaque(hir_id, &define_opaque);
841823
} else {
842824
self.dcx().span_err(
@@ -846,7 +828,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
846828
}
847829
}
848830

849-
(*ident, generics, kind, expr.is_some())
831+
(*ident, generics, kind, body.is_some())
850832
}
851833
AssocItemKind::Fn(box Fn {
852834
sig, ident, generics, body: None, define_opaque, ..
@@ -1010,8 +992,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1010992
ident,
1011993
generics,
1012994
ty,
1013-
body_id,
1014-
expr,
995+
body,
1015996
define_opaque,
1016997
..
1017998
}) => (
@@ -1024,11 +1005,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10241005
let ty = this
10251006
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
10261007
this.lower_define_opaque(hir_id, &define_opaque);
1027-
let body = this.lower_const_item(
1028-
i.span,
1029-
body_id.unwrap(),
1030-
expr.as_deref().unwrap(),
1031-
);
1008+
let body = this.lower_const_item(body.as_deref().unwrap());
10321009
hir::ImplItemKind::Const(ty, body)
10331010
},
10341011
),

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,10 +2085,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20852085
}
20862086

20872087
/// Assumes mgca feature is enabled.
2088-
fn try_lower_as_const_path(&mut self, expr: &Expr) -> Option<&'hir hir::ConstArg<'hir>> {
2089-
let ExprKind::Path(qself, path) = &expr.kind else { return None };
2088+
fn try_lower_as_const_path(&mut self, body: &AnonConst) -> Option<&'hir hir::ConstArg<'hir>> {
2089+
let ExprKind::Path(qself, path) = &body.value.kind else { return None };
20902090
let qpath = self.lower_qpath(
2091-
expr.id,
2091+
body.value.id,
20922092
qself,
20932093
path,
20942094
ParamMode::Optional,

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,9 +1084,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10841084
_ => visit::walk_item(self, item),
10851085
}
10861086
}
1087-
ItemKind::Const(box ConstItem { defaultness, expr, .. }) => {
1087+
ItemKind::Const(box ConstItem { defaultness, body, .. }) => {
10881088
self.check_defaultness(item.span, *defaultness);
1089-
if expr.is_none() {
1089+
if body.is_none() {
10901090
self.dcx().emit_err(errors::ConstWithoutBody {
10911091
span: item.span,
10921092
replace_span: self.ending_semi_or_hi(item.span),
@@ -1436,7 +1436,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14361436

14371437
if let AssocCtxt::Impl { .. } = ctxt {
14381438
match &item.kind {
1439-
AssocItemKind::Const(box ConstItem { expr: None, .. }) => {
1439+
AssocItemKind::Const(box ConstItem { body: None, .. }) => {
14401440
self.dcx().emit_err(errors::AssocConstWithoutBody {
14411441
span: item.span,
14421442
replace_span: self.ending_semi_or_hi(item.span),

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,15 @@ impl<'a> State<'a> {
214214
ident,
215215
generics,
216216
ty,
217-
body_id: _,
218-
expr,
217+
body,
219218
define_opaque,
220219
}) => {
221220
self.print_item_const(
222221
*ident,
223222
None,
224223
generics,
225224
ty,
226-
expr.as_deref(),
225+
body.as_deref().map(|ct| &*ct.value),
227226
&item.vis,
228227
ast::Safety::Default,
229228
*defaultness,
@@ -564,16 +563,15 @@ impl<'a> State<'a> {
564563
ident,
565564
generics,
566565
ty,
567-
body_id: _,
568-
expr,
566+
body,
569567
define_opaque,
570568
}) => {
571569
self.print_item_const(
572570
*ident,
573571
None,
574572
generics,
575573
ty,
576-
expr.as_deref(),
574+
body.as_deref().map(|ct| &*ct.value),
577575
vis,
578576
ast::Safety::Default,
579577
*defaultness,

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

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

4444
// Generate anonymous constant serving as container for the allocator methods.
4545
let const_ty = ecx.ty(sig_span, TyKind::Tup(ThinVec::new()));
46-
let const_body = ecx.expr_block(ecx.block(span, stmts));
46+
let const_body = ecx.anon_const_block(ecx.block(span, stmts));
4747
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
4848
let const_item = if is_stmt {
4949
Annotatable::Stmt(P(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
@@ -48,7 +48,7 @@ pub(crate) fn expand(
4848

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

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P<ast::Item> {
379379
i
380380
});
381381

382-
let block = cx.expr_block(
382+
let block = cx.anon_const_block(
383383
cx.block(span, thin_vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),
384384
);
385385

0 commit comments

Comments
 (0)