Skip to content

Commit a9ff135

Browse files
committed
Auto merge of #57033 - nikic:inhabitedness-perf, r=varkor
Remove "visited" set from inhabitedness checking (fix perf regression) Now that references are no longer recursively checked, this should no longer be necessary, and it's a major performance bottleneck. This should fix #57028. r? @varkor
2 parents e40548b + a8babed commit a9ff135

File tree

1 file changed

+10
-44
lines changed
  • src/librustc/ty/inhabitedness

1 file changed

+10
-44
lines changed

src/librustc/ty/inhabitedness/mod.rs

+10-44
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use util::nodemap::{FxHashMap, FxHashSet};
1211
use ty::context::TyCtxt;
1312
use ty::{AdtDef, VariantDef, FieldDef, Ty, TyS};
1413
use ty::{DefId, Substs};
@@ -113,7 +112,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
113112
}
114113

115114
fn ty_inhabitedness_forest(self, ty: Ty<'tcx>) -> DefIdForest {
116-
ty.uninhabited_from(&mut FxHashMap::default(), self)
115+
ty.uninhabited_from(self)
117116
}
118117

119118
pub fn is_enum_variant_uninhabited_from(self,
@@ -140,20 +139,19 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
140139
let adt_kind = self.adt_def(adt_def_id).adt_kind();
141140

142141
// Compute inhabitedness forest:
143-
variant.uninhabited_from(&mut FxHashMap::default(), self, substs, adt_kind)
142+
variant.uninhabited_from(self, substs, adt_kind)
144143
}
145144
}
146145

147146
impl<'a, 'gcx, 'tcx> AdtDef {
148147
/// Calculate the forest of DefIds from which this adt is visibly uninhabited.
149148
fn uninhabited_from(
150149
&self,
151-
visited: &mut FxHashMap<DefId, FxHashSet<&'tcx Substs<'tcx>>>,
152150
tcx: TyCtxt<'a, 'gcx, 'tcx>,
153151
substs: &'tcx Substs<'tcx>) -> DefIdForest
154152
{
155153
DefIdForest::intersection(tcx, self.variants.iter().map(|v| {
156-
v.uninhabited_from(visited, tcx, substs, self.adt_kind())
154+
v.uninhabited_from(tcx, substs, self.adt_kind())
157155
}))
158156
}
159157
}
@@ -162,7 +160,6 @@ impl<'a, 'gcx, 'tcx> VariantDef {
162160
/// Calculate the forest of DefIds from which this variant is visibly uninhabited.
163161
fn uninhabited_from(
164162
&self,
165-
visited: &mut FxHashMap<DefId, FxHashSet<&'tcx Substs<'tcx>>>,
166163
tcx: TyCtxt<'a, 'gcx, 'tcx>,
167164
substs: &'tcx Substs<'tcx>,
168165
adt_kind: AdtKind) -> DefIdForest
@@ -175,7 +172,7 @@ impl<'a, 'gcx, 'tcx> VariantDef {
175172
AdtKind::Struct => false,
176173
};
177174
DefIdForest::union(tcx, self.fields.iter().map(|f| {
178-
f.uninhabited_from(visited, tcx, substs, is_enum)
175+
f.uninhabited_from(tcx, substs, is_enum)
179176
}))
180177
}
181178
}
@@ -184,13 +181,12 @@ impl<'a, 'gcx, 'tcx> FieldDef {
184181
/// Calculate the forest of DefIds from which this field is visibly uninhabited.
185182
fn uninhabited_from(
186183
&self,
187-
visited: &mut FxHashMap<DefId, FxHashSet<&'tcx Substs<'tcx>>>,
188184
tcx: TyCtxt<'a, 'gcx, 'tcx>,
189185
substs: &'tcx Substs<'tcx>,
190186
is_enum: bool,
191187
) -> DefIdForest {
192-
let mut data_uninhabitedness = move || {
193-
self.ty(tcx, substs).uninhabited_from(visited, tcx)
188+
let data_uninhabitedness = move || {
189+
self.ty(tcx, substs).uninhabited_from(tcx)
194190
};
195191
// FIXME(canndrew): Currently enum fields are (incorrectly) stored with
196192
// Visibility::Invisible so we need to override self.vis if we're
@@ -213,54 +209,24 @@ impl<'a, 'gcx, 'tcx> FieldDef {
213209

214210
impl<'a, 'gcx, 'tcx> TyS<'tcx> {
215211
/// Calculate the forest of DefIds from which this type is visibly uninhabited.
216-
fn uninhabited_from(
217-
&self,
218-
visited: &mut FxHashMap<DefId, FxHashSet<&'tcx Substs<'tcx>>>,
219-
tcx: TyCtxt<'a, 'gcx, 'tcx>) -> DefIdForest
212+
fn uninhabited_from(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> DefIdForest
220213
{
221214
match self.sty {
222-
Adt(def, substs) => {
223-
{
224-
let substs_set = visited.entry(def.did).or_default();
225-
if !substs_set.insert(substs) {
226-
// We are already calculating the inhabitedness of this type.
227-
// The type must contain a reference to itself. Break the
228-
// infinite loop.
229-
return DefIdForest::empty();
230-
}
231-
if substs_set.len() >= tcx.sess.recursion_limit.get() / 4 {
232-
// We have gone very deep, reinstantiating this ADT inside
233-
// itself with different type arguments. We are probably
234-
// hitting an infinite loop. For example, it's possible to write:
235-
// a type Foo<T>
236-
// which contains a Foo<(T, T)>
237-
// which contains a Foo<((T, T), (T, T))>
238-
// which contains a Foo<(((T, T), (T, T)), ((T, T), (T, T)))>
239-
// etc.
240-
let error = format!("reached recursion limit while checking \
241-
inhabitedness of `{}`", self);
242-
tcx.sess.fatal(&error);
243-
}
244-
}
245-
let ret = def.uninhabited_from(visited, tcx, substs);
246-
let substs_set = visited.get_mut(&def.did).unwrap();
247-
substs_set.remove(substs);
248-
ret
249-
}
215+
Adt(def, substs) => def.uninhabited_from(tcx, substs),
250216

251217
Never => DefIdForest::full(tcx),
252218

253219
Tuple(ref tys) => {
254220
DefIdForest::union(tcx, tys.iter().map(|ty| {
255-
ty.uninhabited_from(visited, tcx)
221+
ty.uninhabited_from(tcx)
256222
}))
257223
}
258224

259225
Array(ty, len) => {
260226
match len.assert_usize(tcx) {
261227
// If the array is definitely non-empty, it's uninhabited if
262228
// the type of its elements is uninhabited.
263-
Some(n) if n != 0 => ty.uninhabited_from(visited, tcx),
229+
Some(n) if n != 0 => ty.uninhabited_from(tcx),
264230
_ => DefIdForest::empty()
265231
}
266232
}

0 commit comments

Comments
 (0)