Repository: rust-analyzer/rust-analyzer
Pull request #5585 (2020-07-30 11:29:45)

Finish extern crate grammar
bors r+
🤖


Patch:
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index c33b645f31..bfd574c5dc 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -671,7 +671,7 @@ impl ExprCollector<'_> {
                     ast::Item::ExternBlock(_) => return None, // FIXME: collect from extern blocks
                     ast::Item::ImplDef(_)
                     | ast::Item::UseItem(_)
-                    | ast::Item::ExternCrateItem(_)
+                    | ast::Item::ExternCrate(_)
                     | ast::Item::Module(_)
                     | ast::Item::MacroCall(_) => return None,
                 };
diff --git a/crates/ra_hir_def/src/item_tree.rs b/crates/ra_hir_def/src/item_tree.rs
index 615c1e14ca..4db7b2793f 100644
--- a/crates/ra_hir_def/src/item_tree.rs
+++ b/crates/ra_hir_def/src/item_tree.rs
@@ -412,7 +412,7 @@ macro_rules! mod_items {
 
 mod_items! {
     Import in imports -> ast::UseItem,
-    ExternCrate in extern_crates -> ast::ExternCrateItem,
+    ExternCrate in extern_crates -> ast::ExternCrate,
     Function in functions -> ast::FnDef,
     Struct in structs -> ast::StructDef,
     Union in unions -> ast::UnionDef,
@@ -492,7 +492,7 @@ pub struct ExternCrate {
     pub visibility: RawVisibilityId,
     /// Whether this is a `#[macro_use] extern crate ...`.
     pub is_macro_use: bool,
-    pub ast_id: FileAstId<ast::ExternCrateItem>,
+    pub ast_id: FileAstId<ast::ExternCrate>,
 }
 
 #[derive(Debug, Clone, Eq, PartialEq)]
diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs
index 6c58c63781..8a36de3118 100644
--- a/crates/ra_hir_def/src/item_tree/lower.rs
+++ b/crates/ra_hir_def/src/item_tree/lower.rs
@@ -95,7 +95,7 @@ impl Ctx {
             ast::Item::TraitDef(_) | ast::Item::ImplDef(_) | ast::Item::ExternBlock(_) => {}
 
             // These don't have inner items.
-            ast::Item::Module(_) | ast::Item::ExternCrateItem(_) | ast::Item::UseItem(_) => {}
+            ast::Item::Module(_) | ast::Item::ExternCrate(_) | ast::Item::UseItem(_) => {}
         };
 
         let attrs = Attrs::new(item, &self.hygiene);
@@ -113,7 +113,7 @@ impl Ctx {
             ast::Item::UseItem(ast) => Some(ModItems(
                 self.lower_use(ast).into_iter().map(Into::into).collect::<SmallVec<_>>(),
             )),
-            ast::Item::ExternCrateItem(ast) => self.lower_extern_crate(ast).map(Into::into),
+            ast::Item::ExternCrate(ast) => self.lower_extern_crate(ast).map(Into::into),
             ast::Item::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into),
             ast::Item::ExternBlock(ast) => {
                 Some(ModItems(self.lower_extern_block(ast).into_iter().collect::<SmallVec<_>>()))
@@ -498,7 +498,7 @@ impl Ctx {
 
     fn lower_extern_crate(
         &mut self,
-        extern_crate: &ast::ExternCrateItem,
+        extern_crate: &ast::ExternCrate,
     ) -> Option<FileItemTreeId<ExternCrate>> {
         let path = ModPath::from_name_ref(&extern_crate.name_ref()?);
         let alias = extern_crate.rename().map(|a| {
diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs
index ffcdaaa992..3b73e53464 100644
--- a/crates/ra_parser/src/grammar/items.rs
+++ b/crates/ra_parser/src/grammar/items.rs
@@ -313,7 +313,7 @@ fn extern_crate_item(p: &mut Parser, m: Marker) {
 
     opt_alias(p);
     p.expect(T![;]);
-    m.complete(p, EXTERN_CRATE_ITEM);
+    m.complete(p, EXTERN_CRATE);
 }
 
 pub(crate) fn extern_item_list(p: &mut Parser) {
diff --git a/crates/ra_parser/src/syntax_kind/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs
index f14b60b654..116b58858f 100644
--- a/crates/ra_parser/src/syntax_kind/generated.rs
+++ b/crates/ra_parser/src/syntax_kind/generated.rs
@@ -128,7 +128,7 @@ pub enum SyntaxKind {
     ENUM_DEF,
     FN_DEF,
     RET_TYPE,
-    EXTERN_CRATE_ITEM,
+    EXTERN_CRATE,
     MODULE,
     USE_ITEM,
     STATIC_DEF,
diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs
index 01e8111b04..3fd7613678 100644
--- a/crates/ra_syntax/src/ast/generated/nodes.rs
+++ b/crates/ra_syntax/src/ast/generated/nodes.rs
@@ -65,12 +65,12 @@ impl ExternBlock {
     pub fn extern_item_list(&self) -> Option<ExternItemList> { support::child(&self.syntax) }
 }
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
-pub struct ExternCrateItem {
+pub struct ExternCrate {
     pub(crate) syntax: SyntaxNode,
 }
-impl ast::AttrsOwner for ExternCrateItem {}
-impl ast::VisibilityOwner for ExternCrateItem {}
-impl ExternCrateItem {
+impl ast::AttrsOwner for ExternCrate {}
+impl ast::VisibilityOwner for ExternCrate {}
+impl ExternCrate {
     pub fn extern_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![extern]) }
     pub fn crate_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![crate]) }
     pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) }
@@ -255,6 +255,21 @@ impl ItemList {
     pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) }
 }
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct NameRef {
+    pub(crate) syntax: SyntaxNode,
+}
+impl NameRef {
+    pub fn ident_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![ident]) }
+}
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct Rename {
+    pub(crate) syntax: SyntaxNode,
+}
+impl ast::NameOwner for Rename {}
+impl Rename {
+    pub fn as_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![as]) }
+}
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub struct Abi {
     pub(crate) syntax: SyntaxNode,
 }
