Skip to content

Commit 8bb302d

Browse files
committed
Auto merge of #80106 - jackh726:binder-refactor-part-2, r=lcnr
A lot of refactoring to remove more `Binder::bind`s Split out from #76814
2 parents e461b81 + 5e70958 commit 8bb302d

File tree

25 files changed

+175
-157
lines changed

25 files changed

+175
-157
lines changed

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,10 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
525525
result_subst: &'a CanonicalVarValues<'tcx>,
526526
) -> impl Iterator<Item = PredicateObligation<'tcx>> + 'a + Captures<'tcx> {
527527
unsubstituted_region_constraints.iter().map(move |&constraint| {
528-
let ty::OutlivesPredicate(k1, r2) =
529-
substitute_value(self.tcx, result_subst, constraint).skip_binder();
528+
let predicate = substitute_value(self.tcx, result_subst, constraint);
529+
let ty::OutlivesPredicate(k1, r2) = predicate.skip_binder();
530530

531-
let predicate = match k1.unpack() {
531+
let atom = match k1.unpack() {
532532
GenericArgKind::Lifetime(r1) => {
533533
ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r1, r2))
534534
}
@@ -540,8 +540,9 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
540540
// encounter this branch.
541541
span_bug!(cause.span, "unexpected const outlives {:?}", constraint);
542542
}
543-
}
544-
.potentially_quantified(self.tcx, ty::PredicateKind::ForAll);
543+
};
544+
let predicate =
545+
predicate.rebind(atom).potentially_quantified(self.tcx, ty::PredicateKind::ForAll);
545546

546547
Obligation::new(cause.clone(), param_env, predicate)
547548
})

compiler/rustc_infer/src/infer/combine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
551551
where
552552
T: Relate<'tcx>,
553553
{
554-
Ok(ty::Binder::bind(self.relate(a.skip_binder(), b.skip_binder())?))
554+
Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
555555
}
556556

557557
fn relate_item_substs(
@@ -833,7 +833,7 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
833833
where
834834
T: Relate<'tcx>,
835835
{
836-
Ok(ty::Binder::bind(self.relate(a.skip_binder(), b.skip_binder())?))
836+
Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
837837
}
838838

839839
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
@@ -1008,6 +1008,6 @@ where
10081008
self.first_free_index.shift_in(1);
10091009
let result = self.relate(a.skip_binder(), a.skip_binder())?;
10101010
self.first_free_index.shift_out(1);
1011-
Ok(ty::Binder::bind(result))
1011+
Ok(a.rebind(result))
10121012
}
10131013
}

compiler/rustc_middle/src/ty/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,6 @@ impl TypeRelation<'tcx> for Match<'tcx> {
118118
where
119119
T: Relate<'tcx>,
120120
{
121-
Ok(ty::Binder::bind(self.relate(a.skip_binder(), b.skip_binder())?))
121+
Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
122122
}
123123
}

compiler/rustc_middle/src/ty/context.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ pub struct ResolvedOpaqueTy<'tcx> {
299299
/// Here, we would store the type `T`, the span of the value `x`, the "scope-span" for
300300
/// the scope that contains `x`, the expr `T` evaluated from, and the span of `foo.await`.
301301
#[derive(TyEncodable, TyDecodable, Clone, Debug, Eq, Hash, PartialEq, HashStable)]
302+
#[derive(TypeFoldable)]
302303
pub struct GeneratorInteriorTypeCause<'tcx> {
303304
/// Type of the captured binding.
304305
pub ty: Ty<'tcx>,
@@ -423,7 +424,7 @@ pub struct TypeckResults<'tcx> {
423424

424425
/// Stores the type, expression, span and optional scope span of all types
425426
/// that are live across the yield of this generator (if a generator).
426-
pub generator_interior_types: Vec<GeneratorInteriorTypeCause<'tcx>>,
427+
pub generator_interior_types: ty::Binder<Vec<GeneratorInteriorTypeCause<'tcx>>>,
427428

