From 98718e0544f42e55642d2838b00d6a7bef1e2414 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 15 Jan 2021 21:07:38 +0100 Subject: Wrap remaining self/super/crate in Name{Ref} --- crates/syntax/src/ast/generated/nodes.rs | 7 +-- crates/syntax/src/ast/node_ext.rs | 33 +++++++++----- .../parser/err/0037_visibility_in_traits.rast | 10 ++++- .../parser/inline/ok/0006_self_param.rast | 15 ++++--- .../parser/inline/ok/0018_arb_self_types.rast | 6 ++- .../parser/inline/ok/0021_impl_item_list.rast | 3 +- .../parser/inline/ok/0022_crate_visibility.rast | 50 ++++++++++------------ .../parser/inline/ok/0022_crate_visibility.rs | 3 +- .../inline/ok/0138_self_param_outer_attr.rast | 3 +- .../parser/inline/ok/0160_crate_visibility_in.rast | 42 ++++++++++++++++++ .../parser/inline/ok/0160_crate_visibility_in.rs | 2 + .../test_data/parser/ok/0007_extern_crate.rast | 3 +- .../test_data/parser/ok/0012_visibility.rast | 10 ++++- .../parser/ok/0045_block_inner_attrs.rast | 3 +- .../test_data/parser/ok/0051_parameter_attrs.rast | 24 +++++++---- 15 files changed, 147 insertions(+), 67 deletions(-) create mode 100644 crates/syntax/test_data/parser/inline/ok/0160_crate_visibility_in.rast create mode 100644 crates/syntax/test_data/parser/inline/ok/0160_crate_visibility_in.rs (limited to 'crates/syntax') diff --git a/crates/syntax/src/ast/generated/nodes.rs b/crates/syntax/src/ast/generated/nodes.rs index 1d722db3c..6407d7c85 100644 --- a/crates/syntax/src/ast/generated/nodes.rs +++ b/crates/syntax/src/ast/generated/nodes.rs @@ -11,6 +11,7 @@ pub struct Name { } impl Name { pub fn ident_token(&self) -> Option { support::token(&self.syntax, T![ident]) } + pub fn self_token(&self) -> Option { support::token(&self.syntax, T![self]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct NameRef { @@ -238,7 +239,6 @@ impl ExternCrate { pub fn extern_token(&self) -> Option { support::token(&self.syntax, T![extern]) } pub fn crate_token(&self) -> Option { support::token(&self.syntax, T![crate]) } pub fn name_ref(&self) -> Option { support::child(&self.syntax) } - pub fn self_token(&self) -> Option { support::token(&self.syntax, T![self]) } pub fn rename(&self) -> Option { support::child(&self.syntax) } pub fn semicolon_token(&self) -> Option { support::token(&self.syntax, T![;]) } } @@ -406,9 +406,6 @@ pub struct Visibility { impl Visibility { pub fn pub_token(&self) -> Option { support::token(&self.syntax, T![pub]) } pub fn l_paren_token(&self) -> Option { support::token(&self.syntax, T!['(']) } - pub fn super_token(&self) -> Option { support::token(&self.syntax, T![super]) } - pub fn self_token(&self) -> Option { support::token(&self.syntax, T![self]) } - pub fn crate_token(&self) -> Option { support::token(&self.syntax, T![crate]) } pub fn in_token(&self) -> Option { support::token(&self.syntax, T![in]) } pub fn path(&self) -> Option { support::child(&self.syntax) } pub fn r_paren_token(&self) -> Option { support::token(&self.syntax, T![')']) } @@ -492,11 +489,11 @@ pub struct SelfParam { pub(crate) syntax: SyntaxNode, } impl ast::AttrsOwner for SelfParam {} +impl ast::NameOwner for SelfParam {} impl SelfParam { pub fn amp_token(&self) -> Option { support::token(&self.syntax, T![&]) } pub fn lifetime(&self) -> Option { support::child(&self.syntax) } pub fn mut_token(&self) -> Option { support::token(&self.syntax, T![mut]) } - pub fn self_token(&self) -> Option { support::token(&self.syntax, T![self]) } pub fn colon_token(&self) -> Option { support::token(&self.syntax, T![:]) } pub fn ty(&self) -> Option { support::child(&self.syntax) } } diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index b8ce71d27..738c92a5b 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -198,6 +198,13 @@ impl ast::Path { pub fn parent_path(&self) -> Option { self.syntax().parent().and_then(ast::Path::cast) } + + pub fn as_single_segment(&self) -> Option { + match self.qualifier() { + Some(_) => None, + None => self.segment(), + } + } } impl ast::UseTreeList { @@ -448,16 +455,22 @@ pub enum VisibilityKind { impl ast::Visibility { pub fn kind(&self) -> VisibilityKind { - if let Some(path) = support::children(self.syntax()).next() { - VisibilityKind::In(path) - } else if self.crate_token().is_some() { - VisibilityKind::PubCrate - } else if self.super_token().is_some() { - VisibilityKind::PubSuper - } else if self.self_token().is_some() { - VisibilityKind::PubSelf - } else { - VisibilityKind::Pub + match self.path() { + Some(path) => { + if let Some(segment) = + path.as_single_segment().filter(|it| it.coloncolon_token().is_none()) + { + if segment.crate_token().is_some() { + return VisibilityKind::PubCrate; + } else if segment.super_token().is_some() { + return VisibilityKind::PubSuper; + } else if segment.self_token().is_some() { + return VisibilityKind::PubSelf; + } + } + VisibilityKind::In(path) + } + None => VisibilityKind::Pub, } } } diff --git a/crates/syntax/test_data/parser/err/0037_visibility_in_traits.rast b/crates/syntax/test_data/parser/err/0037_visibility_in_traits.rast index faf87d6e5..ae4dd2f3b 100644 --- a/crates/syntax/test_data/parser/err/0037_visibility_in_traits.rast +++ b/crates/syntax/test_data/parser/err/0037_visibility_in_traits.rast @@ -50,7 +50,10 @@ SOURCE_FILE@0..118 VISIBILITY@56..66 PUB_KW@56..59 "pub" L_PAREN@59..60 "(" - CRATE_KW@60..65 "crate" + PATH@60..65 + PATH_SEGMENT@60..65 + NAME_REF@60..65 + CRATE_KW@60..65 "crate" R_PAREN@65..66 ")" WHITESPACE@66..67 " " TYPE_KW@67..71 "type" @@ -69,7 +72,10 @@ SOURCE_FILE@0..118 VISIBILITY@86..96 PUB_KW@86..89 "pub" L_PAREN@89..90 "(" - CRATE_KW@90..95 "crate" + PATH@90..95 + PATH_SEGMENT@90..95 + NAME_REF@90..95 + CRATE_KW@90..95 "crate" R_PAREN@95..96 ")" WHITESPACE@96..97 " " CONST_KW@97..102 "const" diff --git a/crates/syntax/test_data/parser/inline/ok/0006_self_param.rast b/crates/syntax/test_data/parser/inline/ok/0006_self_param.rast index 8048f5fad..f0d152d33 100644 --- a/crates/syntax/test_data/parser/inline/ok/0006_self_param.rast +++ b/crates/syntax/test_data/parser/inline/ok/0006_self_param.rast @@ -19,7 +19,8 @@ SOURCE_FILE@0..128 PARAM_LIST@17..23 L_PAREN@17..18 "(" SELF_PARAM@18..22 - SELF_KW@18..22 "self" + NAME@18..22 + SELF_KW@18..22 "self" R_PAREN@22..23 ")" WHITESPACE@23..24 " " BLOCK_EXPR@24..26 @@ -35,7 +36,8 @@ SOURCE_FILE@0..128 L_PAREN@35..36 "(" SELF_PARAM@36..41 AMP@36..37 "&" - SELF_KW@37..41 "self" + NAME@37..41 + SELF_KW@37..41 "self" COMMA@41..42 "," R_PAREN@42..43 ")" WHITESPACE@43..44 " " @@ -55,7 +57,8 @@ SOURCE_FILE@0..128 LIFETIME@57..59 LIFETIME_IDENT@57..59 "\'a" WHITESPACE@59..60 " " - SELF_KW@60..64 "self" + NAME@60..64 + SELF_KW@60..64 "self" COMMA@64..65 "," R_PAREN@65..66 ")" WHITESPACE@66..67 " " @@ -77,7 +80,8 @@ SOURCE_FILE@0..128 WHITESPACE@82..83 " " MUT_KW@83..86 "mut" WHITESPACE@86..87 " " - SELF_KW@87..91 "self" + NAME@87..91 + SELF_KW@87..91 "self" COMMA@91..92 "," WHITESPACE@92..93 " " PARAM@93..99 @@ -107,7 +111,8 @@ SOURCE_FILE@0..128 SELF_PARAM@113..121 MUT_KW@113..116 "mut" WHITESPACE@116..117 " " - SELF_KW@117..121 "self" + NAME@117..121 + SELF_KW@117..121 "self" R_PAREN@121..122 ")" WHITESPACE@122..123 " " BLOCK_EXPR@123..125 diff --git a/crates/syntax/test_data/parser/inline/ok/0018_arb_self_types.rast b/crates/syntax/test_data/parser/inline/ok/0018_arb_self_types.rast index ddbd66588..df59f37a2 100644 --- a/crates/syntax/test_data/parser/inline/ok/0018_arb_self_types.rast +++ b/crates/syntax/test_data/parser/inline/ok/0018_arb_self_types.rast @@ -19,7 +19,8 @@ SOURCE_FILE@0..69 PARAM_LIST@17..30 L_PAREN@17..18 "(" SELF_PARAM@18..29 - SELF_KW@18..22 "self" + NAME@18..22 + SELF_KW@18..22 "self" COLON@22..23 ":" WHITESPACE@23..24 " " REF_TYPE@24..29 @@ -45,7 +46,8 @@ SOURCE_FILE@0..69 SELF_PARAM@43..62 MUT_KW@43..46 "mut" WHITESPACE@46..47 " " - SELF_KW@47..51 "self" + NAME@47..51 + SELF_KW@47..51 "self" COLON@51..52 ":" WHITESPACE@52..53 " " PATH_TYPE@53..62 diff --git a/crates/syntax/test_data/parser/inline/ok/0021_impl_item_list.rast b/crates/syntax/test_data/parser/inline/ok/0021_impl_item_list.rast index ca0702aba..dc7f6295b 100644 --- a/crates/syntax/test_data/parser/inline/ok/0021_impl_item_list.rast +++ b/crates/syntax/test_data/parser/inline/ok/0021_impl_item_list.rast @@ -67,7 +67,8 @@ SOURCE_FILE@0..89 L_PAREN@76..77 "(" SELF_PARAM@77..82 AMP@77..78 "&" - SELF_KW@78..82 "self" + NAME@78..82 + SELF_KW@78..82 "self" R_PAREN@82..83 ")" WHITESPACE@83..84 " " BLOCK_EXPR@84..86 diff --git a/crates/syntax/test_data/parser/inline/ok/0022_crate_visibility.rast b/crates/syntax/test_data/parser/inline/ok/0022_crate_visibility.rast index 50742cbcf..f2ead8a62 100644 --- a/crates/syntax/test_data/parser/inline/ok/0022_crate_visibility.rast +++ b/crates/syntax/test_data/parser/inline/ok/0022_crate_visibility.rast @@ -1,9 +1,12 @@ -SOURCE_FILE@0..81 +SOURCE_FILE@0..62 STRUCT@0..20 VISIBILITY@0..10 PUB_KW@0..3 "pub" L_PAREN@3..4 "(" - CRATE_KW@4..9 "crate" + PATH@4..9 + PATH_SEGMENT@4..9 + NAME_REF@4..9 + CRATE_KW@4..9 "crate" R_PAREN@9..10 ")" WHITESPACE@10..11 " " STRUCT_KW@11..17 "struct" @@ -16,7 +19,10 @@ SOURCE_FILE@0..81 VISIBILITY@21..30 PUB_KW@21..24 "pub" L_PAREN@24..25 "(" - SELF_KW@25..29 "self" + PATH@25..29 + PATH_SEGMENT@25..29 + NAME_REF@25..29 + SELF_KW@25..29 "self" R_PAREN@29..30 ")" WHITESPACE@30..31 " " STRUCT_KW@31..37 "struct" @@ -25,29 +31,19 @@ SOURCE_FILE@0..81 IDENT@38..39 "S" SEMICOLON@39..40 ";" WHITESPACE@40..41 "\n" - STRUCT@41..60 - VISIBILITY@41..50 + STRUCT@41..61 + VISIBILITY@41..51 PUB_KW@41..44 "pub" L_PAREN@44..45 "(" - SELF_KW@45..49 "self" - R_PAREN@49..50 ")" - WHITESPACE@50..51 " " - STRUCT_KW@51..57 "struct" - WHITESPACE@57..58 " " - NAME@58..59 - IDENT@58..59 "S" - SEMICOLON@59..60 ";" - WHITESPACE@60..61 "\n" - STRUCT@61..80 - VISIBILITY@61..70 - PUB_KW@61..64 "pub" - L_PAREN@64..65 "(" - SELF_KW@65..69 "self" - R_PAREN@69..70 ")" - WHITESPACE@70..71 " " - STRUCT_KW@71..77 "struct" - WHITESPACE@77..78 " " - NAME@78..79 - IDENT@78..79 "S" - SEMICOLON@79..80 ";" - WHITESPACE@80..81 "\n" + PATH@45..50 + PATH_SEGMENT@45..50 + NAME_REF@45..50 + SUPER_KW@45..50 "super" + R_PAREN@50..51 ")" + WHITESPACE@51..52 " " + STRUCT_KW@52..58 "struct" + WHITESPACE@58..59 " " + NAME@59..60 + IDENT@59..60 "S" + SEMICOLON@60..61 ";" + WHITESPACE@61..62 "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0022_crate_visibility.rs b/crates/syntax/test_data/parser/inline/ok/0022_crate_visibility.rs index faeefde94..a790a485f 100644 --- a/crates/syntax/test_data/parser/inline/ok/0022_crate_visibility.rs +++ b/crates/syntax/test_data/parser/inline/ok/0022_crate_visibility.rs @@ -1,4 +1,3 @@ pub(crate) struct S; pub(self) struct S; -pub(self) struct S; -pub(self) struct S; +pub(super) struct S; diff --git a/crates/syntax/test_data/parser/inline/ok/0138_self_param_outer_attr.rast b/crates/syntax/test_data/parser/inline/ok/0138_self_param_outer_attr.rast index d3219f0b2..c54e64e3f 100644 --- a/crates/syntax/test_data/parser/inline/ok/0138_self_param_outer_attr.rast +++ b/crates/syntax/test_data/parser/inline/ok/0138_self_param_outer_attr.rast @@ -16,7 +16,8 @@ SOURCE_FILE@0..26 IDENT@7..15 "must_use" R_BRACK@15..16 "]" WHITESPACE@16..17 " " - SELF_KW@17..21 "self" + NAME@17..21 + SELF_KW@17..21 "self" R_PAREN@21..22 ")" WHITESPACE@22..23 " " BLOCK_EXPR@23..25 diff --git a/crates/syntax/test_data/parser/inline/ok/0160_crate_visibility_in.rast b/crates/syntax/test_data/parser/inline/ok/0160_crate_visibility_in.rast new file mode 100644 index 000000000..3d855fc6b --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0160_crate_visibility_in.rast @@ -0,0 +1,42 @@ +SOURCE_FILE@0..51 + STRUCT@0..26 + VISIBILITY@0..16 + PUB_KW@0..3 "pub" + L_PAREN@3..4 "(" + IN_KW@4..6 "in" + WHITESPACE@6..7 " " + PATH@7..15 + PATH@7..12 + PATH_SEGMENT@7..12 + NAME_REF@7..12 + SUPER_KW@7..12 "super" + COLON2@12..14 "::" + PATH_SEGMENT@14..15 + NAME_REF@14..15 + IDENT@14..15 "A" + R_PAREN@15..16 ")" + WHITESPACE@16..17 " " + STRUCT_KW@17..23 "struct" + WHITESPACE@23..24 " " + NAME@24..25 + IDENT@24..25 "S" + SEMICOLON@25..26 ";" + WHITESPACE@26..27 "\n" + STRUCT@27..50 + VISIBILITY@27..40 + PUB_KW@27..30 "pub" + L_PAREN@30..31 "(" + IN_KW@31..33 "in" + WHITESPACE@33..34 " " + PATH@34..39 + PATH_SEGMENT@34..39 + NAME_REF@34..39 + CRATE_KW@34..39 "crate" + R_PAREN@39..40 ")" + WHITESPACE@40..41 " " + STRUCT_KW@41..47 "struct" + WHITESPACE@47..48 " " + NAME@48..49 + IDENT@48..49 "S" + SEMICOLON@49..50 ";" + WHITESPACE@50..51 "\n" diff --git a/crates/syntax/test_data/parser/inline/ok/0160_crate_visibility_in.rs b/crates/syntax/test_data/parser/inline/ok/0160_crate_visibility_in.rs new file mode 100644 index 000000000..2856dbd84 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/ok/0160_crate_visibility_in.rs @@ -0,0 +1,2 @@ +pub(in super::A) struct S; +pub(in crate) struct S; diff --git a/crates/syntax/test_data/parser/ok/0007_extern_crate.rast b/crates/syntax/test_data/parser/ok/0007_extern_crate.rast index 594c2f8f2..4babdba92 100644 --- a/crates/syntax/test_data/parser/ok/0007_extern_crate.rast +++ b/crates/syntax/test_data/parser/ok/0007_extern_crate.rast @@ -28,7 +28,8 @@ SOURCE_FILE@0..69 WHITESPACE@49..50 " " CRATE_KW@50..55 "crate" WHITESPACE@55..56 " " - SELF_KW@56..60 "self" + NAME_REF@56..60 + SELF_KW@56..60 "self" WHITESPACE@60..61 " " RENAME@61..67 AS_KW@61..63 "as" diff --git a/crates/syntax/test_data/parser/ok/0012_visibility.rast b/crates/syntax/test_data/parser/ok/0012_visibility.rast index 83a93b5a9..c5dbfb702 100644 --- a/crates/syntax/test_data/parser/ok/0012_visibility.rast +++ b/crates/syntax/test_data/parser/ok/0012_visibility.rast @@ -32,7 +32,10 @@ SOURCE_FILE@0..98 VISIBILITY@24..34 PUB_KW@24..27 "pub" L_PAREN@27..28 "(" - CRATE_KW@28..33 "crate" + PATH@28..33 + PATH_SEGMENT@28..33 + NAME_REF@28..33 + CRATE_KW@28..33 "crate" R_PAREN@33..34 ")" WHITESPACE@34..35 " " FN_KW@35..37 "fn" @@ -51,7 +54,10 @@ SOURCE_FILE@0..98 VISIBILITY@45..55 PUB_KW@45..48 "pub" L_PAREN@48..49 "(" - SUPER_KW@49..54 "super" + PATH@49..54 + PATH_SEGMENT@49..54 + NAME_REF@49..54 + SUPER_KW@49..54 "super" R_PAREN@54..55 ")" WHITESPACE@55..56 " " FN_KW@56..58 "fn" diff --git a/crates/syntax/test_data/parser/ok/0045_block_inner_attrs.rast b/crates/syntax/test_data/parser/ok/0045_block_inner_attrs.rast index 0ac56df6d..6afed5f05 100644 --- a/crates/syntax/test_data/parser/ok/0045_block_inner_attrs.rast +++ b/crates/syntax/test_data/parser/ok/0045_block_inner_attrs.rast @@ -110,7 +110,8 @@ SOURCE_FILE@0..686 L_PAREN@558..559 "(" SELF_PARAM@559..564 AMP@559..560 "&" - SELF_KW@560..564 "self" + NAME@560..564 + SELF_KW@560..564 "self" COMMA@564..565 "," WHITESPACE@565..566 " " PARAM@566..600 diff --git a/crates/syntax/test_data/parser/ok/0051_parameter_attrs.rast b/crates/syntax/test_data/parser/ok/0051_parameter_attrs.rast index 3fed11838..e10521d85 100644 --- a/crates/syntax/test_data/parser/ok/0051_parameter_attrs.rast +++ b/crates/syntax/test_data/parser/ok/0051_parameter_attrs.rast @@ -281,7 +281,8 @@ SOURCE_FILE@0..519 IDENT@259..267 "must_use" R_BRACK@267..268 "]" WHITESPACE@268..269 " " - SELF_KW@269..273 "self" + NAME@269..273 + SELF_KW@269..273 "self" R_PAREN@273..274 ")" WHITESPACE@274..275 " " BLOCK_EXPR@275..277 @@ -305,7 +306,8 @@ SOURCE_FILE@0..519 IDENT@291..295 "attr" R_BRACK@295..296 "]" WHITESPACE@296..297 " " - SELF_KW@297..301 "self" + NAME@297..301 + SELF_KW@297..301 "self" R_PAREN@301..302 ")" WHITESPACE@302..303 " " BLOCK_EXPR@303..305 @@ -330,7 +332,8 @@ SOURCE_FILE@0..519 R_BRACK@323..324 "]" WHITESPACE@324..325 " " AMP@325..326 "&" - SELF_KW@326..330 "self" + NAME@326..330 + SELF_KW@326..330 "self" R_PAREN@330..331 ")" WHITESPACE@331..332 " " BLOCK_EXPR@332..334 @@ -363,7 +366,8 @@ SOURCE_FILE@0..519 AMP@358..359 "&" MUT_KW@359..362 "mut" WHITESPACE@362..363 " " - SELF_KW@363..367 "self" + NAME@363..367 + SELF_KW@363..367 "self" R_PAREN@367..368 ")" WHITESPACE@368..369 " " BLOCK_EXPR@369..371 @@ -397,7 +401,8 @@ SOURCE_FILE@0..519 LIFETIME@396..398 LIFETIME_IDENT@396..398 "\'a" WHITESPACE@398..399 " " - SELF_KW@399..403 "self" + NAME@399..403 + SELF_KW@399..403 "self" R_PAREN@403..404 ")" WHITESPACE@404..405 " " BLOCK_EXPR@405..407 @@ -433,7 +438,8 @@ SOURCE_FILE@0..519 WHITESPACE@434..435 " " MUT_KW@435..438 "mut" WHITESPACE@438..439 " " - SELF_KW@439..443 "self" + NAME@439..443 + SELF_KW@439..443 "self" R_PAREN@443..444 ")" WHITESPACE@444..445 " " BLOCK_EXPR@445..447 @@ -457,7 +463,8 @@ SOURCE_FILE@0..519 IDENT@460..464 "attr" R_BRACK@464..465 "]" WHITESPACE@465..466 " " - SELF_KW@466..470 "self" + NAME@466..470 + SELF_KW@466..470 "self" COLON@470..471 ":" WHITESPACE@471..472 " " PATH_TYPE@472..476 @@ -488,7 +495,8 @@ SOURCE_FILE@0..519 IDENT@493..497 "attr" R_BRACK@497..498 "]" WHITESPACE@498..499 " " - SELF_KW@499..503 "self" + NAME@499..503 + SELF_KW@499..503 "self" COLON@503..504 ":" WHITESPACE@504..505 " " PATH_TYPE@505..513 -- cgit v1.2.3