From ce1b34fd59a6145a4bb5682d672c846e101725d4 Mon Sep 17 00:00:00 2001 From: Michael Chesser Date: Tue, 7 Jan 2020 09:29:03 +1030 Subject: Improve const generics parsing - Handle const generics type args - Fix issue with const generic as first parameter in trait impl --- crates/ra_parser/src/grammar/items/traits.rs | 5 ++++- crates/ra_parser/src/grammar/type_args.rs | 10 +++++++++- crates/ra_parser/src/syntax_kind/generated.rs | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) (limited to 'crates/ra_parser/src') diff --git a/crates/ra_parser/src/grammar/items/traits.rs b/crates/ra_parser/src/grammar/items/traits.rs index 2c560e824..964fd3041 100644 --- a/crates/ra_parser/src/grammar/items/traits.rs +++ b/crates/ra_parser/src/grammar/items/traits.rs @@ -100,6 +100,8 @@ pub(crate) fn impl_item_list(p: &mut Parser) { m.complete(p, ITEM_LIST); } +// test impl_type_params +// impl Bar {} fn choose_type_params_over_qpath(p: &Parser) -> bool { // There's an ambiguity between generic parameters and qualified paths in impls. // If we see `<` it may start both, so we have to inspect some following tokens. @@ -107,6 +109,7 @@ fn choose_type_params_over_qpath(p: &Parser) -> bool { // but not qualified paths (with one exception): // `<` `>` - empty generic parameters // `<` `#` - generic parameters with attributes + // `<` `const` - const generic parameters // `<` (LIFETIME|IDENT) `>` - single generic parameter // `<` (LIFETIME|IDENT) `,` - first generic parameter in a list // `<` (LIFETIME|IDENT) `:` - generic parameter with bounds @@ -119,7 +122,7 @@ fn choose_type_params_over_qpath(p: &Parser) -> bool { if !p.at(T![<]) { return false; } - if p.nth(1) == T![#] || p.nth(1) == T![>] { + if p.nth(1) == T![#] || p.nth(1) == T![>] || p.nth(1) == CONST_KW { return true; } (p.nth(1) == LIFETIME || p.nth(1) == IDENT) diff --git a/crates/ra_parser/src/grammar/type_args.rs b/crates/ra_parser/src/grammar/type_args.rs index 7256c2697..33d9973e9 100644 --- a/crates/ra_parser/src/grammar/type_args.rs +++ b/crates/ra_parser/src/grammar/type_args.rs @@ -26,7 +26,7 @@ pub(super) fn opt_type_arg_list(p: &mut Parser, colon_colon_required: bool) { } // test type_arg -// type A = B<'static, i32, Item=u64>; +// type A = B<'static, i32, 1, { 2 }, Item=u64>; fn type_arg(p: &mut Parser) { let m = p.start(); match p.current() { @@ -47,6 +47,14 @@ fn type_arg(p: &mut Parser) { types::type_(p); m.complete(p, ASSOC_TYPE_ARG); } + T!['{'] => { + expressions::block(p); + m.complete(p, CONST_ARG); + } + k if k.is_literal() => { + p.bump(k); + m.complete(p, CONST_ARG); + } _ => { types::type_(p); m.complete(p, TYPE_ARG); diff --git a/crates/ra_parser/src/syntax_kind/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs index 262c545e4..4b301d67a 100644 --- a/crates/ra_parser/src/syntax_kind/generated.rs +++ b/crates/ra_parser/src/syntax_kind/generated.rs @@ -234,6 +234,7 @@ pub enum SyntaxKind { LIFETIME_ARG, TYPE_ARG, ASSOC_TYPE_ARG, + CONST_ARG, PARAM_LIST, PARAM, SELF_PARAM, -- cgit v1.2.3