Skip to content

Commit 7a08ebe

Browse files
committed
Add dummy NodeId to AST const item for mgca lowering
1 parent eb28574 commit 7a08ebe

File tree

10 files changed

+51
-14
lines changed

10 files changed

+51
-14
lines changed

compiler/rustc_ast/src/ast.rs

+5
Original file line numberDiff line numberDiff line change
@@ -3613,6 +3613,11 @@ pub struct ConstItem {
36133613
pub ident: Ident,
36143614
pub generics: Generics,
36153615
pub ty: P<Ty>,
3616+
/// A [`NodeId`] that can be used for the body of the const, independently of the ID
3617+
/// of the body's root expression.
3618+
// HACK(mgca): this is potentially temporary, tbd, in order to create defs for const bodies.
3619+
// FIXME(mgca): maybe merge this with expr since their Options should be in sync
3620+
pub body_id: Option<NodeId>,
36163621
pub expr: Option<P<Expr>>,
36173622
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
36183623
}

compiler/rustc_ast/src/mut_visit.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1432,11 +1432,12 @@ impl WalkItemKind for AssocItemKind {
14321432
}
14331433

14341434
fn walk_const_item<T: MutVisitor>(vis: &mut T, item: &mut ConstItem) {
1435-
let ConstItem { defaultness, ident, generics, ty, expr, define_opaque } = item;
1435+
let ConstItem { defaultness, ident, generics, ty, body_id, expr, define_opaque } = item;
14361436
visit_defaultness(vis, defaultness);
14371437
vis.visit_ident(ident);
14381438
vis.visit_generics(generics);
14391439
vis.visit_ty(ty);
1440+
visit_opt(body_id, |body_id| vis.visit_id(body_id));
14401441
visit_opt(expr, |expr| vis.visit_expr(expr));
14411442
walk_define_opaques(vis, define_opaque);
14421443
}

compiler/rustc_ast/src/visit.rs

+2
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ impl WalkItemKind for ItemKind {
388388
ident,
389389
generics,
390390
ty,
391+
body_id: _,
391392
expr,
392393
define_opaque,
393394
}) => {
@@ -989,6 +990,7 @@ impl WalkItemKind for AssocItemKind {
989990
ident,
990991
generics,
991992
ty,
993+
body_id: _,
992994
expr,
993995
define_opaque,
994996
}) => {

compiler/rustc_ast_lowering/src/item.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
199199
ident,
200200
generics,
201201
ty,
202+
body_id,
202203
expr,
203204
define_opaque,
204205
..
@@ -211,7 +212,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
211212
|this| {
212213
let ty = this
213214
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
214-
(ty, this.lower_const_item(span, expr.as_deref()))
215+
(ty, this.lower_const_item(span, body_id.zip(expr.as_deref())))
215216
},
216217
);
217218
self.lower_define_opaque(hir_id, &define_opaque);
@@ -495,21 +496,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
495496
}
496497
}
497498

498-
fn lower_const_item(&mut self, span: Span, body: Option<&Expr>) -> hir::BodyId {
499-
self.lower_const_body(span, body)
499+
fn lower_const_item(&mut self, span: Span, body: Option<(NodeId, &Expr)>) -> hir::BodyId {
500+
self.lower_const_body(span, body.map(|b| b.1))
500501
// TODO: code to add next
501-
// let ct_arg = if self.tcx.features().min_generic_const_args()
502-
// && let Some(expr) = body
503-
// {
502+
// let mgca = self.tcx.features().min_generic_const_args();
503+
// let ct_arg = if mgca && let Some((_, expr)) = body {
504504
// self.try_lower_as_const_path(expr)
505505
// } else {
506506
// None
507507
// };
508-
// let body_id = if body.is_some() && ct_arg.is_none() {
509-
// // TODO: lower as const block instead
510-
// self.lower_const_body(span, body)
508+
// let body_id = if mgca && ct_arg.is_none() {
509+
// self.lower_const_body_with_const_block(span, body)
511510
// } else {
512-
// self.lower_const_body(span, body)
511+
// self.lower_const_body(span, body.map(|(_, e)| e))
513512
// };
514513
// (body_id, ct_arg)
515514
}
@@ -809,6 +808,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
809808
ident,
810809
generics,
811810
ty,
811+
body_id,
812812
expr,
813813
define_opaque,
814814
..
@@ -820,7 +820,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
820820
|this| {
821821
let ty = this
822822
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
823-
let body = expr.as_deref().map(|e| this.lower_const_item(i.span, Some(e)));
823+
let body = expr
824+
.as_deref()
825+
.map(|e| this.lower_const_item(i.span, Some((body_id.unwrap(), e))));
824826
hir::TraitItemKind::Const(ty, body)
825827
},
826828
);
@@ -1000,6 +1002,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10001002
ident,
10011003
generics,
10021004
ty,
1005+
body_id,
10031006
expr,
10041007
define_opaque,
10051008
..
@@ -1013,7 +1016,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10131016
let ty = this
10141017
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
10151018
this.lower_define_opaque(hir_id, &define_opaque);
1016-
let body = this.lower_const_item(i.span, expr.as_deref());
1019+
let body = this.lower_const_item(i.span, body_id.zip(expr.as_deref()));
10171020
hir::ImplItemKind::Const(ty, body)
10181021
},
10191022
),
@@ -1290,6 +1293,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
12901293
self.lower_fn_body(decl, contract, |this| this.lower_block_expr(body))
12911294
}
12921295

1296+
// TODO: add lower_const_body_with_const_block
1297+
12931298
pub(super) fn lower_const_body(&mut self, span: Span, expr: Option<&Expr>) -> hir::BodyId {
12941299
self.lower_body(|this| {
12951300
(

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

+2
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ impl<'a> State<'a> {
209209
ident,
210210
generics,
211211
ty,
212+
body_id: _,
212213
expr,
213214
define_opaque,
214215
}) => {
@@ -568,6 +569,7 @@ impl<'a> State<'a> {
568569
ident,
569570
generics,
570571
ty,
572+
body_id: _,
571573
expr,
572574
define_opaque,
573575
}) => {

compiler/rustc_builtin_macros/src/test.rs

+1
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

+1
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ impl<'a> ExtCtxt<'a> {
722722
// FIXME(generic_const_items): Pass the generics as a parameter.
723723
generics: ast::Generics::default(),
724724
ty,
725+
body_id: Some(ast::DUMMY_NODE_ID),
725726
expr: Some(expr),
726727
define_opaque: None,
727728
}

compiler/rustc_parse/src/parser/item.rs

+2
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

+15-1
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

+4
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)