Skip to content

Commit ee2e97c

Browse files
Rollup merge of #89001 - jackh726:binder-cleanup, r=nikomatsakis
Be explicit about using Binder::dummy This is somewhat of a late followup to the binder refactor PR. It removes `ToPredicate` and `ToPolyTraitImpls` that hide the use of `Binder::dummy`. While this does make code a bit more verbose, it allows us be more careful about where we create binders. Another alternative here might be to add a new trait `ToBinder` or something with a `dummy()` fn. Which could still allow grepping but allows doing something like `trait_ref.dummy()` (but I also wonder if longer-term, it would be better to be even more explicit with a `bind_with_vars(ty::List::empty())` *but* that's not clear yet. r? ``@nikomatsakis``
2 parents 2de7f10 + 553f649 commit ee2e97c

File tree

34 files changed

+127
-121
lines changed

34 files changed

+127
-121
lines changed

compiler/rustc_borrowck/src/type_check/canonical.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
8989
category: ConstraintCategory,
9090
) {
9191
self.prove_predicates(
92-
Some(ty::PredicateKind::Trait(ty::TraitPredicate {
92+
Some(ty::Binder::dummy(ty::PredicateKind::Trait(ty::TraitPredicate {
9393
trait_ref,
9494
constness: ty::BoundConstness::NotConst,
95-
})),
95+
}))),
9696
locations,
9797
category,
9898
);

compiler/rustc_borrowck/src/type_check/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
10801080
}
10811081

10821082
self.prove_predicate(
1083-
ty::PredicateKind::WellFormed(inferred_ty.into()).to_predicate(self.tcx()),
1083+
ty::Binder::dummy(ty::PredicateKind::WellFormed(inferred_ty.into()))
1084+
.to_predicate(self.tcx()),
10841085
Locations::All(span),
10851086
ConstraintCategory::TypeAnnotation,
10861087
);
@@ -1316,7 +1317,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13161317
obligations.obligations.push(traits::Obligation::new(
13171318
ObligationCause::dummy(),
13181319
param_env,
1319-
ty::PredicateKind::WellFormed(revealed_ty.into()).to_predicate(infcx.tcx),
1320+
ty::Binder::dummy(ty::PredicateKind::WellFormed(revealed_ty.into()))
1321+
.to_predicate(infcx.tcx),
13201322
));
13211323
obligations.add(
13221324
infcx
@@ -1599,7 +1601,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
15991601
self.check_call_dest(body, term, &sig, destination, term_location);
16001602

16011603
self.prove_predicates(
1602-
sig.inputs_and_output.iter().map(|ty| ty::PredicateKind::WellFormed(ty.into())),
1604+
sig.inputs_and_output
1605+
.iter()
1606+
.map(|ty| ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into()))),
16031607
term_location.to_locations(),
16041608
ConstraintCategory::Boring,
16051609
);

compiler/rustc_infer/src/infer/canonical/query_response.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,10 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> {
669669
self.obligations.push(Obligation {
670670
cause: self.cause.clone(),
671671
param_env: self.param_env,
672-
predicate: ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(sup, sub))
673-
.to_predicate(self.infcx.tcx),
672+
predicate: ty::Binder::dummy(ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(
673+
sup, sub,
674+
)))
675+
.to_predicate(self.infcx.tcx),
674676
recursion_depth: 0,
675677
});
676678
}

compiler/rustc_infer/src/infer/combine.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
360360
self.obligations.push(Obligation::new(
361361
self.trace.cause.clone(),
362362
self.param_env,
363-
ty::PredicateKind::WellFormed(b_ty.into()).to_predicate(self.infcx.tcx),
363+
ty::Binder::dummy(ty::PredicateKind::WellFormed(b_ty.into()))
364+
.to_predicate(self.infcx.tcx),
364365
));
365366
}
366367

@@ -463,7 +464,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
463464
self.obligations.push(Obligation::new(
464465
self.trace.cause.clone(),
465466
self.param_env,
466-
predicate.to_predicate(self.tcx()),
467+
ty::Binder::dummy(predicate).to_predicate(self.tcx()),
467468
));
468469
}
469470
}

