Skip to content

Commit f165843

Browse files
committed
Move EarlyBinder calls in rustc_typeck::outlives a bit further up
1 parent 1517f5d commit f165843

File tree

5 files changed

+35
-27
lines changed

5 files changed

+35
-27
lines changed

Diff for: compiler/rustc_middle/src/ty/sty.rs

+4
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,10 @@ impl<T> EarlyBinder<T> {
932932
let value = f(self.0)?;
933933
Ok(EarlyBinder(value))
934934
}
935+
936+
pub fn rebind<U>(&self, value: U) -> EarlyBinder<U> {
937+
EarlyBinder(value)
938+
}
935939
}
936940

937941
impl<T> EarlyBinder<Option<T>> {

Diff for: compiler/rustc_typeck/src/outlives/explicit.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ use super::utils::*;
66

77
#[derive(Debug)]
88
pub struct ExplicitPredicatesMap<'tcx> {
9-
map: FxHashMap<DefId, RequiredPredicates<'tcx>>,
9+
map: FxHashMap<DefId, ty::EarlyBinder<RequiredPredicates<'tcx>>>,
1010
}
1111

1212
impl<'tcx> ExplicitPredicatesMap<'tcx> {
1313
pub fn new() -> ExplicitPredicatesMap<'tcx> {
1414
ExplicitPredicatesMap { map: FxHashMap::default() }
1515
}
1616

17-
pub fn explicit_predicates_of(
17+
pub(crate) fn explicit_predicates_of(
1818
&mut self,
1919
tcx: TyCtxt<'tcx>,
2020
def_id: DefId,
21-
) -> &RequiredPredicates<'tcx> {
21+
) -> &ty::EarlyBinder<RequiredPredicates<'tcx>> {
2222
self.map.entry(def_id).or_insert_with(|| {
2323
let predicates = if def_id.is_local() {
2424
tcx.explicit_predicates_of(def_id)
@@ -63,7 +63,7 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> {
6363
}
6464
}
6565

66-
required_predicates
66+
ty::EarlyBinder(required_predicates)
6767
})
6868
}
6969
}

Diff for: compiler/rustc_typeck/src/outlives/implicit_infer.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_data_structures::fx::FxHashMap;
22
use rustc_hir::def::DefKind;
33
use rustc_hir::def_id::DefId;
44
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst};
5-
use rustc_middle::ty::{self, EarlyBinder, Ty, TyCtxt};
5+
use rustc_middle::ty::{self, Ty, TyCtxt};
66
use rustc_span::Span;
77

88
use super::explicit::ExplicitPredicatesMap;
@@ -13,20 +13,19 @@ use super::utils::*;
1313
/// `global_inferred_outlives`: this is initially the empty map that
1414
/// was generated by walking the items in the crate. This will
1515
/// now be filled with inferred predicates.
16-
pub fn infer_predicates<'tcx>(
16+
pub(super) fn infer_predicates<'tcx>(
1717
tcx: TyCtxt<'tcx>,
18-
explicit_map: &mut ExplicitPredicatesMap<'tcx>,
19-
) -> FxHashMap<DefId, RequiredPredicates<'tcx>> {
18+
) -> FxHashMap<DefId, ty::EarlyBinder<RequiredPredicates<'tcx>>> {
2019
debug!("infer_predicates");
2120

22-
let mut predicates_added = true;
21+
let mut explicit_map = ExplicitPredicatesMap::new();
2322

2423
let mut global_inferred_outlives = FxHashMap::default();
2524

2625
// If new predicates were added then we need to re-calculate
2726
// all crates since there could be new implied predicates.
28-
while predicates_added {
29-
predicates_added = false;
27+
'outer: loop {
28+
let mut predicates_added = false;
3029

3130
// Visit all the crates and infer predicates
3231
for id in tcx.hir().items() {
@@ -53,9 +52,9 @@ pub fn infer_predicates<'tcx>(
5352
tcx,
5453
field_ty,
5554
field_span,
56-
&mut global_inferred_outlives,
55+
&global_inferred_outlives,
5756
&mut item_required_predicates,
58-
explicit_map,
57+
&mut explicit_map,
5958
);
6059
}
6160
}
@@ -70,12 +69,17 @@ pub fn infer_predicates<'tcx>(
7069
// we walk the crates again and re-calculate predicates for all
7170
// items.
7271
let item_predicates_len: usize =
73-
global_inferred_outlives.get(&item_did.to_def_id()).map_or(0, |p| p.len());
72+
global_inferred_outlives.get(&item_did.to_def_id()).map_or(0, |p| p.0.len());
7473
if item_required_predicates.len() > item_predicates_len {
7574
predicates_added = true;
76-
global_inferred_outlives.insert(item_did.to_def_id(), item_required_predicates);
75+
global_inferred_outlives
76+
.insert(item_did.to_def_id(), ty::EarlyBinder(item_required_predicates));
7777
}
7878
}
79+
80+
if !predicates_added {
81+
break 'outer;
82+
}
7983
}
8084