428429
/// We sometimes treat byte string literals (which are of type `&[u8; N]`)
429430
/// as `&[u8]`, depending on the pattern in which they are used.
@@ -455,7 +456,7 @@ impl<'tcx> TypeckResults<'tcx> {
455456
concrete_opaque_types: Default::default(),
456457
closure_captures: Default::default(),
457458
closure_min_captures: Default::default(),
458-
generator_interior_types: Default::default(),
459+
generator_interior_types: ty::Binder::dummy(Default::default()),
459460
treat_byte_string_as_slice: Default::default(),
460461
}
461462
}

compiler/rustc_middle/src/ty/mod.rs

+24-14
Original file line numberDiff line numberDiff line change
@@ -1149,17 +1149,16 @@ pub enum PredicateAtom<'tcx> {
11491149
TypeWellFormedFromEnv(Ty<'tcx>),
11501150
}
11511151

1152-
impl<'tcx> PredicateAtom<'tcx> {
1152+
impl<'tcx> Binder<PredicateAtom<'tcx>> {
11531153
/// Wraps `self` with the given qualifier if this predicate has any unbound variables.
11541154
pub fn potentially_quantified(
11551155
self,
11561156
tcx: TyCtxt<'tcx>,
11571157
qualifier: impl FnOnce(Binder<PredicateAtom<'tcx>>) -> PredicateKind<'tcx>,
11581158
) -> Predicate<'tcx> {
1159-
if self.has_escaping_bound_vars() {
1160-
qualifier(Binder::bind(self))
1161-
} else {
1162-
PredicateKind::Atom(self)
1159+
match self.no_bound_vars() {
1160+
Some(atom) => PredicateKind::Atom(atom),
1161+
None => qualifier(self),
11631162
}
11641163
.to_predicate(tcx)
11651164
}
@@ -1252,7 +1251,11 @@ impl<'tcx> Predicate<'tcx> {
12521251
let substs = trait_ref.skip_binder().substs;
12531252
let pred = self.skip_binders();
12541253
let new = pred.subst(tcx, substs);
1255-
if new != pred { new.potentially_quantified(tcx, PredicateKind::ForAll) } else { self }
1254+
if new != pred {
1255+
ty::Binder::bind(new).potentially_quantified(tcx, PredicateKind::ForAll)
1256+
} else {
1257+
self
1258+
}
12561259
}
12571260
}
12581261

@@ -1279,6 +1282,10 @@ impl<'tcx> PolyTraitPredicate<'tcx> {
12791282
// Ok to skip binder since trait `DefId` does not care about regions.
12801283
self.skip_binder().def_id()
12811284
}
1285+
1286+
pub fn self_ty(self) -> ty::Binder<Ty<'tcx>> {
1287+
self.map_bound(|trait_ref| trait_ref.self_ty())
1288+
}
12821289
}
12831290

12841291
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
@@ -1403,37 +1410,39 @@ impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitRef<'tcx>> {
14031410

14041411
impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitPredicate<'tcx>> {
14051412
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1406-
PredicateAtom::Trait(self.value.skip_binder(), self.constness)
1413+
self.value
1414+
.map_bound(|value| PredicateAtom::Trait(value, self.constness))
14071415
.potentially_quantified(tcx, PredicateKind::ForAll)
14081416
}
14091417
}
14101418

14111419
impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> {
14121420
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1413-
PredicateAtom::RegionOutlives(self.skip_binder())
1421+
self.map_bound(|value| PredicateAtom::RegionOutlives(value))
14141422
.potentially_quantified(tcx, PredicateKind::ForAll)
14151423
}
14161424
}
14171425

14181426
impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> {
14191427
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1420-
PredicateAtom::TypeOutlives(self.skip_binder())
1428+
self.map_bound(|value| PredicateAtom::TypeOutlives(value))
14211429
.potentially_quantified(tcx, PredicateKind::ForAll)
14221430
}
14231431
}
14241432

14251433
impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
14261434
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1427-
PredicateAtom::Projection(self.skip_binder())
1435+
self.map_bound(|value| PredicateAtom::Projection(value))
14281436
.potentially_quantified(tcx, PredicateKind::ForAll)
14291437
}
14301438
}
14311439

