Skip to content

Commit 54b9a85

Browse files
committed
Add dummy NodeId to AST const item for mgca lowering
1 parent 4b4f8cd commit 54b9a85

File tree

10 files changed

+51
-14
lines changed

10 files changed

+51
-14
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3617,6 +3617,11 @@ 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>,
36203625
pub expr: Option<P<Expr>>,
36213626
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
36223627
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,11 +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, expr, define_opaque } = item;
1310+
let ConstItem { defaultness, ident, generics, ty, body_id, expr, 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));
13151316
visit_opt(expr, |expr| vis.visit_expr(expr));
13161317
walk_define_opaques(vis, define_opaque);
13171318
}

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ impl WalkItemKind for ItemKind {
448448
ident,
449449
generics,
450450
ty,
451+
body_id: _,
451452
expr,
452453
define_opaque,
453454
}) => {
@@ -1044,6 +1045,7 @@ impl WalkItemKind for AssocItemKind {
10441045
ident,
10451046
generics,
10461047
ty,
1048+
body_id: _,
10471049
expr,
10481050
define_opaque,
10491051
}) => {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
187187
ident,
188188
generics,
189189
ty,
190+
body_id,
190191
expr,
191192
define_opaque,
192193
..
@@ -199,7 +200,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
199200
|this| {
200201
let ty = this
201202
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
202-
(ty, this.lower_const_item(span, expr.as_deref()))
203+
(ty, this.lower_const_item(span, body_id.zip(expr.as_deref())))
203204
},
204205
);
205206
self.lower_define_opaque(hir_id, &define_opaque);
@@ -483,21 +484,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
483484
}
484485
}
485486

486-
fn lower_const_item(&mut self, span: Span, body: Option<&Expr>) -> hir::BodyId {
487-
self.lower_const_body(span, body)
487+
fn lower_const_item(&mut self, span: Span, body: Option<(NodeId, &Expr)>) -> hir::BodyId {
488+
self.lower_const_body(span, body.map(|b| b.1))
488489
// TODO: code to add next
489-
// let ct_arg = if self.tcx.features().min_generic_const_args()
490-
// && let Some(expr) = body
491-
// {
490+
// let mgca = self.tcx.features().min_generic_const_args();
491+
// let ct_arg = if mgca && let Some((_, expr)) = body {
492492
// self.try_lower_as_const_path(expr)
493493
// } else {
494494
// None
495495
// };
496-
// let body_id = if body.is_some() && ct_arg.is_none() {
497-
// // TODO: lower as const block instead
498-
// self.lower_const_body(span, body)
496+
// let body_id = if mgca && ct_arg.is_none() {
497+
// self.lower_const_body_with_const_block(span, body)
499498
// } else {
500-
// self.lower_const_body(span, body)
499+
// self.lower_const_body(span, body.map(|(_, e)| e))
501500
// };
502501
// (body_id, ct_arg)
503502
}
@@ -797,6 +796,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
797796
ident,
798797
generics,
799798
ty,
799+
body_id,
800800
expr,
801801
define_opaque,
802802
..
@@ -808,7 +808,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
808808
|this| {
809809
let ty = this
810810
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
811-
let body = expr.as_deref().map(|e| this.lower_const_item(i.span, Some(e)));
811+
let body = expr
812+
.as_deref()
813+
.map(|e| this.lower_const_item(i.span, Some((body_id.unwrap(), e))));
812814
hir::TraitItemKind::Const(ty, body)
813815
},
814816
);
@@ -988,6 +990,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
988990
ident,
989991
generics,
990992
ty,
993+
body_id,
991994
expr,
992995
define_opaque,
993996
..
@@ -1001,7 +1004,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10011004
let ty = this
10021005
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
10031006
this.lower_define_opaque(hir_id, &define_opaque);
1004-
let body = this.lower_const_item(i.span, expr.as_deref());
1007+
let body = this.lower_const_item(i.span, body_id.zip(expr.as_deref()));
10051008
hir::ImplItemKind::Const(ty, body)
10061009
},
10071010
),
@@ -1278,6 +1281,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
12781281
self.lower_fn_body(decl, contract, |this| this.lower_block_expr(body))
12791282
}
12801283

