Skip to content

Commit 9d20aca

Browse files
committed
Store a LocalDefId in hir::Variant & hir::Field.
1 parent 607d0c2 commit 9d20aca

File tree

25 files changed

+122
-149
lines changed

25 files changed

+122
-149
lines changed

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_hir/src/hir.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -2801,7 +2801,8 @@ pub struct Variant<'hir> {
28012801
/// Name of the variant.
28022802
pub ident: Ident,
28032803
/// Id of the variant (not the constructor, see `VariantData::ctor_hir_id()`).
2804-
pub id: HirId,
2804+
pub hir_id: HirId,
2805+
pub def_id: LocalDefId,
28052806
/// Fields and constructor id of the variant.
28062807
pub data: VariantData<'hir>,
28072808
/// Explicit discriminant (e.g., `Foo = 1`).
@@ -2868,6 +2869,7 @@ pub struct FieldDef<'hir> {
28682869
pub vis_span: Span,
28692870
pub ident: Ident,
28702871
pub hir_id: HirId,
2872+
pub def_id: LocalDefId,
28712873
pub ty: &'hir Ty<'hir>,
28722874
}
28732875

@@ -2889,11 +2891,11 @@ pub enum VariantData<'hir> {
28892891
/// A tuple variant.
28902892
///
28912893
/// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
2892-
Tuple(&'hir [FieldDef<'hir>], HirId),
2894+
Tuple(&'hir [FieldDef<'hir>], HirId, LocalDefId),
28932895
/// A unit variant.
28942896
///
28952897
/// E.g., `Bar = ..` as in `enum Foo { Bar = .. }`.
2896-
Unit(HirId),
2898+
Unit(HirId, LocalDefId),
28972899
}
28982900

28992901
impl<'hir> VariantData<'hir> {
@@ -2905,11 +2907,19 @@ impl<'hir> VariantData<'hir> {
29052907
}
29062908
}
29072909

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+
29082918
/// Return the `HirId` of this variant's constructor, if it has one.
29092919
pub fn ctor_hir_id(&self) -> Option<HirId> {
29102920
match *self {
29112921
VariantData::Struct(_, _) => None,
2912-
VariantData::Tuple(_, hir_id) | VariantData::Unit(hir_id) => Some(hir_id),
2922+
VariantData::Tuple(_, hir_id, _) | VariantData::Unit(hir_id, _) => Some(hir_id),
29132923
}
29142924
}
29152925
}
@@ -3535,7 +3545,7 @@ impl<'hir> Node<'hir> {
35353545
/// Get the fields for the tuple-constructor,
35363546
/// if this node is a tuple constructor, otherwise None
35373547
pub fn tuple_fields(&self) -> Option<&'hir [FieldDef<'hir>]> {
3538-
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 }
35393549
}
35403550
}
35413551

compiler/rustc_hir/src/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ pub fn walk_enum_def<'v, V: Visitor<'v>>(
10851085

10861086
pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V, variant: &'v Variant<'v>) {
10871087
visitor.visit_ident(variant.ident);
1088-
visitor.visit_id(variant.id);
1088+
visitor.visit_id(variant.hir_id);
10891089
visitor.visit_variant_data(&variant.data);
10901090
walk_list!(visitor, visit_anon_const, &variant.disr_expr);
10911091
}

compiler/rustc_hir_analysis/src/collect.rs

+16-26
Original file line numberDiff line numberDiff line change
@@ -633,14 +633,12 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
633633
tcx.ensure().predicates_of(def_id);
634634

635635
for f in struct_def.fields() {
636-
let def_id = tcx.hir().local_def_id(f.hir_id);
637-
tcx.ensure().generics_of(def_id);
638-
tcx.ensure().type_of(def_id);
639-
tcx.ensure().predicates_of(def_id);
636+
tcx.ensure().generics_of(f.def_id);
637+
tcx.ensure().type_of(f.def_id);
638+
tcx.ensure().predicates_of(f.def_id);
640639
}
641640