8185
global_inferred_outlives
@@ -85,7 +89,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
8589
tcx: TyCtxt<'tcx>,
8690
field_ty: Ty<'tcx>,
8791
field_span: Span,
88-
global_inferred_outlives: &FxHashMap<DefId, RequiredPredicates<'tcx>>,
92+
global_inferred_outlives: &FxHashMap<DefId, ty::EarlyBinder<RequiredPredicates<'tcx>>>,
8993
required_predicates: &mut RequiredPredicates<'tcx>,
9094
explicit_map: &mut ExplicitPredicatesMap<'tcx>,
9195
) {
@@ -133,11 +137,13 @@ fn insert_required_predicates_to_be_wf<'tcx>(
133137
// 'a` holds for `Foo`.
134138
debug!("Adt");
135139
if let Some(unsubstituted_predicates) = global_inferred_outlives.get(&def.did()) {
136-
for (unsubstituted_predicate, &span) in unsubstituted_predicates {
140+
for (unsubstituted_predicate, &span) in &unsubstituted_predicates.0 {
137141
// `unsubstituted_predicate` is `U: 'b` in the
138142
// example above. So apply the substitution to
139143
// get `T: 'a` (or `predicate`):
140-
let predicate = EarlyBinder(*unsubstituted_predicate).subst(tcx, substs);
144+
let predicate = unsubstituted_predicates
145+
.rebind(*unsubstituted_predicate)
146+
.subst(tcx, substs);
141147
insert_outlives_predicate(
142148
tcx,
143149
predicate.0,
@@ -224,7 +230,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
224230
/// will give us `U: 'static` and `U: Foo`. The latter we
225231
/// can ignore, but we will want to process `U: 'static`,
226232
/// applying the substitution as above.
227-
pub fn check_explicit_predicates<'tcx>(
233+
fn check_explicit_predicates<'tcx>(
228234
tcx: TyCtxt<'tcx>,
229235
def_id: DefId,
230236
substs: &[GenericArg<'tcx>],
@@ -242,7 +248,7 @@ pub fn check_explicit_predicates<'tcx>(
242248
);
243249
let explicit_predicates = explicit_map.explicit_predicates_of(tcx, def_id);
244250

245-
for (outlives_predicate, &span) in explicit_predicates {
251+
for (outlives_predicate, &span) in &explicit_predicates.0 {
246252
debug!("outlives_predicate = {:?}", &outlives_predicate);
247253

248254
// Careful: If we are inferring the effects of a `dyn Trait<..>`
@@ -287,7 +293,7 @@ pub fn check_explicit_predicates<'tcx>(
287293
continue;
288294
}
289295

290-
let predicate = EarlyBinder(*outlives_predicate).subst(tcx, substs);
296+
let predicate = explicit_predicates.rebind(*outlives_predicate).subst(tcx, substs);
291297
debug!("predicate = {:?}", &predicate);
292298
insert_outlives_predicate(tcx, predicate.0, predicate.1, span, required_predicates);
293299
}

Diff for: compiler/rustc_typeck/src/outlives/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, (): ()) -> CratePredicatesMap<'_> {
8888
// for the type.
8989

9090
// Compute the inferred predicates
91-
let mut exp_map = explicit::ExplicitPredicatesMap::new();
92-
93-
let global_inferred_outlives = implicit_infer::infer_predicates(tcx, &mut exp_map);
91+
let global_inferred_outlives = implicit_infer::infer_predicates(tcx);
9492

9593
// Convert the inferred predicates into the "collected" form the
9694
// global data structure expects.
@@ -100,7 +98,7 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, (): ()) -> CratePredicatesMap<'_> {
10098
let predicates = global_inferred_outlives
10199
.iter()
102100
.map(|(&def_id, set)| {
103-
let predicates = &*tcx.arena.alloc_from_iter(set.iter().filter_map(
101+
let predicates = &*tcx.arena.alloc_from_iter(set.0.iter().filter_map(
104102
|(ty::OutlivesPredicate(kind1, region2), &span)| {
105103
match kind1.unpack() {
106104
GenericArgKind::Type(ty1) => Some((

Diff for: compiler/rustc_typeck/src/outlives/utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use std::collections::BTreeMap;
77

88
/// Tracks the `T: 'a` or `'a: 'a` predicates that we have inferred
99
/// must be added to the struct header.
10-
pub type RequiredPredicates<'tcx> =
10+
pub(crate) type RequiredPredicates<'tcx> =
1111
BTreeMap<ty::OutlivesPredicate<GenericArg<'tcx>, ty::Region<'tcx>>, Span>;
1212

1313
/// Given a requirement `T: 'a` or `'b: 'a`, deduce the
1414
/// outlives_component and add it to `required_predicates`
15-
pub fn insert_outlives_predicate<'tcx>(
15+
pub(crate) fn insert_outlives_predicate<'tcx>(
1616
tcx: TyCtxt<'tcx>,
1717
kind: GenericArg<'tcx>,
1818
outlived_region: Region<'tcx>,

0 commit comments

Comments
 (0)