Skip to content

Commit 3df10a2

Browse files
committed
Do not fetch HIR to compute variances.
1 parent 00ebeb8 commit 3df10a2

File tree

4 files changed

+48
-130
lines changed

4 files changed

+48
-130
lines changed

Diff for: compiler/rustc_middle/src/hir/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ impl ModuleItems {
6363
self.foreign_items.iter().copied()
6464
}
6565

66+
pub fn definitions(&self) -> impl Iterator<Item = LocalDefId> + '_ {
67+
self.items
68+
.iter()
69+
.map(|id| id.def_id)
70+
.chain(self.trait_items.iter().map(|id| id.def_id))
71+
.chain(self.impl_items.iter().map(|id| id.def_id))
72+
.chain(self.foreign_items.iter().map(|id| id.def_id))
73+
}
74+
6675
pub fn par_items(&self, f: impl Fn(ItemId) + Send + Sync) {
6776
par_for_each_in(&self.items[..], |&id| f(id))
6877
}

Diff for: compiler/rustc_typeck/src/variance/constraints.rs

+14-62
Original file line numberDiff line numberDiff line change
@@ -64,74 +64,28 @@ pub fn add_constraints_from_crate<'a, 'tcx>(
6464

6565
let crate_items = tcx.hir_crate_items(());
6666

67-
for id in crate_items.items() {
68-
constraint_cx.check_item(id);
69-
}
70-
71-
for id in crate_items.trait_items() {
72-
if let DefKind::AssocFn = tcx.def_kind(id.def_id) {
73-
constraint_cx.check_node_helper(id.hir_id());
74-
}
75-
}
76-
77-
for id in crate_items.impl_items() {
78-
if let DefKind::AssocFn = tcx.def_kind(id.def_id) {
79-
constraint_cx.check_node_helper(id.hir_id());
80-
}
81-
}
82-
83-
for id in crate_items.foreign_items() {
84-
if let DefKind::Fn = tcx.def_kind(id.def_id) {
85-
constraint_cx.check_node_helper(id.hir_id());
86-
}
87-
}
88-
89-
constraint_cx
90-
}
91-
92-
impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
93-
fn check_item(&mut self, id: hir::ItemId) {
94-
let def_kind = self.tcx().def_kind(id.def_id);
67+
for def_id in crate_items.definitions() {
68+
let def_kind = tcx.def_kind(def_id);
9569
match def_kind {
96-
DefKind::Struct | DefKind::Union => {
97-
let item = self.tcx().hir().item(id);
98-
99-
if let hir::ItemKind::Struct(ref struct_def, _)
100-
| hir::ItemKind::Union(ref struct_def, _) = item.kind
101-
{
102-
self.check_node_helper(item.hir_id());
70+
DefKind::Struct | DefKind::Union | DefKind::Enum => {
71+
constraint_cx.build_constraints_for_item(def_id);
10372

104-
if let hir::VariantData::Tuple(..) = *struct_def {
105-
self.check_node_helper(struct_def.ctor_hir_id().unwrap());
73+
let adt = tcx.adt_def(def_id);
74+
for variant in adt.variants() {
75+
if let Some(ctor) = variant.ctor_def_id {
76+
constraint_cx.build_constraints_for_item(ctor.expect_local());
10677
}
10778
}
10879
}
109-
DefKind::Enum => {
110-
let item = self.tcx().hir().item(id);
111-
112-
if let hir::ItemKind::Enum(ref enum_def, _) = item.kind {
113-
self.check_node_helper(item.hir_id());
114-
115-
for variant in enum_def.variants {
116-
if let hir::VariantData::Tuple(..) = variant.data {
117-
self.check_node_helper(variant.data.ctor_hir_id().unwrap());
118-
}
119-
}
120-
}
121-
}
122-
DefKind::Fn => {
123-
self.check_node_helper(id.hir_id());
124-
}
80+
DefKind::Fn | DefKind::AssocFn => constraint_cx.build_constraints_for_item(def_id),
12581
_ => {}
12682
}
12783
}
12884

129-
fn check_node_helper(&mut self, id: hir::HirId) {
130-
let tcx = self.terms_cx.tcx;
131-
let def_id = tcx.hir().local_def_id(id);
132-
self.build_constraints_for_item(def_id);
133-
}
85+
constraint_cx
86+
}
13487

88+
impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
13589
fn tcx(&self) -> TyCtxt<'tcx> {
13690
self.terms_cx.tcx
13791
}
@@ -145,8 +99,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
14599
return;
146100
}
147101

148-
let id = tcx.hir().local_def_id_to_hir_id(def_id);
149-
let inferred_start = self.terms_cx.inferred_starts[&id];
102+
let inferred_start = self.terms_cx.inferred_starts[&def_id];
150103
let current_item = &CurrentItem { inferred_start };
151104
match tcx.type_of(def_id).kind() {
152105
ty::Adt(def, _) => {
@@ -372,8 +325,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
372325
}
373326

374327
let (local, remote) = if let Some(def_id) = def_id.as_local() {
375-
let id = self.tcx().hir().local_def_id_to_hir_id(def_id);
376-
(Some(self.terms_cx.inferred_starts[&id]), None)
328+
(Some(self.terms_cx.inferred_starts[&def_id]), None)
377329
} else {
378330
(None, Some(self.tcx().variances_of(def_id)))
379331
};

Diff for: compiler/rustc_typeck/src/variance/solve.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
9696
self.terms_cx
9797
.inferred_starts
9898
.iter()
99-
.map(|(&id, &InferredIndex(start))| {
100-
let def_id = tcx.hir().local_def_id(id);
99+
.map(|(&def_id, &InferredIndex(start))| {
101100
let generics = tcx.generics_of(def_id);
102101
let count = generics.count();
103102

Diff for: compiler/rustc_typeck/src/variance/terms.rs

+24-66
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
// a variable.
1111

1212
use rustc_arena::DroplessArena;
13-
use rustc_hir as hir;
1413
use rustc_hir::def::DefKind;
15-
use rustc_hir::HirIdMap;
14+
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap};
1615
use rustc_middle::ty::{self, TyCtxt};
1716
use std::fmt;
1817

@@ -52,11 +51,11 @@ pub struct TermsContext<'a, 'tcx> {
5251
// For marker types, UnsafeCell, and other lang items where
5352
// variance is hardcoded, records the item-id and the hardcoded
5453
// variance.
55-
pub lang_items: Vec<(hir::HirId, Vec<ty::Variance>)>,
54+
pub lang_items: Vec<(LocalDefId, Vec<ty::Variance>)>,
5655

5756
// Maps from the node id of an item to the first inferred index
5857
// used for its type & region parameters.
59-
pub inferred_starts: HirIdMap<InferredIndex>,
58+
pub inferred_starts: LocalDefIdMap<InferredIndex>,
6059

6160
// Maps from an InferredIndex to the term for that variable.
6261
pub inferred_terms: Vec<VarianceTermPtr<'a>>,
@@ -81,51 +80,48 @@ pub fn determine_parameters_to_be_inferred<'a, 'tcx>(
8180
// - https://rustc-dev-guide.rust-lang.org/variance.html
8281
let crate_items = tcx.hir_crate_items(());
8382

84-
for id in crate_items.items() {
85-
terms_cx.check_item(id);
86-
}
83+
for def_id in crate_items.definitions() {
84+
debug!("add_inferreds for item {:?}", def_id);
8785

88-
for id in crate_items.trait_items() {
89-
if let DefKind::AssocFn = tcx.def_kind(id.def_id) {
90-
terms_cx.add_inferreds_for_item(id.hir_id());
91-
}
92-
}
86+
let def_kind = tcx.def_kind(def_id);
9387

94-
for id in crate_items.impl_items() {
95-
if let DefKind::AssocFn = tcx.def_kind(id.def_id) {
96-
terms_cx.add_inferreds_for_item(id.hir_id());
97-
}
98-
}
88+
match def_kind {
89+
DefKind::Struct | DefKind::Union | DefKind::Enum => {
90+
terms_cx.add_inferreds_for_item(def_id);
9991

100-
for id in crate_items.foreign_items() {
101-
if let DefKind::Fn = tcx.def_kind(id.def_id) {
102-
terms_cx.add_inferreds_for_item(id.hir_id());
92+
let adt = tcx.adt_def(def_id);
93+
for variant in adt.variants() {
94+
if let Some(ctor) = variant.ctor_def_id {
95+
terms_cx.add_inferreds_for_item(ctor.expect_local());
96+
}
97+
}
98+
}
99+
DefKind::Fn | DefKind::AssocFn => terms_cx.add_inferreds_for_item(def_id),
100+
_ => {}
103101
}
104102
}
105103

106104
terms_cx
107105
}
108106

109-
fn lang_items(tcx: TyCtxt<'_>) -> Vec<(hir::HirId, Vec<ty::Variance>)> {
107+
fn lang_items(tcx: TyCtxt<'_>) -> Vec<(LocalDefId, Vec<ty::Variance>)> {
110108
let lang_items = tcx.lang_items();
111109
let all = [
112110
(lang_items.phantom_data(), vec![ty::Covariant]),
113111
(lang_items.unsafe_cell_type(), vec![ty::Invariant]),
114112
];
115113

116114
all.into_iter() // iterating over (Option<DefId>, Variance)
117-
.filter(|&(ref d, _)| d.is_some())
118-
.map(|(d, v)| (d.unwrap(), v)) // (DefId, Variance)
119115
.filter_map(|(d, v)| {
120-
d.as_local().map(|d| tcx.hir().local_def_id_to_hir_id(d)).map(|n| (n, v))
121-
}) // (HirId, Variance)
116+
let def_id = d?.as_local()?; // LocalDefId
117+
Some((def_id, v))
118+
})
122119
.collect()
123120
}
124121

125122
impl<'a, 'tcx> TermsContext<'a, 'tcx> {
126-
fn add_inferreds_for_item(&mut self, id: hir::HirId) {
123+
fn add_inferreds_for_item(&mut self, def_id: LocalDefId) {
127124
let tcx = self.tcx;
128-
let def_id = tcx.hir().local_def_id(id);
129125
let count = tcx.generics_of(def_id).count();
130126

131127
if count == 0 {
@@ -134,7 +130,7 @@ impl<'a, 'tcx> TermsContext<'a, 'tcx> {
134130

135131
// Record the start of this item's inferreds.
136132
let start = self.inferred_terms.len();
137-
let newly_added = self.inferred_starts.insert(id, InferredIndex(start)).is_none();
133+
let newly_added = self.inferred_starts.insert(def_id, InferredIndex(start)).is_none();
138134
assert!(newly_added);
139135

140136
// N.B., in the code below for writing the results back into the
@@ -146,42 +142,4 @@ impl<'a, 'tcx> TermsContext<'a, 'tcx> {
146142
(start..(start + count)).map(|i| &*arena.alloc(InferredTerm(InferredIndex(i)))),
147143
);
148144
}
149-
150-
fn check_item(&mut self, id: hir::ItemId) {
151-
debug!("add_inferreds for item {}", self.tcx.hir().node_to_string(id.hir_id()));
152-
153-
let def_kind = self.tcx.def_kind(id.def_id);
154-
match def_kind {
155-
DefKind::Struct | DefKind::Union => {
156-
let item = self.tcx.hir().item(id);
157-
158-
if let hir::ItemKind::Struct(ref struct_def, _)
159-
| hir::ItemKind::Union(ref struct_def, _) = item.kind
160-
{
161-
self.add_inferreds_for_item(item.hir_id());
162-
163-
if let hir::VariantData::Tuple(..) = *struct_def {
164-
self.add_inferreds_for_item(struct_def.ctor_hir_id().unwrap());
165-
}
166-
}
167-
}
168-
DefKind::Enum => {
169-
let item = self.tcx.hir().item(id);
170-
171-
if let hir::ItemKind::Enum(ref enum_def, _) = item.kind {
172-
self.add_inferreds_for_item(item.hir_id());
173-
174-
for variant in enum_def.variants {
175-
if let hir::VariantData::Tuple(..) = variant.data {
176-
self.add_inferreds_for_item(variant.data.ctor_hir_id().unwrap());
177-
}
178-
}
179-
}
180-
}
181-
DefKind::Fn => {
182-
self.add_inferreds_for_item(id.hir_id());
183-
}
184-
_ => {}
185-
}
186-
}
187145
}

0 commit comments

Comments
 (0)