@@ -699,13 +714,6 @@ impl MethodCallExpr {
     pub fn type_arg_list(&self) -> Option<TypeArgList> { support::child(&self.syntax) }
 }
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
-pub struct NameRef {
-    pub(crate) syntax: SyntaxNode,
-}
-impl NameRef {
-    pub fn ident_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![ident]) }
-}
-#[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub struct TypeArgList {
     pub(crate) syntax: SyntaxNode,
 }
@@ -1190,14 +1198,6 @@ impl UseTreeList {
     pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) }
 }
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
-pub struct Rename {
-    pub(crate) syntax: SyntaxNode,
-}
-impl ast::NameOwner for Rename {}
-impl Rename {
-    pub fn as_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![as]) }
-}
-#[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub struct PathSegment {
     pub(crate) syntax: SyntaxNode,
 }
@@ -1272,7 +1272,7 @@ pub enum Item {
     ConstDef(ConstDef),
     EnumDef(EnumDef),
     ExternBlock(ExternBlock),
-    ExternCrateItem(ExternCrateItem),
+    ExternCrate(ExternCrate),
     FnDef(FnDef),
     ImplDef(ImplDef),
     MacroCall(MacroCall),
@@ -1451,8 +1451,8 @@ impl AstNode for ExternBlock {
     }
     fn syntax(&self) -> &SyntaxNode { &self.syntax }
 }
