diff --git a/crates/parser/src/grammar/items.rs b/crates/parser/src/grammar/items.rs index 1057ca8c291..9763df63f22 100644 --- a/crates/parser/src/grammar/items.rs +++ b/crates/parser/src/grammar/items.rs @@ -93,6 +93,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> { }; let mut has_mods = false; + let mut has_extern = false; // modifiers if p.at(T![const]) && p.nth(1) != T!['{'] { @@ -102,7 +103,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> { // test_err async_without_semicolon // fn foo() { let _ = async {} } - if p.at(T![async]) && p.nth(1) != T!['{'] && p.nth(1) != T![move] && p.nth(1) != T![|] { + if p.at(T![async]) && !matches!(p.nth(1), T!['{'] | T![move] | T![|]) { p.eat(T![async]); has_mods = true; } @@ -114,7 +115,8 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> { has_mods = true; } - if p.at(T![extern]) && p.nth(1) != T!['{'] && (p.nth(1) != STRING || p.nth(2) != T!['{']) { + if p.at(T![extern]) { + has_extern = true; has_mods = true; abi(p); } @@ -211,25 +213,23 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> { type_alias(p, m); } + // test unsafe_extern_block // unsafe extern "C" {} - T![extern] => { - abi(p); + T!['{'] if has_extern => { extern_item_list(p); m.complete(p, EXTERN_BLOCK); } - _ => { - if !has_visibility && !has_mods { - return Err(m); + _ if has_visibility || has_mods => { + if has_mods { + p.error("expected existential, fn, trait or impl"); } else { - if has_mods { - p.error("expected existential, fn, trait or impl"); - } else { - p.error("expected an item"); - } - m.complete(p, ERROR); + p.error("expected an item"); } + m.complete(p, ERROR); } + + _ => return Err(m), } Ok(()) } diff --git a/crates/syntax/test_data/parser/err/0048_repated_extern_modifier.rast b/crates/syntax/test_data/parser/err/0048_repated_extern_modifier.rast new file mode 100644 index 00000000000..85e10ca36db --- /dev/null +++ b/crates/syntax/test_data/parser/err/0048_repated_extern_modifier.rast @@ -0,0 +1,15 @@ +SOURCE_FILE@0..22 + ERROR@0..10 + ABI@0..10 + EXTERN_KW@0..6 "extern" + WHITESPACE@6..7 " " + STRING@7..10 "\"C\"" + WHITESPACE@10..11 " " + ERROR@11..21 + ABI@11..21 + EXTERN_KW@11..17 "extern" + WHITESPACE@17..18 " " + STRING@18..21 "\"C\"" + WHITESPACE@21..22 "\n" +error 10..10: expected existential, fn, trait or impl +error 21..21: expected existential, fn, trait or impl diff --git a/crates/syntax/test_data/parser/err/0048_repated_extern_modifier.rs b/crates/syntax/test_data/parser/err/0048_repated_extern_modifier.rs new file mode 100644 index 00000000000..1fb18eaf1bc --- /dev/null +++ b/crates/syntax/test_data/parser/err/0048_repated_extern_modifier.rs @@ -0,0 +1 @@ +extern "C" extern "C" diff --git a/crates/syntax/test_data/parser/inline/ok/0167_unsafe_extern_block.rast b/crates/syntax/test_data/parser/inline/ok/0167_unsafe_extern_block.rast new file mode 100644 index 00000000000..8044e6674da --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0167_unsafe_extern_block.rast @@ -0,0 +1,13 @@ +SOURCE_FILE@0..21 + EXTERN_BLOCK@0..20 + UNSAFE_KW@0..6 "unsafe" + WHITESPACE@6..7 " " + ABI@7..17 + EXTERN_KW@7..13 "extern" + WHITESPACE@13..14 " " + STRING@14..17 "\"C\"" + WHITESPACE@17..18 " " + EXTERN_ITEM_LIST@18..20 + L_CURLY@18..19 "{" + R_CURLY@19..20 "}" + WHITESPACE@20..21 "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0167_unsafe_extern_block.rs b/crates/syntax/test_data/parser/inline/ok/0167_unsafe_extern_block.rs new file mode 100644 index 00000000000..9475aec15c1 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0167_unsafe_extern_block.rs @@ -0,0 +1 @@ +unsafe extern "C" {}