Skip to content

Commit a38bbde

Browse files
committed
Use AnonConst as RHS of const items instead of side channel hack
1 parent 4546d77 commit a38bbde

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
@@ -1307,13 +1307,12 @@ impl WalkItemKind for AssocItemKind {
13071307
}
13081308

13091309
fn walk_const_item<T: MutVisitor>(vis: &mut T, item: &mut ConstItem) {
1310-
let ConstItem { defaultness, ident, generics, ty, body_id, expr, define_opaque } = item;
1310+
let ConstItem { defaultness, ident, generics, ty, body, define_opaque } = item;
13111311
visit_defaultness(vis, defaultness);
13121312
vis.visit_ident(ident);
13131313
vis.visit_generics(generics);
13141314
vis.visit_ty(ty);
1315-
visit_opt(body_id, |body_id| vis.visit_id(body_id));
1316-
visit_opt(expr, |expr| vis.visit_expr(expr));
1315+
visit_opt(body, |body| vis.visit_anon_const(body));
13171316
walk_define_opaques(vis, define_opaque);
13181317
}
13191318

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,13 @@ impl WalkItemKind for ItemKind {
448448
ident,
449449
generics,
450450
ty,
451-
body_id: _,
452-
expr,
451+
body,
453452
define_opaque,
454453
}) => {
455454
try_visit!(visitor.visit_ident(ident));
456455
try_visit!(visitor.visit_generics(generics));
457456
try_visit!(visitor.visit_ty(ty));
458-
visit_opt!(visitor, visit_expr, expr);
457+
visit_opt!(visitor, visit_anon_const, body);
459458
try_visit!(walk_define_opaques(visitor, define_opaque));
460459
}
461460
ItemKind::Fn(func) => {
@@ -1045,14 +1044,13 @@ impl WalkItemKind for AssocItemKind {
10451044
ident,
10461045
generics,
10471046
ty,
1048-
body_id: _,
1049-
expr,
1047+
body,
10501048
define_opaque,
10511049
}) => {
10521050
try_visit!(visitor.visit_ident(ident));
10531051
try_visit!(visitor.visit_generics(generics));
10541052
try_visit!(visitor.visit_ty(ty));
1055-
visit_opt!(visitor, visit_expr, expr);
1053+
visit_opt!(visitor, visit_anon_const, body);
10561054
try_visit!(walk_define_opaques(visitor, define_opaque));
10571055
}
10581056
AssocItemKind::Fn(func) => {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
187187
ident,
188188
generics,
189189
ty,
190-
body_id,
191-
expr,
190+
body,
192191
define_opaque,
193192
..
194193
}) => {
@@ -200,8 +199,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
200199
|this| {
201200
let ty = this
202201
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
203-
let body =
204-
this.lower_const_item(span, body_id.unwrap(), expr.as_deref().unwrap());
202+
let body = this.lower_const_item(body.as_deref().unwrap());
205203
(ty, body)
206204
},
207205
);
@@ -486,25 +484,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
486484
}
487485
}
488486

489-
fn lower_const_item(
490-
&mut self,
491-
span: Span,
492-
body_id: NodeId,
493-
body_expr: &Expr,
494-
) -> &'hir hir::ConstArg<'hir> {
487+
fn lower_const_item(&mut self, body: &AnonConst) -> &'hir hir::ConstArg<'hir> {
495488
let mgca = self.tcx.features().min_generic_const_args();
496-
if mgca && let Some(ct_arg) = self.try_lower_as_const_path(body_expr) {
489+
if mgca && let Some(ct_arg) = self.try_lower_as_const_path(body) {
497490
return ct_arg;
498491
}
499-
let anon = self.arena.alloc(self.with_new_scopes(span, |this| {
500-
let body = this.lower_const_body(span, Some(body_expr));
501-
hir::AnonConst {
502-
hir_id: this.lower_node_id(body_id),
503-
def_id: this.local_def_id(body_id),
504-
body,
505-
span,
506-
}
507-
}));
492+
let anon = self.lower_anon_const_to_anon_const(body);
508493
self.arena
509494
.alloc(hir::ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Anon(anon) })
510495
}
@@ -804,8 +789,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
804789
ident,
805790
generics,
806791
ty,
807-
body_id,
808-
expr,
792+
body,
809793
define_opaque,
810794
..
811795
}) => {
@@ -816,15 +800,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
816800
|this| {
817801
let ty = this
818802
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
819-
let body = body_id
820-
.zip(expr.as_deref())
821-
.map(|(b_id, b_ex)| this.lower_const_item(i.span, b_id, b_ex));
803+
let body = body.as_deref().map(|body| this.lower_const_item(body));
822804
hir::TraitItemKind::Const(ty, body)
823805
},
824806
);
825807

