Skip to content

Commit d045b04

Browse files
authored
Add Generic Associated Types support (#124)
* tests: put tests for GAT * feat: support GAT * tests: add normal type parameter to generic associated type * tests: add checks for type binding * feat: support type binding to have type arguments * chore: regenerate parser * chore: fix formatting
1 parent e06d078 commit d045b04

File tree

5 files changed

+47037
-46451
lines changed

5 files changed

+47037
-46451
lines changed

corpus/declarations.txt

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,89 @@ pub trait Graph {
11281128
(associated_type (type_identifier) (trait_bounds (scoped_type_identifier (identifier) (type_identifier))))
11291129
(associated_type (type_identifier)))))
11301130

1131+
============================================
1132+
Generic Associated Types
1133+
============================================
1134+
1135+
pub trait Database {
1136+
type F<'a, D>: Future<Output = D> + 'a;
1137+
}
1138+
1139+
impl Database for Foo {
1140+
type F<'a, D> = DatabaseFuture<'a, D>;
1141+
}
1142+
1143+
fn use_database1<D: Database<F<'a, TD> = F>>() {}
1144+
1145+
fn use_database2<D>()
1146+
where
1147+
D: Database<F<'a, TD> = F>,
1148+
{}
1149+
1150+
---
1151+
1152+
(source_file
1153+
(trait_item
1154+
(visibility_modifier)
1155+
(type_identifier)
1156+
(declaration_list
1157+
(associated_type
1158+
(type_identifier)
1159+
(type_parameters (lifetime (identifier)) (type_identifier))
1160+
(trait_bounds
1161+
(generic_type
1162+
(type_identifier)
1163+
(type_arguments (type_binding (type_identifier) (type_identifier))))
1164+
(lifetime (identifier))))))
1165+
1166+
(impl_item
1167+
(type_identifier)
1168+
(type_identifier)
1169+
(declaration_list
1170+
(type_item
1171+
(type_identifier)
1172+
(type_parameters (lifetime (identifier)) (type_identifier))
1173+
(generic_type
1174+
(type_identifier)
1175+
(type_arguments (lifetime (identifier)) (type_identifier))))))
1176+
1177+
(function_item
1178+
(identifier)
1179+
(type_parameters
1180+
(constrained_type_parameter
1181+
(type_identifier)
1182+
(trait_bounds
1183+
(generic_type
1184+
(type_identifier)
1185+
(type_arguments
1186+
(type_binding
1187+
(type_identifier)
1188+
(type_arguments
1189+
(lifetime (identifier))
1190+
(type_identifier))
1191+
(type_identifier)))))))
1192+
(parameters)
1193+
(block))
1194+
1195+
(function_item
1196+
(identifier)
1197+
(type_parameters (type_identifier))
1198+
(parameters)
1199+
(where_clause
1200+
(where_predicate
1201+
(type_identifier)
1202+
(trait_bounds
1203+
(generic_type
1204+
(type_identifier)
1205+
(type_arguments
1206+
(type_binding
1207+
(type_identifier)
1208+
(type_arguments
1209+
(lifetime (identifier))
1210+
(type_identifier))
1211+
(type_identifier)))))))
1212+
(block)))
1213+
11311214
=====================
11321215
Higher-ranked types
11331216
=====================

grammar.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ module.exports = grammar({
481481
associated_type: $ => seq(
482482
'type',
483483
field('name', $._type_identifier),
484+
field('type_parameters', optional($.type_parameters)),
484485
field('bounds', optional($.trait_bounds)),
485486
';'
486487
),
@@ -786,6 +787,7 @@ module.exports = grammar({
786787

787788
type_binding: $ => seq(
788789
field('name', $._type_identifier),
790+
field('type_arguments', optional($.type_arguments)),
789791
'=',
790792
field('type', $._type)
791793
),
@@ -1450,7 +1452,7 @@ module.exports = grammar({
14501452
self: $ => 'self',
14511453
super: $ => 'super',
14521454
crate: $ => 'crate',
1453-
1455+
14541456
metavariable: $ => /\$[a-zA-Z_]\w*/
14551457
}
14561458
})

src/grammar.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,6 +2728,22 @@
27282728
"name": "_type_identifier"
27292729
}
27302730
},
2731+
{
2732+
"type": "FIELD",
2733+
"name": "type_parameters",
2734+
"content": {
2735+
"type": "CHOICE",
2736+
"members": [
2737+
{
2738+
"type": "SYMBOL",
2739+
"name": "type_parameters"
2740+
},
2741+
{
2742+
"type": "BLANK"
2743+
}
2744+
]
2745+
}
2746+
},
27312747
{
27322748
"type": "FIELD",
27332749
"name": "bounds",
@@ -4429,6 +4445,22 @@
44294445
"name": "_type_identifier"
44304446
}
44314447
},
4448+
{
4449+
"type": "FIELD",
4450+
"name": "type_arguments",
4451+
"content": {
4452+
"type": "CHOICE",
4453+
"members": [
4454+
{
4455+
"type": "SYMBOL",
4456+
"name": "type_arguments"
4457+
},
4458+
{
4459+
"type": "BLANK"
4460+
}
4461+
]
4462+
}
4463+
},
44324464
{
44334465
"type": "STRING",
44344466
"value": "="

src/node-types.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,16 @@
607607
"named": true
608608
}
609609
]
610+
},
611+
"type_parameters": {
612+
"multiple": false,
613+
"required": false,
614+
"types": [
615+
{
616+
"type": "type_parameters",
617+
"named": true
618+
}
619+
]
610620
}
611621
}
612622
},
@@ -3923,6 +3933,16 @@
39233933
"named": true
39243934
}
39253935
]
3936+
},
3937+
"type_arguments": {
3938+
"multiple": false,
3939+
"required": false,
3940+
"types": [
3941+
{
3942+
"type": "type_arguments",
3943+
"named": true
3944+
}
3945+
]
39263946
}
39273947
}
39283948
},

0 commit comments

Comments
 (0)