about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-11-15 09:43:11 +0000
committerbors <bors@rust-lang.org>2023-11-15 09:43:11 +0000
commita47330308b5a50f9f99588e8d51505a75b865b41 (patch)
treea3e761c1981513306edcc7e9603d4b56e11e6325
parentc94a6afbe611b269630e736ffdb162902f016cf8 (diff)
parent3e5bc9a9c8bfd01e827e7f4062356e484e33387a (diff)
downloadrust-a47330308b5a50f9f99588e8d51505a75b865b41.tar.gz
rust-a47330308b5a50f9f99588e8d51505a75b865b41.zip
Auto merge of #15875 - Young-Flash:fix_grammar, r=Veykril
fix `PathSegment` grammar

close https://github.com/rust-lang/rust-analyzer/issues/15778
-rw-r--r--crates/syntax/rust.ungram2
-rw-r--r--crates/syntax/src/ast/generated/nodes.rs93
-rw-r--r--crates/syntax/src/ast/node_ext.rs9
3 files changed, 57 insertions, 47 deletions
diff --git a/crates/syntax/rust.ungram b/crates/syntax/rust.ungram
index 3603560d35c..c3010d090c6 100644
--- a/crates/syntax/rust.ungram
+++ b/crates/syntax/rust.ungram
@@ -36,7 +36,7 @@ PathSegment =
   '::'? NameRef
 | NameRef GenericArgList?
 | NameRef ParamList RetType?
-| '<' PathType ('as' PathType)? '>'
+| '<' Type ('as' PathType)? '>'
 
 GenericArgList =
   '::'? '<' (GenericArg (',' GenericArg)* ','?)? '>'
diff --git a/crates/syntax/src/ast/generated/nodes.rs b/crates/syntax/src/ast/generated/nodes.rs
index 7ba0d4dc656..6c86e591044 100644
--- a/crates/syntax/src/ast/generated/nodes.rs
+++ b/crates/syntax/src/ast/generated/nodes.rs
@@ -59,8 +59,9 @@ impl PathSegment {
     pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) }
     pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) }
     pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) }
-    pub fn path_type(&self) -> Option<PathType> { support::child(&self.syntax) }
+    pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
     pub fn as_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![as]) }
+    pub fn path_type(&self) -> Option<PathType> { support::child(&self.syntax) }
     pub fn r_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![>]) }
 }
 
@@ -1577,14 +1578,6 @@ impl RecordPatField {
 }
 
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
-pub enum GenericArg {
-    TypeArg(TypeArg),
-    AssocTypeArg(AssocTypeArg),
-    LifetimeArg(LifetimeArg),
-    ConstArg(ConstArg),
-}
-
-#[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub enum Type {
     ArrayType(ArrayType),
     DynTraitType(DynTraitType),
@@ -1603,6 +1596,14 @@ pub enum Type {
 }
 
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum GenericArg {
+    TypeArg(TypeArg),
+    AssocTypeArg(AssocTypeArg),
+    LifetimeArg(LifetimeArg),
+    ConstArg(ConstArg),
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub enum Expr {
     ArrayExpr(ArrayExpr),
     AsmExpr(AsmExpr),
@@ -3319,41 +3320,6 @@ impl AstNode for RecordPatField {
     }
     fn syntax(&self) -> &SyntaxNode { &self.syntax }
 }
-impl From<TypeArg> for GenericArg {
-    fn from(node: TypeArg) -> GenericArg { GenericArg::TypeArg(node) }
-}
-impl From<AssocTypeArg> for GenericArg {
-    fn from(node: AssocTypeArg) -> GenericArg { GenericArg::AssocTypeArg(node) }
-}
-impl From<LifetimeArg> for GenericArg {
-    fn from(node: LifetimeArg) -> GenericArg { GenericArg::LifetimeArg(node) }
-}
-impl From<ConstArg> for GenericArg {
-    fn from(node: ConstArg) -> GenericArg { GenericArg::ConstArg(node) }
-}
-impl AstNode for GenericArg {
-    fn can_cast(kind: SyntaxKind) -> bool {
-        matches!(kind, TYPE_ARG | ASSOC_TYPE_ARG | LIFETIME_ARG | CONST_ARG)
-    }
-    fn cast(syntax: SyntaxNode) -> Option<Self> {
-        let res = match syntax.kind() {
-            TYPE_ARG => GenericArg::TypeArg(TypeArg { syntax }),
-            ASSOC_TYPE_ARG => GenericArg::AssocTypeArg(AssocTypeArg { syntax }),
-            LIFETIME_ARG => GenericArg::LifetimeArg(LifetimeArg { syntax }),
-            CONST_ARG => GenericArg::ConstArg(ConstArg { syntax }),
-            _ => return None,
-        };
-        Some(res)
-    }
-    fn syntax(&self) -> &SyntaxNode {
-        match self {
-            GenericArg::TypeArg(it) => &it.syntax,
-            GenericArg::AssocTypeArg(it) => &it.syntax,
-            GenericArg::LifetimeArg(it) => &it.syntax,
-            GenericArg::ConstArg(it) => &it.syntax,
-        }
-    }
-}
 impl From<ArrayType> for Type {
     fn from(node: ArrayType) -> Type { Type::ArrayType(node) }
 }
@@ -3455,6 +3421,41 @@ impl AstNode for Type {
         }
     }
 }
