Skip to content

Merge WhereClause and WhereClauseGoal into a single enum #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 11 additions & 39 deletions src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,88 +24,60 @@ macro_rules! reflexive_impl {

reflexive_impl!(TraitRef);
reflexive_impl!(WhereClause);
reflexive_impl!(WhereClauseGoal);

impl Cast<WhereClause> for TraitRef {
fn cast(self) -> WhereClause {
WhereClause::Implemented(self)
}
}

impl Cast<WhereClauseGoal> for TraitRef {
fn cast(self) -> WhereClauseGoal {
WhereClauseGoal::Implemented(self)
}
}

impl Cast<WhereClause> for Normalize {
fn cast(self) -> WhereClause {
WhereClause::Normalize(self)
}
}

impl Cast<WhereClauseGoal> for Normalize {
fn cast(self) -> WhereClauseGoal {
WhereClauseGoal::Normalize(self)
}
}

impl Cast<WhereClauseGoal> for WellFormed {
fn cast(self) -> WhereClauseGoal {
WhereClauseGoal::WellFormed(self)
impl Cast<WhereClause> for WellFormed {
fn cast(self) -> WhereClause {
WhereClause::WellFormed(self)
}
}

impl Cast<Goal> for WellFormed {
fn cast(self) -> Goal {
let wcg: WhereClauseGoal = self.cast();
let wcg: WhereClause = self.cast();
wcg.cast()
}
}

impl Cast<Goal> for Normalize {
fn cast(self) -> Goal {
let wcg: WhereClauseGoal = self.cast();
let wcg: WhereClause = self.cast();
wcg.cast()
}
}

impl Cast<WhereClauseGoal> for WhereClause {
fn cast(self) -> WhereClauseGoal {
match self {
WhereClause::Implemented(a) => a.cast(),
WhereClause::Normalize(a) => a.cast(),
}
}
}

impl Cast<Goal> for TraitRef {
fn cast(self) -> Goal {
Goal::Leaf(self.cast())
}
}

impl Cast<Goal> for WhereClause {
fn cast(self) -> Goal {
Goal::Leaf(self.cast())
}
}

impl Cast<Goal> for WhereClauseGoal {
fn cast(self) -> Goal {
Goal::Leaf(self)
}
}

impl Cast<WhereClauseGoal> for Unify<Ty> {
fn cast(self) -> WhereClauseGoal {
WhereClauseGoal::UnifyTys(self)
impl Cast<WhereClause> for Unify<Ty> {
fn cast(self) -> WhereClause {
WhereClause::UnifyTys(self)
}
}

impl Cast<WhereClauseGoal> for Unify<Lifetime> {
fn cast(self) -> WhereClauseGoal {
WhereClauseGoal::UnifyLifetimes(self)
impl Cast<WhereClause> for Unify<Lifetime> {
fn cast(self) -> WhereClause {
WhereClause::UnifyLifetimes(self)
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/fold/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,8 @@ macro_rules! enum_fold {
}

enum_fold!(ParameterKind[T,L] { Ty(a), Lifetime(a) } where T: Fold, L: Fold);
enum_fold!(WhereClause[] { Implemented(a), Normalize(a) });
enum_fold!(WellFormed[] { Ty(a), TraitRef(a) });
enum_fold!(WhereClauseGoal[] { Implemented(a), Normalize(a), UnifyTys(a),
enum_fold!(WhereClause[] { Implemented(a), Normalize(a), UnifyTys(a),
UnifyLifetimes(a), WellFormed(a) });
enum_fold!(Constraint[] { LifetimeEq(a, b) });
enum_fold!(Goal[] { Quantified(qkind, subgoal), Implies(wc, subgoal), And(g1, g2), Leaf(wc) });
Expand Down
21 changes: 3 additions & 18 deletions src/ir/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,9 @@ impl Debug for WhereClause {
n.trait_id,
Angle(&n.parameters[1..]))
}
}
}
}

impl Debug for WhereClauseGoal {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
match *self {
WhereClauseGoal::Normalize(ref n) => write!(fmt, "{:?}", n),
WhereClauseGoal::Implemented(ref n) => {
write!(fmt,
"{:?}: {:?}{:?}",
n.parameters[0],
n.trait_id,
Angle(&n.parameters[1..]))
}
WhereClauseGoal::UnifyTys(ref n) => write!(fmt, "{:?}", n),
WhereClauseGoal::UnifyLifetimes(ref n) => write!(fmt, "{:?}", n),
WhereClauseGoal::WellFormed(ref n) => write!(fmt, "{:?}", n),
WhereClause::UnifyTys(ref n) => write!(fmt, "{:?}", n),
WhereClause::UnifyLifetimes(ref n) => write!(fmt, "{:?}", n),
WhereClause::WellFormed(ref n) => write!(fmt, "{:?}", n),
}
}
}
Expand Down
11 changes: 3 additions & 8 deletions src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl Environment {
};
push_clause(trait_ref.cast());
}
_ => (),
}
}

Expand Down Expand Up @@ -370,12 +371,6 @@ pub struct TraitRef {
pub enum WhereClause {
Implemented(TraitRef),
Normalize(Normalize),
}

#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum WhereClauseGoal {
Implemented(TraitRef),
Normalize(Normalize),
UnifyTys(Unify<Ty>),
UnifyLifetimes(Unify<Lifetime>),
WellFormed(WellFormed),
Expand Down Expand Up @@ -436,7 +431,7 @@ pub struct ProgramClause {
/// Represents one clause of the form `consequence :- conditions`.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ProgramClauseImplication {
pub consequence: WhereClauseGoal,
pub consequence: WhereClause,
pub conditions: Vec<Goal>,
}

Expand Down Expand Up @@ -480,7 +475,7 @@ pub enum Goal {
Quantified(QuantifierKind, Binders<Box<Goal>>),
Implies(Vec<WhereClause>, Box<Goal>),
And(Box<Goal>, Box<Goal>),
Leaf(WhereClauseGoal),
Leaf(WhereClause),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
Expand Down
32 changes: 5 additions & 27 deletions src/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,48 +362,25 @@ trait LowerWhereClause<T> {
fn lower(&self, env: &Env) -> Result<T>;
}

/// Lowers a where-clause in the context of a clause; this is limited
/// to the kinds of where-clauses users can actually type in Rust.
impl LowerWhereClause<ir::WhereClause> for WhereClause {
fn lower(&self, env: &Env) -> Result<ir::WhereClause> {
Ok(match *self {
WhereClause::Implemented { ref trait_ref } => {
ir::WhereClause::Implemented(trait_ref.lower(env)?)
trait_ref.lower(env)?.cast()
}
WhereClause::ProjectionEq { ref projection, ref ty } => {
ir::WhereClause::Normalize(ir::Normalize {
ir::Normalize {
projection: projection.lower(env)?,
ty: ty.lower(env)?,
})
}
WhereClause::TyWellFormed { .. } |
WhereClause::TraitRefWellFormed { .. } |
WhereClause::UnifyTys { .. } |
WhereClause::UnifyLifetimes { .. } => {
bail!("this form of where-clause not allowed here")
}
})
}
}

/// Lowers a where-clause in the context of a goal; this is richer in
/// terms of the legal sorts of where-clauses that can appear, because
/// it includes all the sorts of things that the compiler must verify.
impl LowerWhereClause<ir::WhereClauseGoal> for WhereClause {
fn lower(&self, env: &Env) -> Result<ir::WhereClauseGoal> {
Ok(match *self {
WhereClause::Implemented { .. } |
WhereClause::ProjectionEq { .. } => {
let wc: ir::WhereClause = self.lower(env)?;
wc.cast()
}.cast()
}
WhereClause::TyWellFormed { ref ty } => {
ir::WellFormed::Ty(ty.lower(env)?).cast()
}
WhereClause::TraitRefWellFormed { ref trait_ref } => {
ir::WellFormed::TraitRef(trait_ref.lower(env)?).cast()
}
WhereClause::UnifyTys { ref a, ref b} => {
WhereClause::UnifyTys { ref a, ref b } => {
ir::Unify {
a: a.lower(env)?,
b: b.lower(env)?,
Expand Down Expand Up @@ -820,6 +797,7 @@ impl ir::AssociatedTyValue {
// 1. require that the trait is implemented
// 2. any where-clauses from the `type` declaration in the impl
let impl_trait_ref = impl_datum.binders.value.trait_ref.up_shift(self.value.len());

let conditions: Vec<ir::Goal> =
Some(impl_trait_ref.clone().cast())
.into_iter()
Expand Down
8 changes: 4 additions & 4 deletions src/solve/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use zip::Zip;
pub struct Fulfill<'s> {
solver: &'s mut Solver,
infer: InferenceTable,
obligations: Vec<InEnvironment<WhereClauseGoal>>,
obligations: Vec<InEnvironment<WhereClause>>,
constraints: HashSet<InEnvironment<Constraint>>,
}

Expand Down Expand Up @@ -79,13 +79,13 @@ impl<'s> Fulfill<'s> {
/// Adds the given where-clauses to the internal list of
/// obligations that must be solved.
pub fn extend<WC>(&mut self, wc: WC)
where WC: IntoIterator<Item=InEnvironment<WhereClauseGoal>>
where WC: IntoIterator<Item=InEnvironment<WhereClause>>
{
self.obligations.extend(wc);
}

/// Return current list of pending obligations; used for unit testing primarily
pub fn pending_obligations(&self) -> &[InEnvironment<WhereClauseGoal>] {
pub fn pending_obligations(&self) -> &[InEnvironment<WhereClause>] {
&self.obligations
}

Expand Down Expand Up @@ -210,7 +210,7 @@ impl<'s> Fulfill<'s> {
}

fn solve_one(&mut self,
wc: &InEnvironment<WhereClauseGoal>,
wc: &InEnvironment<WhereClause>,
inference_progress: &mut bool)
-> Result<Successful> {
debug!("fulfill::solve_one(wc={:?})", wc);
Expand Down
4 changes: 2 additions & 2 deletions src/solve/infer/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ struct Unifier<'t> {
table: &'t mut InferenceTable,
environment: &'t Arc<Environment>,
snapshot: InferenceSnapshot,
goals: Vec<InEnvironment<WhereClauseGoal>>,
goals: Vec<InEnvironment<WhereClause>>,
constraints: Vec<InEnvironment<Constraint>>,
}

#[derive(Debug)]
pub struct UnificationResult {
pub goals: Vec<InEnvironment<WhereClauseGoal>>,
pub goals: Vec<InEnvironment<WhereClause>>,
pub constraints: Vec<InEnvironment<Constraint>>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/solve/match_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum Technique<'t> {
}

impl<'s, G> MatchAny<'s, G>
where G: Cast<WhereClause> + Cast<WhereClauseGoal> + Clone + Hash + Eq + Fold<Result = G>
where G: Cast<WhereClause> + Clone + Hash + Eq + Fold<Result = G>
{
pub fn new(solver: &'s mut Solver, env_goal: &'s Query<InEnvironment<G>>) -> Self {
MatchAny {
Expand Down
2 changes: 1 addition & 1 deletion src/solve/match_program_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct MatchProgramClause<'s, G: 's> {
}

impl<'s, G> MatchProgramClause<'s, G>
where G: Clone + Cast<WhereClauseGoal> + Fold<Result = G>
where G: Clone + Cast<WhereClause> + Fold<Result = G>
{
pub fn new(solver: &'s mut Solver,
q: &'s Query<InEnvironment<G>>,
Expand Down
1 change: 1 addition & 0 deletions src/solve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod match_clause;
pub mod match_program_clause;
pub mod normalize;
pub mod normalize_application;
pub mod well_formed;
pub mod prove;
pub mod solver;
pub mod unify;
Expand Down
4 changes: 2 additions & 2 deletions src/solve/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use solve::Solution;

pub struct Prove<'s> {
fulfill: Fulfill<'s>,
goals: Vec<InEnvironment<WhereClauseGoal>>,
goals: Vec<InEnvironment<WhereClause>>,
}

impl<'s> Prove<'s> {
Expand All @@ -21,7 +21,7 @@ impl<'s> Prove<'s> {
prove
}

pub fn solve(mut self) -> Result<Solution<Vec<WhereClauseGoal>>> {
pub fn solve(mut self) -> Result<Solution<Vec<WhereClause>>> {
let successful = self.fulfill.solve_all()?;
let goals: Vec<_> = self.goals.into_iter().map(|g| g.goal).collect();
let refined_goal = self.fulfill.refine_goal(goals);
Expand Down
Loading