Skip to content

Commit a473303

Browse files
committed
Auto merge of rust-lang#15875 - Young-Flash:fix_grammar, r=Veykril
fix `PathSegment` grammar close rust-lang/rust-analyzer#15778
2 parents c94a6af + 3e5bc9a commit a473303

File tree

3 files changed

+57
-47
lines changed

3 files changed

+57
-47
lines changed

crates/syntax/rust.ungram

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ PathSegment =
3636
'::'? NameRef
3737
| NameRef GenericArgList?
3838
| NameRef ParamList RetType?
39-
| '<' PathType ('as' PathType)? '>'
39+
| '<' Type ('as' PathType)? '>'
4040

4141
GenericArgList =
4242
'::'? '<' (GenericArg (',' GenericArg)* ','?)? '>'

crates/syntax/src/ast/generated/nodes.rs

+47-46
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ impl PathSegment {
5959
pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) }
6060
pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) }
6161
pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) }
62-
pub fn path_type(&self) -> Option<PathType> { support::child(&self.syntax) }
62+
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
6363
pub fn as_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![as]) }
64+
pub fn path_type(&self) -> Option<PathType> { support::child(&self.syntax) }
6465
pub fn r_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![>]) }
6566
}
6667

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

1579-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1580-
pub enum GenericArg {
1581-
TypeArg(TypeArg),
1582-
AssocTypeArg(AssocTypeArg),
1583-
LifetimeArg(LifetimeArg),
1584-
ConstArg(ConstArg),
1585-
}
1586-
15871580
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
15881581
pub enum Type {
15891582
ArrayType(ArrayType),
@@ -1602,6 +1595,14 @@ pub enum Type {
16021595
TupleType(TupleType),
16031596
}
16041597

1598+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1599+
pub enum GenericArg {
1600+
TypeArg(TypeArg),
1601+
AssocTypeArg(AssocTypeArg),
1602+
LifetimeArg(LifetimeArg),
1603+
ConstArg(ConstArg),
1604+
}
1605+
16051606
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
16061607
pub enum Expr {
16071608
ArrayExpr(ArrayExpr),
@@ -3319,41 +3320,6 @@ impl AstNode for RecordPatField {
33193320
}
33203321
fn syntax(&self) -> &SyntaxNode { &self.syntax }
33213322
}
3322-
impl From<TypeArg> for GenericArg {
3323-
fn from(node: TypeArg) -> GenericArg { GenericArg::TypeArg(node) }
3324-
}
3325-
impl From<AssocTypeArg> for GenericArg {
3326-
fn from(node: AssocTypeArg) -> GenericArg { GenericArg::AssocTypeArg(node) }
3327-
}
3328-
impl From<LifetimeArg> for GenericArg {
3329-
fn from(node: LifetimeArg) -> GenericArg { GenericArg::LifetimeArg(node) }
3330-
}
3331-
impl From<ConstArg> for GenericArg {
3332-
fn from(node: ConstArg) -> GenericArg { GenericArg::ConstArg(node) }
3333-
}
3334-
impl AstNode for GenericArg {
3335-
fn can_cast(kind: SyntaxKind) -> bool {
3336-
matches!(kind, TYPE_ARG | ASSOC_TYPE_ARG | LIFETIME_ARG | CONST_ARG)
3337-
}
3338-
fn cast(syntax: SyntaxNode) -> Option<Self> {
3339-
let res = match syntax.kind() {
3340-
TYPE_ARG => GenericArg::TypeArg(TypeArg { syntax }),
3341-
ASSOC_TYPE_ARG => GenericArg::AssocTypeArg(AssocTypeArg { syntax }),
3342-
LIFETIME_ARG => GenericArg::LifetimeArg(LifetimeArg { syntax }),
3343-
CONST_ARG => GenericArg::ConstArg(ConstArg { syntax }),
3344-
_ => return None,
3345-
};
3346-
Some(res)
3347-
}
3348-
fn syntax(&self) -> &SyntaxNode {
3349-
match self {
3350-
GenericArg::TypeArg(it) => &it.syntax,
3351-
GenericArg::AssocTypeArg(it) => &it.syntax,
3352-
GenericArg::LifetimeArg(it) => &it.syntax,
3353-
GenericArg::ConstArg(it) => &it.syntax,
3354-
}
3355-
}
3356-
}
33573323
impl From<ArrayType> for Type {
33583324
fn from(node: ArrayType) -> Type { Type::ArrayType(node) }
33593325
}
@@ -3455,6 +3421,41 @@ impl AstNode for Type {
34553421
}
34563422
}
34573423
}
3424+
impl From<TypeArg> for GenericArg {
3425+
fn from(node: TypeArg) -> GenericArg { GenericArg::TypeArg(node) }
3426+
}
3427+
impl From<AssocTypeArg> for GenericArg {
3428+
fn from(node: AssocTypeArg) -> GenericArg { GenericArg::AssocTypeArg(node) }
3429+
}
3430+
impl From<LifetimeArg> for GenericArg {
3431+
fn from(node: LifetimeArg) -> GenericArg { GenericArg::LifetimeArg(node) }
3432+
}
3433+
impl From<ConstArg> for GenericArg {
3434+
fn from(node: ConstArg) -> GenericArg { GenericArg::ConstArg(node) }
3435+
}
3436+
impl AstNode for GenericArg {
3437+
fn can_cast(kind: SyntaxKind) -> bool {
3438+
matches!(kind, TYPE_ARG | ASSOC_TYPE_ARG | LIFETIME_ARG | CONST_ARG)
3439+
}
3440+
fn cast(syntax: SyntaxNode) -> Option<Self> {
3441+
let res = match syntax.kind() {
3442+
TYPE_ARG => GenericArg::TypeArg(TypeArg { syntax }),
3443+
ASSOC_TYPE_ARG => GenericArg::AssocTypeArg(AssocTypeArg { syntax }),
3444+
LIFETIME_ARG => GenericArg::LifetimeArg(LifetimeArg { syntax }),
3445+
CONST_ARG => GenericArg::ConstArg(ConstArg { syntax }),
3446+
_ => return None,
3447+
};
3448+
Some(res)
3449+
}
3450+
fn syntax(&self) -> &SyntaxNode {
3451+
match self {
3452+
GenericArg::TypeArg(it) => &it.syntax,
3453+
GenericArg::AssocTypeArg(it) => &it.syntax,
3454+
GenericArg::LifetimeArg(it) => &it.syntax,
3455+
GenericArg::ConstArg(it) => &it.syntax,
3456+
}
3457+
}
3458+
}
34583459
impl From<ArrayExpr> for Expr {
34593460
fn from(node: ArrayExpr) -> Expr { Expr::ArrayExpr(node) }
34603461
}
@@ -4340,12 +4341,12 @@ impl AstNode for AnyHasVisibility {
43404341
}
43414342
fn syntax(&self) -> &SyntaxNode { &self.syntax }
43424343
}
4343-
impl std::fmt::Display for GenericArg {
4344+
impl std::fmt::Display for Type {
43444345
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43454346
std::fmt::Display::fmt(self.syntax(), f)
43464347
}
43474348
}
4348-
impl std::fmt::Display for Type {
4349+
impl std::fmt::Display for GenericArg {
43494350
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43504351
std::fmt::Display::fmt(self.syntax(), f)
43514352
}

crates/syntax/src/ast/node_ext.rs

+9
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,15 @@ impl ast::Impl {
361361
}
362362
}
363363

364+
// [#15778](https://github.com/rust-lang/rust-analyzer/issues/15778)
365+
impl ast::PathSegment {
366+
pub fn qualifying_trait(&self) -> Option<ast::PathType> {
367+
let mut path_types = support::children(self.syntax());
368+
let first = path_types.next()?;
369+
path_types.next().or(Some(first))
370+
}
371+
}
372+
364373
#[derive(Debug, Clone, PartialEq, Eq)]
365374
pub enum StructKind {
366375
Record(ast::RecordFieldList),

0 commit comments

Comments
 (0)