+impl From<TypeArg> for GenericArg {
+    fn from(node: TypeArg) -> GenericArg { GenericArg::TypeArg(node) }
+}
+impl From<AssocTypeArg> for GenericArg {
+    fn from(node: AssocTypeArg) -> GenericArg { GenericArg::AssocTypeArg(node) }
+}
+impl From<LifetimeArg> for GenericArg {
+    fn from(node: LifetimeArg) -> GenericArg { GenericArg::LifetimeArg(node) }
+}
+impl From<ConstArg> for GenericArg {
+    fn from(node: ConstArg) -> GenericArg { GenericArg::ConstArg(node) }
+}
+impl AstNode for GenericArg {
+    fn can_cast(kind: SyntaxKind) -> bool {
+        matches!(kind, TYPE_ARG | ASSOC_TYPE_ARG | LIFETIME_ARG | CONST_ARG)
+    }
+    fn cast(syntax: SyntaxNode) -> Option<Self> {
+        let res = match syntax.kind() {
+            TYPE_ARG => GenericArg::TypeArg(TypeArg { syntax }),
+            ASSOC_TYPE_ARG => GenericArg::AssocTypeArg(AssocTypeArg { syntax }),
+            LIFETIME_ARG => GenericArg::LifetimeArg(LifetimeArg { syntax }),
+            CONST_ARG => GenericArg::ConstArg(ConstArg { syntax }),
+            _ => return None,
+        };
+        Some(res)
+    }
+    fn syntax(&self) -> &SyntaxNode {
+        match self {
+            GenericArg::TypeArg(it) => &it.syntax,
+            GenericArg::AssocTypeArg(it) => &it.syntax,
+            GenericArg::LifetimeArg(it) => &it.syntax,
+            GenericArg::ConstArg(it) => &it.syntax,
+        }
+    }
+}
 impl From<ArrayExpr> for Expr {
     fn from(node: ArrayExpr) -> Expr { Expr::ArrayExpr(node) }
 }
@@ -4340,12 +4341,12 @@ impl AstNode for AnyHasVisibility {
     }
     fn syntax(&self) -> &SyntaxNode { &self.syntax }
 }
-impl std::fmt::Display for GenericArg {
+impl std::fmt::Display for Type {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         std::fmt::Display::fmt(self.syntax(), f)
     }
 }
-impl std::fmt::Display for Type {
+impl std::fmt::Display for GenericArg {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         std::fmt::Display::fmt(self.syntax(), f)
     }
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs
index 691d0c618f3..be5b954ad34 100644
--- a/crates/syntax/src/ast/node_ext.rs
+++ b/crates/syntax/src/ast/node_ext.rs
@@ -361,6 +361,15 @@ impl ast::Impl {
     }
 }
 
+// [#15778](https://github.com/rust-lang/rust-analyzer/issues/15778)
+impl ast::PathSegment {
+    pub fn qualifying_trait(&self) -> Option<ast::PathType> {
+        let mut path_types = support::children(self.syntax());
+        let first = path_types.next()?;
+        path_types.next().or(Some(first))
+    }
+}
+
 #[derive(Debug, Clone, PartialEq, Eq)]
 pub enum StructKind {
     Record(ast::RecordFieldList),