642-
if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
643-
let ctor_def_id = tcx.hir().local_def_id(ctor_hir_id);
641+
if let Some(ctor_def_id) = struct_def.ctor_def_id() {
644642
convert_variant_ctor(tcx, ctor_def_id);
645643
}
646644
}
@@ -817,7 +815,6 @@ fn convert_variant(
817815
.fields()
818816
.iter()
819817
.map(|f| {
820-
let fid = tcx.hir().local_def_id(f.hir_id);
821818
let dup_span = seen_fields.get(&f.ident.normalize_to_macros_2_0()).cloned();
822819
if let Some(prev_span) = dup_span {
823820
tcx.sess.emit_err(errors::FieldAlreadyDeclared {
@@ -829,7 +826,11 @@ fn convert_variant(
829826
seen_fields.insert(f.ident.normalize_to_macros_2_0(), f.span);
830827
}
831828

832-
ty::FieldDef { did: fid.to_def_id(), name: f.ident.name, vis: tcx.visibility(fid) }
829+
ty::FieldDef {
830+
did: f.def_id.to_def_id(),
831+
name: f.ident.name,
832+
vis: tcx.visibility(f.def_id),
833+
}
833834
})
834835
.collect();
835836
let recovered = match def {
@@ -870,10 +871,6 @@ fn adt_def<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AdtDef<'tcx> {
870871
.variants
871872
.iter()
872873
.map(|v| {
873-
let variant_did = Some(tcx.hir().local_def_id(v.id));
874-
let ctor_did =
875-
v.data.ctor_hir_id().map(|hir_id| tcx.hir().local_def_id(hir_id));
876-
877874
let discr = if let Some(ref e) = v.disr_expr {
878875
distance_from_explicit = 0;
879876
ty::VariantDiscr::Explicit(e.def_id.to_def_id())
@@ -884,8 +881,8 @@ fn adt_def<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AdtDef<'tcx> {
884881

885882
convert_variant(
886883
tcx,
887-
variant_did,
888-
ctor_did,
884+
Some(v.def_id),
885+
v.data.ctor_def_id(),
889886
v.ident,
890887
discr,
891888
&v.data,
@@ -898,13 +895,10 @@ fn adt_def<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AdtDef<'tcx> {
898895
(AdtKind::Enum, variants)
899896
}
900897
ItemKind::Struct(ref def, _) => {
901-
let variant_did = None::<LocalDefId>;
902-
let ctor_did = def.ctor_hir_id().map(|hir_id| tcx.hir().local_def_id(hir_id));
903-
904898
let variants = std::iter::once(convert_variant(
905899
tcx,
906-
variant_did,
907-
ctor_did,
900+
None,
901+
def.ctor_def_id(),
908902
item.ident,
909903
ty::VariantDiscr::Relative(0),
910904
def,
@@ -916,13 +910,10 @@ fn adt_def<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AdtDef<'tcx> {
916910
(AdtKind::Struct, variants)
917911
}
918912
ItemKind::Union(ref def, _) => {
919-
let variant_did = None;
920-
let ctor_did = def.ctor_hir_id().map(|hir_id| tcx.hir().local_def_id(hir_id));
921-
922913
let variants = std::iter::once(convert_variant(
923914
tcx,
924-
variant_did,
925-
ctor_did,
915+
None,
916+
def.ctor_def_id(),
926917
item.ident,
927918
ty::VariantDiscr::Relative(0),
928919
def,
@@ -1182,8 +1173,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
11821173

11831174
Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor_hir_id().is_some() => {
11841175
let ty = tcx.type_of(tcx.hir().get_parent_item(hir_id));
1185-
let inputs =
1186-
data.fields().iter().map(|f| tcx.type_of(tcx.hir().local_def_id(f.hir_id)));
1176+
let inputs = data.fields().iter().map(|f| tcx.type_of(f.def_id));
11871177
ty::Binder::dummy(tcx.mk_fn_sig(
11881178
inputs,
11891179
ty,

compiler/rustc_hir_pretty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ impl<'a> State<'a> {
754754
for v in variants {
755755
self.space_if_not_bol();
756756
self.maybe_print_comment(v.span.lo());
757-
self.print_outer_attributes(self.attrs(v.id));
757+
self.print_outer_attributes(self.attrs(v.hir_id));
758758
self.ibox(INDENT_UNIT);
759759
self.print_variant(v);
760760
self.word(",");

compiler/rustc_lint/src/builtin.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,8 @@ impl<'tcx> LateLintPass<'tcx> for BoxPointers {
185185
// If it's a struct, we also have to check the fields' types
186186
match it.kind {
187187
hir::ItemKind::Struct(ref struct_def, _) | hir::ItemKind::Union(ref struct_def, _) => {
188-
for struct_field in struct_def.fields() {
189-
let def_id = cx.tcx.hir().local_def_id(struct_field.hir_id);
190-
self.check_heap_type(cx, struct_field.span, cx.tcx.type_of(def_id));
188+
for field in struct_def.fields() {
189+
self.check_heap_type(cx, field.span, cx.tcx.type_of(field.def_id));
191190
}
192191
}
193192
_ => (),
@@ -673,13 +672,12 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
673672

674673
fn check_field_def(&mut self, cx: &LateContext<'_>, sf: &hir::FieldDef<'_>) {
675674
if !sf.is_positional() {
676-
let def_id = cx.tcx.hir().local_def_id(sf.hir_id);
677-
self.check_missing_docs_attrs(cx, def_id, "a", "struct field")
675+
self.check_missing_docs_attrs(cx, sf.def_id, "a", "struct field")
678676
}
679677
}
680678

681679
fn check_variant(&mut self, cx: &LateContext<'_>, v: &hir::Variant<'_>) {
682-
self.check_missing_docs_attrs(cx, cx.tcx.hir().local_def_id(v.id), "a", "variant");
680+
self.check_missing_docs_attrs(cx, v.def_id, "a", "variant");
683681
}
684682
}
685683

@@ -1424,11 +1422,10 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
14241422

14251423
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
14261424
let map = cx.tcx.hir();
1427-
let def_id = map.local_def_id(field.hir_id);
14281425
if matches!(map.get(map.get_parent_node(field.hir_id)), Node::Variant(_)) {
14291426
return;
14301427
}
1431-
self.perform_lint(cx, "field", def_id, field.vis_span, false);
1428+
self.perform_lint(cx, "field", field.def_id, field.vis_span, false);
14321429
}
14331430

14341431
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {

compiler/rustc_lint/src/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
205205
}
206206

207207
fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) {
208-
self.with_lint_attrs(v.id, |cx| {
208+
self.with_lint_attrs(v.hir_id, |cx| {
209209
lint_callback!(cx, check_variant, v);
210210
hir_visit::walk_variant(cx, v);
211211
})

compiler/rustc_lint/src/levels.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, LintLevelQueryMap<'tcx>> {
320320
}
321321

322322
fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) {
323-
self.add_id(v.id);
323+
self.add_id(v.hir_id);
324324
intravisit::walk_variant(self, v);
325325
}
326326

@@ -392,7 +392,7 @@ impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, QueryMapExpectationsWrapper<'
392392
}
393393

394394
fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) {
395-
self.add_id(v.id);
395+
self.add_id(v.hir_id);
396396
intravisit::walk_variant(self, v);
397397
}
398398

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1558,9 +1558,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15581558
// Encode def_ids for each field and method
15591559
// for methods, write all the stuff get_trait_method
15601560
// needs to know
1561-
let ctor = struct_def
1562-
.ctor_hir_id()
1563-
.map(|ctor_hir_id| self.tcx.hir().local_def_id(ctor_hir_id).local_def_index);
1561+
let ctor = struct_def.ctor_def_id().map(|ctor_def_id| ctor_def_id.local_def_index);
15641562

15651563
let variant = adt_def.non_enum_variant();
15661564
record!(self.tables.variant_data[def_id] <- VariantData {
@@ -1685,8 +1683,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16851683
hir::ItemKind::Struct(ref struct_def, _) => {
16861684
let def = self.tcx.adt_def(item.owner_id.to_def_id());
16871685
// If the struct has a constructor, encode it.
1688-
if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
1689-
let ctor_def_id = self.tcx.hir().local_def_id(ctor_hir_id);
1686+
if let Some(ctor_def_id) = struct_def.ctor_def_id() {
16901687
self.encode_struct_ctor(def, ctor_def_id.to_def_id());
16911688
}
16921689
}

compiler/rustc_mir_transform/src/lib.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,18 @@ fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LocalDefId> {
217217

218218
// Additionally, tuple struct/variant constructors have MIR, but
219219
// they don't have a BodyId, so we need to build them separately.
220-
struct GatherCtors<'a, 'tcx> {
221-
tcx: TyCtxt<'tcx>,
220+
struct GatherCtors<'a> {
222221
set: &'a mut FxIndexSet<LocalDefId>,
223222
}
224-
impl<'tcx> Visitor<'tcx> for GatherCtors<'_, 'tcx> {
223+
impl<'tcx> Visitor<'tcx> for GatherCtors<'_> {
225224
fn visit_variant_data(&mut self, v: &'tcx hir::VariantData<'tcx>) {
226-
if let hir::VariantData::Tuple(_, hir_id) = *v {
227-
self.set.insert(self.tcx.hir().local_def_id(hir_id));
225+
if let hir::VariantData::Tuple(_, _, def_id) = *v {
226+
self.set.insert(def_id);
228227
}
229228
intravisit::walk_struct_def(self, v)
230229
}
231230
}
232-
tcx.hir().visit_all_item_likes_in_crate(&mut GatherCtors { tcx, set: &mut set });
231+
tcx.hir().visit_all_item_likes_in_crate(&mut GatherCtors { set: &mut set });
233232

234233
set
235234
}

compiler/rustc_passes/src/check_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2137,7 +2137,7 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {
21372137
}
21382138

21392139
fn visit_variant(&mut self, variant: &'tcx hir::Variant<'tcx>) {
2140-
self.check_attributes(variant.id, variant.span, Target::Variant, None);
2140+
self.check_attributes(variant.hir_id, variant.span, Target::Variant, None);
21412141
intravisit::walk_variant(self, variant)
21422142
}
21432143

compiler/rustc_passes/src/dead.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
362362
let has_repr_c = self.repr_has_repr_c;
363363
let has_repr_simd = self.repr_has_repr_simd;
364364
let live_fields = def.fields().iter().filter_map(|f| {
365-
let def_id = tcx.hir().local_def_id(f.hir_id);
365+
let def_id = f.def_id;
366366
if has_repr_c || (f.is_positional() && has_repr_simd) {
367367
return Some(def_id);
368368
}
@@ -522,17 +522,13 @@ fn check_item<'tcx>(
522522
DefKind::Enum => {
523523
let item = tcx.hir().item(id);
524524
if let hir::ItemKind::Enum(ref enum_def, _) = item.kind {
525-
let hir = tcx.hir();
526525
if allow_dead_code {
527-
worklist.extend(
528-
enum_def.variants.iter().map(|variant| hir.local_def_id(variant.id)),
529-
);
526+
worklist.extend(enum_def.variants.iter().map(|variant| variant.def_id));
530527
}
531528

532529
for variant in enum_def.variants {
533-
if let Some(ctor_hir_id) = variant.data.ctor_hir_id() {
534-
struct_constructors
535-
.insert(hir.local_def_id(ctor_hir_id), hir.local_def_id(variant.id));
530+
if let Some(ctor_def_id) = variant.data.ctor_def_id() {
531+
struct_constructors.insert(ctor_def_id, variant.def_id);
536532
}
537533
}
538534
}

compiler/rustc_passes/src/lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
219219
let item = tcx.hir().item(id);
220220
if let hir::ItemKind::Enum(def, ..) = &item.kind {
221221
for variant in def.variants {
222-
collector.check_for_lang(Target::Variant, variant.id);
222+
collector.check_for_lang(Target::Variant, variant.hir_id);
223223
}
224224
}
225225
}

0 commit comments

Comments
 (0)