compiler/rustc_infer/src/infer/sub.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> {
9797
self.fields.obligations.push(Obligation::new(
9898
self.fields.trace.cause.clone(),
9999
self.fields.param_env,
100-
ty::PredicateKind::Subtype(ty::SubtypePredicate {
100+
ty::Binder::dummy(ty::PredicateKind::Subtype(ty::SubtypePredicate {
101101
a_is_expected: self.a_is_expected,
102102
a,
103103
b,
104-
})
104+
}))
105105
.to_predicate(self.tcx()),
106106
));
107107

compiler/rustc_infer/src/traits/engine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub trait TraitEngine<'tcx>: 'tcx {
3535
cause,
3636
recursion_depth: 0,
3737
param_env,
38-
predicate: trait_ref.without_const().to_predicate(infcx.tcx),
38+
predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(infcx.tcx),
3939
},
4040
);
4141
}

compiler/rustc_infer/src/traits/util.rs

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ impl Elaborator<'tcx> {
231231
None
232232
}
233233
})
234+
.map(ty::Binder::dummy)
234235
.map(|predicate_kind| predicate_kind.to_predicate(tcx))
235236
.filter(|&predicate| visited.insert(predicate))
236237
.map(|predicate| {

compiler/rustc_middle/src/ty/mod.rs

-23
Original file line numberDiff line numberDiff line change
@@ -769,12 +769,6 @@ pub trait ToPolyTraitRef<'tcx> {
769769
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx>;
770770
}
771771

772-
impl<'tcx> ToPolyTraitRef<'tcx> for TraitRef<'tcx> {
773-
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx> {
774-
ty::Binder::dummy(*self)
775-
}
776-
}
777-
778772
impl<'tcx> ToPolyTraitRef<'tcx> for PolyTraitPredicate<'tcx> {
779773
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx> {
780774
self.map_bound_ref(|trait_pred| trait_pred.trait_ref)
@@ -792,23 +786,6 @@ impl ToPredicate<'tcx> for Binder<'tcx, PredicateKind<'tcx>> {
792786
}
793787
}
794788

795-
impl ToPredicate<'tcx> for PredicateKind<'tcx> {
796-
#[inline(always)]
797-
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
798-
tcx.mk_predicate(Binder::dummy(self))
799-
}
800-
}
801-
802-
impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<TraitRef<'tcx>> {
803-
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
804-
PredicateKind::Trait(ty::TraitPredicate {
805-
trait_ref: self.value,
806-
constness: self.constness,
807-
})
808-
.to_predicate(tcx)
809-
}
810-
}
811-
812789
impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitRef<'tcx>> {
813790
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
814791
self.value

