Support GATs for associated type arg parsing

This commit is contained in:
Lukas Wirth 2021-07-17 13:47:53 +02:00
parent a2f83c956e
commit e7aa37c20a
6 changed files with 165 additions and 82 deletions

4
Cargo.lock generated
View file

@ -1758,9 +1758,9 @@ dependencies = [
[[package]] [[package]]
name = "ungrammar" name = "ungrammar"
version = "1.14.1" version = "1.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dbe67c0c0f5ded8e404578a9ab1009ffdb6ee219779329aefc0cb4fe7f44891" checksum = "5334230a6ae9ca52bc811c968c0ae12f1750c0698ed52ea68dabab7ce5a80972"
[[package]] [[package]]
name = "unicase" name = "unicase"

View file

@ -27,6 +27,10 @@ pub(super) fn expr_path(p: &mut Parser) {
path(p, Mode::Expr) path(p, Mode::Expr)
} }
pub(crate) fn type_path_for_qualifier(p: &mut Parser, qual: CompletedMarker) {
path_for_qualifier(p, Mode::Type, qual)
}
#[derive(Clone, Copy, Eq, PartialEq)] #[derive(Clone, Copy, Eq, PartialEq)]
enum Mode { enum Mode {
Use, Use,
@ -37,7 +41,11 @@ enum Mode {
fn path(p: &mut Parser, mode: Mode) { fn path(p: &mut Parser, mode: Mode) {
let path = p.start(); let path = p.start();
path_segment(p, mode, true); path_segment(p, mode, true);
let mut qual = path.complete(p, PATH); let qual = path.complete(p, PATH);
path_for_qualifier(p, mode, qual)
}
fn path_for_qualifier(p: &mut Parser, mode: Mode, mut qual: CompletedMarker) {
loop { loop {
let use_tree = matches!(p.nth(2), T![*] | T!['{']); let use_tree = matches!(p.nth(2), T![*] | T!['{']);
if p.at(T![::]) && !use_tree { if p.at(T![::]) && !use_tree {

View file

@ -65,17 +65,51 @@ fn generic_arg(p: &mut Parser) {
m.complete(p, LIFETIME_ARG); m.complete(p, LIFETIME_ARG);
} }
// test associated_type_bounds // test associated_type_bounds
// fn print_all<T: Iterator<Item: Display>>(printables: T) {} // fn print_all<T: Iterator<Item, Item::Item, Item: Display, Item<'a> = Item>>(printables: T) {}
IDENT if p.nth(1) == T![:] && p.nth(2) != T![:] => { IDENT if [T![<], T![=], T![:]].contains(&p.nth(1)) => {
let path_ty = p.start();
let path = p.start();
let path_seg = p.start();
name_ref(p); name_ref(p);
type_params::bounds(p); if p.current() == T![<] {
m.complete(p, ASSOC_TYPE_ARG); opt_generic_arg_list(p, false);
} }
IDENT if p.nth(1) == T![=] => { match p.current() {
name_ref(p); // NameRef<...> =
p.bump_any(); T![=] => {
types::type_(p); p.bump_any();
m.complete(p, ASSOC_TYPE_ARG); types::type_(p);
path_seg.abandon(p);
path.abandon(p);
path_ty.abandon(p);
m.complete(p, ASSOC_TYPE_ARG);
}
T![:] if p.nth(1) == T![:] => {
// NameRef::, this is a path type
path_seg.complete(p, PATH_SEGMENT);
let qual = path.complete(p, PATH);
paths::type_path_for_qualifier(p, qual);
path_ty.complete(p, PATH_TYPE);
m.complete(p, TYPE_ARG);
}
// NameRef<...>:
T![:] => {
type_params::bounds(p);
path_seg.abandon(p);
path.abandon(p);
path_ty.abandon(p);
m.complete(p, ASSOC_TYPE_ARG);
}
// NameRef, this is a single segment path type
_ => {
path_seg.complete(p, PATH_SEGMENT);
path.complete(p, PATH);
path_ty.complete(p, PATH_TYPE);
m.complete(p, TYPE_ARG);
}
}
} }
T!['{'] => { T!['{'] => {
expressions::block_expr(p); expressions::block_expr(p);

View file

@ -107,6 +107,7 @@ pub struct AssocTypeArg {
impl ast::TypeBoundsOwner for AssocTypeArg {} impl ast::TypeBoundsOwner for AssocTypeArg {}
impl AssocTypeArg { impl AssocTypeArg {
pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) } pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) }
pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) }
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) } pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
} }
@ -125,6 +126,15 @@ impl ConstArg {
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GenericParamList {
pub(crate) syntax: SyntaxNode,
}
impl GenericParamList {
pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) }
pub fn generic_params(&self) -> AstChildren<GenericParam> { support::children(&self.syntax) }
pub fn r_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![>]) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TypeBoundList { pub struct TypeBoundList {
pub(crate) syntax: SyntaxNode, pub(crate) syntax: SyntaxNode,
} }
@ -454,15 +464,6 @@ impl Abi {
pub fn extern_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![extern]) } pub fn extern_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![extern]) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GenericParamList {
pub(crate) syntax: SyntaxNode,
}
impl GenericParamList {
pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) }
pub fn generic_params(&self) -> AstChildren<GenericParam> { support::children(&self.syntax) }
pub fn r_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![>]) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct WhereClause { pub struct WhereClause {
pub(crate) syntax: SyntaxNode, pub(crate) syntax: SyntaxNode,
} }
@ -1584,6 +1585,17 @@ impl AstNode for ConstArg {
} }
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
impl AstNode for GenericParamList {
fn can_cast(kind: SyntaxKind) -> bool { kind == GENERIC_PARAM_LIST }
fn cast(syntax: SyntaxNode) -> Option<Self> {
if Self::can_cast(syntax.kind()) {
Some(Self { syntax })
} else {
None
}
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
impl AstNode for TypeBoundList { impl AstNode for TypeBoundList {
fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_BOUND_LIST } fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_BOUND_LIST }
fn cast(syntax: SyntaxNode) -> Option<Self> { fn cast(syntax: SyntaxNode) -> Option<Self> {
@ -1892,17 +1904,6 @@ impl AstNode for Abi {
} }
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
impl AstNode for GenericParamList {
fn can_cast(kind: SyntaxKind) -> bool { kind == GENERIC_PARAM_LIST }
fn cast(syntax: SyntaxNode) -> Option<Self> {
if Self::can_cast(syntax.kind()) {
Some(Self { syntax })
} else {
None
}
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
impl AstNode for WhereClause { impl AstNode for WhereClause {
fn can_cast(kind: SyntaxKind) -> bool { kind == WHERE_CLAUSE } fn can_cast(kind: SyntaxKind) -> bool { kind == WHERE_CLAUSE }
fn cast(syntax: SyntaxNode) -> Option<Self> { fn cast(syntax: SyntaxNode) -> Option<Self> {
@ -3680,6 +3681,11 @@ impl std::fmt::Display for ConstArg {
std::fmt::Display::fmt(self.syntax(), f) std::fmt::Display::fmt(self.syntax(), f)
} }
} }
impl std::fmt::Display for GenericParamList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for TypeBoundList { impl std::fmt::Display for TypeBoundList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f) std::fmt::Display::fmt(self.syntax(), f)
@ -3820,11 +3826,6 @@ impl std::fmt::Display for Abi {
std::fmt::Display::fmt(self.syntax(), f) std::fmt::Display::fmt(self.syntax(), f)
} }
} }
impl std::fmt::Display for GenericParamList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for WhereClause { impl std::fmt::Display for WhereClause {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f) std::fmt::Display::fmt(self.syntax(), f)

View file

@ -1,55 +1,95 @@
SOURCE_FILE@0..59 SOURCE_FILE@0..94
FN@0..58 FN@0..93
FN_KW@0..2 "fn" FN_KW@0..2 "fn"
WHITESPACE@2..3 " " WHITESPACE@2..3 " "
NAME@3..12 NAME@3..12
IDENT@3..12 "print_all" IDENT@3..12 "print_all"
GENERIC_PARAM_LIST@12..40 GENERIC_PARAM_LIST@12..75
L_ANGLE@12..13 "<" L_ANGLE@12..13 "<"
TYPE_PARAM@13..39 TYPE_PARAM@13..74
NAME@13..14 NAME@13..14
IDENT@13..14 "T" IDENT@13..14 "T"
COLON@14..15 ":" COLON@14..15 ":"
WHITESPACE@15..16 " " WHITESPACE@15..16 " "
TYPE_BOUND_LIST@16..39 TYPE_BOUND_LIST@16..74
TYPE_BOUND@16..39 TYPE_BOUND@16..74
PATH_TYPE@16..39 PATH_TYPE@16..74
PATH@16..39 PATH@16..74
PATH_SEGMENT@16..39 PATH_SEGMENT@16..74
NAME_REF@16..24 NAME_REF@16..24
IDENT@16..24 "Iterator" IDENT@16..24 "Iterator"
GENERIC_ARG_LIST@24..39 GENERIC_ARG_LIST@24..74
L_ANGLE@24..25 "<" L_ANGLE@24..25 "<"
ASSOC_TYPE_ARG@25..38 TYPE_ARG@25..29
NAME_REF@25..29 PATH_TYPE@25..29
IDENT@25..29 "Item" PATH@25..29
COLON@29..30 ":" PATH_SEGMENT@25..29
WHITESPACE@30..31 " " NAME_REF@25..29
TYPE_BOUND_LIST@31..38 IDENT@25..29 "Item"
TYPE_BOUND@31..38 COMMA@29..30 ","
PATH_TYPE@31..38 WHITESPACE@30..31 " "
PATH@31..38 TYPE_ARG@31..41
PATH_SEGMENT@31..38 PATH_TYPE@31..41
NAME_REF@31..38 PATH@31..41
IDENT@31..38 "Display" PATH@31..35
R_ANGLE@38..39 ">" PATH_SEGMENT@31..35
R_ANGLE@39..40 ">" NAME_REF@31..35
PARAM_LIST@40..55 IDENT@31..35 "Item"
L_PAREN@40..41 "(" COLON2@35..37 "::"
PARAM@41..54 PATH_SEGMENT@37..41
IDENT_PAT@41..51 NAME_REF@37..41
NAME@41..51 IDENT@37..41 "Item"
IDENT@41..51 "printables" COMMA@41..42 ","
COLON@51..52 ":" WHITESPACE@42..43 " "
WHITESPACE@52..53 " " ASSOC_TYPE_ARG@43..56
PATH_TYPE@53..54 NAME_REF@43..47
PATH@53..54 IDENT@43..47 "Item"
PATH_SEGMENT@53..54 COLON@47..48 ":"
NAME_REF@53..54 WHITESPACE@48..49 " "
IDENT@53..54 "T" TYPE_BOUND_LIST@49..56
R_PAREN@54..55 ")" TYPE_BOUND@49..56
WHITESPACE@55..56 " " PATH_TYPE@49..56
BLOCK_EXPR@56..58 PATH@49..56
L_CURLY@56..57 "{" PATH_SEGMENT@49..56
R_CURLY@57..58 "}" NAME_REF@49..56
WHITESPACE@58..59 "\n" IDENT@49..56 "Display"
COMMA@56..57 ","
WHITESPACE@57..58 " "
ASSOC_TYPE_ARG@58..73
NAME_REF@58..62
IDENT@58..62 "Item"
GENERIC_ARG_LIST@62..66
L_ANGLE@62..63 "<"
LIFETIME_ARG@63..65
LIFETIME@63..65
LIFETIME_IDENT@63..65 "'a"
R_ANGLE@65..66 ">"
WHITESPACE@66..67 " "
EQ@67..68 "="
WHITESPACE@68..69 " "
PATH_TYPE@69..73
PATH@69..73
PATH_SEGMENT@69..73
NAME_REF@69..73
IDENT@69..73 "Item"
R_ANGLE@73..74 ">"
R_ANGLE@74..75 ">"
PARAM_LIST@75..90
L_PAREN@75..76 "("
PARAM@76..89
IDENT_PAT@76..86
NAME@76..86
IDENT@76..86 "printables"
COLON@86..87 ":"
WHITESPACE@87..88 " "
PATH_TYPE@88..89
PATH@88..89
PATH_SEGMENT@88..89
NAME_REF@88..89
IDENT@88..89 "T"
R_PAREN@89..90 ")"
WHITESPACE@90..91 " "
BLOCK_EXPR@91..93
L_CURLY@91..92 "{"
R_CURLY@92..93 "}"
WHITESPACE@93..94 "\n"

View file

@ -1 +1 @@
fn print_all<T: Iterator<Item: Display>>(printables: T) {} fn print_all<T: Iterator<Item, Item::Item, Item: Display, Item<'a> = Item>>(printables: T) {}