Skip to content

Commit c0bcbe8

Browse files
authored
Rollup merge of rust-lang#99038 - jackh726:earlybinder-cleanup, r=lcnr
Some more `EarlyBinder` cleanups First commit has a couple unrelated cleanups, but otherwise each commit is self-explanatory r? rust-lang/types
2 parents 99fc65b + 988e754 commit c0bcbe8

File tree

12 files changed

+57
-45
lines changed

12 files changed

+57
-45
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ impl GenericParamDef {
8585
) -> Option<EarlyBinder<ty::GenericArg<'tcx>>> {
8686
match self.kind {
8787
GenericParamDefKind::Type { has_default, .. } if has_default => {
88-
Some(EarlyBinder(tcx.type_of(self.def_id).into()))
88+
Some(tcx.bound_type_of(self.def_id).map_bound(|t| t.into()))
8989
}
9090
GenericParamDefKind::Const { has_default } if has_default => {
91-
Some(EarlyBinder(tcx.const_param_default(self.def_id).into()))
91+
Some(tcx.bound_const_param_default(self.def_id).map_bound(|c| c.into()))
9292
}
9393
_ => None,
9494
}

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_middle/src/ty/util.rs

+4
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,10 @@ impl<'tcx> TyCtxt<'tcx> {
676676
) -> ty::EarlyBinder<&'tcx ty::List<ty::Predicate<'tcx>>> {
677677
ty::EarlyBinder(self.item_bounds(def_id))
678678
}
679+
680+
pub fn bound_const_param_default(self, def_id: DefId) -> ty::EarlyBinder<ty::Const<'tcx>> {
681+
ty::EarlyBinder(self.const_param_default(def_id))
682+
}
679683
}
680684

681685
struct OpaqueTypeExpander<'tcx> {

Diff for: compiler/rustc_mir_transform/src/shim.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -537,13 +537,12 @@ fn build_call_shim<'tcx>(
537537
};
538538

539539
let def_id = instance.def_id();
540-
let sig = tcx.fn_sig(def_id);
541-
let mut sig = tcx.erase_late_bound_regions(sig);
540+
let sig = tcx.bound_fn_sig(def_id);
541+
let sig = sig.map_bound(|sig| tcx.erase_late_bound_regions(sig));
542542

543543
assert_eq!(sig_substs.is_some(), !instance.has_polymorphic_mir_body());
544-
if let Some(sig_substs) = sig_substs {
545-
sig = EarlyBinder(sig).subst(tcx, sig_substs);
546-
}
544+
let mut sig =
545+
if let Some(sig_substs) = sig_substs { sig.subst(tcx, sig_substs) } else { sig.0 };
547546

548547
if let CallKind::Indirect(fnty) = call_kind {
549548
// `sig` determines our local decls, and thus the callee type in the `Call` terminator. This

Diff for: compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_index::bit_set::GrowableBitSet;
1212
use rustc_infer::infer::InferOk;
1313
use rustc_infer::infer::LateBoundRegionConversionTime::HigherRankedType;
1414
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef};
15-
use rustc_middle::ty::{self, EarlyBinder, GenericParamDefKind, Ty, TyCtxt};
15+
use rustc_middle::ty::{self, GenericParamDefKind, Ty, TyCtxt};
1616
use rustc_middle::ty::{ToPolyTraitRef, ToPredicate};
1717
use rustc_span::def_id::DefId;
1818

@@ -555,7 +555,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
555555

556556
let bound_vars = tcx.mk_bound_variable_kinds(bound_vars.into_iter());
557557
let bound =
558-
EarlyBinder(bound.0.kind().skip_binder()).subst(tcx, assoc_ty_substs);
558+
bound.map_bound(|b| b.kind().skip_binder()).subst(tcx, assoc_ty_substs);
559559
tcx.mk_predicate(ty::Binder::bind_with_vars(bound, bound_vars))
560560
};
561561
let normalized_bound = normalize_with_depth_to(

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
550550
GenericParamDefKind::Const { has_default } => {
551551
let ty = tcx.at(self.span).type_of(param.def_id);
552552
if !infer_args && has_default {
553-
EarlyBinder(tcx.const_param_default(param.def_id))
553+
tcx.bound_const_param_default(param.def_id)
554554
.subst(tcx, substs.unwrap())
555555
.into()
556556
} else {

Diff for: compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14261426
}
14271427
GenericParamDefKind::Const { has_default } => {
14281428
if !infer_args && has_default {
1429-
EarlyBinder(tcx.const_param_default(param.def_id))
1429+
tcx.bound_const_param_default(param.def_id)
14301430
.subst(tcx, substs.unwrap())
14311431
.into()
14321432
} else {

Diff for: compiler/rustc_typeck/src/check/method/probe.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ use rustc_middle::middle::stability;
2121
use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
2222
use rustc_middle::ty::subst::{InternalSubsts, Subst, SubstsRef};
2323
use rustc_middle::ty::GenericParamDefKind;
24-
use rustc_middle::ty::{
25-
self, EarlyBinder, ParamEnvAnd, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeVisitable,
26-
};
24+
use rustc_middle::ty::{self, ParamEnvAnd, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeVisitable};
2725
use rustc_session::lint;
2826
use rustc_span::def_id::LocalDefId;
2927
use rustc_span::lev_distance::{
@@ -713,7 +711,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
713711
}
714712

715713
let (impl_ty, impl_substs) = self.impl_ty_and_substs(impl_def_id);
716-
let impl_ty = EarlyBinder(impl_ty).subst(self.tcx, impl_substs);
714+
let impl_ty = impl_ty.subst(self.tcx, impl_substs);
717715

718716
debug!("impl_ty: {:?}", impl_ty);
719717

@@ -1811,9 +1809,12 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
18111809
self.erase_late_bound_regions(xform_fn_sig)
18121810
}
18131811

1814-
/// Gets the type of an impl and generate substitutions with placeholders.
1815-
fn impl_ty_and_substs(&self, impl_def_id: DefId) -> (Ty<'tcx>, SubstsRef<'tcx>) {
1816-
(self.tcx.type_of(impl_def_id), self.fresh_item_substs(impl_def_id))
1812+
/// Gets the type of an impl and generate substitutions with inference vars.
1813+
fn impl_ty_and_substs(
1814+
&self,
1815+
impl_def_id: DefId,
1816+
) -> (ty::EarlyBinder<Ty<'tcx>>, SubstsRef<'tcx>) {
1817+
(self.tcx.bound_type_of(impl_def_id), self.fresh_item_substs(impl_def_id))
18171818
}
18181819

18191820
fn fresh_item_substs(&self, def_id: DefId) -> SubstsRef<'tcx> {

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)