From 25adc5e9f01885e71bf065afd5e5b8cdc823876c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 19 Sep 2021 11:19:31 +0300 Subject: [PATCH 1/3] minor: reduce duplication --- crates/parser/src/grammar/generic_args.rs | 26 +++++------------------ 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/crates/parser/src/grammar/generic_args.rs b/crates/parser/src/grammar/generic_args.rs index cd75d20a42f..9f6f74c6697 100644 --- a/crates/parser/src/grammar/generic_args.rs +++ b/crates/parser/src/grammar/generic_args.rs @@ -26,15 +26,16 @@ pub(super) fn opt_generic_arg_list(p: &mut Parser, colon_colon_required: bool) { // test type_arg // type A = B<'static, i32, 1, { 2 }, Item=u64, true, false>; fn generic_arg(p: &mut Parser) { - let m = p.start(); match p.current() { LIFETIME_IDENT => { + let m = p.start(); lifetime(p); m.complete(p, LIFETIME_ARG); } // test associated_type_bounds // fn print_all, Item: Display, Item<'a> = Item>>(printables: T) {} IDENT if [T![<], T![=], T![:]].contains(&p.nth(1)) => { + let m = p.start(); let path_ty = p.start(); let path = p.start(); let path_seg = p.start(); @@ -78,28 +79,12 @@ fn generic_arg(p: &mut Parser) { } } } - T!['{'] => { - expressions::block_expr(p); - m.complete(p, CONST_ARG); - } - k if k.is_literal() => { - expressions::literal(p); - m.complete(p, CONST_ARG); - } - T![true] | T![false] => { - expressions::literal(p); - m.complete(p, CONST_ARG); - } // test const_generic_negated_literal // fn f() { S::<-1> } - T![-] => { - let lm = p.start(); - p.bump(T![-]); - expressions::literal(p); - lm.complete(p, PREFIX_EXPR); - m.complete(p, CONST_ARG); - } + T!['{'] | T![true] | T![false] | T![-] => const_arg(p), + k if k.is_literal() => const_arg(p), _ => { + let m = p.start(); types::type_(p); m.complete(p, TYPE_ARG); } @@ -108,7 +93,6 @@ fn generic_arg(p: &mut Parser) { pub(super) fn const_arg(p: &mut Parser) { let m = p.start(); - // FIXME: duplicates the code above match p.current() { T!['{'] => { expressions::block_expr(p); From 8009ccc27d35a0886ef61ef804666a284ae5c114 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 19 Sep 2021 11:34:25 +0300 Subject: [PATCH 2/3] minor: improve readability --- crates/parser/src/grammar/generic_args.rs | 24 +++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/crates/parser/src/grammar/generic_args.rs b/crates/parser/src/grammar/generic_args.rs index 9f6f74c6697..3b9444a6ff2 100644 --- a/crates/parser/src/grammar/generic_args.rs +++ b/crates/parser/src/grammar/generic_args.rs @@ -27,11 +27,7 @@ pub(super) fn opt_generic_arg_list(p: &mut Parser, colon_colon_required: bool) { // type A = B<'static, i32, 1, { 2 }, Item=u64, true, false>; fn generic_arg(p: &mut Parser) { match p.current() { - LIFETIME_IDENT => { - let m = p.start(); - lifetime(p); - m.complete(p, LIFETIME_ARG); - } + LIFETIME_IDENT => lifetime_arg(p), // test associated_type_bounds // fn print_all, Item: Display, Item<'a> = Item>>(printables: T) {} IDENT if [T![<], T![=], T![:]].contains(&p.nth(1)) => { @@ -83,14 +79,16 @@ fn generic_arg(p: &mut Parser) { // fn f() { S::<-1> } T!['{'] | T![true] | T![false] | T![-] => const_arg(p), k if k.is_literal() => const_arg(p), - _ => { - let m = p.start(); - types::type_(p); - m.complete(p, TYPE_ARG); - } + _ => type_arg(p), } } +fn lifetime_arg(p: &mut Parser) { + let m = p.start(); + lifetime(p); + m.complete(p, LIFETIME_ARG); +} + pub(super) fn const_arg(p: &mut Parser) { let m = p.start(); match p.current() { @@ -121,3 +119,9 @@ pub(super) fn const_arg(p: &mut Parser) { } } } + +fn type_arg(p: &mut Parser) { + let m = p.start(); + types::type_(p); + m.complete(p, TYPE_ARG); +} From 8ae1f9c33599351dd31027c446d6565b170ec99a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 19 Sep 2021 11:42:10 +0300 Subject: [PATCH 3/3] internal: add erroneous test for double turbo fish We parse `f` successfully, but that is a bug. --- .../parser/err/0049_double_fish.rast | 115 ++++++++++++++++++ .../test_data/parser/err/0049_double_fish.rs | 7 ++ 2 files changed, 122 insertions(+) create mode 100644 crates/syntax/test_data/parser/err/0049_double_fish.rast create mode 100644 crates/syntax/test_data/parser/err/0049_double_fish.rs diff --git a/crates/syntax/test_data/parser/err/0049_double_fish.rast b/crates/syntax/test_data/parser/err/0049_double_fish.rast new file mode 100644 index 00000000000..aced9884acc --- /dev/null +++ b/crates/syntax/test_data/parser/err/0049_double_fish.rast @@ -0,0 +1,115 @@ +SOURCE_FILE@0..90 + FN@0..40 + FN_KW@0..2 "fn" + WHITESPACE@2..3 " " + NAME@3..4 + IDENT@3..4 "f" + PARAM_LIST@4..6 + L_PAREN@4..5 "(" + R_PAREN@5..6 ")" + WHITESPACE@6..7 " " + BLOCK_EXPR@7..40 + L_CURLY@7..8 "{" + WHITESPACE@8..13 "\n " + EXPR_STMT@13..38 + PATH_EXPR@13..37 + PATH@13..37 + PATH_SEGMENT@13..37 + NAME_REF@13..14 + IDENT@13..14 "S" + GENERIC_ARG_LIST@14..37 + COLON2@14..16 "::" + L_ANGLE@16..17 "<" + TYPE_ARG@17..36 + PATH_TYPE@17..36 + PATH@17..28 + PATH_SEGMENT@17..28 + NAME_REF@17..21 + IDENT@17..21 "Item" + GENERIC_ARG_LIST@21..28 + COLON2@21..23 "::" + L_ANGLE@23..24 "<" + TYPE_ARG@24..27 + PATH_TYPE@24..27 + PATH@24..27 + PATH_SEGMENT@24..27 + NAME_REF@24..27 + IDENT@24..27 "lol" + R_ANGLE@27..28 ">" + GENERIC_ARG_LIST@28..36 + COLON2@28..30 "::" + L_ANGLE@30..31 "<" + TYPE_ARG@31..35 + PATH_TYPE@31..35 + PATH@31..35 + PATH_SEGMENT@31..35 + NAME_REF@31..35 + IDENT@31..35 "nope" + R_ANGLE@35..36 ">" + R_ANGLE@36..37 ">" + SEMICOLON@37..38 ";" + WHITESPACE@38..39 "\n" + R_CURLY@39..40 "}" + WHITESPACE@40..42 "\n\n" + FN@42..89 + FN_KW@42..44 "fn" + WHITESPACE@44..45 " " + NAME@45..46 + IDENT@45..46 "g" + PARAM_LIST@46..48 + L_PAREN@46..47 "(" + R_PAREN@47..48 ")" + WHITESPACE@48..49 " " + BLOCK_EXPR@49..89 + L_CURLY@49..50 "{" + WHITESPACE@50..55 "\n " + LET_STMT@55..76 + LET_KW@55..58 "let" + WHITESPACE@58..59 " " + WILDCARD_PAT@59..60 + UNDERSCORE@59..60 "_" + COLON@60..61 ":" + WHITESPACE@61..62 " " + PATH_TYPE@62..76 + PATH@62..76 + PATH@62..73 + PATH_SEGMENT@62..73 + NAME_REF@62..66 + IDENT@62..66 "Item" + GENERIC_ARG_LIST@66..73 + COLON2@66..68 "::" + L_ANGLE@68..69 "<" + TYPE_ARG@69..72 + PATH_TYPE@69..72 + PATH@69..72 + PATH_SEGMENT@69..72 + NAME_REF@69..72 + IDENT@69..72 "lol" + R_ANGLE@72..73 ">" + COLON2@73..75 "::" + ERROR@75..76 + L_ANGLE@75..76 "<" + EXPR_STMT@76..83 + BIN_EXPR@76..83 + PATH_EXPR@76..80 + PATH@76..80 + PATH_SEGMENT@76..80 + NAME_REF@76..80 + IDENT@76..80 "nope" + R_ANGLE@80..81 ">" + WHITESPACE@81..82 " " + ERROR@82..83 + EQ@82..83 "=" + WHITESPACE@83..84 " " + EXPR_STMT@84..87 + TUPLE_EXPR@84..86 + L_PAREN@84..85 "(" + R_PAREN@85..86 ")" + SEMICOLON@86..87 ";" + WHITESPACE@87..88 "\n" + R_CURLY@88..89 "}" + WHITESPACE@89..90 "\n" +error 75..75: expected identifier +error 76..76: expected SEMICOLON +error 82..82: expected expression +error 83..83: expected SEMICOLON diff --git a/crates/syntax/test_data/parser/err/0049_double_fish.rs b/crates/syntax/test_data/parser/err/0049_double_fish.rs new file mode 100644 index 00000000000..31c12bfff9f --- /dev/null +++ b/crates/syntax/test_data/parser/err/0049_double_fish.rs @@ -0,0 +1,7 @@ +fn f() { + S::::>; +} + +fn g() { + let _: Item:::: = (); +}