Skip to content
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

Implement GATs, step 1 #118

Merged
merged 5 commits into from
May 10, 2018
Merged
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
25 changes: 24 additions & 1 deletion chalk-parse/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub struct TraitFlags {
pub struct AssocTyDefn {
pub name: Identifier,
pub parameter_kinds: Vec<ParameterKind>,
pub bounds: Vec<InlineBound>,
pub where_clauses: Vec<QuantifiedWhereClause>,
}

pub enum ParameterKind {
Expand All @@ -66,6 +68,28 @@ pub enum Parameter {
Lifetime(Lifetime),
}

/// An inline bound, e.g. `: Foo<K>` in `impl<K, T: Foo<K>> SomeType<T>`.
pub enum InlineBound {
TraitBound(TraitBound),
ProjectionEqBound(ProjectionEqBound),
}

/// Represents a trait bound on e.g. a type or type parameter.
/// Does not know anything about what it's binding.
pub struct TraitBound {
pub trait_name: Identifier,
pub args_no_self: Vec<Parameter>,
}

/// Represents a projection equality bound on e.g. a type or type parameter.
/// Does not know anything about what it's binding.
pub struct ProjectionEqBound {
pub trait_bound: TraitBound,
pub name: Identifier,
pub parameters: Vec<Parameter>,
pub value: Ty,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Kind {
Ty,
Expand Down Expand Up @@ -115,7 +139,6 @@ pub struct Impl {
pub struct AssocTyValue {
pub name: Identifier,
pub parameter_kinds: Vec<ParameterKind>,
pub where_clauses: Vec<WhereClause>,
pub value: Ty,
}

Expand Down
53 changes: 43 additions & 10 deletions chalk-parse/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,43 @@ TraitDefn: TraitDefn = {
};

AssocTyDefn: AssocTyDefn = {
"type" <name:Id> <p:Angle<ParameterKind>> ";" => AssocTyDefn {
name: name,
parameter_kinds: p
"type" <name:Id> <p:Angle<ParameterKind>> <b:(":" <Plus<InlineBound>>)?>
<w:QuantifiedWhereClauses> ";" =>
{
AssocTyDefn {
name: name,
parameter_kinds: p,
where_clauses: w,
bounds: b.unwrap_or(vec![]),
}
}
};

InlineBound: InlineBound = {
TraitBound => InlineBound::TraitBound(<>),
ProjectionEqBound => InlineBound::ProjectionEqBound(<>),
};

TraitBound: TraitBound = {
<t:Id> <a:Angle<Parameter>> => {
TraitBound {
trait_name: t,
args_no_self: a,
}
}
};

ProjectionEqBound: ProjectionEqBound = {
<t:Id> "<" <a:(<Comma<Parameter>> ",")?> <name:Id> <a2:Angle<Parameter>>
"=" <ty:Ty> ">" => ProjectionEqBound
{
trait_bound: TraitBound {
trait_name: t,
args_no_self: a.unwrap_or(vec![]),
},
name,
parameters: a2,
value: ty,
}
};

Expand Down Expand Up @@ -102,11 +136,10 @@ ParameterKind: ParameterKind = {
};

AssocTyValue: AssocTyValue = {
"type" <n:Id> <a:Angle<ParameterKind>> <wc:WhereClauses> "=" <v:Ty> ";" => AssocTyValue {
"type" <n:Id> <a:Angle<ParameterKind>> "=" <v:Ty> ";" => AssocTyValue {
name: n,
parameter_kinds: a,
value: v,
where_clauses: wc,
},
};

Expand Down Expand Up @@ -201,11 +234,6 @@ InlineClause: Clause = {
}
};

WhereClauses: Vec<WhereClause> = {
"where" <Comma<WhereClause>>,
() => vec![],
};

QuantifiedWhereClauses: Vec<QuantifiedWhereClause> = {
"where" <Comma<QuantifiedWhereClause>>,
() => vec![],
Expand Down Expand Up @@ -290,6 +318,11 @@ SemiColon<T>: Vec<T> = {
<Separator<";", T>>
};

#[inline]
Plus<T>: Vec<T> = {
<Separator<"+", T>>
};

Angle<T>: Vec<T> = {
"<" <Comma<T>> ">",
() => vec![],
Expand Down
2 changes: 1 addition & 1 deletion src/fold/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ struct_fold!(AssociatedTyValue {
associated_ty_id,
value,
});
struct_fold!(AssociatedTyValueBound { ty, where_clauses });
struct_fold!(AssociatedTyValueBound { ty });
struct_fold!(Environment { clauses });
struct_fold!(InEnvironment[F] { environment, goal } where F: Fold<Result = F>);
struct_fold!(EqGoal { a, b });
Expand Down
17 changes: 10 additions & 7 deletions src/ir/lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<'k> Env<'k> {
/// Introduces new parameters, shifting the indices of existing
/// parameters to accommodate them. The indices of the new binders
/// will be assigned in order as they are iterated.
fn introduce<I>(&self, binders: I) -> Self
fn introduce<I>(&self, binders: I) -> Result<Self>
where
I: IntoIterator<Item = ir::ParameterKind<ir::Identifier>>,
I::IntoIter: ExactSizeIterator,
Expand All @@ -82,10 +82,13 @@ impl<'k> Env<'k> {
.map(|(&k, &v)| (k, v + len))
.chain(binders)
.collect();
Env {
if parameter_map.len() != self.parameter_map.len() + len {
bail!("duplicate parameters");
}
Ok(Env {
parameter_map,
..*self
}
})
}

fn in_binders<I, T, OP>(&self, binders: I, op: OP) -> Result<ir::Binders<T>>
Expand All @@ -95,7 +98,7 @@ impl<'k> Env<'k> {
OP: FnOnce(&Self) -> Result<T>,
{
let binders: Vec<_> = binders.into_iter().collect();
let env = self.introduce(binders.iter().cloned());
let env = self.introduce(binders.iter().cloned())?;
Ok(ir::Binders {
binders: binders.anonymize(),
value: op(&env)?,
Expand Down Expand Up @@ -178,6 +181,7 @@ impl LowerProgram for Program {

let mut parameter_kinds = defn.all_parameters();
parameter_kinds.extend(d.all_parameters());
let env = empty_env.introduce(parameter_kinds.clone())?;

associated_ty_data.insert(
info.id,
Expand All @@ -186,7 +190,7 @@ impl LowerProgram for Program {
id: info.id,
name: defn.name.str,
parameter_kinds: parameter_kinds,
where_clauses: vec![],
where_clauses: defn.where_clauses.lower(&env)?,
},
);
}
Expand Down Expand Up @@ -757,7 +761,7 @@ impl LowerTy for Ty {
lifetime_names
.iter()
.map(|id| ir::ParameterKind::Lifetime(id.str)),
);
)?;

let ty = ty.lower(&quantified_env)?;
let quantified_ty = ir::QuantifiedTy {
Expand Down Expand Up @@ -872,7 +876,6 @@ impl LowerAssocTyValue for AssocTyValue {
let value = env.in_binders(self.all_parameters(), |env| {
Ok(ir::AssociatedTyValueBound {
ty: self.value.lower(env)?,
where_clauses: self.where_clauses.lower(env)?,
})
})?;
Ok(ir::AssociatedTyValue {
Expand Down
71 changes: 69 additions & 2 deletions src/ir/lowering/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ fn atc_accounting() {
AssociatedTyValue {
associated_ty_id: (Iterable::Iter),
value: for<lifetime> AssociatedTyValueBound {
ty: Iter<'?0, ?1>,
where_clauses: []
ty: Iter<'?0, ?1>
}
}
],
Expand Down Expand Up @@ -303,3 +302,71 @@ fn check_parameter_kinds() {
}
}
}

#[test]
fn gat_parse() {
lowering_success! {
program {
trait Sized {}

trait Foo {
type Item<'a, T>: Sized + Clone where Self: Sized;
}

trait Bar {
type Item<'a, T> where Self: Sized;
}

struct Container<T> {
value: T
}

trait Baz {
type Item<'a, 'b, T>: Foo<Item<'b, T> = Container<T>> + Clone;
}

trait Quux {
type Item<'a, T>;
}
}
}

lowering_error! {
program {
trait Sized { }

trait Foo {
type Item where K: Sized;
}
}

error_msg {
"invalid type name `K`"
}
}
}

#[test]
fn duplicate_parameters() {
lowering_error! {
program {
trait Foo<T, T> { }
}

error_msg {
"duplicate parameters"
}
}

lowering_error! {
program {
trait Foo<T> {
type Item<T>;
}
}

error_msg {
"duplicate parameters"
}
}
}
8 changes: 3 additions & 5 deletions src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,10 @@ pub struct AssociatedTyDatum {
/// but possibly including more.
crate parameter_kinds: Vec<ParameterKind<Identifier>>,

// FIXME: inline bounds on the associated ty need to be implemented

/// Where clauses that must hold for the projection be well-formed.
crate where_clauses: Vec<DomainGoal>,
crate where_clauses: Vec<QuantifiedDomainGoal>,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand All @@ -289,10 +291,6 @@ pub struct AssociatedTyValue {
pub struct AssociatedTyValueBound {
/// Type that we normalize to. The X in `type Foo<'a> = X`.
crate ty: Ty,

/// Where-clauses that must hold for projection to be valid. The
/// WC in `type Foo<'a> = X where WC`.
crate where_clauses: Vec<DomainGoal>,
}

#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
#![feature(catch_expr)]
#![feature(crate_in_paths)]
#![feature(crate_visibility_modifier)]
#![feature(dyn_trait)]
#![feature(in_band_lifetimes)]
#![feature(macro_at_most_once_rep)]
#![feature(macro_vis_matcher)]
#![feature(specialization)]
#![feature(step_trait)]
#![feature(underscore_lifetimes)]

extern crate chalk_parse;
#[macro_use]
Expand Down
5 changes: 1 addition & 4 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@ impl ir::AssociatedTyValue {
.trait_ref
.trait_ref()
.up_shift(self.value.len());
let conditions: Vec<ir::Goal> = Some(impl_trait_ref.clone().cast())
.into_iter()
.chain(self.value.value.where_clauses.clone().cast())
.collect();
let conditions: Vec<ir::Goal> = vec![impl_trait_ref.clone().cast()];

// Bound parameters + `Self` type of the trait-ref
let parameters: Vec<_> = {
Expand Down