Skip to content

Commit 7c75fe4

Browse files
committed
Auto merge of #104170 - cjgillot:hir-def-id, r=fee1-dead
Record `LocalDefId` in HIR nodes instead of a side table This is part of an attempt to remove the `HirId -> LocalDefId` table from HIR. This attempt is a prerequisite to creation of `LocalDefId` after HIR lowering (#96840), by controlling how `def_id` information is accessed. This first part adds the information to HIR nodes themselves instead of a table. The second part is #103902 The third part will be to make `hir::Visitor::visit_fn` take a `LocalDefId` as last parameter. The fourth part will be to completely remove the side table.
2 parents 9340e5c + df5c11a commit 7c75fe4

File tree

63 files changed

+449
-549
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+449
-549
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
643643
// `static |_task_context| -> <ret_ty> { body }`:
644644
let generator_kind = {
645645
let c = self.arena.alloc(hir::Closure {
646+
def_id: self.local_def_id(closure_node_id),
646647
binder: hir::ClosureBinder::Default,
647648
capture_clause,
648649
bound_generic_params: &[],
@@ -895,6 +896,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
895896
let fn_decl = self.lower_fn_decl(decl, None, fn_decl_span, FnDeclKind::Closure, None);
896897

897898
let c = self.arena.alloc(hir::Closure {
899+
def_id: self.local_def_id(closure_id),
898900
binder: binder_clause,
899901
capture_clause,
900902
bound_generic_params,
@@ -999,6 +1001,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
9991001
self.lower_fn_decl(&outer_decl, None, fn_decl_span, FnDeclKind::Closure, None);
10001002

10011003
let c = self.arena.alloc(hir::Closure {
1004+
def_id: self.local_def_id(closure_id),
10021005
binder: binder_clause,
10031006
capture_clause,
10041007
bound_generic_params,

compiler/rustc_ast_lowering/src/index.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
307307
}
308308

309309
fn visit_variant(&mut self, v: &'hir Variant<'hir>) {
310-
self.insert(v.span, v.id, Node::Variant(v));
311-
self.with_parent(v.id, |this| {
310+
self.insert(v.span, v.hir_id, Node::Variant(v));
311+
self.with_parent(v.hir_id, |this| {
312312
// Register the constructor of this variant.
313313
if let Some(ctor_hir_id) = v.data.ctor_hir_id() {
314314
this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data));

compiler/rustc_ast_lowering/src/item.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -709,11 +709,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
709709
}
710710

711711
fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
712-
let id = self.lower_node_id(v.id);
713-
self.lower_attrs(id, &v.attrs);
712+
let hir_id = self.lower_node_id(v.id);
713+
self.lower_attrs(hir_id, &v.attrs);
714714
hir::Variant {
715-
id,
716-
data: self.lower_variant_data(id, &v.data),
715+
hir_id,
716+
def_id: self.local_def_id(v.id),
717+
data: self.lower_variant_data(hir_id, &v.data),
717718
disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
718719
ident: self.lower_ident(v.ident),
719720
span: self.lower_span(v.span),
@@ -739,12 +740,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
739740
fields.iter().enumerate().map(|f| self.lower_field_def(f)),
740741
),
741742
ctor_id,
743+
self.local_def_id(id),
742744
)
743745
}
744746
VariantData::Unit(id) => {
745747
let ctor_id = self.lower_node_id(id);
746748
self.alias_attrs(ctor_id, parent_id);
747-
hir::VariantData::Unit(ctor_id)
749+
hir::VariantData::Unit(ctor_id, self.local_def_id(id))
748750
}
749751
}
750752
}
@@ -767,6 +769,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
767769
hir::FieldDef {
768770
span: self.lower_span(f.span),
769771
hir_id,
772+
def_id: self.local_def_id(f.id),
770773
ident: match f.ident {
771774
Some(ident) => self.lower_ident(ident),
772775
// FIXME(jseyfried): positional field hygiene.

compiler/rustc_ast_lowering/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -830,8 +830,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
830830
),
831831
};
832832
let hir_id = self.lower_node_id(node_id);
833+
let def_id = self.local_def_id(node_id);
833834
Some(hir::GenericParam {
834835
hir_id,
836+
def_id,
835837
name,
836838
span: self.lower_span(ident.span),
837839
pure_wrt_drop: false,
@@ -1165,7 +1167,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11651167
let node_id = self.next_node_id();
11661168

11671169
// Add a definition for the in-band const def.
1168-
self.create_def(
1170+
let def_id = self.create_def(
11691171
parent_def_id.def_id,
11701172
node_id,
11711173
DefPathData::AnonConst,
@@ -1181,6 +1183,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11811183
};
11821184

11831185
let ct = self.with_new_scopes(|this| hir::AnonConst {
1186+
def_id,
11841187
hir_id: this.lower_node_id(node_id),
11851188
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
11861189
});
@@ -1528,6 +1531,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15281531

15291532
hir::GenericParam {
15301533
hir_id,
1534+
def_id: lctx.local_def_id(new_node_id),
15311535
name,
15321536
span: lifetime.ident.span,
15331537
pure_wrt_drop: false,
@@ -1985,6 +1989,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19851989

19861990
hir::GenericParam {
19871991
hir_id,
1992+
def_id: this.local_def_id(new_node_id),
19881993
name,
19891994
span: lifetime.ident.span,
19901995
pure_wrt_drop: false,
@@ -2183,6 +2188,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21832188
self.lower_attrs(hir_id, &param.attrs);
21842189
hir::GenericParam {
21852190
hir_id,
2191+
def_id: self.local_def_id(param.id),
21862192
name,
21872193
span: self.lower_span(param.span()),
21882194
pure_wrt_drop: self.tcx.sess.contains_name(&param.attrs, sym::may_dangle),
@@ -2287,6 +2293,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22872293
// Set the name to `impl Bound1 + Bound2`.
22882294
let param = hir::GenericParam {
22892295
hir_id: self.lower_node_id(node_id),
2296+
def_id,
22902297
name: ParamName::Plain(self.lower_ident(ident)),
22912298
pure_wrt_drop: false,
22922299
span: self.lower_span(span),
@@ -2347,6 +2354,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23472354

23482355
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
23492356
self.with_new_scopes(|this| hir::AnonConst {
2357+
def_id: this.local_def_id(c.id),
23502358
hir_id: this.lower_node_id(c.id),
23512359
body: this.lower_const_body(c.value.span, Some(&c.value)),
23522360
})

compiler/rustc_borrowck/src/type_check/input_output.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
4242
user_provided_sig = None;
4343
} else {
4444
let typeck_results = self.tcx().typeck(mir_def_id);
45-
user_provided_sig = typeck_results.user_provided_sigs.get(&mir_def_id.to_def_id()).map(
46-
|user_provided_poly_sig| {
45+
user_provided_sig =
46+
typeck_results.user_provided_sigs.get(&mir_def_id).map(|user_provided_poly_sig| {
4747
// Instantiate the canonicalized variables from
4848
// user-provided signature (e.g., the `_` in the code
4949
// above) with fresh variables.
@@ -60,8 +60,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
6060
LateBoundRegionConversionTime::FnCall,
6161
poly_sig,
6262
)
63-
},
64-
);
63+
});
6564
}
6665

6766
debug!(?normalized_input_tys, ?body.local_decls);

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

+21-8
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ pub enum GenericParamKind<'hir> {
487487
#[derive(Debug, HashStable_Generic)]
488488
pub struct GenericParam<'hir> {
489489
pub hir_id: HirId,
490+
pub def_id: LocalDefId,
490491
pub name: ParamName,
491492
pub span: Span,
492493
pub pure_wrt_drop: bool,
@@ -921,6 +922,7 @@ pub struct Crate<'hir> {
921922

922923
#[derive(Debug, HashStable_Generic)]
923924
pub struct Closure<'hir> {
925+
pub def_id: LocalDefId,
924926
pub binder: ClosureBinder,
925927
pub capture_clause: CaptureBy,
926928
pub bound_generic_params: &'hir [GenericParam<'hir>],
@@ -1615,7 +1617,7 @@ pub enum ArrayLen {
16151617
impl ArrayLen {
16161618
pub fn hir_id(&self) -> HirId {
16171619
match self {
1618-
&ArrayLen::Infer(hir_id, _) | &ArrayLen::Body(AnonConst { hir_id, body: _ }) => hir_id,
1620+
&ArrayLen::Infer(hir_id, _) | &ArrayLen::Body(AnonConst { hir_id, .. }) => hir_id,
16191621
}
16201622
}
16211623
}
@@ -1627,10 +1629,11 @@ impl ArrayLen {
16271629
/// explicit discriminant values for enum variants.
16281630
///
16291631
/// You can check if this anon const is a default in a const param
1630-
/// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_hir_id(..)`
1632+
/// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_def_id(..)`
16311633
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
16321634
pub struct AnonConst {
16331635
pub hir_id: HirId,
1636+
pub def_id: LocalDefId,
16341637
pub body: BodyId,
16351638
}
16361639

@@ -2798,7 +2801,8 @@ pub struct Variant<'hir> {
27982801
/// Name of the variant.
27992802
pub ident: Ident,
28002803
/// Id of the variant (not the constructor, see `VariantData::ctor_hir_id()`).
2801-
pub id: HirId,
2804+
pub hir_id: HirId,
2805+
pub def_id: LocalDefId,
28022806
/// Fields and constructor id of the variant.
28032807
pub data: VariantData<'hir>,
28042808
/// Explicit discriminant (e.g., `Foo = 1`).
@@ -2865,6 +2869,7 @@ pub struct FieldDef<'hir> {
28652869
pub vis_span: Span,
28662870
pub ident: Ident,
28672871
pub hir_id: HirId,
2872+
pub def_id: LocalDefId,
28682873
pub ty: &'hir Ty<'hir>,
28692874
}
28702875

@@ -2886,11 +2891,11 @@ pub enum VariantData<'hir> {
28862891
/// A tuple variant.
28872892
///
28882893
/// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
2889-
Tuple(&'hir [FieldDef<'hir>], HirId),
2894+
Tuple(&'hir [FieldDef<'hir>], HirId, LocalDefId),
28902895
/// A unit variant.
28912896
///
28922897
/// E.g., `Bar = ..` as in `enum Foo { Bar = .. }`.
2893-
Unit(HirId),
2898+
Unit(HirId, LocalDefId),
28942899
}
28952900

28962901
impl<'hir> VariantData<'hir> {
@@ -2902,11 +2907,19 @@ impl<'hir> VariantData<'hir> {
29022907
}
29032908
}
29042909

2910+
/// Return the `LocalDefId` of this variant's constructor, if it has one.
2911+
pub fn ctor_def_id(&self) -> Option<LocalDefId> {
2912+
match *self {
2913+
VariantData::Struct(_, _) => None,
2914+
VariantData::Tuple(_, _, def_id) | VariantData::Unit(_, def_id) => Some(def_id),
2915+
}
2916+
}
2917+
29052918
/// Return the `HirId` of this variant's constructor, if it has one.
29062919
pub fn ctor_hir_id(&self) -> Option<HirId> {
29072920
match *self {
29082921
VariantData::Struct(_, _) => None,
2909-
VariantData::Tuple(_, hir_id) | VariantData::Unit(hir_id) => Some(hir_id),
2922+
VariantData::Tuple(_, hir_id, _) | VariantData::Unit(hir_id, _) => Some(hir_id),
29102923
}
29112924
}
29122925
}
@@ -3532,7 +3545,7 @@ impl<'hir> Node<'hir> {
35323545
/// Get the fields for the tuple-constructor,
35333546
/// if this node is a tuple constructor, otherwise None
35343547
pub fn tuple_fields(&self) -> Option<&'hir [FieldDef<'hir>]> {
3535-
if let Node::Ctor(&VariantData::Tuple(fields, _)) = self { Some(fields) } else { None }
3548+
if let Node::Ctor(&VariantData::Tuple(fields, _, _)) = self { Some(fields) } else { None }
35363549
}
35373550
}
35383551

@@ -3548,7 +3561,7 @@ mod size_asserts {
35483561
static_assert_size!(FnDecl<'_>, 40);
35493562
static_assert_size!(ForeignItem<'_>, 72);
35503563
static_assert_size!(ForeignItemKind<'_>, 40);
3551-
static_assert_size!(GenericArg<'_>, 24);
3564+
static_assert_size!(GenericArg<'_>, 32);
35523565
static_assert_size!(GenericBound<'_>, 48);
35533566
static_assert_size!(Generics<'_>, 56);
35543567
static_assert_size!(Impl<'_>, 80);

compiler/rustc_hir/src/intravisit.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
733733
walk_list!(visitor, visit_arm, arms);
734734
}
735735
ExprKind::Closure(&Closure {
736+
def_id: _,
736737
binder: _,
737738
bound_generic_params,
738739
fn_decl,
@@ -1084,7 +1085,7 @@ pub fn walk_enum_def<'v, V: Visitor<'v>>(
10841085

10851086
pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V, variant: &'v Variant<'v>) {
10861087
visitor.visit_ident(variant.ident);
1087-
visitor.visit_id(variant.id);
1088+
visitor.visit_id(variant.hir_id);
10881089
visitor.visit_variant_data(&variant.data);
10891090
walk_list!(visitor, visit_anon_const, &variant.disr_expr);
10901091
}

compiler/rustc_hir_analysis/src/astconv/mod.rs

+6-9
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
},
@@ -856,7 +855,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
856855
&self,
857856
bounds: &mut Bounds<'hir>,
858857
ast_bounds: &'hir [hir::GenericBound<'hir>],
859-
self_ty_where_predicates: Option<(hir::HirId, &'hir [hir::WherePredicate<'hir>])>,
858+
self_ty_where_predicates: Option<(LocalDefId, &'hir [hir::WherePredicate<'hir>])>,
860859
span: Span,
861860
) {
862861
let tcx = self.tcx();
@@ -876,10 +875,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
876875
};
877876
search_bounds(ast_bounds);
878877
if let Some((self_ty, where_clause)) = self_ty_where_predicates {
879-
let self_ty_def_id = tcx.hir().local_def_id(self_ty).to_def_id();
880878
for clause in where_clause {
881879
if let hir::WherePredicate::BoundPredicate(pred) = clause {
882-
if pred.is_param_bound(self_ty_def_id) {
880+
if pred.is_param_bound(self_ty.to_def_id()) {
883881
search_bounds(pred.bounds);
884882
}
885883
}
@@ -2722,16 +2720,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
27222720
let length = match length {
27232721
&hir::ArrayLen::Infer(_, span) => self.ct_infer(tcx.types.usize, None, span),
27242722
hir::ArrayLen::Body(constant) => {
2725-
let length_def_id = tcx.hir().local_def_id(constant.hir_id);
2726-
ty::Const::from_anon_const(tcx, length_def_id)
2723+
ty::Const::from_anon_const(tcx, constant.def_id)
27272724
}
27282725
};
27292726

27302727
let array_ty = tcx.mk_ty(ty::Array(self.ast_ty_to_ty(ty), length));
27312728
self.normalize_ty(ast_ty.span, array_ty)
27322729
}
27332730
hir::TyKind::Typeof(ref e) => {
2734-
let ty_erased = tcx.type_of(tcx.hir().local_def_id(e.hir_id));
2731+
let ty_erased = tcx.type_of(e.def_id);
27352732
let ty = tcx.fold_regions(ty_erased, |r, _| {
27362733
if r.is_erased() { tcx.lifetimes.re_static } else { r }
27372734
});

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
853853

854854
// Const parameters are well formed if their type is structural match.
855855
hir::GenericParamKind::Const { ty: hir_ty, default: _ } => {
856-
let ty = tcx.type_of(tcx.hir().local_def_id(param.hir_id));
856+
let ty = tcx.type_of(param.def_id);
857857

858858
if tcx.features().adt_const_params {
859859
if let Some(non_structural_match_ty) =

0 commit comments

Comments
 (0)