From 004971f3f07140fe2c9eb5c63c2a103b90018da6 Mon Sep 17 00:00:00 2001 From: Ryo Yoshida Date: Sat, 15 Jul 2023 23:55:57 +0900 Subject: [PATCH] Remove `crate` visibility modifier --- crates/parser/src/grammar.rs | 96 ++++++++----------- crates/parser/src/tests/prefix_entries.rs | 1 - .../inline/ok/0040_crate_keyword_vis.rast | 63 ------------ .../inline/ok/0040_crate_keyword_vis.rs | 3 - .../inline/ok/0125_crate_keyword_path.rast | 33 ------- .../inline/ok/0125_crate_keyword_path.rs | 1 - 6 files changed, 40 insertions(+), 157 deletions(-) delete mode 100644 crates/parser/test_data/parser/inline/ok/0040_crate_keyword_vis.rast delete mode 100644 crates/parser/test_data/parser/inline/ok/0040_crate_keyword_vis.rs delete mode 100644 crates/parser/test_data/parser/inline/ok/0125_crate_keyword_path.rast delete mode 100644 crates/parser/test_data/parser/inline/ok/0125_crate_keyword_path.rs diff --git a/crates/parser/src/grammar.rs b/crates/parser/src/grammar.rs index 1814e0e54c..a868419821 100644 --- a/crates/parser/src/grammar.rs +++ b/crates/parser/src/grammar.rs @@ -211,70 +211,54 @@ impl BlockLike { const VISIBILITY_FIRST: TokenSet = TokenSet::new(&[T![pub], T![crate]]); fn opt_visibility(p: &mut Parser<'_>, in_tuple_field: bool) -> bool { - match p.current() { - T![pub] => { - let m = p.start(); - p.bump(T![pub]); - if p.at(T!['(']) { - match p.nth(1) { - // test crate_visibility - // pub(crate) struct S; - // pub(self) struct S; - // pub(super) struct S; + if !p.at(T![pub]) { + return false; + } - // test_err crate_visibility_empty_recover - // pub() struct S; + let m = p.start(); + p.bump(T![pub]); + if p.at(T!['(']) { + match p.nth(1) { + // test crate_visibility + // pub(crate) struct S; + // pub(self) struct S; + // pub(super) struct S; - // test pub_parens_typepath - // struct B(pub (super::A)); - // struct B(pub (crate::A,)); - T![crate] | T![self] | T![super] | T![ident] | T![')'] if p.nth(2) != T![:] => { - // If we are in a tuple struct, then the parens following `pub` - // might be an tuple field, not part of the visibility. So in that - // case we don't want to consume an identifier. + // test_err crate_visibility_empty_recover + // pub() struct S; - // test pub_tuple_field - // struct MyStruct(pub (u32, u32)); - // struct MyStruct(pub (u32)); - // struct MyStruct(pub ()); - if !(in_tuple_field && matches!(p.nth(1), T![ident] | T![')'])) { - p.bump(T!['(']); - paths::use_path(p); - p.expect(T![')']); - } - } - // test crate_visibility_in - // pub(in super::A) struct S; - // pub(in crate) struct S; - T![in] => { - p.bump(T!['(']); - p.bump(T![in]); - paths::use_path(p); - p.expect(T![')']); - } - _ => {} + // test pub_parens_typepath + // struct B(pub (super::A)); + // struct B(pub (crate::A,)); + T![crate] | T![self] | T![super] | T![ident] | T![')'] if p.nth(2) != T![:] => { + // If we are in a tuple struct, then the parens following `pub` + // might be an tuple field, not part of the visibility. So in that + // case we don't want to consume an identifier. + + // test pub_tuple_field + // struct MyStruct(pub (u32, u32)); + // struct MyStruct(pub (u32)); + // struct MyStruct(pub ()); + if !(in_tuple_field && matches!(p.nth(1), T![ident] | T![')'])) { + p.bump(T!['(']); + paths::use_path(p); + p.expect(T![')']); } } - m.complete(p, VISIBILITY); - true - } - // test crate_keyword_vis - // crate fn main() { } - // struct S { crate field: u32 } - // struct T(crate u32); - T![crate] => { - if p.nth_at(1, T![::]) { - // test crate_keyword_path - // fn foo() { crate::foo(); } - return false; + // test crate_visibility_in + // pub(in super::A) struct S; + // pub(in crate) struct S; + T![in] => { + p.bump(T!['(']); + p.bump(T![in]); + paths::use_path(p); + p.expect(T![')']); } - let m = p.start(); - p.bump(T![crate]); - m.complete(p, VISIBILITY); - true + _ => {} } - _ => false, } + m.complete(p, VISIBILITY); + true } fn opt_rename(p: &mut Parser<'_>) { diff --git a/crates/parser/src/tests/prefix_entries.rs b/crates/parser/src/tests/prefix_entries.rs index 11f9c34abd..2f3c7febc0 100644 --- a/crates/parser/src/tests/prefix_entries.rs +++ b/crates/parser/src/tests/prefix_entries.rs @@ -6,7 +6,6 @@ fn vis() { check(PrefixEntryPoint::Vis, "fn foo() {}", ""); check(PrefixEntryPoint::Vis, "pub(fn foo() {}", "pub"); check(PrefixEntryPoint::Vis, "pub(crate fn foo() {}", "pub(crate"); - check(PrefixEntryPoint::Vis, "crate fn foo() {}", "crate"); } #[test] diff --git a/crates/parser/test_data/parser/inline/ok/0040_crate_keyword_vis.rast b/crates/parser/test_data/parser/inline/ok/0040_crate_keyword_vis.rast deleted file mode 100644 index 07b0210e44..0000000000 --- a/crates/parser/test_data/parser/inline/ok/0040_crate_keyword_vis.rast +++ /dev/null @@ -1,63 +0,0 @@ -SOURCE_FILE - FN - VISIBILITY - CRATE_KW "crate" - WHITESPACE " " - FN_KW "fn" - WHITESPACE " " - NAME - IDENT "main" - PARAM_LIST - L_PAREN "(" - R_PAREN ")" - WHITESPACE " " - BLOCK_EXPR - STMT_LIST - L_CURLY "{" - WHITESPACE " " - R_CURLY "}" - WHITESPACE "\n" - STRUCT - STRUCT_KW "struct" - WHITESPACE " " - NAME - IDENT "S" - WHITESPACE " " - RECORD_FIELD_LIST - L_CURLY "{" - WHITESPACE " " - RECORD_FIELD - VISIBILITY - CRATE_KW "crate" - WHITESPACE " " - NAME - IDENT "field" - COLON ":" - WHITESPACE " " - PATH_TYPE - PATH - PATH_SEGMENT - NAME_REF - IDENT "u32" - WHITESPACE " " - R_CURLY "}" - WHITESPACE "\n" - STRUCT - STRUCT_KW "struct" - WHITESPACE " " - NAME - IDENT "T" - TUPLE_FIELD_LIST - L_PAREN "(" - TUPLE_FIELD - VISIBILITY - CRATE_KW "crate" - WHITESPACE " " - PATH_TYPE - PATH - PATH_SEGMENT - NAME_REF - IDENT "u32" - R_PAREN ")" - SEMICOLON ";" - WHITESPACE "\n" diff --git a/crates/parser/test_data/parser/inline/ok/0040_crate_keyword_vis.rs b/crates/parser/test_data/parser/inline/ok/0040_crate_keyword_vis.rs deleted file mode 100644 index e2b5f2161d..0000000000 --- a/crates/parser/test_data/parser/inline/ok/0040_crate_keyword_vis.rs +++ /dev/null @@ -1,3 +0,0 @@ -crate fn main() { } -struct S { crate field: u32 } -struct T(crate u32); diff --git a/crates/parser/test_data/parser/inline/ok/0125_crate_keyword_path.rast b/crates/parser/test_data/parser/inline/ok/0125_crate_keyword_path.rast deleted file mode 100644 index 8d9b61630a..0000000000 --- a/crates/parser/test_data/parser/inline/ok/0125_crate_keyword_path.rast +++ /dev/null @@ -1,33 +0,0 @@ -SOURCE_FILE - FN - FN_KW "fn" - WHITESPACE " " - NAME - IDENT "foo" - PARAM_LIST - L_PAREN "(" - R_PAREN ")" - WHITESPACE " " - BLOCK_EXPR - STMT_LIST - L_CURLY "{" - WHITESPACE " " - EXPR_STMT - CALL_EXPR - PATH_EXPR - PATH - PATH - PATH_SEGMENT - NAME_REF - CRATE_KW "crate" - COLON2 "::" - PATH_SEGMENT - NAME_REF - IDENT "foo" - ARG_LIST - L_PAREN "(" - R_PAREN ")" - SEMICOLON ";" - WHITESPACE " " - R_CURLY "}" - WHITESPACE "\n" diff --git a/crates/parser/test_data/parser/inline/ok/0125_crate_keyword_path.rs b/crates/parser/test_data/parser/inline/ok/0125_crate_keyword_path.rs deleted file mode 100644 index 0f454d121d..0000000000 --- a/crates/parser/test_data/parser/inline/ok/0125_crate_keyword_path.rs +++ /dev/null @@ -1 +0,0 @@ -fn foo() { crate::foo(); }