14321440
impl<'tcx> Predicate<'tcx> {
14331441
pub fn to_opt_poly_trait_ref(self) -> Option<ConstnessAnd<PolyTraitRef<'tcx>>> {
1434-
match self.skip_binders() {
1442+
let predicate = self.bound_atom();
1443+
match predicate.skip_binder() {
14351444
PredicateAtom::Trait(t, constness) => {
1436-
Some(ConstnessAnd { constness, value: ty::Binder::bind(t.trait_ref) })
1445+
Some(ConstnessAnd { constness, value: predicate.rebind(t.trait_ref) })
14371446
}
14381447
PredicateAtom::Projection(..)
14391448
| PredicateAtom::Subtype(..)
@@ -1449,8 +1458,9 @@ impl<'tcx> Predicate<'tcx> {
14491458
}
14501459

14511460
pub fn to_opt_type_outlives(self) -> Option<PolyTypeOutlivesPredicate<'tcx>> {
1452-
match self.skip_binders() {
1453-
PredicateAtom::TypeOutlives(data) => Some(ty::Binder::bind(data)),
1461+
let predicate = self.bound_atom();
1462+
match predicate.skip_binder() {
1463+
PredicateAtom::TypeOutlives(data) => Some(predicate.rebind(data)),
14541464
PredicateAtom::Trait(..)
14551465
| PredicateAtom::Projection(..)
14561466
| PredicateAtom::Subtype(..)

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

+16-18
Original file line numberDiff line numberDiff line change
@@ -1151,9 +1151,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11511151
) -> DiagnosticBuilder<'tcx> {
11521152
crate fn build_fn_sig_string<'tcx>(
11531153
tcx: TyCtxt<'tcx>,
1154-
trait_ref: ty::TraitRef<'tcx>,
1154+
trait_ref: ty::PolyTraitRef<'tcx>,
11551155
) -> String {
1156-
let inputs = trait_ref.substs.type_at(1);
1156+
let inputs = trait_ref.skip_binder().substs.type_at(1);
11571157
let sig = if let ty::Tuple(inputs) = inputs.kind() {
11581158
tcx.mk_fn_sig(
11591159
inputs.iter().map(|k| k.expect_ty()),
@@ -1171,7 +1171,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11711171
abi::Abi::Rust,
11721172
)
11731173
};
1174-
ty::Binder::bind(sig).to_string()
1174+
trait_ref.rebind(sig).to_string()
11751175
}
11761176

11771177
let argument_is_closure = expected_ref.skip_binder().substs.type_at(0).is_closure();
@@ -1183,17 +1183,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11831183
if argument_is_closure { "closure" } else { "function" }
11841184
);
11851185

1186-
let found_str = format!(
1187-
"expected signature of `{}`",
1188-
build_fn_sig_string(self.tcx, found.skip_binder())
1189-
);
1186+
let found_str = format!("expected signature of `{}`", build_fn_sig_string(self.tcx, found));
11901187
err.span_label(span, found_str);
11911188

11921189
let found_span = found_span.unwrap_or(span);
1193-
let expected_str = format!(
1194-
"found signature of `{}`",
1195-
build_fn_sig_string(self.tcx, expected_ref.skip_binder())
1196-
);
1190+
let expected_str =
1191+
format!("found signature of `{}`", build_fn_sig_string(self.tcx, expected_ref));
11971192
err.span_label(found_span, expected_str);
11981193

11991194
err
@@ -1422,7 +1417,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
14221417
// generator frame. Bound regions are preserved by
14231418
// `erase_regions` and so we must also call
14241419
// `erase_late_bound_regions`.
1425-
let ty_erased = self.tcx.erase_late_bound_regions(ty::Binder::bind(ty));
1420+
let ty_erased = self.tcx.erase_late_bound_regions(ty);
14261421
let ty_erased = self.tcx.erase_regions(ty_erased);
14271422
let eq = ty::TyS::same_type(ty_erased, target_ty_erased);
14281423
debug!(
@@ -1440,18 +1435,21 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
14401435
interior_or_upvar_span = upvars.iter().find_map(|(upvar_id, upvar)| {
14411436
let upvar_ty = typeck_results.node_type(*upvar_id);
14421437
let upvar_ty = self.resolve_vars_if_possible(upvar_ty);
1443-
if ty_matches(&upvar_ty) {
1438+
if ty_matches(ty::Binder::dummy(upvar_ty)) {
14441439
Some(GeneratorInteriorOrUpvar::Upvar(upvar.span))
14451440
} else {
14461441
None
14471442
}
14481443
});
14491444
};
14501445

1451-
if let Some(cause) = typeck_results
1452-
.generator_interior_types
1453-
.iter()
1454-
.find(|ty::GeneratorInteriorTypeCause { ty, .. }| ty_matches(ty))
1446+
// The generator interior types share the same binders
1447+
if let Some(cause) =
1448+
typeck_results.generator_interior_types.as_ref().skip_binder().iter().find(
1449+
|ty::GeneratorInteriorTypeCause { ty, .. }| {
1450+
ty_matches(typeck_results.generator_interior_types.rebind(ty))
1451+
},
1452+
)
14551453
{
14561454
// Check to see if any awaited expressions have the target type.
14571455
let from_awaited_ty = visitor
@@ -1464,7 +1462,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
14641462
"maybe_note_obligation_cause_for_async_await: await_expr={:?}",
14651463
await_expr
14661464
);
1467-
ty_matches(ty)
1465+
ty_matches(ty::Binder::dummy(ty))
14681466
})
14691467
.map(|expr| expr.span);
14701468
let ty::GeneratorInteriorTypeCause { span, scope_span, yield_span, expr, .. } = cause;

compiler/rustc_trait_selection/src/traits/object_safety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,11 @@ fn virtual_call_violation_for_method<'tcx>(
418418
}
419419

