Skip to content

Commit 833b1d8

Browse files
committed
cleanup
1 parent 51cbcca commit 833b1d8

File tree

5 files changed

+35
-59
lines changed

5 files changed

+35
-59
lines changed

src/librustc_middle/ty/mod.rs

+23-43
Original file line numberDiff line numberDiff line change
@@ -1067,20 +1067,23 @@ impl<'tcx> Predicate<'tcx> {
10671067

10681068
/// Returns the inner `PredicateAtom`.
10691069
///
1070-
/// Note that this method does not check if predicate has unbound variables,
1071-
/// rebinding the returned atom potentially causes the previously bound variables
1070+
/// Note that this method does not check if the predicate has unbound variables.
1071+
///
1072+
/// Rebinding the returned atom can causes the previously bound variables
10721073
/// to end up at the wrong binding level.
10731074
pub fn skip_binders_unchecked(self) -> PredicateAtom<'tcx> {
10741075
match self.kind() {
10751076
&PredicateKind::ForAll(binder) => binder.skip_binder(),
1076-
&ty::PredicateKind::Atom(atom) => atom,
1077+
&PredicateKind::Atom(atom) => atom,
10771078
}
10781079
}
10791080

1081+
/// Allows using a `Binder<PredicateAtom<'tcx>>` even if the given predicate previously
1082+
/// contained unbound variables by shifting these variables outwards.
10801083
pub fn bound_atom(self, tcx: TyCtxt<'tcx>) -> Binder<PredicateAtom<'tcx>> {
10811084
match self.kind() {
10821085
&PredicateKind::ForAll(binder) => binder,
1083-
&ty::PredicateKind::Atom(atom) => Binder::wrap_nonbinding(tcx, atom),
1086+
&PredicateKind::Atom(atom) => Binder::wrap_nonbinding(tcx, atom),
10841087
}
10851088
}
10861089
}
@@ -1105,7 +1108,6 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Predicate<'tcx> {
11051108
pub enum PredicateKind<'tcx> {
11061109
/// `for<'a>: ...`
11071110
ForAll(Binder<PredicateAtom<'tcx>>),
1108-
11091111
Atom(PredicateAtom<'tcx>),
11101112
}
11111113

@@ -1179,7 +1181,7 @@ pub struct CratePredicatesMap<'tcx> {
11791181
/// For each struct with outlive bounds, maps to a vector of the
11801182
/// predicate of its outlive bounds. If an item has no outlives
11811183
/// bounds, it will have no entry.
1182-
pub predicates: FxHashMap<DefId, &'tcx [(ty::Predicate<'tcx>, Span)]>,
1184+
pub predicates: FxHashMap<DefId, &'tcx [(Predicate<'tcx>, Span)]>,
11831185
}
11841186

11851187
impl<'tcx> Predicate<'tcx> {
@@ -1192,7 +1194,7 @@ impl<'tcx> Predicate<'tcx> {
11921194
self,
11931195
tcx: TyCtxt<'tcx>,
11941196
trait_ref: &ty::PolyTraitRef<'tcx>,
1195-
) -> ty::Predicate<'tcx> {
1197+
) -> Predicate<'tcx> {
11961198
// The interaction between HRTB and supertraits is not entirely
11971199
// obvious. Let me walk you (and myself) through an example.
11981200
//
@@ -1384,13 +1386,13 @@ impl ToPredicate<'tcx> for PredicateAtom<'tcx> {
13841386
#[inline(always)]
13851387
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
13861388
debug_assert!(!self.has_escaping_bound_vars(), "escaping bound vars for {:?}", self);
1387-
tcx.mk_predicate(ty::PredicateKind::Atom(self))
1389+
tcx.mk_predicate(PredicateKind::Atom(self))
13881390
}
13891391
}
13901392

13911393
impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<TraitRef<'tcx>> {
13921394
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1393-
ty::PredicateAtom::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness)
1395+
PredicateAtom::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness)
13941396
.to_predicate(tcx)
13951397
}
13961398
}
@@ -1407,51 +1409,29 @@ impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitRef<'tcx>> {
14071409