1284+
// TODO: add lower_const_body_with_const_block
1285+
12811286
pub(super) fn lower_const_body(&mut self, span: Span, expr: Option<&Expr>) -> hir::BodyId {
12821287
self.lower_body(|this| {
12831288
(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ impl<'a> State<'a> {
214214
ident,
215215
generics,
216216
ty,
217+
body_id: _,
217218
expr,
218219
define_opaque,
219220
}) => {
@@ -563,6 +564,7 @@ impl<'a> State<'a> {
563564
ident,
564565
generics,
565566
ty,
567+
body_id: _,
566568
expr,
567569
define_opaque,
568570
}) => {

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ 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),
296297
// test::TestDescAndFn {
297298
expr: Some(
298299
cx.expr_struct(

compiler/rustc_expand/src/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ impl<'a> ExtCtxt<'a> {
724724
// FIXME(generic_const_items): Pass the generics as a parameter.
725725
generics: ast::Generics::default(),
726726
ty,
727+
body_id: Some(ast::DUMMY_NODE_ID),
727728
expr: Some(expr),
728729
define_opaque: None,
729730
}

compiler/rustc_parse/src/parser/item.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ impl<'a> Parser<'a> {
258258
ident,
259259
generics,
260260
ty,
261+
body_id: expr.is_some().then_some(DUMMY_NODE_ID),
261262
expr,
262263
define_opaque: None,
263264
}))
@@ -973,6 +974,7 @@ impl<'a> Parser<'a> {
973974
ident,
974975
generics: Generics::default(),
975976
ty,
977+
body_id: expr.is_some().then_some(DUMMY_NODE_ID),
976978
expr,
977979
define_opaque,
978980
}))

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
174174
);
175175
}
176176
}
177+
// HACK(mgca): see lower_const_body_with_const_block in ast_lowering
178+
ItemKind::Const(box ConstItem { body_id: Some(body_id), .. })
179+
if this.resolver.tcx.features().min_generic_const_args() =>
180+
{
181+
this.create_def(body_id, None, DefKind::InlineConst, i.span);
182+
}
177183
_ => {}
178184
}
179185
visit::walk_item(this, i);
@@ -334,7 +340,15 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
334340
};
335341

336342
let def = self.create_def(i.id, Some(ident.name), def_kind, i.span);
337-
self.with_parent(def, |this| visit::walk_assoc_item(this, i, ctxt));
343+
self.with_parent(def, |this| {
344+
// HACK(mgca): see lower_const_body_with_const_block in ast_lowering
345+
if let AssocItemKind::Const(box ConstItem { body_id: Some(body_id), .. }) = i.kind
346+
&& this.resolver.tcx.features().min_generic_const_args()
347+
{
348+
this.create_def(body_id, None, DefKind::InlineConst, i.span);
349+
}
350+
visit::walk_assoc_item(this, i, ctxt)
351+
});
338352
}
339353

340354
fn visit_pat(&mut self, pat: &'a Pat) {

src/tools/clippy/clippy_utils/src/ast_utils/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
355355
ident: li,
356356
generics: lg,
357357
ty: lt,
358+
body_id: _,
358359
expr: le,
359360
define_opaque: _,
360361
}),
@@ -363,6 +364,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
363364
ident: ri,
364365
generics: rg,
365366
ty: rt,
367+
body_id: _,
366368
expr: re,
367369
define_opaque: _,
368370
}),
@@ -595,6 +597,7 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
595597
ident: li,
596598
generics: lg,
597599
ty: lt,
600+
body_id: _,
598601
expr: le,
599602
define_opaque: _,
600603
}),
@@ -603,6 +606,7 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
603606
ident: ri,
604607
generics: rg,
605608
ty: rt,
609+
body_id: _,
606610
expr: re,
607611
define_opaque: _,
608612
}),

0 commit comments

Comments
 (0)