Skip to content

Commit 87a6204

Browse files
committed
HasGenericArgs syntax trait
1 parent ba9f93e commit 87a6204

26 files changed

+83
-41
lines changed

src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use span::AstIdMap;
1515
use stdx::never;
1616
use syntax::{
1717
ast::{
18-
self, ArrayExprKind, AstChildren, BlockExpr, HasArgList, HasAttrs, HasLoopBody, HasName,
19-
RangeItem, SlicePatComponents,
18+
self, ArrayExprKind, AstChildren, BlockExpr, HasArgList, HasAttrs, HasGenericArgs,
19+
HasLoopBody, HasName, RangeItem, SlicePatComponents,
2020
},
2121
AstNode, AstPtr, AstToken as _, SyntaxNodePtr,
2222
};

src/tools/rust-analyzer/crates/hir-def/src/hir/type_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hir_expand::{
1010
AstId,
1111
};
1212
use intern::Interned;
13-
use syntax::ast::{self, HasName, IsString};
13+
use syntax::ast::{self, HasGenericArgs, HasName, IsString};
1414

1515
use crate::{
1616
builtin_type::{BuiltinInt, BuiltinType, BuiltinUint},

src/tools/rust-analyzer/crates/hir-def/src/path/lower.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use hir_expand::{
99
name::{name, AsName},
1010
};
1111
use intern::Interned;
12-
use syntax::ast::{self, AstNode, HasTypeBounds};
12+
use syntax::ast::{self, AstNode, HasGenericArgs, HasTypeBounds};
1313

1414
use crate::{
1515
path::{AssociatedTypeBinding, GenericArg, GenericArgs, ModPath, Path, PathKind},

src/tools/rust-analyzer/crates/ide-assists/src/handlers/add_turbo_fish.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use either::Either;
22
use ide_db::defs::{Definition, NameRefClass};
33
use syntax::{
4-
ast::{self, make, HasArgList},
4+
ast::{self, make, HasArgList, HasGenericArgs},
55
ted, AstNode,
66
};
77

src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_from_to_tryfrom.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ide_db::{famous_defs::FamousDefs, traits::resolve_target_trait};
22
use itertools::Itertools;
33
use syntax::{
4-
ast::{self, make, AstNode, HasName},
4+
ast::{self, make, AstNode, HasGenericArgs, HasName},
55
ted,
66
};
77

src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_into_to_from.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use hir::ImportPathConfig;
22
use ide_db::{famous_defs::FamousDefs, helpers::mod_path_to_ast, traits::resolve_target_trait};
3-
use syntax::ast::{self, AstNode, HasName};
3+
use syntax::ast::{self, AstNode, HasGenericArgs, HasName};
44

55
use crate::{AssistContext, AssistId, AssistKind, Assists};
66

src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_type_alias.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use either::Either;
22
use ide_db::syntax_helpers::node_ext::walk_ty;
33
use syntax::{
4-
ast::{self, edit::IndentLevel, make, AstNode, HasGenericParams, HasName},
4+
ast::{self, edit::IndentLevel, make, AstNode, HasGenericArgs, HasGenericParams, HasName},
55
ted,
66
};
77

src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_delegate_trait.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ use syntax::{
1717
self,
1818
edit::{self, AstNodeEdit},
1919
edit_in_place::AttrsOwnerEdit,
20-
make, AssocItem, GenericArgList, GenericParamList, HasAttrs, HasGenericParams, HasName,
21-
HasTypeBounds, HasVisibility as astHasVisibility, Path, WherePred,
20+
make, AssocItem, GenericArgList, GenericParamList, HasAttrs, HasGenericArgs,
21+
HasGenericParams, HasName, HasTypeBounds, HasVisibility as astHasVisibility, Path,
22+
WherePred,
2223
},
2324
ted::{self, Position},
2425
AstNode, NodeOrToken, SmolStr, SyntaxKind,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_documentation_template.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use itertools::Itertools;
44
use stdx::{format_to, to_lower_snake_case};
55
use syntax::{
66
algo::skip_whitespace_token,
7-
ast::{self, edit::IndentLevel, HasDocComments, HasName},
7+
ast::{self, edit::IndentLevel, HasDocComments, HasGenericArgs, HasName},
88
match_ast, AstNode, AstToken,
99
};
1010

src/tools/rust-analyzer/crates/ide-assists/src/handlers/inline_call.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use ide_db::{
1515
};
1616
use itertools::{izip, Itertools};
1717
use syntax::{
18-
ast::{self, edit::IndentLevel, edit_in_place::Indent, HasArgList, Pat, PathExpr},
18+
ast::{
19+
self, edit::IndentLevel, edit_in_place::Indent, HasArgList, HasGenericArgs, Pat, PathExpr,
20+
},
1921
ted, AstNode, NodeOrToken, SyntaxKind,
2022
};
2123

src/tools/rust-analyzer/crates/ide-assists/src/handlers/qualify_path.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use ide_db::{
66
helpers::mod_path_to_ast,
77
imports::import_assets::{ImportCandidate, LocatedImport},
88
};
9+
use syntax::ast::HasGenericArgs;
910
use syntax::{
1011
ast,
1112
ast::{make, HasArgList},

src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ide_db::{
44
imports::insert_use::{insert_use, ImportScope},
55
};
66
use syntax::{
7-
ast::{self, make},
7+
ast::{self, make, HasGenericArgs},
88
match_ast, ted, AstNode, SyntaxNode,
99
};
1010

src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_turbofish_with_explicit_type.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use hir::HirDisplay;
22
use syntax::{
3-
ast::{Expr, GenericArg, GenericArgList},
4-
ast::{LetStmt, Type::InferType},
3+
ast::{Expr, GenericArg, GenericArgList, HasGenericArgs, LetStmt, Type::InferType},
54
AstNode, TextRange,
65
};
76

src/tools/rust-analyzer/crates/ide-assists/src/handlers/toggle_async_sugar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ide_db::{
44
famous_defs::FamousDefs,
55
};
66
use syntax::{
7-
ast::{self, HasVisibility},
7+
ast::{self, HasGenericArgs, HasVisibility},
88
AstNode, NodeOrToken, SyntaxKind, SyntaxNode, SyntaxToken, TextRange,
99
};
1010

src/tools/rust-analyzer/crates/ide-assists/src/handlers/unwrap_result_return_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ide_db::{
44
};
55
use itertools::Itertools;
66
use syntax::{
7-
ast::{self, Expr},
7+
ast::{self, Expr, HasGenericArgs},
88
match_ast, AstNode, NodeOrToken, SyntaxKind, TextRange,
99
};
1010

src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use ide_db::{active_parameter::ActiveParameter, RootDatabase};
66
use itertools::Either;
77
use syntax::{
88
algo::{ancestors_at_offset, find_node_at_offset, non_trivia_sibling},
9-
ast::{self, AttrKind, HasArgList, HasGenericParams, HasLoopBody, HasName, NameOrNameRef},
9+
ast::{
10+
self, AttrKind, HasArgList, HasGenericArgs, HasGenericParams, HasLoopBody, HasName,
11+
NameOrNameRef,
12+
},
1013
match_ast, AstNode, AstToken, Direction, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode,
1114
SyntaxToken, TextRange, TextSize, T,
1215
};

src/tools/rust-analyzer/crates/ide-db/src/path_transform.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hir::{AsAssocItem, HirDisplay, ImportPathConfig, ModuleDef, SemanticsScope};
66
use itertools::Itertools;
77
use rustc_hash::FxHashMap;
88
use syntax::{
9-
ast::{self, make, AstNode},
9+
ast::{self, make, AstNode, HasGenericArgs},
1010
ted, SyntaxNode,
1111
};
1212

src/tools/rust-analyzer/crates/ide-ssr/src/matching.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hir::{ImportPathConfig, Semantics};
1010
use ide_db::{base_db::FileRange, FxHashMap};
1111
use std::{cell::Cell, iter::Peekable};
1212
use syntax::{
13-
ast::{self, AstNode, AstToken},
13+
ast::{self, AstNode, AstToken, HasGenericArgs},
1414
SmolStr, SyntaxElement, SyntaxElementChildren, SyntaxKind, SyntaxNode, SyntaxToken,
1515
};
1616

src/tools/rust-analyzer/crates/ide-ssr/src/resolving.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
use hir::AsAssocItem;
44
use ide_db::{base_db::FilePosition, FxHashMap};
55
use parsing::Placeholder;
6-
use syntax::{ast, SmolStr, SyntaxKind, SyntaxNode, SyntaxToken};
6+
use syntax::{
7+
ast::{self, HasGenericArgs},
8+
SmolStr, SyntaxKind, SyntaxNode, SyntaxToken,
9+
};
710

811
use crate::{errors::error, parsing, SsrError};
912

src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use ide_db::{base_db::FileId, famous_defs::FamousDefs, RootDatabase};
88

99
use itertools::Itertools;
1010
use syntax::{
11-
ast::{self, AstNode, HasName},
11+
ast::{self, AstNode, HasGenericArgs, HasName},
1212
match_ast,
1313
};
1414

src/tools/rust-analyzer/crates/syntax/src/ast.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ pub use self::{
3131
operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
3232
token_ext::{CommentKind, CommentPlacement, CommentShape, IsString, QuoteOffsets, Radix},
3333
traits::{
34-
AttrDocCommentIter, DocCommentIter, HasArgList, HasAttrs, HasDocComments, HasGenericParams,
35-
HasLoopBody, HasModuleItem, HasName, HasTypeBounds, HasVisibility,
34+
AttrDocCommentIter, DocCommentIter, HasArgList, HasAttrs, HasDocComments, HasGenericArgs,
35+
HasGenericParams, HasLoopBody, HasModuleItem, HasName, HasTypeBounds, HasVisibility,
3636
},
3737
};
3838

@@ -149,14 +149,17 @@ pub trait RangeItem {
149149
mod support {
150150
use super::{AstChildren, AstNode, SyntaxKind, SyntaxNode, SyntaxToken};
151151

152+
#[inline]
152153
pub(super) fn child<N: AstNode>(parent: &SyntaxNode) -> Option<N> {
153154
parent.children().find_map(N::cast)
154155
}
155156

157+
#[inline]
156158
pub(super) fn children<N: AstNode>(parent: &SyntaxNode) -> AstChildren<N> {
157159
AstChildren::new(parent)
158160
}
159161

162+
#[inline]
160163
pub(super) fn token(parent: &SyntaxNode, kind: SyntaxKind) -> Option<SyntaxToken> {
161164
parent.children_with_tokens().filter_map(|it| it.into_token()).find(|it| it.kind() == kind)
162165
}

src/tools/rust-analyzer/crates/syntax/src/ast/edit_in_place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use parser::{SyntaxKind, T};
66

77
use crate::{
88
algo::{self, neighbor},
9-
ast::{self, edit::IndentLevel, make, HasGenericParams},
9+
ast::{self, edit::IndentLevel, make, HasGenericArgs, HasGenericParams},
1010
ted::{self, Position},
1111
AstNode, AstToken, Direction, SyntaxElement,
1212
SyntaxKind::{ATTR, COMMENT, WHITESPACE},

src/tools/rust-analyzer/crates/syntax/src/ast/generated/nodes.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ impl AssocItemList {
7878
pub struct AssocTypeArg {
7979
pub(crate) syntax: SyntaxNode,
8080
}
81+
impl ast::HasGenericArgs for AssocTypeArg {}
8182
impl ast::HasTypeBounds for AssocTypeArg {}
8283
impl AssocTypeArg {
8384
pub fn const_arg(&self) -> Option<ConstArg> { support::child(&self.syntax) }
84-
pub fn generic_arg_list(&self) -> Option<GenericArgList> { support::child(&self.syntax) }
8585
pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) }
8686
pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) }
8787
pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) }
@@ -783,8 +783,8 @@ pub struct MethodCallExpr {
783783
}
784784
impl ast::HasArgList for MethodCallExpr {}
785785
impl ast::HasAttrs for MethodCallExpr {}
786+
impl ast::HasGenericArgs for MethodCallExpr {}
786787
impl MethodCallExpr {
787-
pub fn generic_arg_list(&self) -> Option<GenericArgList> { support::child(&self.syntax) }
788788
pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) }
789789
pub fn receiver(&self) -> Option<Expr> { support::child(&self.syntax) }
790790
pub fn dot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![.]) }
@@ -946,8 +946,8 @@ impl PathPat {
946946
pub struct PathSegment {
947947
pub(crate) syntax: SyntaxNode,
948948
}
949+
impl ast::HasGenericArgs for PathSegment {}
949950
impl PathSegment {
950-
pub fn generic_arg_list(&self) -> Option<GenericArgList> { support::child(&self.syntax) }
951951
pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) }
952952
pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) }
953953
pub fn path_type(&self) -> Option<PathType> { support::child(&self.syntax) }
@@ -1763,6 +1763,12 @@ pub struct AnyHasDocComments {
17631763
}
17641764
impl ast::HasDocComments for AnyHasDocComments {}
17651765

