Skip to content

Commit 96d02e9

Browse files
committed
Add dummy NodeId to AST const item for mgca lowering
1 parent 4a0cca3 commit 96d02e9

File tree

10 files changed

+56
-39
lines changed

10 files changed

+56
-39
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3538,6 +3538,11 @@ pub struct ConstItem {
35383538
pub ident: Ident,
35393539
pub generics: Generics,
35403540
pub ty: P<Ty>,
3541+
/// A [`NodeId`] that can be used for the body of the const, independently of the ID
3542+
/// of the body's root expression.
3543+
// HACK(mgca): this is potentially temporary, tbd, in order to create defs for const bodies.
3544+
// FIXME(mgca): maybe merge this with expr since their Options should be in sync
3545+
pub body_id: Option<NodeId>,
35413546
pub expr: Option<P<Expr>>,
35423547
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
35433548
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1465,11 +1465,12 @@ impl WalkItemKind for AssocItemKind {
14651465
}
14661466

14671467
fn walk_const_item<T: MutVisitor>(vis: &mut T, item: &mut ConstItem) {
1468-
let ConstItem { defaultness, ident, generics, ty, expr, define_opaque } = item;
1468+
let ConstItem { defaultness, ident, generics, ty, body_id, expr, define_opaque } = item;
14691469
visit_defaultness(vis, defaultness);
14701470
vis.visit_ident(ident);
14711471
vis.visit_generics(generics);
14721472
vis.visit_ty(ty);
1473+
visit_opt(body_id, |body_id| vis.visit_id(body_id));
14731474
visit_opt(expr, |expr| vis.visit_expr(expr));
14741475
walk_define_opaques(vis, define_opaque);
14751476
}

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 0 deletions
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

Lines changed: 23 additions & 16 deletions
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);
@@ -498,15 +499,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
498499
fn lower_const_item(
499500
&mut self,
500501
span: Span,
501-
body: Option<&Expr>,
502+
body: Option<(NodeId, &Expr)>,
502503
) -> (hir::BodyId, Option<&'hir hir::ConstArg<'hir>>) {
503504
let mgca = self.tcx.features().min_generic_const_args();
504-
let ct_arg =
505-
if mgca && let Some(expr) = body { self.try_lower_as_const_path(expr) } else { None };
505+
let ct_arg = if mgca && let Some((_, expr)) = body {
506+
self.try_lower_as_const_path(expr)
507+
} else {
508+
None
509+
};
506510
let body_id = if mgca && ct_arg.is_none() {
507511
self.lower_const_body_with_const_block(span, body)
508512
} else {
509-
self.lower_const_body(span, body)
513+
self.lower_const_body(span, body.map(|(_, e)| e))
510514
};
511515
(body_id, ct_arg)
512516
}
@@ -809,6 +813,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
809813
ident,
810814
generics,
811815
ty,
816+
body_id,
812817
expr,
813818
define_opaque,
814819
..
@@ -820,7 +825,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
820825
|this| {
821826
let ty = this
822827
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
823-
match expr.as_deref().map(|e| this.lower_const_item(i.span, Some(e))) {
828+
let lowered_body = expr
829+
.as_deref()
830+
.map(|e| this.lower_const_item(i.span, Some((body_id.unwrap(), e))));
831+
match lowered_body {
824832
Some((body, ct_arg)) => {
825833
hir::TraitItemKind::Const(ty, Some(body), ct_arg)
826834
}
@@ -1004,6 +1012,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10041012
ident,
10051013
generics,
10061014
ty,
1015+
body_id,
10071016
expr,
10081017
define_opaque,
10091018
..
@@ -1017,7 +1026,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
10171026
let ty = this
10181027
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
10191028
this.lower_define_opaque(hir_id, &define_opaque);
1020-
let (body, ct_arg) = this.lower_const_item(i.span, expr.as_deref());
1029+
let (body, ct_arg) =
1030+
this.lower_const_item(i.span, body_id.zip(expr.as_deref()));
10211031
hir::ImplItemKind::Const(ty, body, ct_arg)
10221032
},
10231033
),
@@ -1293,23 +1303,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
12931303
pub(super) fn lower_const_body_with_const_block(
12941304
&mut self,
12951305
span: Span,
1296-
expr: Option<&Expr>,
1306+
body: Option<(NodeId, &Expr)>,
12971307
) -> hir::BodyId {
12981308
self.lower_body(|this| {
12991309
(
13001310
&[],
1301-
match expr {
1302-
Some(expr) => {
1303-
let def_id = this.local_def_id(expr.id);
1304-
// TODO: somehow avoid reusing the same nodeid for the const block and the body expr
1305-
let hir_id = this.lower_node_id(expr.id);
1311+
match body {
1312+
Some((body_id, expr)) => {
13061313
let block = hir::ConstBlock {
1307-
def_id,
1308-
hir_id,
1314+
def_id: this.local_def_id(body_id),
1315+
hir_id: this.lower_node_id(body_id),
13091316
body: this.lower_const_body(expr.span, Some(expr)),
13101317
};
13111318
hir::Expr {
1312-
hir_id,
1319+
hir_id: this.next_id(),
13131320
span: this.lower_span(expr.span),
13141321
kind: hir::ExprKind::ConstBlock(block),
13151322
}

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

Lines changed: 2 additions & 0 deletions
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

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
@@ -720,6 +720,7 @@ impl<'a> ExtCtxt<'a> {
720720
// FIXME(generic_const_items): Pass the generics as a parameter.
721721
generics: ast::Generics::default(),
722722
ty,
723+
body_id: Some(ast::DUMMY_NODE_ID),
723724
expr: Some(expr),
724725
define_opaque: None,
725726
}

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
}))
@@ -984,6 +985,7 @@ impl<'a> Parser<'a> {
984985
ident,
985986
generics: Generics::default(),
986987
ty,
988+
body_id: expr.is_some().then_some(DUMMY_NODE_ID),
987989
expr,
988990
define_opaque,
989991
}))

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
175175
}
176176
}
177177
// HACK(mgca): see lower_const_body_with_const_block in ast_lowering
178-
ItemKind::Const(box ConstItem { expr: Some(ref expr), .. })
178+
ItemKind::Const(box ConstItem { body_id: Some(body_id), .. })
179179
if this.resolver.tcx.features().min_generic_const_args() =>
180180
{
181-
this.create_def(expr.id, None, DefKind::InlineConst, i.span);
181+
this.create_def(body_id, None, DefKind::InlineConst, i.span);
182182
}
183183
_ => {}
184184
}
@@ -342,10 +342,10 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
342342
let def = self.create_def(i.id, Some(ident.name), def_kind, i.span);
343343
self.with_parent(def, |this| {
344344
// HACK(mgca): see lower_const_body_with_const_block in ast_lowering
345-
if let AssocItemKind::Const(box ConstItem { expr: Some(expr), .. }) = &i.kind
345+
if let AssocItemKind::Const(box ConstItem { body_id: Some(body_id), .. }) = i.kind
346346
&& this.resolver.tcx.features().min_generic_const_args()
347347
{
348-
this.create_def(expr.id, None, DefKind::InlineConst, i.span);
348+
this.create_def(body_id, None, DefKind::InlineConst, i.span);
349349
}
350350
visit::walk_assoc_item(this, i, ctxt)
351351
});

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -347,17 +347,14 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
347347
safety: rs,
348348
define_opaque: _,
349349
}),
350-
) => eq_id(*li, *ri)
351-
&& lm == rm
352-
&& ls == rs
353-
&& eq_ty(lt, rt)
354-
&& eq_expr_opt(le.as_ref(), re.as_ref()),
350+
) => eq_id(*li, *ri) && lm == rm && ls == rs && eq_ty(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref()),
355351
(
356352
Const(box ConstItem {
357353
defaultness: ld,
358354
ident: li,
359355
generics: lg,
360356
ty: lt,
357+
body_id: _,
361358
expr: le,
362359
define_opaque: _,
363360
}),
@@ -366,14 +363,17 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
366363
ident: ri,
367364
generics: rg,
368365
ty: rt,
366+
body_id: _,
369367
expr: re,
370368
define_opaque: _,
371369
}),
372-
) => eq_defaultness(*ld, *rd)
370+
) => {
371+
eq_defaultness(*ld, *rd)
373372
&& eq_id(*li, *ri)
374373
&& eq_generics(lg, rg)
375374
&& eq_ty(lt, rt)
376-
&& eq_expr_opt(le.as_ref(), re.as_ref()),
375+
&& eq_expr_opt(le.as_ref(), re.as_ref())
376+
},
377377
(
378378
Fn(box ast::Fn {
379379
defaultness: ld,
@@ -439,7 +439,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
439439
},
440440
(Enum(li, le, lg), Enum(ri, re, rg)) => {
441441
eq_id(*li, *ri) && over(&le.variants, &re.variants, eq_variant) && eq_generics(lg, rg)
442-
}
442+
},
443443
(Struct(li, lv, lg), Struct(ri, rv, rg)) | (Union(li, lv, lg), Union(ri, rv, rg)) => {
444444
eq_id(*li, *ri) && eq_variant_data(lv, rv) && eq_generics(lg, rg)
445445
},
@@ -470,7 +470,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
470470
},
471471
(TraitAlias(li, lg, lb), TraitAlias(ri, rg, rb)) => {
472472
eq_id(*li, *ri) && eq_generics(lg, rg) && over(lb, rb, eq_generic_bound)
473-
}
473+
},
474474
(
475475
Impl(box ast::Impl {
476476
safety: lu,
@@ -505,7 +505,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
505505
(MacCall(l), MacCall(r)) => eq_mac_call(l, r),
506506
(MacroDef(li, ld), MacroDef(ri, rd)) => {
507507
eq_id(*li, *ri) && ld.macro_rules == rd.macro_rules && eq_delim_args(&ld.body, &rd.body)
508-
}
508+
},
509509
_ => false,
510510
}
511511
}
@@ -530,13 +530,7 @@ pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
530530
safety: rs,
531531
define_opaque: _,
532532
}),
533-
) => {
534-
eq_id(*li, *ri)
535-
&& eq_ty(lt, rt)
536-
&& lm == rm
537-
&& eq_expr_opt(le.as_ref(), re.as_ref())
538-
&& ls == rs
539-
}
533+
) => eq_id(*li, *ri) && eq_ty(lt, rt) && lm == rm && eq_expr_opt(le.as_ref(), re.as_ref()) && ls == rs,
540534
(
541535
Fn(box ast::Fn {
542536
defaultness: ld,
@@ -602,6 +596,7 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
602596
ident: li,
603597
generics: lg,
604598
ty: lt,
599+
body_id: _,
605600
expr: le,
606601
define_opaque: _,
607602
}),
@@ -610,6 +605,7 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
610605
ident: ri,
611606
generics: rg,
612607
ty: rt,
608+
body_id: _,
613609
expr: re,
614610
define_opaque: _,
615611
}),
@@ -619,7 +615,7 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
619615
&& eq_generics(lg, rg)
620616
&& eq_ty(lt, rt)
621617
&& eq_expr_opt(le.as_ref(), re.as_ref())
622-
}
618+
},
623619
(
624620
Fn(box ast::Fn {
625621
defaultness: ld,

0 commit comments

Comments
 (0)