Skip to content

Commit

Permalink
Address niko's nits
Browse files Browse the repository at this point in the history
  • Loading branch information
scalexm committed Mar 14, 2018
1 parent 1271f0b commit 04b228c
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 195 deletions.
70 changes: 35 additions & 35 deletions src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,9 +1294,9 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::WhereClauseAtom<
use traits::WhereClauseAtom::*;

mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
Implemented(ref trait_ref) => trait_ref.hash_stable(hcx, hasher),
ProjectionEq(ref projection) => projection.hash_stable(hcx, hasher),
match self {
Implemented(trait_ref) => trait_ref.hash_stable(hcx, hasher),
ProjectionEq(projection) => projection.hash_stable(hcx, hasher),
}
}
}
Expand All @@ -1308,54 +1308,59 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::DomainGoal<'tcx>
use traits::DomainGoal::*;

mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
Holds(ref where_clause) |
WellFormed(ref where_clause) |
FromEnv(ref where_clause) => where_clause.hash_stable(hcx, hasher),

WellFormedTy(ref ty) => ty.hash_stable(hcx, hasher),
FromEnvTy(ref ty) => ty.hash_stable(hcx, hasher),
RegionOutlives(ref predicate) => predicate.hash_stable(hcx, hasher),
TypeOutlives(ref predicate) => predicate.hash_stable(hcx, hasher),
match self {
Holds(where_clause) |
WellFormed(where_clause) |
FromEnv(where_clause) => where_clause.hash_stable(hcx, hasher),

WellFormedTy(ty) => ty.hash_stable(hcx, hasher),
FromEnvTy(ty) => ty.hash_stable(hcx, hasher),
RegionOutlives(predicate) => predicate.hash_stable(hcx, hasher),
TypeOutlives(predicate) => predicate.hash_stable(hcx, hasher),
}
}
}

impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::LeafGoal<'tcx> {
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::Goal<'tcx> {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use traits::LeafGoal::*;
use traits::Goal::*;

mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
DomainGoal(ref domain_goal) => domain_goal.hash_stable(hcx, hasher),
match self {
Implies(hypotheses, goal) => {
hypotheses.hash_stable(hcx, hasher);
goal.hash_stable(hcx, hasher);
},
And(goal1, goal2) => {
goal1.hash_stable(hcx, hasher);
goal2.hash_stable(hcx, hasher);
}
Not(goal) => goal.hash_stable(hcx, hasher),
DomainGoal(domain_goal) => domain_goal.hash_stable(hcx, hasher),
Quantified(quantifier, goal) => {
quantifier.hash_stable(hcx, hasher);
goal.hash_stable(hcx, hasher);
},
}
}
}

impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::Goal<'tcx> {
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::Clause<'tcx> {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
use traits::Goal::*;
use traits::Clause::*;

mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
Implies(ref hypotheses, ref goal) => {
match self {
Implies(hypotheses, goal) => {
hypotheses.hash_stable(hcx, hasher);
goal.hash_stable(hcx, hasher);
},
And(ref goal1, ref goal2) => {
goal1.hash_stable(hcx, hasher);
goal2.hash_stable(hcx, hasher);
}
Not(ref goal) => goal.hash_stable(hcx, hasher),
Leaf(ref leaf_goal) => leaf_goal.hash_stable(hcx, hasher),
Quantified(quantifier, ref goal) => {
quantifier.hash_stable(hcx, hasher);
goal.hash_stable(hcx, hasher);
},
DomainGoal(domain_goal) => domain_goal.hash_stable(hcx, hasher),
ForAll(clause) => clause.hash_stable(hcx, hasher),
}
}
}
Expand All @@ -1364,8 +1369,3 @@ impl_stable_hash_for!(enum traits::QuantifierKind {
Universal,
Existential
});

impl_stable_hash_for!(struct traits::ProgramClause<'tcx> {
consequence,
conditions
});
59 changes: 40 additions & 19 deletions src/librustc/traits/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use hir::{self, ImplPolarity};
use hir::def_id::DefId;
use hir::intravisit::{self, NestedVisitorMap, Visitor};
use ty::{self, PolyTraitPredicate, TraitPredicate, PolyProjectionPredicate, TyCtxt, Predicate};
use super::{DomainGoal, ProgramClause, WhereClauseAtom};
use ty::{self, TyCtxt};
use super::{QuantifierKind, Goal, DomainGoal, Clause, WhereClauseAtom};
use rustc_data_structures::sync::Lrc;
use syntax::ast;

Expand All @@ -26,13 +26,13 @@ impl<T, U> Lower<Vec<U>> for Vec<T> where T: Lower<U> {
}
}

impl<'tcx> Lower<WhereClauseAtom<'tcx>> for PolyTraitPredicate<'tcx> {
impl<'tcx> Lower<WhereClauseAtom<'tcx>> for ty::TraitPredicate<'tcx> {
fn lower(&self) -> WhereClauseAtom<'tcx> {
WhereClauseAtom::Implemented(*self)
}
}

impl<'tcx> Lower<WhereClauseAtom<'tcx>> for PolyProjectionPredicate<'tcx> {
impl<'tcx> Lower<WhereClauseAtom<'tcx>> for ty::ProjectionPredicate<'tcx> {
fn lower(&self) -> WhereClauseAtom<'tcx> {
WhereClauseAtom::ProjectionEq(*self)
}
Expand All @@ -44,27 +44,52 @@ impl<'tcx, T> Lower<DomainGoal<'tcx>> for T where T: Lower<WhereClauseAtom<'tcx>
}
}