compiler/rustc_middle/src/ty/sty.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -844,8 +844,11 @@ impl<'tcx> TraitRef<'tcx> {
844844

845845
/// Returns a `TraitRef` of the form `P0: Foo<P1..Pn>` where `Pi`
846846
/// are the parameters defined on trait.
847-
pub fn identity(tcx: TyCtxt<'tcx>, def_id: DefId) -> TraitRef<'tcx> {
848-
TraitRef { def_id, substs: InternalSubsts::identity_for_item(tcx, def_id) }
847+
pub fn identity(tcx: TyCtxt<'tcx>, def_id: DefId) -> Binder<'tcx, TraitRef<'tcx>> {
848+
ty::Binder::dummy(TraitRef {
849+
def_id,
850+
substs: InternalSubsts::identity_for_item(tcx, def_id),
851+
})
849852
}
850853

851854
#[inline]

compiler/rustc_trait_selection/src/autoderef.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
135135
let obligation = traits::Obligation::new(
136136
cause.clone(),
137137
self.param_env,
138-
trait_ref.without_const().to_predicate(tcx),
138+
ty::Binder::dummy(trait_ref).without_const().to_predicate(tcx),
139139
);
140140
if !self.infcx.predicate_may_hold(&obligation) {
141141
debug!("overloaded_deref_ty: cannot match obligation");

compiler/rustc_trait_selection/src/infer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> {
120120
cause: traits::ObligationCause::dummy(),
121121
param_env,
122122
recursion_depth: 0,
123-
predicate: trait_ref.without_const().to_predicate(self.tcx),
123+
predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(self.tcx),
124124
};
125125
self.evaluate_obligation(&obligation).unwrap_or(traits::EvaluationResult::EvaluatedToErr)
126126
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
726726
let new_obligation = Obligation::new(
727727
ObligationCause::dummy(),
728728
param_env,
729-
new_trait_ref.without_const().to_predicate(self.tcx),
729+
ty::Binder::dummy(new_trait_ref).without_const().to_predicate(self.tcx),
730730
);
731731

732732
if self.predicate_must_hold_modulo_regions(&new_obligation) {

compiler/rustc_trait_selection/src/traits/fulfill.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
418418
| ty::PredicateKind::Coerce(_)
419419
| ty::PredicateKind::ConstEvaluatable(..)
420420
| ty::PredicateKind::ConstEquate(..) => {
421-
let pred = infcx.replace_bound_vars_with_placeholders(binder);
421+
let pred =
422+
ty::Binder::dummy(infcx.replace_bound_vars_with_placeholders(binder));
422423
ProcessResult::Changed(mk_pending(vec![
423424
obligation.with(pred.to_predicate(self.selcx.tcx())),
424425
]))

compiler/rustc_trait_selection/src/traits/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ pub fn type_known_to_meet_bound_modulo_regions<'a, 'tcx>(
140140
infcx.tcx.def_path_str(def_id)
141141
);
142142

143-
let trait_ref = ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) };
143+
let trait_ref =
144+
ty::Binder::dummy(ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) });
144145
let obligation = Obligation {
145146
param_env,
146147
cause: ObligationCause::misc(span, hir::CRATE_HIR_ID),

compiler/rustc_trait_selection/src/traits/object_safety.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ fn predicates_reference_self(
250250
trait_def_id: DefId,
251251
supertraits_only: bool,
252252
) -> SmallVec<[Span; 1]> {
253-
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(tcx, trait_def_id));
253+
let trait_ref = ty::TraitRef::identity(tcx, trait_def_id);
254254
let predicates = if supertraits_only {
255255
tcx.super_predicates_of(trait_def_id)
256256
} else {
@@ -554,11 +554,11 @@ fn object_ty_for_trait<'tcx>(
554554

555555
let trait_ref = ty::TraitRef::identity(tcx, trait_def_id);
556556

557-
let trait_predicate = ty::Binder::dummy(ty::ExistentialPredicate::Trait(
558-
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref),
559-
));
557+
let trait_predicate = trait_ref.map_bound(|trait_ref| {
558+
ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref))
559+
});
560560

561-
let mut associated_types = traits::supertraits(tcx, ty::Binder::dummy(trait_ref))
561+
let mut associated_types = traits::supertraits(tcx, trait_ref)
562562
.flat_map(|super_trait_ref| {
563563
tcx.associated_items(super_trait_ref.def_id())
564564
.in_definition_order()
@@ -671,10 +671,10 @@ fn receiver_is_dispatchable<'tcx>(
671671
let param_env = tcx.param_env(method.def_id);
672672

673673
// Self: Unsize<U>
674-
let unsize_predicate = ty::TraitRef {
674+
let unsize_predicate = ty::Binder::dummy(ty::TraitRef {
675675
def_id: unsize_did,
676676
substs: tcx.mk_substs_trait(tcx.types.self_param, &[unsized_self_ty.into()]),
677-
}
677+
})
678678
.without_const()
679679
.to_predicate(tcx);
680680

@@ -689,7 +689,9 @@ fn receiver_is_dispatchable<'tcx>(
689689
}
690690
});
691691

692-
ty::TraitRef { def_id: unsize_did, substs }.without_const().to_predicate(tcx)
692+
ty::Binder::dummy(ty::TraitRef { def_id: unsize_did, substs })
693+
.without_const()
694+
.to_predicate(tcx)
693695
};
694696

695697
let caller_bounds: Vec<Predicate<'tcx>> = param_env
@@ -703,10 +705,10 @@ fn receiver_is_dispatchable<'tcx>(
703705