-impl AstNode for ExternCrateItem {
-    fn can_cast(kind: SyntaxKind) -> bool { kind == EXTERN_CRATE_ITEM }
+impl AstNode for ExternCrate {
+    fn can_cast(kind: SyntaxKind) -> bool { kind == EXTERN_CRATE }
     fn cast(syntax: SyntaxNode) -> Option<Self> {
         if Self::can_cast(syntax.kind()) {
             Some(Self { syntax })
@@ -1605,6 +1605,28 @@ impl AstNode for ItemList {
     }
     fn syntax(&self) -> &SyntaxNode { &self.syntax }
 }
+impl AstNode for NameRef {
+    fn can_cast(kind: SyntaxKind) -> bool { kind == NAME_REF }
+    fn cast(syntax: SyntaxNode) -> Option<Self> {
+        if Self::can_cast(syntax.kind()) {
+            Some(Self { syntax })
+        } else {
+            None
+        }
+    }
+    fn syntax(&self) -> &SyntaxNode { &self.syntax }
+}
+impl AstNode for Rename {
+    fn can_cast(kind: SyntaxKind) -> bool { kind == RENAME }
+    fn cast(syntax: SyntaxNode) -> Option<Self> {
+        if Self::can_cast(syntax.kind()) {
+            Some(Self { syntax })
+        } else {
+            None
+        }
+    }
+    fn syntax(&self) -> &SyntaxNode { &self.syntax }
+}
 impl AstNode for Abi {
     fn can_cast(kind: SyntaxKind) -> bool { kind == ABI }
     fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -2111,17 +2133,6 @@ impl AstNode for MethodCallExpr {
     }
     fn syntax(&self) -> &SyntaxNode { &self.syntax }
 }
-impl AstNode for NameRef {
-    fn can_cast(kind: SyntaxKind) -> bool { kind == NAME_REF }
-    fn cast(syntax: SyntaxNode) -> Option<Self> {
-        if Self::can_cast(syntax.kind()) {
-            Some(Self { syntax })
-        } else {
-            None
-        }
-    }
-    fn syntax(&self) -> &SyntaxNode { &self.syntax }
-}
 impl AstNode for TypeArgList {
     fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_ARG_LIST }
     fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -2683,17 +2694,6 @@ impl AstNode for UseTreeList {
     }
     fn syntax(&self) -> &SyntaxNode { &self.syntax }
 }
-impl AstNode for Rename {
-    fn can_cast(kind: SyntaxKind) -> bool { kind == RENAME }
-    fn cast(syntax: SyntaxNode) -> Option<Self> {
-        if Self::can_cast(syntax.kind()) {
-            Some(Self { syntax })
-        } else {
-            None
-        }
-    }
-    fn syntax(&self) -> &SyntaxNode { &self.syntax }
-}
 impl AstNode for PathSegment {
     fn can_cast(kind: SyntaxKind) -> bool { kind == PATH_SEGMENT }
     fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -2780,8 +2780,8 @@ impl From<EnumDef> for Item {
 impl From<ExternBlock> for Item {
     fn from(node: ExternBlock) -> Item { Item::ExternBlock(node) }
 }
-impl From<ExternCrateItem> for Item {
-    fn from(node: ExternCrateItem) -> Item { Item::ExternCrateItem(node) }
+impl From<ExternCrate> for Item {
+    fn from(node: ExternCrate) -> Item { Item::ExternCrate(node) }
 }
 impl From<FnDef> for Item {
     fn from(node: FnDef) -> Item { Item::FnDef(node) }
@@ -2816,9 +2816,9 @@ impl From<UseItem> for Item {
 impl AstNode for Item {
     fn can_cast(kind: SyntaxKind) -> bool {
         match kind {
-            CONST_DEF | ENUM_DEF | EXTERN_BLOCK | EXTERN_CRATE_ITEM | FN_DEF | IMPL_DEF
-            | MACRO_CALL | MODULE | STATIC_DEF | STRUCT_DEF | TRAIT_DEF | TYPE_ALIAS_DEF
-            | UNION_DEF | USE_ITEM => true,
+            CONST_DEF | ENUM_DEF | EXTERN_BLOCK | EXTERN_CRATE | FN_DEF | IMPL_DEF | MACRO_CALL
+            | MODULE | STATIC_DEF | STRUCT_DEF | TRAIT_DEF | TYPE_ALIAS_DEF | UNION_DEF
+            | USE_ITEM => true,
             _ => false,
         }
     }
@@ -2827,7 +2827,7 @@ impl AstNode for Item {
             CONST_DEF => Item::ConstDef(ConstDef { syntax }),
             ENUM_DEF => Item::EnumDef(EnumDef { syntax }),
             EXTERN_BLOCK => Item::ExternBlock(ExternBlock { syntax }),
-            EXTERN_CRATE_ITEM => Item::ExternCrateItem(ExternCrateItem { syntax }),
+            EXTERN_CRATE => Item::ExternCrate(ExternCrate { syntax }),
             FN_DEF => Item::FnDef(FnDef { syntax }),
             IMPL_DEF => Item::ImplDef(ImplDef { syntax }),
             MACRO_CALL => Item::MacroCall(MacroCall { syntax }),
@@ -2847,7 +2847,7 @@ impl AstNode for Item {
             Item::ConstDef(it) => &it.syntax,
             Item::EnumDef(it) => &it.syntax,
             Item::ExternBlock(it) => &it.syntax,
-            Item::ExternCrateItem(it) => &it.syntax,
+            Item::ExternCrate(it) => &it.syntax,
             Item::FnDef(it) => &it.syntax,
             Item::ImplDef(it) => &it.syntax,
             Item::MacroCall(it) => &it.syntax,
@@ -3480,7 +3480,7 @@ impl std::fmt::Display for ExternBlock {
         std::fmt::Display::fmt(self.syntax(), f)
     }
 }
-impl std::fmt::Display for ExternCrateItem {
+impl std::fmt::Display for ExternCrate {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         std::fmt::Display::fmt(self.syntax(), f)
     }
@@ -3550,6 +3550,16 @@ impl std::fmt::Display for ItemList {
         std::fmt::Display::fmt(self.syntax(), f)
     }
 }
+impl std::fmt::Display for NameRef {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        std::fmt::Display::fmt(self.syntax(), f)
+    }
+}
+impl std::fmt::Display for Rename {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        std::fmt::Display::fmt(self.syntax(), f)
+    }
+}
 impl std::fmt::Display for Abi {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         std::fmt::Display::fmt(self.syntax(), f)
@@ -3780,11 +3790,6 @@ impl std::fmt::Display for MethodCallExpr {
         std::fmt::Display::fmt(self.syntax(), f)
     }
 }
-impl std::fmt::Display for NameRef {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        std::fmt::Display::fmt(self.syntax(), f)
-    }
-}
 impl std::fmt::Display for TypeArgList {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         std::fmt::Display::fmt(self.syntax(), f)
@@ -4040,11 +4045,6 @@ impl std::fmt::Display for UseTreeList {
         std::fmt::Display::fmt(self.syntax(), f)
     }
 }
-impl std::fmt::Display for Rename {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        std::fmt::Display::fmt(self.syntax(), f)
-    }
-}
 impl std::fmt::Display for PathSegment {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         std::fmt::Display::fmt(self.syntax(), f)
diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs
index bcd92c3bcb..083727c5cd 100644
--- a/xtask/src/ast_src.rs
+++ b/xtask/src/ast_src.rs
@@ -98,7 +98,7 @@ pub(crate) const KINDS_SRC: KindsSrc = KindsSrc {
         "ENUM_DEF",
         "FN_DEF",
         "RET_TYPE",
-        "EXTERN_CRATE_ITEM",
+        "EXTERN_CRATE",
         "MODULE",
         "USE_ITEM",
         "STATIC_DEF",
diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram
index 76c070402f..e6e7c7518f 100644
--- a/xtask/src/codegen/rust.ungram
+++ b/xtask/src/codegen/rust.ungram
@@ -7,7 +7,7 @@ Item =
   ConstDef
 | EnumDef
 | ExternBlock
-| ExternCrateItem
+| ExternCrate
 | FnDef
 | ImplDef
 | MacroCall
@@ -26,6 +26,9 @@ Module =
 ItemList =
   '{' Attr* Item* '}'
 
+ExternCrate =
+  Attr* Visibility? 'extern' 'crate' (NameRef | 'self') Rename? ';'
+
 FnDef =
  Attr* Visibility? Abi? 'const' 'default' 'async' 'unsafe' 'fn' Name TypeParamList?
  ParamList RetType?
@@ -404,9 +407,6 @@ UseTreeList =
 Rename =
   'as' Name
 
-ExternCrateItem =
-  Attr* Visibility? 'extern' 'crate' (NameRef | 'self') Rename? ';'
-
 Path =
   (qualifier:Path '::')? segment:PathSegment
 


Test patch:
diff --git a/crates/ra_hir_def/src/item_tree/tests.rs b/crates/ra_hir_def/src/item_tree/tests.rs
index a6057ceab5..68be1cb40c 100644
--- a/crates/ra_hir_def/src/item_tree/tests.rs
+++ b/crates/ra_hir_def/src/item_tree/tests.rs
@@ -232,7 +232,7 @@ fn smoke() {
             #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_on_use"))] }, input: None }]) }]
             Import { path: ModPath { kind: Plain, segments: [Name(Text("b"))] }, alias: None, visibility: RawVisibilityId("pub(self)"), is_glob: true, is_prelude: false, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::UseItem>(0) }
             #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("ext_crate"))] }, input: None }]) }]
-            ExternCrate { path: ModPath { kind: Plain, segments: [Name(Text("krate"))] }, alias: None, visibility: RawVisibilityId("pub(self)"), is_macro_use: false, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::ExternCrateItem>(1) }
+            ExternCrate { path: ModPath { kind: Plain, segments: [Name(Text("krate"))] }, alias: None, visibility: RawVisibilityId("pub(self)"), is_macro_use: false, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::ExternCrate>(1) }
             #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("on_trait"))] }, input: None }]) }]
             Trait { name: Name(Text("Tr")), visibility: RawVisibilityId("pub(self)"), generic_params: GenericParamsId(0), auto: false, items: [TypeAlias(Idx::<TypeAlias>(0)), Const(Idx::<Const>(0)), Function(Idx::<Function>(0)), Function(Idx::<Function>(1))], ast_id: FileAstId::<ra_syntax::ast::generated::nodes::TraitDef>(2) }
             > #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_ty"))] }, input: None }]) }]
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0060_extern_crate.rast b/crates/ra_syntax/test_data/parser/inline/ok/0060_extern_crate.rast
index d06fc0be51..25e1777d1a 100644
--- a/crates/ra_syntax/test_data/parser/inline/ok/0060_extern_crate.rast
+++ b/crates/ra_syntax/test_data/parser/inline/ok/0060_extern_crate.rast
@@ -1,5 +1,5 @@
 SOURCE_FILE@0..18
-  EXTERN_CRATE_ITEM@0..17
+  EXTERN_CRATE@0..17
     EXTERN_KW@0..6 "extern"
     WHITESPACE@6..7 " "
     CRATE_KW@7..12 "crate"
diff --git a/crates/ra_syntax/test_data/parser/ok/0007_extern_crate.rast b/crates/ra_syntax/test_data/parser/ok/0007_extern_crate.rast
index 948c4ddb37..594c2f8f20 100644
--- a/crates/ra_syntax/test_data/parser/ok/0007_extern_crate.rast
+++ b/crates/ra_syntax/test_data/parser/ok/0007_extern_crate.rast
@@ -1,5 +1,5 @@
 SOURCE_FILE@0..69
-  EXTERN_CRATE_ITEM@0..17
+  EXTERN_CRATE@0..17
     EXTERN_KW@0..6 "extern"
     WHITESPACE@6..7 " "
     CRATE_KW@7..12 "crate"
@@ -8,7 +8,7 @@ SOURCE_FILE@0..69
       IDENT@13..16 "foo"
     SEMICOLON@16..17 ";"
   WHITESPACE@17..18 "\n"
-  EXTERN_CRATE_ITEM@18..42
+  EXTERN_CRATE@18..42
     EXTERN_KW@18..24 "extern"
     WHITESPACE@24..25 " "
     CRATE_KW@25..30 "crate"
@@ -23,7 +23,7 @@ SOURCE_FILE@0..69
         IDENT@38..41 "bar"
     SEMICOLON@41..42 ";"
   WHITESPACE@42..43 "\n"
-  EXTERN_CRATE_ITEM@43..68
+  EXTERN_CRATE@43..68
     EXTERN_KW@43..49 "extern"
     WHITESPACE@49..50 " "
     CRATE_KW@50..55 "crate"