Skip to content

Commit eba1027

Browse files
committed
map_bound_ref -> rebind
1 parent 11d62aa commit eba1027

File tree

21 files changed

+77
-78
lines changed

21 files changed

+77
-78
lines changed

compiler/rustc_infer/src/infer/combine.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
551551
where
552552
T: Relate<'tcx>,
553553
{
554-
let result = self.relate(a.skip_binder(), b.skip_binder())?;
555-
Ok(a.map_bound(|_| result))
554+
Ok(ty::Binder::bind(self.relate(a.skip_binder(), b.skip_binder())?))
556555
}
557556

558557
fn relate_item_substs(
@@ -834,8 +833,7 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
834833
where
835834
T: Relate<'tcx>,
836835
{
837-
let result = self.relate(a.skip_binder(), b.skip_binder())?;
838-
Ok(a.map_bound(|_| result))
836+
Ok(ty::Binder::bind(self.relate(a.skip_binder(), b.skip_binder())?))
839837
}
840838

841839
fn tys(&mut self, t: Ty<'tcx>, _t: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {

compiler/rustc_infer/src/infer/nll_relate/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,6 @@ where
10041004
self.first_free_index.shift_in(1);
10051005
let result = self.relate(a.skip_binder(), a.skip_binder())?;
10061006
self.first_free_index.shift_out(1);
1007-
Ok(a.map_bound(|_| result))
1007+
Ok(ty::Binder::bind(result))
10081008
}
10091009
}

compiler/rustc_infer/src/traits/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl Elaborator<'tcx> {
134134

