Skip to content

Commit 607d0c2

Browse files
committed
Store a LocalDefId in hir::AnonConst.
1 parent 18482f7 commit 607d0c2

File tree

12 files changed

+49
-56
lines changed

12 files changed

+49
-56
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11601160
let node_id = self.next_node_id();
11611161

11621162
// Add a definition for the in-band const def.
1163-
self.create_def(
1163+
let def_id = self.create_def(
11641164
parent_def_id.def_id,
11651165
node_id,
11661166
DefPathData::AnonConst,
@@ -1176,6 +1176,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11761176
};
11771177

11781178
let ct = self.with_new_scopes(|this| hir::AnonConst {
1179+
def_id,
11791180
hir_id: this.lower_node_id(node_id),
11801181
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
11811182
});
@@ -2346,6 +2347,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23462347

23472348
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
23482349
self.with_new_scopes(|this| hir::AnonConst {
2350+
def_id: this.local_def_id(c.id),
23492351
hir_id: this.lower_node_id(c.id),
23502352
body: this.lower_const_body(c.value.span, Some(&c.value)),
23512353
})

compiler/rustc_codegen_ssa/src/mono_item.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
4040
.iter()
4141
.map(|(op, op_sp)| match *op {
4242
hir::InlineAsmOperand::Const { ref anon_const } => {
43-
let anon_const_def_id =
44-
cx.tcx().hir().local_def_id(anon_const.hir_id).to_def_id();
45-
let const_value =
46-
cx.tcx().const_eval_poly(anon_const_def_id).unwrap_or_else(
47-
|_| span_bug!(*op_sp, "asm const cannot be resolved"),
48-
);
43+
let const_value = cx
44+
.tcx()
45+
.const_eval_poly(anon_const.def_id.to_def_id())
46+
.unwrap_or_else(|_| {
47+
span_bug!(*op_sp, "asm const cannot be resolved")
48+
});
4949
let ty = cx
5050
.tcx()
5151
.typeck_body(anon_const.body)

compiler/rustc_hir/src/hir.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ pub enum ArrayLen {
16171617
impl ArrayLen {
16181618
pub fn hir_id(&self) -> HirId {
16191619
match self {
1620-
&ArrayLen::Infer(hir_id, _) | &ArrayLen::Body(AnonConst { hir_id, body: _ }) => hir_id,
1620+
&ArrayLen::Infer(hir_id, _) | &ArrayLen::Body(AnonConst { hir_id, .. }) => hir_id,
16211621
}
16221622
}
16231623
}
@@ -1633,6 +1633,7 @@ impl ArrayLen {
16331633
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
16341634
pub struct AnonConst {
16351635
pub hir_id: HirId,
1636+
pub def_id: LocalDefId,
16361637
pub body: BodyId,
16371638
}
16381639

@@ -3550,7 +3551,7 @@ mod size_asserts {
35503551
static_assert_size!(FnDecl<'_>, 40);
35513552
static_assert_size!(ForeignItem<'_>, 72);
35523553
static_assert_size!(ForeignItemKind<'_>, 40);
3553-
static_assert_size!(GenericArg<'_>, 24);
3554+
static_assert_size!(GenericArg<'_>, 32);
35543555
static_assert_size!(GenericBound<'_>, 48);
35553556
static_assert_size!(Generics<'_>, 56);
35563557
static_assert_size!(Impl<'_>, 80);

compiler/rustc_hir_analysis/src/astconv/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
432432
ty::Const::from_opt_const_arg_anon_const(
433433
tcx,
434434
ty::WithOptConstParam {
435-
did: tcx.hir().local_def_id(ct.value.hir_id),
435+
did: ct.value.def_id,
436436
const_param_did: Some(param.def_id),
437437
},
438438
)
@@ -570,8 +570,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
570570
ConvertedBindingKind::Equality(self.ast_ty_to_ty(ty).into())
571571
}
572572
hir::Term::Const(ref c) => {
573-
let local_did = self.tcx().hir().local_def_id(c.hir_id);
574-
let c = Const::from_anon_const(self.tcx(), local_did);
573+
let c = Const::from_anon_const(self.tcx(), c.def_id);
575574
ConvertedBindingKind::Equality(c.into())
576575
}
577576
},
@@ -2712,16 +2711,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
27122711
let length = match length {
27132712
&hir::ArrayLen::Infer(_, span) => self.ct_infer(tcx.types.usize, None, span),
27142713
hir::ArrayLen::Body(constant) => {
2715-
let length_def_id = tcx.hir().local_def_id(constant.hir_id);
2716-
ty::Const::from_anon_const(tcx, length_def_id)
2714+
ty::Const::from_anon_const(tcx, constant.def_id)
27172715
}
27182716
};
27192717

27202718
let array_ty = tcx.mk_ty(ty::Array(self.ast_ty_to_ty(ty), length));
27212719
self.normalize_ty(ast_ty.span, array_ty)
27222720
}
27232721
hir::TyKind::Typeof(ref e) => {
2724-
let ty_erased = tcx.type_of(tcx.hir().local_def_id(e.hir_id));
2722+
let ty_erased = tcx.type_of(e.def_id);
27252723
let ty = tcx.fold_regions(ty_erased, |r, _| {
27262724
if r.is_erased() { tcx.lifetimes.re_static } else { r }
27272725
});

compiler/rustc_hir_analysis/src/collect.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,8 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
297297
hir::GenericParamKind::Const { default, .. } => {
298298
self.tcx.ensure().type_of(param.def_id);
299299
if let Some(default) = default {
300-
let default_def_id = self.tcx.hir().local_def_id(default.hir_id);
301300
// need to store default and type of default
302-
self.tcx.ensure().type_of(default_def_id);
301+
self.tcx.ensure().type_of(default.def_id);
303302
self.tcx.ensure().const_param_default(param.def_id);
304303
}
305304
}
@@ -877,7 +876,7 @@ fn adt_def<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AdtDef<'tcx> {
877876

878877
let discr = if let Some(ref e) = v.disr_expr {
879878
distance_from_explicit = 0;
880-
ty::VariantDiscr::Explicit(tcx.hir().local_def_id(e.hir_id).to_def_id())
879+
ty::VariantDiscr::Explicit(e.def_id.to_def_id())
881880
} else {
882881
ty::VariantDiscr::Relative(distance_from_explicit)
883882
};

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,9 @@ fn const_evaluatable_predicates_of<'tcx>(
316316

317317
impl<'tcx> intravisit::Visitor<'tcx> for ConstCollector<'tcx> {
318318
fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
319-
let def_id = self.tcx.hir().local_def_id(c.hir_id);
320-
let ct = ty::Const::from_anon_const(self.tcx, def_id);
319+
let ct = ty::Const::from_anon_const(self.tcx, c.def_id);
321320
if let ty::ConstKind::Unevaluated(_) = ct.kind() {
322-
let span = self.tcx.hir().span(c.hir_id);
321+
let span = self.tcx.def_span(c.def_id);
323322
self.preds.insert((
324323
ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(ct))
325324
.to_predicate(self.tcx),

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
488488
match length {
489489
&hir::ArrayLen::Infer(_, span) => self.ct_infer(self.tcx.types.usize, None, span),
490490
hir::ArrayLen::Body(anon_const) => {
491-
let const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
492-
let span = self.tcx.hir().span(anon_const.hir_id);
493-
let c = ty::Const::from_anon_const(self.tcx, const_def_id);
491+
let span = self.tcx.def_span(anon_const.def_id);
492+
let c = ty::Const::from_anon_const(self.tcx, anon_const.def_id);
494493
self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None));
495494
self.normalize_associated_types_in(span, c)
496495
}
@@ -502,10 +501,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
502501
ast_c: &hir::AnonConst,
503502
param_def_id: DefId,
504503
) -> ty::Const<'tcx> {
505-
let const_def = ty::WithOptConstParam {
506-
did: self.tcx.hir().local_def_id(ast_c.hir_id),
507-
const_param_did: Some(param_def_id),
508-
};
504+
let const_def =
505+
ty::WithOptConstParam { did: ast_c.def_id, const_param_did: Some(param_def_id) };
509506
let c = ty::Const::from_opt_const_arg_anon_const(self.tcx, const_def);
510507
self.register_wf_obligation(
511508
c.into(),

compiler/rustc_middle/src/hir/map/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
14071407
}
14081408

14091409
fn visit_anon_const(&mut self, c: &'hir AnonConst) {
1410-
self.body_owners.push(self.tcx.hir().local_def_id(c.hir_id));
1410+
self.body_owners.push(c.def_id);
14111411
intravisit::walk_anon_const(self, c)
14121412
}
14131413

compiler/rustc_middle/src/ty/consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ impl<'tcx> Const<'tcx> {
266266
pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Const<'tcx> {
267267
let default_def_id = match tcx.hir().get_by_def_id(def_id.expect_local()) {
268268
hir::Node::GenericParam(hir::GenericParam {
269-
kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) },
269+
kind: hir::GenericParamKind::Const { default: Some(ac), .. },
270270
..
271-
}) => tcx.hir().local_def_id(ac.hir_id),
271+
}) => ac.def_id,
272272
_ => span_bug!(
273273
tcx.def_span(def_id),
274274
"`const_param_default` expected a generic parameter with a constant"

compiler/rustc_mir_build/src/thir/cx/expr.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -608,24 +608,22 @@ impl<'tcx> Cx<'tcx> {
608608
out_expr: out_expr.as_ref().map(|expr| self.mirror_expr(expr)),
609609
},
610610
hir::InlineAsmOperand::Const { ref anon_const } => {
611-
let anon_const_def_id = tcx.hir().local_def_id(anon_const.hir_id);
612611
let value = mir::ConstantKind::from_anon_const(
613612
tcx,
614-
anon_const_def_id,
613+
anon_const.def_id,
615614
self.param_env,
616615
);
617-
let span = tcx.hir().span(anon_const.hir_id);
616+
let span = tcx.def_span(anon_const.def_id);
618617

619618
InlineAsmOperand::Const { value, span }
620619
}
621620
hir::InlineAsmOperand::SymFn { ref anon_const } => {
622-
let anon_const_def_id = tcx.hir().local_def_id(anon_const.hir_id);
623621
let value = mir::ConstantKind::from_anon_const(
624622
tcx,
625-
anon_const_def_id,
623+
anon_const.def_id,
626624
self.param_env,
627625
);
628-
let span = tcx.hir().span(anon_const.hir_id);
626+
let span = tcx.def_span(anon_const.def_id);
629627

630628
InlineAsmOperand::SymFn { value, span }
631629
}
@@ -640,7 +638,7 @@ impl<'tcx> Cx<'tcx> {
640638

641639
hir::ExprKind::ConstBlock(ref anon_const) => {
642640
let ty = self.typeck_results().node_type(anon_const.hir_id);
643-
let did = tcx.hir().local_def_id(anon_const.hir_id).to_def_id();
641+
let did = anon_const.def_id.to_def_id();
644642
let typeck_root_def_id = tcx.typeck_root_def_id(did);
645643
let parent_substs =
646644
tcx.erase_regions(InternalSubsts::identity_for_item(tcx, typeck_root_def_id));

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
565565
id: hir::HirId,
566566
span: Span,
567567
) -> PatKind<'tcx> {
568-
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
569-
let value = mir::ConstantKind::from_inline_const(self.tcx, anon_const_def_id);
568+
let value = mir::ConstantKind::from_inline_const(self.tcx, anon_const.def_id);
570569

571570
// Evaluate early like we do in `lower_path`.
572571
let value = value.eval(self.tcx, self.param_env);

src/test/ui/stats/hir-stats.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -119,33 +119,33 @@ hir-stats HIR STATS
119119
hir-stats Name Accumulated Size Count Item Size
120120
hir-stats ----------------------------------------------------------------
121121
hir-stats ForeignItemRef 24 ( 0.3%) 1 24
122-
hir-stats Lifetime 32 ( 0.4%) 1 32
123-
hir-stats Mod 32 ( 0.4%) 1 32
122+
hir-stats Lifetime 32 ( 0.3%) 1 32
123+
hir-stats Mod 32 ( 0.3%) 1 32
124124
hir-stats ExprField 40 ( 0.4%) 1 40
125125
hir-stats TraitItemRef 56 ( 0.6%) 2 28
126126
hir-stats Local 64 ( 0.7%) 1 64
127127
hir-stats Param 64 ( 0.7%) 2 32
128128
hir-stats InlineAsm 72 ( 0.8%) 1 72
129129
hir-stats ImplItemRef 72 ( 0.8%) 2 36
130-
hir-stats Body 96 ( 1.1%) 3 32
131-
hir-stats GenericArg 96 ( 1.1%) 4 24
132-
hir-stats - Type 24 ( 0.3%) 1
133-
hir-stats - Lifetime 72 ( 0.8%) 3
134-
hir-stats FieldDef 96 ( 1.1%) 2 48
135-
hir-stats Arm 96 ( 1.1%) 2 48
136-
hir-stats Stmt 96 ( 1.1%) 3 32
137-
hir-stats - Local 32 ( 0.4%) 1
138-
hir-stats - Semi 32 ( 0.4%) 1
139-
hir-stats - Expr 32 ( 0.4%) 1
130+
hir-stats Body 96 ( 1.0%) 3 32
131+
hir-stats FieldDef 96 ( 1.0%) 2 48
132+
hir-stats Arm 96 ( 1.0%) 2 48
133+
hir-stats Stmt 96 ( 1.0%) 3 32
134+
hir-stats - Local 32 ( 0.3%) 1
135+
hir-stats - Semi 32 ( 0.3%) 1
136+
hir-stats - Expr 32 ( 0.3%) 1
140137
hir-stats FnDecl 120 ( 1.3%) 3 40
141138
hir-stats Attribute 128 ( 1.4%) 4 32
139+
hir-stats GenericArg 128 ( 1.4%) 4 32
140+
hir-stats - Type 32 ( 0.3%) 1
141+
hir-stats - Lifetime 96 ( 1.0%) 3
142142
hir-stats GenericArgs 144 ( 1.6%) 3 48
143-
hir-stats Variant 160 ( 1.8%) 2 80
143+
hir-stats Variant 160 ( 1.7%) 2 80
144144
hir-stats GenericBound 192 ( 2.1%) 4 48
145145
hir-stats - Trait 192 ( 2.1%) 4
146146
hir-stats WherePredicate 192 ( 2.1%) 3 64
147147
hir-stats - BoundPredicate 192 ( 2.1%) 3
148-
hir-stats Block 288 ( 3.2%) 6 48
148+
hir-stats Block 288 ( 3.1%) 6 48
149149
hir-stats Pat 360 ( 3.9%) 5 72
150150
hir-stats - Wild 72 ( 0.8%) 1
151151
hir-stats - Struct 72 ( 0.8%) 1
@@ -169,10 +169,10 @@ hir-stats - Enum 80 ( 0.9%) 1
169169
hir-stats - ExternCrate 80 ( 0.9%) 1
170170
hir-stats - ForeignMod 80 ( 0.9%) 1
171171
hir-stats - Impl 80 ( 0.9%) 1
172-
hir-stats - Fn 160 ( 1.8%) 2
172+
hir-stats - Fn 160 ( 1.7%) 2
173173
hir-stats - Use 400 ( 4.4%) 5
174174
hir-stats Path 1_280 (14.0%) 32 40
175175
hir-stats PathSegment 1_920 (21.0%) 40 48
176176
hir-stats ----------------------------------------------------------------
177-
hir-stats Total 9_128
177+
hir-stats Total 9_160
178178
hir-stats

0 commit comments

Comments
 (0)