Skip to content

Commit 23ec213

Browse files
committed
Add parse rules for GATs
1 parent e490665 commit 23ec213

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

chalk-parse/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ pub struct TraitFlags {
5454
pub struct AssocTyDefn {
5555
pub name: Identifier,
5656
pub parameter_kinds: Vec<ParameterKind>,
57+
pub bound: Option<TraitRef>,
58+
pub where_clauses: Vec<QuantifiedWhereClause>,
5759
}
5860

5961
pub enum ParameterKind {

chalk-parse/src/parser.lalrpop

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,26 @@ TraitDefn: TraitDefn = {
7272
};
7373

7474
AssocTyDefn: AssocTyDefn = {
75-
"type" <name:Id> <p:Angle<ParameterKind>> ";" => AssocTyDefn {
76-
name: name,
77-
parameter_kinds: p
75+
"type" <name:Id> <p:Angle<ParameterKind>> <b:(":" <Id> <Angle<Parameter>>)?>
76+
<w:QuantifiedWhereClauses> ";" =>
77+
{
78+
let bound = match b {
79+
Some((t, a)) => {
80+
let mut args = vec![Parameter::Ty(Ty::Id{ name })];
81+
args.extend(a);
82+
Some(TraitRef {
83+
trait_name: t,
84+
args: args,
85+
})
86+
}
87+
None => None
88+
};
89+
AssocTyDefn {
90+
name: name,
91+
parameter_kinds: p,
92+
where_clauses: w,
93+
bound,
94+
}
7895
}
7996
};
8097

src/ir/lowering/test.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,34 @@ fn check_parameter_kinds() {
302302
}
303303
}
304304
}
305+
306+
#[test]
307+
fn gat_parse() {
308+
let program = Arc::new(
309+
parse_and_lower_program(
310+
"
311+
trait Sized {}
312+
313+
trait Foo {
314+
type Item<'a, T>: Clone where Self: Sized;
315+
}
316+
317+
trait Bar {
318+
type Item<'a, T> where Self: Sized;
319+
}
320+
321+
trait Baz {
322+
type Item<'a, T>: Clone;
323+
}
324+
325+
trait Quux {
326+
type Item<'a, T>;
327+
}
328+
",
329+
SolverChoice::slg()
330+
).unwrap()
331+
);
332+
tls::set_current_program(&program, || {
333+
println!("{:#?}", program.associated_ty_data.values());
334+
});
335+
}

0 commit comments

Comments
 (0)