Skip to content

Commit

Permalink
Add Generic Associated Types support (#124)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kawaemon committed Dec 12, 2021
1 parent e06d078 commit d045b04
Show file tree
Hide file tree
Showing 5 changed files with 47,037 additions and 46,451 deletions.
83 changes: 83 additions & 0 deletions corpus/declarations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,89 @@ pub trait Graph {
(associated_type (type_identifier) (trait_bounds (scoped_type_identifier (identifier) (type_identifier))))
(associated_type (type_identifier)))))

============================================
Generic Associated Types
============================================

pub trait Database {
type F<'a, D>: Future<Output = D> + 'a;
}

impl Database for Foo {
type F<'a, D> = DatabaseFuture<'a, D>;
}

fn use_database1<D: Database<F<'a, TD> = F>>() {}

fn use_database2<D>()
where
D: Database<F<'a, TD> = F>,
{}

---

(source_file
(trait_item
(visibility_modifier)
(type_identifier)
(declaration_list
(associated_type
(type_identifier)
(type_parameters (lifetime (identifier)) (type_identifier))
(trait_bounds
(generic_type
(type_identifier)
(type_arguments (type_binding (type_identifier) (type_identifier))))
(lifetime (identifier))))))

(impl_item
(type_identifier)
(type_identifier)
(declaration_list
(type_item
(type_identifier)
(type_parameters (lifetime (identifier)) (type_identifier))
(generic_type
(type_identifier)
(type_arguments (lifetime (identifier)) (type_identifier))))))

(function_item
(identifier)
(type_parameters
(constrained_type_parameter
(type_identifier)
(trait_bounds
(generic_type
(type_identifier)
(type_arguments
(type_binding
(type_identifier)
(type_arguments
(lifetime (identifier))
(type_identifier))
(type_identifier)))))))
(parameters)
(block))

(function_item
(identifier)
(type_parameters (type_identifier))
(parameters)
(where_clause
(where_predicate
(type_identifier)
(trait_bounds
(generic_type
(type_identifier)
(type_arguments
(type_binding
(type_identifier)
(type_arguments
(lifetime (identifier))
(type_identifier))
(type_identifier)))))))
(block)))

=====================
Higher-ranked types
=====================
Expand Down
4 changes: 3 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ module.exports = grammar({
associated_type: $ => seq(
'type',
field('name', $._type_identifier),
field('type_parameters', optional($.type_parameters)),
field('bounds', optional($.trait_bounds)),
';'
),
Expand Down Expand Up @@ -786,6 +787,7 @@ module.exports = grammar({

type_binding: $ => seq(
field('name', $._type_identifier),
field('type_arguments', optional($.type_arguments)),
'=',
field('type', $._type)
),
Expand Down Expand Up @@ -1450,7 +1452,7 @@ module.exports = grammar({
self: $ => 'self',
super: $ => 'super',
crate: $ => 'crate',

metavariable: $ => /\$[a-zA-Z_]\w*/
}
})
Expand Down
32 changes: 32 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2728,6 +2728,22 @@
"name": "_type_identifier"
}
},
{
"type": "FIELD",
"name": "type_parameters",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "type_parameters"
},
{
"type": "BLANK"
}
]
}
},
{
"type": "FIELD",
"name": "bounds",
Expand Down Expand Up @@ -4429,6 +4445,22 @@
"name": "_type_identifier"
}
},
{
"type": "FIELD",
"name": "type_arguments",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "type_arguments"
},
{
"type": "BLANK"
}
]
}
},
{
"type": "STRING",
"value": "="
Expand Down
20 changes: 20 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,16 @@
"named": true
}
]
},
"type_parameters": {
"multiple": false,
"required": false,
"types": [
{
"type": "type_parameters",
"named": true
}
]
}
}
},
Expand Down Expand Up @@ -3923,6 +3933,16 @@
"named": true
}
]
},
"type_arguments": {
"multiple": false,
"required": false,
"types": [
{
"type": "type_arguments",
"named": true
}
]
}
}
},
Expand Down

0 comments on commit d045b04

Please sign in to comment.