From de930237ffb5bec9489443477a5b4ff964f56b0e Mon Sep 17 00:00:00 2001 From: zjy Date: Wed, 26 Jun 2019 11:36:14 +0800 Subject: fixed #1384 --- crates/ra_parser/src/grammar/expressions.rs | 11 ++++ crates/ra_parser/src/grammar/items.rs | 16 +++--- .../inline/ok/0134_nocontentexpr_after_item.rs | 8 +++ .../inline/ok/0134_nocontentexpr_after_item.txt | 62 ++++++++++++++++++++++ 4 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 crates/ra_syntax/tests/data/parser/inline/ok/0134_nocontentexpr_after_item.rs create mode 100644 crates/ra_syntax/tests/data/parser/inline/ok/0134_nocontentexpr_after_item.txt (limited to 'crates') diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 795dccea1..298030cb9 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -181,6 +181,17 @@ pub(crate) fn expr_block_contents(p: &mut Parser) { // fn foo(){ // ;;;some_expr();;;;{;;;};;;;Ok(()) // } + + // test nocontentexpr_after_item + // fn simple_function() { + // enum LocalEnum { + // One, + // Two, + // }; + // fn f() {}; + // struct S {}; + // } + if p.current() == T![;] { p.bump(); continue; diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs index 424d0476d..543af7c4b 100644 --- a/crates/ra_parser/src/grammar/items.rs +++ b/crates/ra_parser/src/grammar/items.rs @@ -38,7 +38,15 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemF let m = p.start(); attributes::outer_attributes(p); let m = match maybe_item(p, m, flavor) { - Ok(()) => return, + Ok(()) => { + if p.at(T![;]) { + p.err_and_bump( + "expected item, found `;`\n\ + consider removing this semicolon", + ); + } + return; + } Err(m) => m, }; if paths::is_path_start(p) { @@ -263,12 +271,6 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> { } _ => return Err(m), }; - if p.at(T![;]) { - p.err_and_bump( - "expected item, found `;`\n\ - consider removing this semicolon", - ); - } Ok(()) } diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0134_nocontentexpr_after_item.rs b/crates/ra_syntax/tests/data/parser/inline/ok/0134_nocontentexpr_after_item.rs new file mode 100644 index 000000000..eadc7fffb --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0134_nocontentexpr_after_item.rs @@ -0,0 +1,8 @@ +fn simple_function() { + enum LocalEnum { + One, + Two, + }; + fn f() {}; + struct S {}; +} diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0134_nocontentexpr_after_item.txt b/crates/ra_syntax/tests/data/parser/inline/ok/0134_nocontentexpr_after_item.txt new file mode 100644 index 000000000..50d3b5def --- /dev/null +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0134_nocontentexpr_after_item.txt @@ -0,0 +1,62 @@ +SOURCE_FILE@[0; 111) + FN_DEF@[0; 110) + FN_KW@[0; 2) "fn" + WHITESPACE@[2; 3) " " + NAME@[3; 18) + IDENT@[3; 18) "simple_function" + PARAM_LIST@[18; 20) + L_PAREN@[18; 19) "(" + R_PAREN@[19; 20) ")" + WHITESPACE@[20; 21) " " + BLOCK@[21; 110) + L_CURLY@[21; 22) "{" + WHITESPACE@[22; 27) "\n " + ENUM_DEF@[27; 75) + ENUM_KW@[27; 31) "enum" + WHITESPACE@[31; 32) " " + NAME@[32; 41) + IDENT@[32; 41) "LocalEnum" + WHITESPACE@[41; 42) " " + ENUM_VARIANT_LIST@[42; 75) + L_CURLY@[42; 43) "{" + WHITESPACE@[43; 52) "\n " + ENUM_VARIANT@[52; 55) + NAME@[52; 55) + IDENT@[52; 55) "One" + COMMA@[55; 56) "," + WHITESPACE@[56; 65) "\n " + ENUM_VARIANT@[65; 68) + NAME@[65; 68) + IDENT@[65; 68) "Two" + COMMA@[68; 69) "," + WHITESPACE@[69; 74) "\n " + R_CURLY@[74; 75) "}" + SEMI@[75; 76) ";" + WHITESPACE@[76; 81) "\n " + FN_DEF@[81; 90) + FN_KW@[81; 83) "fn" + WHITESPACE@[83; 84) " " + NAME@[84; 85) + IDENT@[84; 85) "f" + PARAM_LIST@[85; 87) + L_PAREN@[85; 86) "(" + R_PAREN@[86; 87) ")" + WHITESPACE@[87; 88) " " + BLOCK@[88; 90) + L_CURLY@[88; 89) "{" + R_CURLY@[89; 90) "}" + SEMI@[90; 91) ";" + WHITESPACE@[91; 96) "\n " + STRUCT_DEF@[96; 107) + STRUCT_KW@[96; 102) "struct" + WHITESPACE@[102; 103) " " + NAME@[103; 104) + IDENT@[103; 104) "S" + WHITESPACE@[104; 105) " " + NAMED_FIELD_DEF_LIST@[105; 107) + L_CURLY@[105; 106) "{" + R_CURLY@[106; 107) "}" + SEMI@[107; 108) ";" + WHITESPACE@[108; 109) "\n" + R_CURLY@[109; 110) "}" + WHITESPACE@[110; 111) "\n" -- cgit v1.2.3