Skip to content

Commit 5e70958

Browse files
committed
More rebinds
1 parent af3b1cb commit 5e70958

File tree

24 files changed

+152
-148
lines changed

24 files changed

+152
-148
lines changed

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ impl<'tcx> Predicate<'tcx> {
12521252
let pred = self.skip_binders();
12531253
let new = pred.subst(tcx, substs);
12541254
if new != pred {
1255-
trait_ref.rebind(new).potentially_quantified(tcx, PredicateKind::ForAll)
1255+
ty::Binder::bind(new).potentially_quantified(tcx, PredicateKind::ForAll)
12561256
} else {
12571257
self
12581258
}
@@ -1282,6 +1282,10 @@ impl<'tcx> PolyTraitPredicate<'tcx> {
12821282
// Ok to skip binder since trait `DefId` does not care about regions.
12831283
self.skip_binder().def_id()
12841284
}
1285+
1286+
pub fn self_ty(self) -> ty::Binder<Ty<'tcx>> {
1287+
self.map_bound(|trait_ref| trait_ref.self_ty())
1288+
}
12851289
}
12861290

12871291
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
@@ -1435,9 +1439,10 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
14351439

14361440
impl<'tcx> Predicate<'tcx> {
14371441
pub fn to_opt_poly_trait_ref(self) -> Option<ConstnessAnd<PolyTraitRef<'tcx>>> {
1438-
match self.skip_binders() {
1442+
let predicate = self.bound_atom();
1443+
match predicate.skip_binder() {
14391444
PredicateAtom::Trait(t, constness) => {
1440-
Some(ConstnessAnd { constness, value: ty::Binder::bind(t.trait_ref) })
1445+
Some(ConstnessAnd { constness, value: predicate.rebind(t.trait_ref) })
14411446
}
14421447
PredicateAtom::Projection(..)
14431448
| PredicateAtom::Subtype(..)
@@ -1453,8 +1458,9 @@ impl<'tcx> Predicate<'tcx> {
14531458
}
14541459

14551460
pub fn to_opt_type_outlives(self) -> Option<PolyTypeOutlivesPredicate<'tcx>> {
1456-
match self.skip_binders() {
1457-
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)),
14581464
PredicateAtom::Trait(..)
14591465
| PredicateAtom::Projection(..)
14601466
| 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

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

+18-17
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12761276

12771277
// FIXME(generic_associated_types): Compare the whole projections
12781278
let data_poly_trait_ref = projection_ty.map_bound(|proj| proj.trait_ref(self.tcx()));
1279-
let obligation_poly_trait_ref = obligation_trait_ref.to_poly_trait_ref();
1279+
let obligation_poly_trait_ref = ty::Binder::dummy(*obligation_trait_ref);
12801280
self.infcx
12811281
.at(&obligation.cause, obligation.param_env)
12821282
.sup(obligation_poly_trait_ref, data_poly_trait_ref)
@@ -1648,8 +1648,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16481648
/// Bar<i32> where struct Bar<T> { x: T, y: u32 } -> [i32, u32]
16491649
/// Zed<i32> where enum Zed { A(T), B(u32) } -> [i32, u32]
16501650
/// ```
1651-
fn constituent_types_for_ty(&self, t: Ty<'tcx>) -> Vec<Ty<'tcx>> {
1652-
match *t.kind() {
1651+
fn constituent_types_for_ty(&self, t: ty::Binder<Ty<'tcx>>) -> ty::Binder<Vec<Ty<'tcx>>> {
1652+
match *t.skip_binder().kind() {
16531653
ty::Uint(_)
16541654
| ty::Int(_)
16551655
| ty::Bool
@@ -1660,7 +1660,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16601660
| ty::Error(_)
16611661
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
16621662
| ty::Never
1663-
| ty::Char => Vec::new(),
1663+
| ty::Char => ty::Binder::dummy(Vec::new()),
16641664

16651665
ty::Placeholder(..)
16661666
| ty::Dynamic(..)
@@ -1673,44 +1673,44 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16731673
}
16741674

16751675
ty::RawPtr(ty::TypeAndMut { ty: element_ty, .. }) | ty::Ref(_, element_ty, _) => {
1676-
vec![element_ty]
1676+
t.rebind(vec![element_ty])
16771677
}
16781678

1679-
ty::Array(element_ty, _) | ty::Slice(element_ty) => vec![element_ty],
1679+
ty::Array(element_ty, _) | ty::Slice(element_ty) => t.rebind(vec![element_ty]),
16801680

16811681
ty::Tuple(ref tys) => {
16821682
// (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
1683-
tys.iter().map(|k| k.expect_ty()).collect()
1683+
t.rebind(tys.iter().map(|k| k.expect_ty()).collect())
16841684
}
16851685

16861686
ty::Closure(_, ref substs) => {
16871687
let ty = self.infcx.shallow_resolve(substs.as_closure().tupled_upvars_ty());
1688-
vec![ty]
1688+
t.rebind(vec![ty])
16891689
}
16901690

16911691
ty::Generator(_, ref substs, _) => {
16921692
let ty = self.infcx.shallow_resolve(substs.as_generator().tupled_upvars_ty());
16931693
let witness = substs.as_generator().witness();
1694-
vec![ty].into_iter().chain(iter::once(witness)).collect()
1694+
t.rebind(vec![ty].into_iter().chain(iter::once(witness)).collect())
16951695
}
16961696

16971697
ty::GeneratorWitness(types) => {
1698-
// This is sound because no regions in the witness can refer to
1699-
// the binder outside the witness. So we'll effectivly reuse
1700-
// the implicit binder around the witness.
1701-
types.skip_binder().to_vec()
1698+
debug_assert!(!types.has_escaping_bound_vars());
1699+
types.map_bound(|types| types.to_vec())
17021700
}
17031701

17041702
// For `PhantomData<T>`, we pass `T`.
1705-
ty::Adt(def, substs) if def.is_phantom_data() => substs.types().collect(),
1703+
ty::Adt(def, substs) if def.is_phantom_data() => t.rebind(substs.types().collect()),
17061704

1707-
ty::Adt(def, substs) => def.all_fields().map(|f| f.ty(self.tcx(), substs)).collect(),
1705+
ty::Adt(def, substs) => {
1706+
t.rebind(def.all_fields().map(|f| f.ty(self.tcx(), substs)).collect())
1707+
}
17081708

17091709
ty::Opaque(def_id, substs) => {
17101710
// We can resolve the `impl Trait` to its concrete type,
17111711
// which enforces a DAG between the functions requiring
17121712
// the auto trait bounds in question.
1713-
vec![self.tcx().type_of(def_id).subst(self.tcx(), substs)]
1713+
t.rebind(vec![self.tcx().type_of(def_id).subst(self.tcx(), substs)])
17141714
}
17151715
}
17161716
}
@@ -1738,10 +1738,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
17381738
// 3. Re-bind the regions back to `for<'a> &'a i32 : Copy`
17391739

17401740
types
1741+
.as_ref()
17411742
.skip_binder() // binder moved -\
17421743
.iter()
17431744
.flat_map(|ty| {
1744-
let ty: ty::Binder<Ty<'tcx>> = ty::Binder::bind(ty); // <----/
1745+
let ty: ty::Binder<Ty<'tcx>> = types.rebind(ty); // <----/
17451746

17461747
self.infcx.commit_unconditionally(|_| {
17471748
let placeholder_ty = self.infcx.replace_bound_vars_with_placeholders(ty);

compiler/rustc_trait_selection/src/traits/util.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use smallvec::SmallVec;
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_hir::def_id::DefId;
88
use rustc_middle::ty::subst::{GenericArg, Subst, SubstsRef};
9-
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt, WithConstness};
9+
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
1010

1111
use super::{Normalized, Obligation, ObligationCause, PredicateObligation, SelectionContext};
1212
pub use rustc_infer::traits::util::*;
@@ -333,11 +333,12 @@ pub fn closure_trait_ref_and_return_type(
333333
TupleArgumentsFlag::No => sig.skip_binder().inputs()[0],
334334
TupleArgumentsFlag::Yes => tcx.intern_tup(sig.skip_binder().inputs()),
335335
};
336+
debug_assert!(!self_ty.has_escaping_bound_vars());
336337
let trait_ref = ty::TraitRef {
337338
def_id: fn_trait_def_id,
338339
substs: tcx.mk_substs_trait(self_ty, &[arguments_tuple.into()]),
339340
};
340-
ty::Binder::bind((trait_ref, sig.skip_binder().output()))
341+
sig.map_bound(|sig| (trait_ref, sig.output()))
341342
}
342343

343344
pub fn generator_trait_ref_and_outputs(
@@ -346,11 +347,12 @@ pub fn generator_trait_ref_and_outputs(
346347
self_ty: Ty<'tcx>,
347348
sig: ty::PolyGenSig<'tcx>,
348349
) -> ty::Binder<(ty::TraitRef<'tcx>, Ty<'tcx>, Ty<'tcx>)> {
350+
debug_assert!(!self_ty.has_escaping_bound_vars());
349351
let trait_ref = ty::TraitRef {
350352
def_id: fn_trait_def_id,
351353
substs: tcx.mk_substs_trait(self_ty, &[sig.skip_binder().resume_ty.into()]),
352354
};
353-
ty::Binder::bind((trait_ref, sig.skip_binder().yield_ty, sig.skip_binder().return_ty))
355+
sig.map_bound(|sig| (trait_ref, sig.yield_ty, sig.return_ty))
354356
}
355357

356358
pub fn impl_item_is_final(tcx: TyCtxt<'_>, assoc_item: &ty::AssocItem) -> bool {

compiler/rustc_typeck/src/astconv/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -837,9 +837,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
837837
.instantiate_lang_item_trait_ref(
838838
lang_item, span, hir_id, args, param_ty, bounds,
839839
),
840-
hir::GenericBound::Outlives(ref l) => {
841-
bounds.region_bounds.push((self.ast_region_to_region(l, None), l.span))
842-
}
840+
hir::GenericBound::Outlives(ref l) => bounds
841+
.region_bounds
842+
.push((ty::Binder::bind(self.ast_region_to_region(l, None)), l.span)),
843843
}
844844
}
845845
}

0 commit comments

Comments
 (0)