From 9149fd2c0ca3d23719082852a2cddd8ba5804ce6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 13 Aug 2018 18:23:14 +0300 Subject: [PATCH] Fix some parser bugs --- crates/libsyntax2/src/grammar/paths.rs | 2 +- crates/libsyntax2/src/grammar/patterns.rs | 6 +- crates/libsyntax2/src/grammar/type_params.rs | 7 +- crates/libsyntax2/src/grammar/types.rs | 7 +- .../parser/err/0004_use_path_bad_segment.txt | 16 +- .../data/parser/err/0014_where_no_bounds.rs | 1 + .../data/parser/err/0014_where_no_bounds.txt | 31 ++++ .../tests/data/parser/ok/0030_traits.rs | 11 ++ .../tests/data/parser/ok/0030_traits.txt | 147 ++++++++++++++++++ 9 files changed, 213 insertions(+), 15 deletions(-) create mode 100644 crates/libsyntax2/tests/data/parser/err/0014_where_no_bounds.rs create mode 100644 crates/libsyntax2/tests/data/parser/err/0014_where_no_bounds.txt create mode 100644 crates/libsyntax2/tests/data/parser/ok/0030_traits.rs create mode 100644 crates/libsyntax2/tests/data/parser/ok/0030_traits.txt diff --git a/crates/libsyntax2/src/grammar/paths.rs b/crates/libsyntax2/src/grammar/paths.rs index c277e2a6b5..aa5ecf4b84 100644 --- a/crates/libsyntax2/src/grammar/paths.rs +++ b/crates/libsyntax2/src/grammar/paths.rs @@ -62,7 +62,7 @@ fn path_segment(p: &mut Parser, mode: Mode, first: bool) { } SELF_KW | SUPER_KW => p.bump(), _ => { - p.error("expected identifier"); + p.err_and_bump("expected identifier"); } }; segment.complete(p, PATH_SEGMENT); diff --git a/crates/libsyntax2/src/grammar/patterns.rs b/crates/libsyntax2/src/grammar/patterns.rs index 436f3b26d8..220f36db72 100644 --- a/crates/libsyntax2/src/grammar/patterns.rs +++ b/crates/libsyntax2/src/grammar/patterns.rs @@ -60,6 +60,7 @@ fn atom_pat(p: &mut Parser) -> Option { // let Bar(..) = (); // } fn path_pat(p: &mut Parser) -> CompletedMarker { + assert!(paths::is_path_start(p)); let m = p.start(); paths::expr_path(p); let kind = match p.current() { @@ -116,8 +117,11 @@ fn struct_pat_fields(p: &mut Parser) { p.bump(); pattern(p); } - _ => { + REF_KW | MUT_KW | IDENT => { bind_pat(p, false); + }, + _ => { + p.err_and_bump("expected ident"); } } if !p.at(R_CURLY) { diff --git a/crates/libsyntax2/src/grammar/type_params.rs b/crates/libsyntax2/src/grammar/type_params.rs index 0a3e8fd070..32b69dc5b9 100644 --- a/crates/libsyntax2/src/grammar/type_params.rs +++ b/crates/libsyntax2/src/grammar/type_params.rs @@ -121,7 +121,12 @@ fn where_predicate(p: &mut Parser) { lifetime_bounds(p) } else { types::path_type(p); - bounds(p); + if p.at(COLON) { + bounds(p); + } else { + p.error("expected colon") + } + } m.complete(p, WHERE_PRED); } diff --git a/crates/libsyntax2/src/grammar/types.rs b/crates/libsyntax2/src/grammar/types.rs index 5ba3fcca0d..88631fefee 100644 --- a/crates/libsyntax2/src/grammar/types.rs +++ b/crates/libsyntax2/src/grammar/types.rs @@ -166,8 +166,11 @@ fn fn_pointer_type(p: &mut Parser) { p.error("expected `fn`"); return; } - - params::param_list_opt_patterns(p); + if p.at(L_PAREN) { + params::param_list_opt_patterns(p); + } else { + p.error("expected parameters") + } // test fn_pointer_type_with_ret // type F = fn() -> (); fn_ret_type(p); diff --git a/crates/libsyntax2/tests/data/parser/err/0004_use_path_bad_segment.txt b/crates/libsyntax2/tests/data/parser/err/0004_use_path_bad_segment.txt index c6ae681037..64aa078016 100644 --- a/crates/libsyntax2/tests/data/parser/err/0004_use_path_bad_segment.txt +++ b/crates/libsyntax2/tests/data/parser/err/0004_use_path_bad_segment.txt @@ -1,20 +1,16 @@ FILE@[0; 12) - USE_ITEM@[0; 9) + USE_ITEM@[0; 12) USE_KW@[0; 3) WHITESPACE@[3; 4) - USE_TREE@[4; 9) - PATH@[4; 9) + USE_TREE@[4; 11) + PATH@[4; 11) PATH@[4; 7) PATH_SEGMENT@[4; 7) NAME_REF@[4; 7) IDENT@[4; 7) "foo" COLONCOLON@[7; 9) err: `expected identifier` - err: `expected SEMI` - err: `expected an item` - PATH_SEGMENT@[9; 9) - ERROR@[9; 11) - INT_NUMBER@[9; 11) "92" - err: `expected an item` - ERROR@[11; 12) + PATH_SEGMENT@[9; 11) + ERROR@[9; 11) + INT_NUMBER@[9; 11) "92" SEMI@[11; 12) diff --git a/crates/libsyntax2/tests/data/parser/err/0014_where_no_bounds.rs b/crates/libsyntax2/tests/data/parser/err/0014_where_no_bounds.rs new file mode 100644 index 0000000000..75c1d2f986 --- /dev/null +++ b/crates/libsyntax2/tests/data/parser/err/0014_where_no_bounds.rs @@ -0,0 +1 @@ +fn foo() where T {} diff --git a/crates/libsyntax2/tests/data/parser/err/0014_where_no_bounds.txt b/crates/libsyntax2/tests/data/parser/err/0014_where_no_bounds.txt new file mode 100644 index 0000000000..61444a88df --- /dev/null +++ b/crates/libsyntax2/tests/data/parser/err/0014_where_no_bounds.txt @@ -0,0 +1,31 @@ +FILE@[0; 23) + FUNCTION@[0; 22) + FN_KW@[0; 2) + WHITESPACE@[2; 3) + NAME@[3; 6) + IDENT@[3; 6) "foo" + TYPE_PARAM_LIST@[6; 9) + L_ANGLE@[6; 7) + TYPE_PARAM@[7; 8) + NAME@[7; 8) + IDENT@[7; 8) "T" + R_ANGLE@[8; 9) + PARAM_LIST@[9; 11) + L_PAREN@[9; 10) + R_PAREN@[10; 11) + WHITESPACE@[11; 12) + WHERE_CLAUSE@[12; 19) + WHERE_KW@[12; 17) + WHITESPACE@[17; 18) + WHERE_PRED@[18; 19) + PATH_TYPE@[18; 19) + PATH@[18; 19) + PATH_SEGMENT@[18; 19) + NAME_REF@[18; 19) + IDENT@[18; 19) "T" + err: `expected colon` + WHITESPACE@[19; 20) + BLOCK_EXPR@[20; 22) + L_CURLY@[20; 21) + R_CURLY@[21; 22) + WHITESPACE@[22; 23) diff --git a/crates/libsyntax2/tests/data/parser/ok/0030_traits.rs b/crates/libsyntax2/tests/data/parser/ok/0030_traits.rs new file mode 100644 index 0000000000..23c4be0e18 --- /dev/null +++ b/crates/libsyntax2/tests/data/parser/ok/0030_traits.rs @@ -0,0 +1,11 @@ +pub trait WriteMessage { + fn write_message(&FrontendMessage); +} + +trait Runnable { + fn handler(); +} + +trait TraitWithExpr { + fn fn_with_expr(x: [i32; 1]); +} diff --git a/crates/libsyntax2/tests/data/parser/ok/0030_traits.txt b/crates/libsyntax2/tests/data/parser/ok/0030_traits.txt new file mode 100644 index 0000000000..8abcb01e0b --- /dev/null +++ b/crates/libsyntax2/tests/data/parser/ok/0030_traits.txt @@ -0,0 +1,147 @@ +FILE@[0; 164) + TRAIT@[0; 164) + VISIBILITY@[0; 3) + PUB_KW@[0; 3) + WHITESPACE@[3; 4) + TRAIT_KW@[4; 9) + WHITESPACE@[9; 10) + NAME@[10; 22) + IDENT@[10; 22) "WriteMessage" + WHITESPACE@[22; 23) + L_CURLY@[23; 24) + WHITESPACE@[24; 29) + FUNCTION@[29; 164) + FN_KW@[29; 31) + WHITESPACE@[31; 32) + NAME@[32; 45) + IDENT@[32; 45) "write_message" + PARAM_LIST@[45; 164) + L_PAREN@[45; 46) + PARAM@[46; 63) + REF_PAT@[46; 62) + AMP@[46; 47) + BIND_PAT@[47; 62) + NAME@[47; 62) + IDENT@[47; 62) "FrontendMessage" + err: `expected COLON` + err: `expected type` + ERROR@[62; 63) + R_PAREN@[62; 63) + err: `expected COMMA` + err: `expected pattern` + PARAM@[63; 66) + ERROR@[63; 64) + SEMI@[63; 64) + err: `expected COLON` + WHITESPACE@[64; 65) + err: `expected type` + ERROR@[65; 66) + R_CURLY@[65; 66) + err: `expected COMMA` + WHITESPACE@[66; 68) + err: `expected pattern` + PARAM@[68; 82) + ERROR@[68; 73) + TRAIT_KW@[68; 73) + err: `expected COLON` + WHITESPACE@[73; 74) + PATH_TYPE@[74; 82) + PATH@[74; 82) + PATH_SEGMENT@[74; 82) + NAME_REF@[74; 82) + IDENT@[74; 82) "Runnable" + err: `expected COMMA` + WHITESPACE@[82; 83) + err: `expected pattern` + PARAM@[83; 91) + ERROR@[83; 84) + L_CURLY@[83; 84) + err: `expected COLON` + WHITESPACE@[84; 89) + FN_POINTER_TYPE@[89; 91) + FN_KW@[89; 91) + err: `expected parameters` + err: `expected COMMA` + WHITESPACE@[91; 92) + PARAM@[92; 102) + TUPLE_STRUCT_PAT@[92; 101) + PATH@[92; 99) + PATH_SEGMENT@[92; 99) + NAME_REF@[92; 99) + IDENT@[92; 99) "handler" + L_PAREN@[99; 100) + R_PAREN@[100; 101) + err: `expected COLON` + err: `expected type` + ERROR@[101; 102) + SEMI@[101; 102) + err: `expected COMMA` + WHITESPACE@[102; 103) + err: `expected pattern` + PARAM@[103; 111) + ERROR@[103; 104) + R_CURLY@[103; 104) + err: `expected COLON` + WHITESPACE@[104; 106) + err: `expected type` + ERROR@[106; 111) + TRAIT_KW@[106; 111) + err: `expected COMMA` + WHITESPACE@[111; 112) + PARAM@[112; 164) + STRUCT_PAT@[112; 163) + PATH@[112; 125) + PATH_SEGMENT@[112; 125) + NAME_REF@[112; 125) + IDENT@[112; 125) "TraitWithExpr" + WHITESPACE@[125; 126) + L_CURLY@[126; 127) + WHITESPACE@[127; 132) + err: `expected ident` + ERROR@[132; 134) + FN_KW@[132; 134) + err: `expected COMMA` + WHITESPACE@[134; 135) + BIND_PAT@[135; 147) + NAME@[135; 147) + IDENT@[135; 147) "fn_with_expr" + err: `expected COMMA` + err: `expected ident` + ERROR@[147; 148) + L_PAREN@[147; 148) + err: `expected COMMA` + IDENT@[148; 149) "x" + COLON@[149; 150) + WHITESPACE@[150; 151) + SLICE_PAT@[151; 159) + L_BRACK@[151; 152) + BIND_PAT@[152; 155) + NAME@[152; 155) + IDENT@[152; 155) "i32" + err: `expected COMMA` + err: `expected pattern` + ERROR@[155; 156) + SEMI@[155; 156) + err: `expected COMMA` + WHITESPACE@[156; 157) + LITERAL@[157; 158) + INT_NUMBER@[157; 158) "1" + R_BRACK@[158; 159) + err: `expected COMMA` + err: `expected ident` + ERROR@[159; 160) + R_PAREN@[159; 160) + err: `expected COMMA` + err: `expected ident` + ERROR@[160; 161) + SEMI@[160; 161) + WHITESPACE@[161; 162) + R_CURLY@[162; 163) + err: `expected COLON` + WHITESPACE@[163; 164) + err: `expected type` + err: `expected COMMA` + err: `expected R_PAREN` + err: `expected block` + err: `expected R_CURLY` + ERROR@[164; 164)