1766+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1767+
pub struct AnyHasGenericArgs {
1768+
pub(crate) syntax: SyntaxNode,
1769+
}
1770+
impl ast::HasGenericArgs for AnyHasGenericArgs {}
1771+
17661772
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
17671773
pub struct AnyHasGenericParams {
17681774
pub(crate) syntax: SyntaxNode,
@@ -4235,6 +4241,21 @@ impl AstNode for AnyHasDocComments {
42354241
}
42364242
fn syntax(&self) -> &SyntaxNode { &self.syntax }
42374243
}
4244+
impl AnyHasGenericArgs {
4245+
#[inline]
4246+
pub fn new<T: ast::HasGenericArgs>(node: T) -> AnyHasGenericArgs {
4247+
AnyHasGenericArgs { syntax: node.syntax().clone() }
4248+
}
4249+
}
4250+
impl AstNode for AnyHasGenericArgs {
4251+
fn can_cast(kind: SyntaxKind) -> bool {
4252+
matches!(kind, ASSOC_TYPE_ARG | METHOD_CALL_EXPR | PATH_SEGMENT)
4253+
}
4254+
fn cast(syntax: SyntaxNode) -> Option<Self> {
4255+
Self::can_cast(syntax.kind()).then_some(AnyHasGenericArgs { syntax })
4256+
}
4257+
fn syntax(&self) -> &SyntaxNode { &self.syntax }
4258+
}
42384259
impl AnyHasGenericParams {
42394260
#[inline]
42404261
pub fn new<T: ast::HasGenericParams>(node: T) -> AnyHasGenericParams {

src/tools/rust-analyzer/crates/syntax/src/ast/node_ext.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use parser::SyntaxKind;
1010
use rowan::{GreenNodeData, GreenTokenData};
1111

1212
use crate::{
13-
ast::{self, support, AstNode, AstToken, HasAttrs, HasGenericParams, HasName, SyntaxNode},
13+
ast::{
14+
self, support, AstNode, AstToken, HasAttrs, HasGenericArgs, HasGenericParams, HasName,
15+
SyntaxNode,
16+
},
1417
ted, NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
1518
};
1619

src/tools/rust-analyzer/crates/syntax/src/ast/traits.rs

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ pub trait HasGenericParams: AstNode {
5252
support::child(self.syntax())
5353
}
5454
}
55+
pub trait HasGenericArgs: AstNode {
56+
fn generic_arg_list(&self) -> Option<ast::GenericArgList> {
57+
support::child(self.syntax())
58+
}
59+
}
5560

5661
pub trait HasTypeBounds: AstNode {
5762
fn type_bound_list(&self) -> Option<ast::TypeBoundList> {

src/tools/rust-analyzer/xtask/src/codegen/grammar.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -797,20 +797,21 @@ fn extract_enums(ast: &mut AstSrc) {
797797
}
798798
}
799799

800-
fn extract_struct_traits(ast: &mut AstSrc) {
801-
let traits: &[(&str, &[&str])] = &[
802-
("HasAttrs", &["attrs"]),
803-
("HasName", &["name"]),
804-
("HasVisibility", &["visibility"]),
805-
("HasGenericParams", &["generic_param_list", "where_clause"]),
806-
("HasTypeBounds", &["type_bound_list", "colon_token"]),
807-
("HasModuleItem", &["items"]),
808-
("HasLoopBody", &["label", "loop_body"]),
809-
("HasArgList", &["arg_list"]),
810-
];
800+
const TRAITS: &[(&str, &[&str])] = &[
801+
("HasAttrs", &["attrs"]),
802+
("HasName", &["name"]),
803+
("HasVisibility", &["visibility"]),
804+
("HasGenericParams", &["generic_param_list", "where_clause"]),
805+
("HasGenericArgs", &["generic_arg_list"]),
806+
("HasTypeBounds", &["type_bound_list", "colon_token"]),
807+
("HasModuleItem", &["items"]),
808+
("HasLoopBody", &["label", "loop_body"]),
809+
("HasArgList", &["arg_list"]),
810+
];
811811

812+
fn extract_struct_traits(ast: &mut AstSrc) {
812813
for node in &mut ast.nodes {
813-
for (name, methods) in traits {
814+
for (name, methods) in TRAITS {
814815
extract_struct_trait(node, name, methods);
815816
}
816817
}

0 commit comments

Comments
 (0)