704706
// Receiver: DispatchFromDyn<Receiver[Self => U]>
705707
let obligation = {
706-
let predicate = ty::TraitRef {
708+
let predicate = ty::Binder::dummy(ty::TraitRef {
707709
def_id: dispatch_from_dyn_did,
708710
substs: tcx.mk_substs_trait(receiver_ty, &[unsized_receiver_ty.into()]),
709-
}
711+
})
710712
.without_const()
711713
.to_predicate(tcx);
712714

@@ -789,8 +791,7 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeFoldable<'tcx>>(
789791

790792
// Compute supertraits of current trait lazily.
791793
if self.supertraits.is_none() {
792-
let trait_ref =
793-
ty::Binder::dummy(ty::TraitRef::identity(self.tcx, self.trait_def_id));
794+
let trait_ref = ty::TraitRef::identity(self.tcx, self.trait_def_id);
794795
self.supertraits = Some(
795796
traits::supertraits(self.tcx, trait_ref).map(|t| t.def_id()).collect(),
796797
);

compiler/rustc_trait_selection/src/traits/project.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_hir::lang_items::LangItem;
2727
use rustc_infer::infer::resolve::OpportunisticRegionResolver;
2828
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
2929
use rustc_middle::ty::subst::Subst;
30-
use rustc_middle::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, WithConstness};
30+
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt, WithConstness};
3131
use rustc_span::symbol::sym;
3232

3333
use std::collections::BTreeMap;
@@ -1028,7 +1028,7 @@ fn normalize_to_error<'a, 'tcx>(
10281028
cause: ObligationCause<'tcx>,
10291029
depth: usize,
10301030
) -> NormalizedTy<'tcx> {
1031-
let trait_ref = projection_ty.trait_ref(selcx.tcx()).to_poly_trait_ref();
1031+
let trait_ref = ty::Binder::dummy(projection_ty.trait_ref(selcx.tcx()));
10321032
let trait_obligation = Obligation {
10331033
cause,
10341034
recursion_depth: depth,
@@ -1290,7 +1290,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
12901290

12911291
// If we are resolving `<T as TraitRef<...>>::Item == Type`,
12921292
// start out by selecting the predicate `T as TraitRef<...>`:
1293-
let poly_trait_ref = obligation.predicate.trait_ref(selcx.tcx()).to_poly_trait_ref();
1293+
let poly_trait_ref = ty::Binder::dummy(obligation.predicate.trait_ref(selcx.tcx()));
12941294
let trait_obligation = obligation.with(poly_trait_ref.to_poly_trait_predicate());
12951295
let _ = selcx.infcx().commit_if_ok(|_| {
12961296
let impl_source = match selcx.select(&trait_obligation) {

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
141141
let placeholder_trait_predicate =
142142
self.infcx().replace_bound_vars_with_placeholders(trait_predicate);
143143
let placeholder_self_ty = placeholder_trait_predicate.self_ty();
144+
let placeholder_trait_predicate = ty::Binder::dummy(placeholder_trait_predicate);
144145
let (def_id, substs) = match *placeholder_self_ty.kind() {
145146
ty::Projection(proj) => (proj.item_def_id, proj.substs),
146147
ty::Opaque(def_id, substs) => (def_id, substs),
@@ -164,7 +165,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
164165
obligations.extend(self.infcx.commit_if_ok(|_| {
165166
self.infcx
166167
.at(&obligation.cause, obligation.param_env)
167-
.sup(placeholder_trait_predicate.trait_ref.to_poly_trait_ref(), candidate.value)
168+
.sup(placeholder_trait_predicate.to_poly_trait_ref(), candidate.value)
168169
.map(|InferOk { obligations, .. }| obligations)
169170
.map_err(|_| Unimplemented)
170171
})?);
@@ -646,7 +647,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
646647
obligations.push(Obligation::new(
647648
obligation.cause.clone(),
648649
obligation.param_env,
649-
ty::PredicateKind::ClosureKind(closure_def_id, substs, kind)
650+
ty::Binder::dummy(ty::PredicateKind::ClosureKind(closure_def_id, substs, kind))
650651
.to_predicate(self.tcx()),
651652
));
652653
}
@@ -898,10 +899,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
898899
);
899900

900901
// We can only make objects from sized types.
901-
let tr = ty::TraitRef::new(
902+
let tr = ty::Binder::dummy(ty::TraitRef::new(
902903
tcx.require_lang_item(LangItem::Sized, None),
903904
tcx.mk_substs_trait(source, &[]),
904-
);
905+
));
905906
nested.push(predicate_to_obligation(tr.without_const().to_predicate(tcx)));
906907

907908
// If the type is `Foo + 'a`, ensure that the type

compiler/rustc_trait_selection/src/traits/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ pub fn predicate_for_trait_ref<'tcx>(
248248
cause,
249249
param_env,
250250
recursion_depth,
251-
predicate: trait_ref.without_const().to_predicate(tcx),
251+
predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(tcx),
252252
}
253253
}
254254

0 commit comments

Comments
 (0)