14081410
impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitPredicate<'tcx>> {
14091411
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1410-
if let Some(pred) = self.value.no_bound_vars() {
1411-
ty::PredicateAtom::Trait(pred, self.constness).to_predicate(tcx)
1412-
} else {
1413-
ty::PredicateKind::ForAll(
1414-
self.value.map_bound(|pred| ty::PredicateAtom::Trait(pred, self.constness)),
1415-
)
1416-
.to_predicate(tcx)
1417-
}
1412+
PredicateAtom::Trait(self.value.skip_binder(), self.constness)
1413+
.potentially_quantified(tcx, PredicateKind::ForAll)
14181414
}
14191415
}
14201416

14211417
impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> {
14221418
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1423-
if let Some(outlives) = self.no_bound_vars() {
1424-
PredicateAtom::RegionOutlives(outlives).to_predicate(tcx)
1425-
} else {
1426-
ty::PredicateKind::ForAll(
1427-
self.map_bound(|outlives| PredicateAtom::RegionOutlives(outlives)),
1428-
)
1429-
.to_predicate(tcx)
1430-
}
1419+
PredicateAtom::RegionOutlives(self.skip_binder())
1420+
.potentially_quantified(tcx, PredicateKind::ForAll)
14311421
}
14321422
}
14331423

14341424
impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> {
14351425
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1436-
if let Some(outlives) = self.no_bound_vars() {
1437-
PredicateAtom::TypeOutlives(outlives).to_predicate(tcx)
1438-
} else {
1439-
ty::PredicateKind::ForAll(
1440-
self.map_bound(|outlives| PredicateAtom::TypeOutlives(outlives)),
1441-
)
1442-
.to_predicate(tcx)
1443-
}
1426+
PredicateAtom::TypeOutlives(self.skip_binder())
1427+
.potentially_quantified(tcx, PredicateKind::ForAll)
14441428
}
14451429
}
14461430

14471431
impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
14481432
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1449-
if let Some(proj) = self.no_bound_vars() {
1450-
PredicateAtom::Projection(proj).to_predicate(tcx)
1451-
} else {
1452-
ty::PredicateKind::ForAll(self.map_bound(|proj| PredicateAtom::Projection(proj)))
1453-
.to_predicate(tcx)
1454-
}
1433+
PredicateAtom::Projection(self.skip_binder())
1434+
.potentially_quantified(tcx, PredicateKind::ForAll)
14551435
}
14561436
}
14571437

@@ -1746,7 +1726,7 @@ pub struct ParamEnv<'tcx> {
17461726
// Specifically, the low bit represents Reveal, with 0 meaning `UserFacing`
17471727
// and 1 meaning `All`. The rest is the pointer.
17481728
//
1749-
// This relies on the List<ty::Predicate<'tcx>> type having at least 2-byte
1729+
// This relies on the List<Predicate<'tcx>> type having at least 2-byte
17501730
// alignment. Lists start with a usize and are repr(C) so this should be
17511731
// fine; there is a debug_assert in the constructor as well.
17521732
//
@@ -1760,7 +1740,7 @@ pub struct ParamEnv<'tcx> {
17601740
///
17611741
/// Note: This is packed into the `packed_data` usize above, use the
17621742
/// `caller_bounds()` method to access it.
1763-
caller_bounds: PhantomData<&'tcx List<ty::Predicate<'tcx>>>,
1743+
caller_bounds: PhantomData<&'tcx List<Predicate<'tcx>>>,
17641744

17651745
/// Typically, this is `Reveal::UserFacing`, but during codegen we
17661746
/// want `Reveal::All`.
@@ -1838,7 +1818,7 @@ impl<'tcx> ParamEnv<'tcx> {
18381818
}
18391819