420420
for (i, &input_ty) in sig.skip_binder().inputs()[1..].iter().enumerate() {
421-
if contains_illegal_self_type_reference(tcx, trait_def_id, input_ty) {
421+
if contains_illegal_self_type_reference(tcx, trait_def_id, sig.rebind(input_ty)) {
422422
return Some(MethodViolationCode::ReferencesSelfInput(i));
423423
}
424424
}
425-
if contains_illegal_self_type_reference(tcx, trait_def_id, sig.output().skip_binder()) {
425+
if contains_illegal_self_type_reference(tcx, trait_def_id, sig.output()) {
426426
return Some(MethodViolationCode::ReferencesSelfOutput);
427427
}
428428

compiler/rustc_trait_selection/src/traits/project.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
951951

952952
// If we are resolving `<T as TraitRef<...>>::Item == Type`,
953953
// start out by selecting the predicate `T as TraitRef<...>`:
954-
let poly_trait_ref = obligation_trait_ref.to_poly_trait_ref();
954+
let poly_trait_ref = ty::Binder::dummy(*obligation_trait_ref);
955955
let trait_obligation = obligation.with(poly_trait_ref.to_poly_trait_predicate());
956956
let _ = selcx.infcx().commit_if_ok(|_| {
957957
let impl_source = match selcx.select(&trait_obligation) {
@@ -1247,7 +1247,9 @@ fn confirm_discriminant_kind_candidate<'cx, 'tcx>(
12471247
ty: self_ty.discriminant_ty(tcx),
12481248
};
12491249

1250-
confirm_param_env_candidate(selcx, obligation, ty::Binder::bind(predicate), false)
1250+
// We get here from `poly_project_and_unify_type` which replaces bound vars
1251+
// with placeholders, so dummy is okay here.
1252+
confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
12511253
}
12521254

12531255
fn confirm_fn_pointer_candidate<'cx, 'tcx>(

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
259259
) -> ImplSourceAutoImplData<PredicateObligation<'tcx>> {
260260
debug!(?obligation, ?trait_def_id, "confirm_auto_impl_candidate");
261261

262-
let types = obligation.predicate.map_bound(|inner| {
263-
let self_ty = self.infcx.shallow_resolve(inner.self_ty());
264-
self.constituent_types_for_ty(self_ty)
265-
});
262+
let self_ty = self.infcx.shallow_resolve(obligation.predicate.self_ty());
263+
let types = self.constituent_types_for_ty(self_ty);
266264
self.vtable_auto_impl(obligation, trait_def_id, types)
267265
}
268266

0 commit comments

Comments
 (0)