Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Jul 27, 2020
1 parent 51cbcca commit 833b1d8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 59 deletions.
66 changes: 23 additions & 43 deletions src/librustc_middle/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,20 +1067,23 @@ impl<'tcx> Predicate<'tcx> {

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

/// Allows using a `Binder<PredicateAtom<'tcx>>` even if the given predicate previously
/// contained unbound variables by shifting these variables outwards.
pub fn bound_atom(self, tcx: TyCtxt<'tcx>) -> Binder<PredicateAtom<'tcx>> {
match self.kind() {
&PredicateKind::ForAll(binder) => binder,
&ty::PredicateKind::Atom(atom) => Binder::wrap_nonbinding(tcx, atom),
&PredicateKind::Atom(atom) => Binder::wrap_nonbinding(tcx, atom),
}
}
}
Expand All @@ -1105,7 +1108,6 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Predicate<'tcx> {
pub enum PredicateKind<'tcx> {
/// `for<'a>: ...`
ForAll(Binder<PredicateAtom<'tcx>>),

Atom(PredicateAtom<'tcx>),
}

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

impl<'tcx> Predicate<'tcx> {
Expand All @@ -1192,7 +1194,7 @@ impl<'tcx> Predicate<'tcx> {
self,
tcx: TyCtxt<'tcx>,
trait_ref: &ty::PolyTraitRef<'tcx>,
) -> ty::Predicate<'tcx> {
) -> Predicate<'tcx> {
// The interaction between HRTB and supertraits is not entirely
// obvious. Let me walk you (and myself) through an example.
//
Expand Down Expand Up @@ -1384,13 +1386,13 @@ impl ToPredicate<'tcx> for PredicateAtom<'tcx> {
#[inline(always)]
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
debug_assert!(!self.has_escaping_bound_vars(), "escaping bound vars for {:?}", self);
tcx.mk_predicate(ty::PredicateKind::Atom(self))
tcx.mk_predicate(PredicateKind::Atom(self))
}
}

impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<TraitRef<'tcx>> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
ty::PredicateAtom::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness)
PredicateAtom::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness)
.to_predicate(tcx)
}
}
Expand All @@ -1407,51 +1409,29 @@ impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitRef<'tcx>> {

impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitPredicate<'tcx>> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
if let Some(pred) = self.value.no_bound_vars() {
ty::PredicateAtom::Trait(pred, self.constness).to_predicate(tcx)
} else {
ty::PredicateKind::ForAll(
self.value.map_bound(|pred| ty::PredicateAtom::Trait(pred, self.constness)),
)
.to_predicate(tcx)
}
PredicateAtom::Trait(self.value.skip_binder(), self.constness)
.potentially_quantified(tcx, PredicateKind::ForAll)
}
}

impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
if let Some(outlives) = self.no_bound_vars() {
PredicateAtom::RegionOutlives(outlives).to_predicate(tcx)
} else {
ty::PredicateKind::ForAll(
self.map_bound(|outlives| PredicateAtom::RegionOutlives(outlives)),
)
.to_predicate(tcx)
}
PredicateAtom::RegionOutlives(self.skip_binder())
.potentially_quantified(tcx, PredicateKind::ForAll)
}
}

impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
if let Some(outlives) = self.no_bound_vars() {
PredicateAtom::TypeOutlives(outlives).to_predicate(tcx)
} else {
ty::PredicateKind::ForAll(
self.map_bound(|outlives| PredicateAtom::TypeOutlives(outlives)),
)
.to_predicate(tcx)
}
PredicateAtom::TypeOutlives(self.skip_binder())
.potentially_quantified(tcx, PredicateKind::ForAll)
}
}

impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
if let Some(proj) = self.no_bound_vars() {
PredicateAtom::Projection(proj).to_predicate(tcx)
} else {
ty::PredicateKind::ForAll(self.map_bound(|proj| PredicateAtom::Projection(proj)))
.to_predicate(tcx)
}
PredicateAtom::Projection(self.skip_binder())
.potentially_quantified(tcx, PredicateKind::ForAll)
}
}

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

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