826808
if define_opaque.is_some() {
827-
if expr.is_some() {
809+
if body.is_some() {
828810
self.lower_define_opaque(hir_id, &define_opaque);
829811
} else {
830812
self.dcx().span_err(
@@ -834,7 +816,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
834816
}
835817
}
836818

837-
(*ident, generics, kind, expr.is_some())
819+
(*ident, generics, kind, body.is_some())
838820
}
839821
AssocItemKind::Fn(box Fn {
840822
sig, ident, generics, body: None, define_opaque, ..
@@ -998,8 +980,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
998980
ident,
999981
generics,
1000982
ty,
1001-
body_id,
1002-
expr,
983+
body,
1003984
define_opaque,
1004985
..
1005986
}) => (
@@ -1012,11 +993,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1012993
let ty = this
1013994
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
1014995
this.lower_define_opaque(hir_id, &define_opaque);
1015-
let body = this.lower_const_item(
1016-
i.span,
1017-
body_id.unwrap(),
1018-
expr.as_deref().unwrap(),
1019-
);
996+
let body = this.lower_const_item(body.as_deref().unwrap());
1020997
hir::ImplItemKind::Const(ty, body)
1021998
},
1022999
),

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

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ pub(crate) fn expand_test_or_bench(
293293
generics: ast::Generics::default(),
294294
ty: cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
295295
define_opaque: None,
296-
body_id: Some(ast::DUMMY_NODE_ID),
297296
// test::TestDescAndFn {
298-
expr: Some(
299-
cx.expr_struct(
297+
body: Some(P(ast::AnonConst {
298+
id: ast::DUMMY_NODE_ID,
299+
value: cx.expr_struct(
300300
sp,
301301
test_path("TestDescAndFn"),
302302
thin_vec![
@@ -377,7 +377,7 @@ pub(crate) fn expand_test_or_bench(
377377
field("testfn", test_fn), // }
378378
],
379379
), // }
380-
),
380+
})),
381381
}
382382
.into(),
383383
),

compiler/rustc_expand/src/build.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ impl<'a> ExtCtxt<'a> {
105105
}
106106
}
107107

108+
pub fn anon_const_block(&self, b: P<ast::Block>) -> P<ast::AnonConst> {
109+
P(self.anon_const(b.span, ast::ExprKind::Block(b, None)))
110+
}
111+
108112
pub fn const_ident(&self, span: Span, ident: Ident) -> ast::AnonConst {
109113
self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
110114
}
@@ -711,7 +715,7 @@ impl<'a> ExtCtxt<'a> {
711715
span: Span,
712716
ident: Ident,
713717
ty: P<ast::Ty>,
714-
expr: P<ast::Expr>,
718+
body: P<ast::AnonConst>,
715719
) -> P<ast::Item> {
716720
let defaultness = ast::Defaultness::Final;
717721
self.item(
@@ -724,8 +728,7 @@ impl<'a> ExtCtxt<'a> {
724728
// FIXME(generic_const_items): Pass the generics as a parameter.
725729
generics: ast::Generics::default(),
726730
ty,
727-
body_id: Some(ast::DUMMY_NODE_ID),
728-
expr: Some(expr),
731+
body: Some(body),
729732
define_opaque: None,
730733
}
731734
.into(),

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ use rustc_abi::ExternAbi;
9595
use rustc_hir as hir;
9696
use rustc_hir::def::DefKind;
9797
use rustc_middle::middle;
98-
use rustc_middle::mir::interpret::GlobalId;
9998
use rustc_middle::query::Providers;
100-
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
99+
use rustc_middle::ty::{Const, Ty, TyCtxt};
101100
use rustc_session::parse::feature_err;
102101
use rustc_span::symbol::sym;
103102
use rustc_span::{ErrorGuaranteed, Span};

compiler/rustc_lint/src/unused.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -983,19 +983,22 @@ trait UnusedDelimLint {
983983
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
984984
use ast::ItemKind::*;
985985

986-
if let Const(box ast::ConstItem { expr: Some(expr), .. })
987-
| Static(box ast::StaticItem { expr: Some(expr), .. }) = &item.kind
988-
{
989-
self.check_unused_delims_expr(
990-
cx,
991-
expr,
992-
UnusedDelimsCtx::AssignedValue,
993-
false,
994-
None,
995-
None,
996-
false,
997-
);
998-
}
986+
let expr = if let Const(box ast::ConstItem { body: Some(body), .. }) = &item.kind {
987+
&body.value
988+
} else if let Static(box ast::StaticItem { expr: Some(expr), .. }) = &item.kind {
989+
expr
990+
} else {
991+
return;
992+
};
993+
self.check_unused_delims_expr(
994+
cx,
995+
expr,
996+
UnusedDelimsCtx::AssignedValue,
997+
false,
998+
None,
999+
None,
1000+
false,
1001+
);
9991002
}
10001003
}
10011004

0 commit comments

Comments
 (0)