18401820
#[inline]
1841-
pub fn caller_bounds(self) -> &'tcx List<ty::Predicate<'tcx>> {
1821+
pub fn caller_bounds(self) -> &'tcx List<Predicate<'tcx>> {
18421822
// mask out bottom bit
18431823
unsafe { &*((self.packed_data & (!1)) as *const _) }
18441824
}
@@ -1863,7 +1843,7 @@ impl<'tcx> ParamEnv<'tcx> {
18631843
/// Construct a trait environment with the given set of predicates.
18641844
#[inline]
18651845
pub fn new(
1866-
caller_bounds: &'tcx List<ty::Predicate<'tcx>>,
1846+
caller_bounds: &'tcx List<Predicate<'tcx>>,
18671847
reveal: Reveal,
18681848
def_id: Option<DefId>,
18691849
) -> Self {

src/librustc_mir/monomorphize/polymorphize.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,21 @@ fn mark_used_by_predicates<'tcx>(
131131
let predicates = tcx.explicit_predicates_of(def_id);
132132
debug!("mark_parameters_used_in_predicates: predicates_of={:?}", predicates);
133133
for (predicate, _) in predicates.predicates {
134-
match predicate.kind() {
135-
ty::PredicateKind::Trait(predicate, ..) => {
136-
let trait_ref = predicate.skip_binder().trait_ref;
134+
match predicate.skip_binders() {
135+
ty::PredicateAtom::Trait(predicate, ..) => {
136+
let trait_ref = predicate.trait_ref;
137137
if is_self_ty_used(unused_parameters, trait_ref.self_ty()) {
138138
for ty in trait_ref.substs.types() {
139139
debug!("unused_generic_params: (trait) ty={:?}", ty);
140140
mark_ty(unused_parameters, ty);
141141
}
142142
}
143143
}
144-
ty::PredicateKind::Projection(predicate, ..) => {
145-
let self_ty = predicate.skip_binder().projection_ty.self_ty();
144+
ty::PredicateAtom::Projection(proj, ..) => {
145+
let self_ty = proj.projection_ty.self_ty();
146146
if is_self_ty_used(unused_parameters, self_ty) {
147-
let ty = predicate.ty();
148-
debug!("unused_generic_params: (projection) ty={:?}", ty);
149-
mark_ty(unused_parameters, ty.skip_binder());
147+
debug!("unused_generic_params: (projection ty={:?}", proj.ty);
148+
mark_ty(unused_parameters, proj.ty);
150149
}
151150
}
152151
_ => (),

src/librustc_trait_selection/traits/fulfill.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_data_structures::obligation_forest::ProcessResult;
33
use rustc_data_structures::obligation_forest::{DoCompleted, Error, ForestObligation};
44
use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
55
use rustc_errors::ErrorReported;
6-
use rustc_infer::traits::{TraitObligation, TraitEngine, TraitEngineExt as _};
6+
use rustc_infer::traits::{TraitEngine, TraitEngineExt as _, TraitObligation};
77
use rustc_middle::mir::interpret::ErrorHandled;
88
use rustc_middle::ty::error::ExpectedFound;
99
use rustc_middle::ty::ToPredicate;

src/librustc_typeck/check/method/suggest.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
571571
let mut type_params = FxHashMap::default();
572572
let mut bound_spans = vec![];
573573

574-
let mut collect_type_param_suggestions = {
575-
// We need to move `tcx` while only borrowing the rest,
576-
// this is kind of ugly.
574+
let mut collect_type_param_suggestions =
577575
|self_ty: Ty<'tcx>, parent_pred: &ty::Predicate<'tcx>, obligation: &str| {
578576
// We don't care about regions here, so it's fine to skip the binder here.
579577
if let (ty::Param(_), ty::PredicateAtom::Trait(p, _)) =
@@ -601,8 +599,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
601599
}
602600
}
603601
}
604-
}
605-
};
602+
};
606603
let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
607604
let msg = format!(
608605
"doesn't satisfy `{}`",

src/librustc_typeck/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1930,8 +1930,8 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
19301930
let re_root_empty = tcx.lifetimes.re_root_empty;
19311931
let predicate = ty::OutlivesPredicate(ty, re_root_empty);
19321932
predicates.push((
1933-
ty::PredicateKind::TypeOutlives(ty::Binder::bind(predicate))
1934-
.to_predicate(tcx),
1933+
ty::PredicateAtom::TypeOutlives(predicate)
1934+
.potentially_quantified(tcx, ty::PredicateKind::ForAll),
19351935
span,
19361936
));
19371937
}

0 commit comments

Comments
 (0)