Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22169 from ChayimFriedman2/impl-restriction-order
fix: Parse impl restrictions after the visibility
Chayim Refael Friedman 4 weeks ago
parent b58d6e5 · parent c058dac · commit 04ec801
-rw-r--r--crates/parser/src/grammar/items.rs38
-rw-r--r--crates/parser/test_data/parser/inline/ok/impl_restrictions.rast4
-rw-r--r--crates/parser/test_data/parser/inline/ok/impl_restrictions.rs2
3 files changed, 22 insertions, 22 deletions
diff --git a/crates/parser/src/grammar/items.rs b/crates/parser/src/grammar/items.rs
index c5c6e04dd4..88a4397232 100644
--- a/crates/parser/src/grammar/items.rs
+++ b/crates/parser/src/grammar/items.rs
@@ -119,6 +119,25 @@ pub(super) fn opt_item(p: &mut Parser<'_>, m: Marker, is_in_extern: bool) -> Res
let mut has_mods = false;
let mut has_extern = false;
+ if p.at(T![impl])
+ && p.nth(1) == T!['(']
+ && ((matches!(p.nth(2), T![crate] | T![super] | T![self]) && p.nth(3) == T![')'])
+ || p.nth(2) == T![in])
+ {
+ // test impl_restrictions
+ // pub impl(crate) unsafe trait Foo {}
+ // impl(in super::bar) trait Bar {}
+ // impl () {}
+ // impl (i32) {}
+ let m = p.start();
+ p.bump(T![impl]);
+ if !opt_visibility_inner(p, false) {
+ p.error("expected an impl restriction");
+ }
+ m.complete(p, IMPL_RESTRICTION);
+ has_mods = true;
+ }
+
// modifiers
if p.at(T![const]) && p.nth(1) != T!['{'] {
p.eat(T![const]);
@@ -167,25 +186,6 @@ pub(super) fn opt_item(p: &mut Parser<'_>, m: Marker, is_in_extern: bool) -> Res
has_mods = true;
}
- if p.at(T![impl])
- && p.nth(1) == T!['(']
- && ((matches!(p.nth(2), T![crate] | T![super] | T![self]) && p.nth(3) == T![')'])
- || p.nth(2) == T![in])
- {
- // test impl_restrictions
- // pub unsafe impl(crate) trait Foo {}
- // impl(in super::bar) trait Bar {}
- // impl () {}
- // impl (i32) {}
- let m = p.start();
- p.bump(T![impl]);
- if !opt_visibility_inner(p, false) {
- p.error("expected an impl restriction");
- }
- m.complete(p, IMPL_RESTRICTION);
- has_mods = true;
- }
-
// test default_item
// default impl T for Foo {}
if p.at_contextual_kw(T![default]) {
diff --git a/crates/parser/test_data/parser/inline/ok/impl_restrictions.rast b/crates/parser/test_data/parser/inline/ok/impl_restrictions.rast
index 5f2680cbaa..bf7b5c5a34 100644
--- a/crates/parser/test_data/parser/inline/ok/impl_restrictions.rast
+++ b/crates/parser/test_data/parser/inline/ok/impl_restrictions.rast
@@ -3,8 +3,6 @@ SOURCE_FILE
VISIBILITY
PUB_KW "pub"
WHITESPACE " "
- UNSAFE_KW "unsafe"
- WHITESPACE " "
IMPL_RESTRICTION
IMPL_KW "impl"
VISIBILITY_INNER
@@ -15,6 +13,8 @@ SOURCE_FILE
CRATE_KW "crate"
R_PAREN ")"
WHITESPACE " "
+ UNSAFE_KW "unsafe"
+ WHITESPACE " "
TRAIT_KW "trait"
WHITESPACE " "
NAME
diff --git a/crates/parser/test_data/parser/inline/ok/impl_restrictions.rs b/crates/parser/test_data/parser/inline/ok/impl_restrictions.rs
index 0a46b158af..429ac93ad5 100644
--- a/crates/parser/test_data/parser/inline/ok/impl_restrictions.rs
+++ b/crates/parser/test_data/parser/inline/ok/impl_restrictions.rs
@@ -1,4 +1,4 @@
-pub unsafe impl(crate) trait Foo {}
+pub impl(crate) unsafe trait Foo {}
impl(in super::bar) trait Bar {}
impl () {}
impl (i32) {}