135135
let obligations = predicates.predicates.iter().map(|&(pred, _)| {
136136
predicate_obligation(
137-
pred.subst_supertrait(tcx, &bound_predicate.map_bound(|_| data.trait_ref)),
137+
pred.subst_supertrait(tcx, &bound_predicate.rebind(data.trait_ref)),
138138
obligation.param_env,
139139
obligation.cause.clone(),
140140
)

compiler/rustc_middle/src/ty/_match.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ impl TypeRelation<'tcx> for Match<'tcx> {
118118
where
119119
T: Relate<'tcx>,
120120
{
121-
let result = self.relate(a.skip_binder(), b.skip_binder())?;
122-
Ok(a.map_bound(|_| result))
121+
Ok(ty::Binder::bind(self.relate(a.skip_binder(), b.skip_binder())?))
123122
}
124123
}

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ pub trait PrettyPrinter<'tcx>:
620620
// FIXME(lcnr): Find out why exactly this is the case :)
621621
let bound_predicate = predicate.bound_atom_with_opt_escaping(self.tcx());
622622
if let ty::PredicateAtom::Trait(pred, _) = bound_predicate.skip_binder() {
623-
let trait_ref = bound_predicate.map_bound(|_| pred.trait_ref);
623+
let trait_ref = bound_predicate.rebind(pred.trait_ref);
624624
// Don't print +Sized, but rather +?Sized if absent.
625625
if Some(trait_ref.def_id()) == self.tcx().lang_items().sized_trait() {
626626
is_sized = true;

compiler/rustc_middle/src/ty/structural_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::PredicateAtom<'a> {
549549
impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
550550
type Lifted = ty::Binder<T::Lifted>;
551551
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
552-
tcx.lift(self.as_ref().skip_binder()).map(|v| self.map_bound_ref(|_| v))
552+
tcx.lift(self.as_ref().skip_binder()).map(|v| self.rebind(v))
553553
}
554554
}
555555

compiler/rustc_middle/src/ty/sty.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -702,16 +702,14 @@ impl<'tcx> Binder<ExistentialPredicate<'tcx>> {
702702
pub fn with_self_ty(&self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> ty::Predicate<'tcx> {
703703
use crate::ty::ToPredicate;
704704
match self.skip_binder() {
705-
ExistentialPredicate::Trait(tr) => self
706-
.map_bound_ref(|_| tr)
707-
.with_self_ty(tcx, self_ty)
708-
.without_const()
709-
.to_predicate(tcx),
705+
ExistentialPredicate::Trait(tr) => {
706+
self.rebind(tr).with_self_ty(tcx, self_ty).without_const().to_predicate(tcx)
707+
}
710708
ExistentialPredicate::Projection(p) => {
711-
self.map_bound_ref(|_| p.with_self_ty(tcx, self_ty)).to_predicate(tcx)
709+
self.rebind(p.with_self_ty(tcx, self_ty)).to_predicate(tcx)
712710
}
713711
ExistentialPredicate::AutoTrait(did) => {
714-
let trait_ref = self.map_bound_ref(|_| ty::TraitRef {
712+
let trait_ref = self.rebind(ty::TraitRef {
715713
def_id: did,
716714
substs: tcx.mk_substs_trait(self_ty, &[]),
717715
});
@@ -779,7 +777,7 @@ impl<'tcx> List<ExistentialPredicate<'tcx>> {
779777

780778
impl<'tcx> Binder<&'tcx List<ExistentialPredicate<'tcx>>> {
781779
pub fn principal(&self) -> Option<ty::Binder<ExistentialTraitRef<'tcx>>> {
782-
self.map_bound_ref(|b| b.principal()).transpose()
780+
self.map_bound(|b| b.principal()).transpose()
783781
}
784782

785783
pub fn principal_def_id(&self) -> Option<DefId> {
@@ -1004,6 +1002,14 @@ impl<T> Binder<T> {
10041002
Binder(f(self.0))
10051003
}
10061004

1005+
/// Wraps a `value` in a binder, using the same bound variables as the
1006+
/// current `Binder`. This should not be used if the new value *changes*
1007+
/// the bound variables. Note: the (old or new) value itself does not
1008+
/// necessarily need to *name* all the bound variables.
1009+
pub fn rebind<U>(&self, value: U) -> Binder<U> {
1010+
Binder(value)
1011+
}
1012+
10071013
/// Unwraps and returns the value within, but only if it contains
10081014
/// no bound vars at all. (In other words, if this binder --
10091015
/// and indeed any enclosing binder -- doesn't bind anything at

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,10 @@ impl AutoTraitFinder<'tcx> {
651651
{
652652
self.add_user_pred(computed_preds, predicate);
653653
}
654-
predicates.push_back(bound_predicate.map_bound_ref(|_| p));
654+
predicates.push_back(bound_predicate.rebind(p));
655655
}
656656
ty::PredicateAtom::Projection(p) => {
657-
let p = bound_predicate.map_bound_ref(|_| p);
657+
let p = bound_predicate.rebind(p);
658658
debug!(
659659
"evaluate_nested_obligations: examining projection predicate {:?}",
660660
predicate
@@ -784,13 +784,13 @@ impl AutoTraitFinder<'tcx> {
784784
}
785785
}
786786
ty::PredicateAtom::RegionOutlives(binder) => {
787-
let binder = bound_predicate.map_bound_ref(|_| binder);
787+
let binder = bound_predicate.rebind(binder);
788788
if select.infcx().region_outlives_predicate(&dummy_cause, binder).is_err() {
789789
return false;
790790
}
791791
}
792792
ty::PredicateAtom::TypeOutlives(binder) => {
793-
let binder = bound_predicate.map_bound_ref(|_| binder);
793+
let binder = bound_predicate.rebind(binder);
794794
match (
795795
binder.no_bound_vars(),
796796
binder.map_bound_ref(|pred| pred.0).no_bound_vars(),

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
258258
let bound_predicate = obligation.predicate.bound_atom(self.tcx);
259259
match bound_predicate.skip_binder() {
260260
ty::PredicateAtom::Trait(trait_predicate, _) => {
261-
let trait_predicate = bound_predicate.map_bound_ref(|_| trait_predicate);
261+
let trait_predicate = bound_predicate.rebind(trait_predicate);
262262
let trait_predicate = self.resolve_vars_if_possible(&trait_predicate);
263263

264264
if self.tcx.sess.has_errors() && trait_predicate.references_error() {
@@ -532,7 +532,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
532532
}
533533

534534
ty::PredicateAtom::RegionOutlives(predicate) => {
535-
let predicate = bound_predicate.map_bound_ref(|_| predicate);
535+
let predicate = bound_predicate.rebind(predicate);
536536
let predicate = self.resolve_vars_if_possible(&predicate);
537537
let err = self
538538
.region_outlives_predicate(&obligation.cause, predicate)
@@ -1082,7 +1082,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
10821082
let bound_error = error.bound_atom(self.tcx);
10831083
let (cond, error) = match (cond.skip_binders(), bound_error.skip_binder()) {
10841084
(ty::PredicateAtom::Trait(..), ty::PredicateAtom::Trait(error, _)) => {
1085-
(cond, bound_error.map_bound_ref(|_| error))
1085+
(cond, bound_error.rebind(error))
10861086
}
10871087
_ => {
10881088
// FIXME: make this work in other cases too.
@@ -1094,7 +1094,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
10941094
let bound_predicate = obligation.predicate.bound_atom(self.tcx);
10951095
if let ty::PredicateAtom::Trait(implication, _) = bound_predicate.skip_binder() {
10961096
let error = error.to_poly_trait_ref();
1097-
let implication = bound_predicate.map_bound_ref(|_| implication.trait_ref);
1097+
let implication = bound_predicate.rebind(implication.trait_ref);
10981098
// FIXME: I'm just not taking associated types at all here.
10991099
// Eventually I'll need to implement param-env-aware
11001100
// `Γ₁ ⊦ φ₁ => Γ₂ ⊦ φ₂` logic.
@@ -1178,7 +1178,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
11781178
let (data, _) = self.replace_bound_vars_with_fresh_vars(
11791179
obligation.cause.span,
11801180
infer::LateBoundRegionConversionTime::HigherRankedType,
1181-
&bound_predicate.map_bound_ref(|_| data),
1181+
&bound_predicate.rebind(data),
11821182
);
11831183
let mut obligations = vec![];
11841184
let normalized_ty = super::normalize_projection_type(
@@ -1463,7 +1463,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
14631463
let mut err = match bound_predicate.skip_binder() {
14641464
ty::PredicateAtom::Trait(data, _) => {
14651465
let self_ty = data.trait_ref.self_ty();
1466-
let trait_ref = bound_predicate.map_bound_ref(|_| data.trait_ref);
1466+
let trait_ref = bound_predicate.rebind(data.trait_ref);
14671467
debug!("self_ty {:?} {:?} trait_ref {:?}", self_ty, self_ty.kind(), trait_ref);
14681468

14691469
if predicate.references_error() {
@@ -1587,7 +1587,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
15871587
self.emit_inference_failure_err(body_id, span, a.into(), ErrorCode::E0282)
15881588
}
15891589
ty::PredicateAtom::Projection(data) => {
1590-
let trait_ref = bound_predicate.map_bound_ref(|_| data).to_poly_trait_ref(self.tcx);
1590+
let trait_ref = bound_predicate.rebind(data).to_poly_trait_ref(self.tcx);
15911591
let self_ty = trait_ref.skip_binder().self_ty();
15921592
let ty = data.ty;
15931593
if predicate.references_error() {

compiler/rustc_trait_selection/src/traits/fulfill.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
353353
// This means we need to pass it the bound version of our
354354
// predicate.
355355
ty::PredicateAtom::Trait(trait_ref, _constness) => {
356-
let trait_obligation = obligation.with(binder.map_bound_ref(|_| trait_ref));
356+
let trait_obligation = obligation.with(binder.rebind(trait_ref));
357357

358358
self.process_trait_obligation(
359359
obligation,
@@ -362,7 +362,7 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
362362
)
363363
}
364364
ty::PredicateAtom::Projection(data) => {
365-
let project_obligation = obligation.with(binder.map_bound_ref(|_| data));
365+
let project_obligation = obligation.with(binder.rebind(data));
366366

367367
self.process_projection_obligation(
368368
project_obligation,

compiler/rustc_trait_selection/src/traits/project.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,9 @@ fn prune_cache_value_obligations<'a, 'tcx>(
634634
// indirect obligations (e.g., we project to `?0`,
635635
// but we have `T: Foo<X = ?1>` and `?1: Bar<X =
636636
// ?0>`).
637-
ty::PredicateAtom::Projection(data) => infcx
638-
.unresolved_type_vars(&bound_predicate.map_bound_ref(|_| data.ty))
639-
.is_some(),
637+
ty::PredicateAtom::Projection(data) => {
638+
infcx.unresolved_type_vars(&bound_predicate.rebind(data.ty)).is_some()
639+
}
640640

641641
// We are only interested in `T: Foo<X = U>` predicates, whre
642642
// `U` references one of `unresolved_type_vars`. =)
@@ -910,7 +910,7 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>(
910910
debug!(?predicate);
911911
let bound_predicate = predicate.bound_atom(infcx.tcx);
912912
if let ty::PredicateAtom::Projection(data) = predicate.skip_binders() {
913-
let data = bound_predicate.map_bound_ref(|_| data);
913+
let data = bound_predicate.rebind(data);
914914
let same_def_id = data.projection_def_id() == obligation.predicate.item_def_id;
915915

916916
let is_match = same_def_id

compiler/rustc_trait_selection/src/traits/select/mod.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -449,17 +449,18 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
449449
}
450450

451451
let result = ensure_sufficient_stack(|| {
452-
let bound_predicate = obligation.predicate.bound_atom(self.infcx().tcx);
452+
let bound_predicate =
453+
obligation.predicate.bound_atom_with_opt_escaping(self.infcx().tcx);
453454
match bound_predicate.skip_binder() {
454455
ty::PredicateAtom::Trait(t, _) => {
455-
let t = bound_predicate.map_bound_ref(|_| t);
456+
let t = bound_predicate.rebind(t);
456457
debug_assert!(!t.has_escaping_bound_vars());
457458
let obligation = obligation.with(t);
458459
self.evaluate_trait_predicate_recursively(previous_stack, obligation)
459460
}
460461

461462
ty::PredicateAtom::Subtype(p) => {
462-
let p = bound_predicate.map_bound_ref(|_| p);
463+
let p = bound_predicate.rebind(p);
463464
// Does this code ever run?
464465
match self.infcx.subtype_predicate(&obligation.cause, obligation.param_env, p) {
465466
Some(Ok(InferOk { mut obligations, .. })) => {
@@ -503,7 +504,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
503504
}
504505

505506
ty::PredicateAtom::Projection(data) => {
506-
let data = bound_predicate.map_bound_ref(|_| data);
507+
let data = bound_predicate.rebind(data);
507508
let project_obligation = obligation.with(data);
508509
match project::poly_project_and_unify_type(self, &project_obligation) {
509510
Ok(Ok(Some(mut subobligations))) => {
@@ -1177,7 +1178,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11771178
.filter_map(|(idx, bound)| {
11781179
let bound_predicate = bound.bound_atom(self.infcx.tcx);
11791180
if let ty::PredicateAtom::Trait(pred, _) = bound_predicate.skip_binder() {
1180-
let bound = bound_predicate.map_bound_ref(|_| pred.trait_ref);
1181+
let bound = bound_predicate.rebind(pred.trait_ref);
11811182
if self.infcx.probe(|_| {
11821183
match self.match_projection(
11831184
obligation,
@@ -1534,15 +1535,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15341535
ty::Tuple(tys) => Where(
15351536
obligation
15361537
.predicate
1537-
.map_bound_ref(|_| tys.last().into_iter().map(|k| k.expect_ty()).collect()),
1538+
.rebind(tys.last().into_iter().map(|k| k.expect_ty()).collect()),
15381539
),
15391540

15401541
ty::Adt(def, substs) => {
15411542
let sized_crit = def.sized_constraint(self.tcx());
15421543
// (*) binder moved here
1543-
Where(obligation.predicate.map_bound_ref(|_| {
1544-
sized_crit.iter().map(|ty| ty.subst(self.tcx(), substs)).collect()
1545-
}))
1544+
Where(
1545+
obligation.predicate.rebind({
1546+
sized_crit.iter().map(|ty| ty.subst(self.tcx(), substs)).collect()
1547+
}),
1548+
)
15461549
}
15471550

15481551
ty::Projection(_) | ty::Param(_) | ty::Opaque(..) => None,
@@ -1594,16 +1597,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15941597

15951598
ty::Array(element_ty, _) => {
15961599
// (*) binder moved here
1597-
Where(obligation.predicate.map_bound_ref(|_| vec![*element_ty]))
1600+
Where(obligation.predicate.rebind(vec![*element_ty]))
15981601
}
15991602

16001603
ty::Tuple(tys) => {
16011604
// (*) binder moved here
1602-
Where(
1603-
obligation
1604-
.predicate
1605-
.map_bound_ref(|_| tys.iter().map(|k| k.expect_ty()).collect()),
1606-
)
1605+
Where(obligation.predicate.rebind(tys.iter().map(|k| k.expect_ty()).collect()))
16071606
}
16081607

16091608
ty::Closure(_, substs) => {
@@ -1613,11 +1612,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16131612
// Not yet resolved.
16141613
Ambiguous
16151614
} else {
1616-
Where(
1617-
obligation
1618-
.predicate
1619-
.map_bound_ref(|_| substs.as_closure().upvar_tys().collect()),
1620-
)
1615+
Where(obligation.predicate.rebind(substs.as_closure().upvar_tys().collect()))
16211616
}
16221617
}
16231618

compiler/rustc_typeck/src/astconv/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10981098
let bound_predicate = obligation.predicate.bound_atom(tcx);
10991099
match bound_predicate.skip_binder() {
11001100
ty::PredicateAtom::Trait(pred, _) => {
1101-
let pred = bound_predicate.map_bound_ref(|_| pred);
1101+
let pred = bound_predicate.rebind(pred);
11021102
associated_types.entry(span).or_default().extend(
11031103
tcx.associated_items(pred.def_id())
11041104
.in_definition_order()
@@ -1107,7 +1107,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11071107
);
11081108
}
11091109
ty::PredicateAtom::Projection(pred) => {
1110-
let pred = bound_predicate.map_bound_ref(|_| pred);
1110+
let pred = bound_predicate.rebind(pred);
11111111
// A `Self` within the original bound will be substituted with a
11121112
// `trait_object_dummy_self`, so check for that.
11131113
let references_self =

compiler/rustc_typeck/src/check/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
200200
// the complete signature.
201201
self.deduce_sig_from_projection(
202202
Some(obligation.cause.span),
203-
bound_predicate.map_bound_ref(|_| proj_predicate),
203+
bound_predicate.rebind(proj_predicate),
204204
)
205205
} else {
206206
None

compiler/rustc_typeck/src/check/coercion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
595595
has_unsized_tuple_coercion = true;
596596
}
597597
}
598-
bound_predicate.map_bound_ref(|_| trait_pred)
598+
bound_predicate.rebind(trait_pred)
599599
}
600600
_ => {
601601
coercion.obligations.push(obligation);

0 commit comments

Comments
 (0)