#[inline]
pub fn caller_bounds(self) -> &'tcx List<ty::Predicate<'tcx>> {
pub fn caller_bounds(self) -> &'tcx List<Predicate<'tcx>> {
// mask out bottom bit
unsafe { &*((self.packed_data & (!1)) as *const _) }
}
Expand All @@ -1863,7 +1843,7 @@ impl<'tcx> ParamEnv<'tcx> {
/// Construct a trait environment with the given set of predicates.
#[inline]
pub fn new(
caller_bounds: &'tcx List<ty::Predicate<'tcx>>,
caller_bounds: &'tcx List<Predicate<'tcx>>,
reveal: Reveal,
def_id: Option<DefId>,
) -> Self {
Expand Down
15 changes: 7 additions & 8 deletions src/librustc_mir/monomorphize/polymorphize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,21 @@ fn mark_used_by_predicates<'tcx>(
let predicates = tcx.explicit_predicates_of(def_id);
debug!("mark_parameters_used_in_predicates: predicates_of={:?}", predicates);
for (predicate, _) in predicates.predicates {
match predicate.kind() {
ty::PredicateKind::Trait(predicate, ..) => {
let trait_ref = predicate.skip_binder().trait_ref;
match predicate.skip_binders() {
ty::PredicateAtom::Trait(predicate, ..) => {
let trait_ref = predicate.trait_ref;
if is_self_ty_used(unused_parameters, trait_ref.self_ty()) {
for ty in trait_ref.substs.types() {
debug!("unused_generic_params: (trait) ty={:?}", ty);
mark_ty(unused_parameters, ty);
}
}
}
ty::PredicateKind::Projection(predicate, ..) => {
let self_ty = predicate.skip_binder().projection_ty.self_ty();
ty::PredicateAtom::Projection(proj, ..) => {
let self_ty = proj.projection_ty.self_ty();
if is_self_ty_used(unused_parameters, self_ty) {
let ty = predicate.ty();
debug!("unused_generic_params: (projection) ty={:?}", ty);
mark_ty(unused_parameters, ty.skip_binder());
debug!("unused_generic_params: (projection ty={:?}", proj.ty);
mark_ty(unused_parameters, proj.ty);
}
}
_ => (),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trait_selection/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_data_structures::obligation_forest::ProcessResult;
use rustc_data_structures::obligation_forest::{DoCompleted, Error, ForestObligation};
use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
use rustc_errors::ErrorReported;
use rustc_infer::traits::{TraitObligation, TraitEngine, TraitEngineExt as _};
use rustc_infer::traits::{TraitEngine, TraitEngineExt as _, TraitObligation};
use rustc_middle::mir::interpret::ErrorHandled;
use rustc_middle::ty::error::ExpectedFound;
use rustc_middle::ty::ToPredicate;
Expand Down
7 changes: 2 additions & 5 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut type_params = FxHashMap::default();
let mut bound_spans = vec![];

let mut collect_type_param_suggestions = {
// We need to move `tcx` while only borrowing the rest,
// this is kind of ugly.
let mut collect_type_param_suggestions =
|self_ty: Ty<'tcx>, parent_pred: &ty::Predicate<'tcx>, obligation: &str| {
// We don't care about regions here, so it's fine to skip the binder here.
if let (ty::Param(_), ty::PredicateAtom::Trait(p, _)) =
Expand Down Expand Up @@ -601,8 +599,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
}
}
};
};
let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
let msg = format!(
"doesn't satisfy `{}`",
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1930,8 +1930,8 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
let re_root_empty = tcx.lifetimes.re_root_empty;
let predicate = ty::OutlivesPredicate(ty, re_root_empty);
predicates.push((
ty::PredicateKind::TypeOutlives(ty::Binder::bind(predicate))
.to_predicate(tcx),
ty::PredicateAtom::TypeOutlives(predicate)
.potentially_quantified(tcx, ty::PredicateKind::ForAll),
span,
));
}
Expand Down

0 comments on commit 833b1d8

Please sign in to comment.