impl<'tcx> Lower<DomainGoal<'tcx>> for Predicate<'tcx> {
impl<'tcx> Lower<DomainGoal<'tcx>> for ty::RegionOutlivesPredicate<'tcx> {
fn lower(&self) -> DomainGoal<'tcx> {
use self::Predicate::*;
DomainGoal::RegionOutlives(*self)
}
}

impl<'tcx> Lower<DomainGoal<'tcx>> for ty::TypeOutlivesPredicate<'tcx> {
fn lower(&self) -> DomainGoal<'tcx> {
DomainGoal::TypeOutlives(*self)
}
}

impl<'tcx, T> Lower<Goal<'tcx>> for ty::Binder<T>
where T: Lower<DomainGoal<'tcx>> + ty::fold::TypeFoldable<'tcx> + Copy
{
fn lower(&self) -> Goal<'tcx> {
match self.no_late_bound_regions() {
Some(p) => p.lower().into(),
None => Goal::Quantified(
QuantifierKind::Universal,
Box::new(self.map_bound(|p| p.lower().into()))
),
}
}
}

match *self {
impl<'tcx> Lower<Goal<'tcx>> for ty::Predicate<'tcx> {
fn lower(&self) -> Goal<'tcx> {
use ty::Predicate::*;

match self {
Trait(predicate) => predicate.lower(),
RegionOutlives(predicate) => DomainGoal::RegionOutlives(predicate),
TypeOutlives(predicate) => DomainGoal::TypeOutlives(predicate),
RegionOutlives(predicate) => predicate.lower(),
TypeOutlives(predicate) => predicate.lower(),
Projection(predicate) => predicate.lower(),
WellFormed(ty) => DomainGoal::WellFormedTy(ty),
WellFormed(ty) => DomainGoal::WellFormedTy(*ty).into(),
ObjectSafe(..) |
ClosureKind(..) |
Subtype(..) |
ConstEvaluatable(..) => unimplemented!(),

}
}
}

pub fn program_clauses_for<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
-> Lrc<Vec<ProgramClause<'tcx>>>
-> Lrc<Vec<Clause<'tcx>>>
{
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
let item = tcx.hir.expect_item(node_id);
Expand All @@ -75,21 +100,17 @@ pub fn program_clauses_for<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
}

fn program_clauses_for_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
-> Lrc<Vec<ProgramClause<'tcx>>>
-> Lrc<Vec<Clause<'tcx>>>
{
if let ImplPolarity::Negative = tcx.impl_polarity(def_id) {
return Lrc::new(vec![]);
}

let trait_ref = tcx.impl_trait_ref(def_id).unwrap();
let trait_ref = ty::Binder(TraitPredicate { trait_ref }).lower();
let trait_ref = ty::TraitPredicate { trait_ref }.lower();
let where_clauses = tcx.predicates_of(def_id).predicates.lower();

let clause = ProgramClause {
consequence: trait_ref,
conditions: where_clauses.into_iter().map(|wc| wc.into()).collect(),
};

let clause = Clause::Implies(where_clauses, trait_ref);
Lrc::new(vec![clause])
}

Expand Down
32 changes: 17 additions & 15 deletions src/librustc/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ pub type TraitObligations<'tcx> = Vec<TraitObligation<'tcx>>;

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum WhereClauseAtom<'tcx> {
Implemented(ty::PolyTraitPredicate<'tcx>),
ProjectionEq(ty::PolyProjectionPredicate<'tcx>),
Implemented(ty::TraitPredicate<'tcx>),
ProjectionEq(ty::ProjectionPredicate<'tcx>),
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
Expand All @@ -259,13 +259,8 @@ pub enum DomainGoal<'tcx> {
FromEnv(WhereClauseAtom<'tcx>),
WellFormedTy(Ty<'tcx>),
FromEnvTy(Ty<'tcx>),
RegionOutlives(ty::PolyRegionOutlivesPredicate<'tcx>),
TypeOutlives(ty::PolyTypeOutlivesPredicate<'tcx>),
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum LeafGoal<'tcx> {
DomainGoal(DomainGoal<'tcx>),
RegionOutlives(ty::RegionOutlivesPredicate<'tcx>),
TypeOutlives(ty::TypeOutlivesPredicate<'tcx>),
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
Expand All @@ -276,23 +271,30 @@ pub enum QuantifierKind {

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum Goal<'tcx> {
Implies(Vec<DomainGoal<'tcx>>, Box<Goal<'tcx>>),
Implies(Vec<Clause<'tcx>>, Box<Goal<'tcx>>),
And(Box<Goal<'tcx>>, Box<Goal<'tcx>>),
Not(Box<Goal<'tcx>>),
Leaf(LeafGoal<'tcx>),
DomainGoal(DomainGoal<'tcx>),
Quantified(QuantifierKind, Box<ty::Binder<Goal<'tcx>>>)
}

impl<'tcx> From<DomainGoal<'tcx>> for Goal<'tcx> {
fn from(domain_goal: DomainGoal<'tcx>) -> Self {
Goal::Leaf(LeafGoal::DomainGoal(domain_goal))
Goal::DomainGoal(domain_goal)
}
}

impl<'tcx> From<DomainGoal<'tcx>> for Clause<'tcx> {
fn from(domain_goal: DomainGoal<'tcx>) -> Self {
Clause::DomainGoal(domain_goal)
}
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct ProgramClause<'tcx> {
pub consequence: DomainGoal<'tcx>,
pub conditions: Vec<Goal<'tcx>>,
pub enum Clause<'tcx> {
Implies(Vec<Goal<'tcx>>, DomainGoal<'tcx>),
DomainGoal(DomainGoal<'tcx>),
ForAll(Box<ty::Binder<Clause<'tcx>>>),
}

pub fn dump_program_clauses<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
Expand Down
Loading

0 comments on commit 04b228c

Please sign in to comment.