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 3 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
8 changes: 7 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<TraitBound>,
pub where_clauses: Vec<QuantifiedWhereClause>,
}

pub enum ParameterKind {
Expand Down Expand Up @@ -115,7 +117,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 Expand Up @@ -176,6 +177,11 @@ impl PolarizedTraitRef {
}
}

pub struct TraitBound {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful to add a comment here explaining what this represents and how it is used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

pub trait_name: Identifier,
pub args_no_self: Vec<Parameter>,
}

#[derive(Copy, Clone, Debug)]
pub struct Identifier {
pub str: InternedString,
Expand Down
34 changes: 24 additions & 10 deletions chalk-parse/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,24 @@ TraitDefn: TraitDefn = {
};

AssocTyDefn: AssocTyDefn = {
"type" <name:Id> <p:Angle<ParameterKind>> ";" => AssocTyDefn {
name: name,
parameter_kinds: p
"type" <name:Id> <p:Angle<ParameterKind>> <b:(":" <Plus<TraitBound>>)?>
<w:QuantifiedWhereClauses> ";" =>
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so currently we do not handle at all the + separator which expresses multiple bounds as in where T: Clone + Sized. This is not a problem for e.g. where clauses on traits because you can still write where T: Clone, T: Sized but for associated types since there is no other way to express these multiple bounds, we need to be able to parse them with the + acting as a separator.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

AssocTyDefn {
name: name,
parameter_kinds: p,
where_clauses: w,
bounds: b.unwrap_or(vec![]),
}
}
};

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

Expand Down Expand Up @@ -102,11 +117,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 +215,6 @@ InlineClause: Clause = {
}
};

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

QuantifiedWhereClauses: Vec<QuantifiedWhereClause> = {
"where" <Comma<QuantifiedWhereClause>>,
() => vec![],
Expand Down Expand Up @@ -290,6 +299,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
1 change: 0 additions & 1 deletion src/ir/lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,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
34 changes: 32 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,34 @@ fn check_parameter_kinds() {
}
}
}

#[test]
fn gat_parse() {
let program = Arc::new(
parse_and_lower_program(
"
trait Sized {}

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

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

trait Baz {
type Item<'a, T>: Clone;
}

trait Quux {
type Item<'a, T>;
}
",
SolverChoice::slg()
).unwrap()
);
tls::set_current_program(&program, || {
println!("{:#?}", program.associated_ty_data.values());
});
}
4 changes: 0 additions & 4 deletions src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,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
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