Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix PathSegment grammar #15875

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/syntax/rust.ungram
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ PathSegment =
'::'? NameRef
| NameRef GenericArgList?
| NameRef ParamList RetType?
| '<' PathType ('as' PathType)? '>'
| '<' Type ('as' PathType)? '>'

GenericArgList =
'::'? '<' (GenericArg (',' GenericArg)* ','?)? '>'
Expand Down
93 changes: 47 additions & 46 deletions crates/syntax/src/ast/generated/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![>]) }
}

Expand Down Expand Up @@ -1576,14 +1577,6 @@ impl RecordPatField {
pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) }
}

#[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),
Expand All @@ -1602,6 +1595,14 @@ pub enum Type {
TupleType(TupleType),
}

#[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),
Expand Down Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 9 additions & 0 deletions crates/syntax/src/ast/node_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down