This commit is contained in:
Lukas Wirth 2021-09-21 15:56:06 +02:00
parent 6d6e0b8f21
commit b36f12dba5
4 changed files with 112 additions and 184 deletions

View file

@ -682,20 +682,7 @@ impl<'db> SemanticsImpl<'db> {
fn resolve_lifetime_param(&self, lifetime: &ast::Lifetime) -> Option<LifetimeParam> { fn resolve_lifetime_param(&self, lifetime: &ast::Lifetime) -> Option<LifetimeParam> {
let text = lifetime.text(); let text = lifetime.text();
let lifetime_param = lifetime.syntax().ancestors().find_map(|syn| { let lifetime_param = lifetime.syntax().ancestors().find_map(|syn| {
let gpl = match_ast! { let gpl = ast::DynGenericParamsOwner::cast(syn)?.generic_param_list()?;
match syn {
ast::Fn(it) => it.generic_param_list()?,
ast::TypeAlias(it) => it.generic_param_list()?,
ast::Struct(it) => it.generic_param_list()?,
ast::Enum(it) => it.generic_param_list()?,
ast::Union(it) => it.generic_param_list()?,
ast::Trait(it) => it.generic_param_list()?,
ast::Impl(it) => it.generic_param_list()?,
ast::WherePred(it) => it.generic_param_list()?,
ast::ForType(it) => it.generic_param_list()?,
_ => return None,
}
};
gpl.lifetime_params() gpl.lifetime_params()
.find(|tp| tp.lifetime().as_ref().map(|lt| lt.text()).as_ref() == Some(&text)) .find(|tp| tp.lifetime().as_ref().map(|lt| lt.text()).as_ref() == Some(&text))
})?; })?;

View file

@ -3618,12 +3618,10 @@ impl AstNode for DynArgListOwner {
} }
} }
fn cast(syntax: SyntaxNode) -> Option<Self> { fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() { match syntax.kind() {
CALL_EXPR => DynArgListOwner { syntax }, CALL_EXPR | METHOD_CALL_EXPR => Some(DynArgListOwner { syntax }),
METHOD_CALL_EXPR => DynArgListOwner { syntax }, _ => None,
_ => return None, }
};
Some(res)
} }
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
@ -3705,75 +3703,74 @@ impl AstNode for DynAttrsOwner {
} }
} }
fn cast(syntax: SyntaxNode) -> Option<Self> { fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() { match syntax.kind() {
MACRO_CALL => DynAttrsOwner { syntax }, MACRO_CALL
SOURCE_FILE => DynAttrsOwner { syntax }, | SOURCE_FILE
CONST => DynAttrsOwner { syntax }, | CONST
ENUM => DynAttrsOwner { syntax }, | ENUM
EXTERN_BLOCK => DynAttrsOwner { syntax }, | EXTERN_BLOCK
EXTERN_CRATE => DynAttrsOwner { syntax }, | EXTERN_CRATE
FN => DynAttrsOwner { syntax }, | FN
IMPL => DynAttrsOwner { syntax }, | IMPL
MACRO_RULES => DynAttrsOwner { syntax }, | MACRO_RULES
MACRO_DEF => DynAttrsOwner { syntax }, | MACRO_DEF
MODULE => DynAttrsOwner { syntax }, | MODULE
STATIC => DynAttrsOwner { syntax }, | STATIC
STRUCT => DynAttrsOwner { syntax }, | STRUCT
TRAIT => DynAttrsOwner { syntax }, | TRAIT
TYPE_ALIAS => DynAttrsOwner { syntax }, | TYPE_ALIAS
UNION => DynAttrsOwner { syntax }, | UNION
USE => DynAttrsOwner { syntax }, | USE
ITEM_LIST => DynAttrsOwner { syntax }, | ITEM_LIST
BLOCK_EXPR => DynAttrsOwner { syntax }, | BLOCK_EXPR
SELF_PARAM => DynAttrsOwner { syntax }, | SELF_PARAM
PARAM => DynAttrsOwner { syntax }, | PARAM
RECORD_FIELD => DynAttrsOwner { syntax }, | RECORD_FIELD
TUPLE_FIELD => DynAttrsOwner { syntax }, | TUPLE_FIELD
VARIANT => DynAttrsOwner { syntax }, | VARIANT
ASSOC_ITEM_LIST => DynAttrsOwner { syntax }, | ASSOC_ITEM_LIST
EXTERN_ITEM_LIST => DynAttrsOwner { syntax }, | EXTERN_ITEM_LIST
CONST_PARAM => DynAttrsOwner { syntax }, | CONST_PARAM
LIFETIME_PARAM => DynAttrsOwner { syntax }, | LIFETIME_PARAM
TYPE_PARAM => DynAttrsOwner { syntax }, | TYPE_PARAM
EXPR_STMT => DynAttrsOwner { syntax }, | EXPR_STMT
LET_STMT => DynAttrsOwner { syntax }, | LET_STMT
ARRAY_EXPR => DynAttrsOwner { syntax }, | ARRAY_EXPR
AWAIT_EXPR => DynAttrsOwner { syntax }, | AWAIT_EXPR
BIN_EXPR => DynAttrsOwner { syntax }, | BIN_EXPR
BOX_EXPR => DynAttrsOwner { syntax }, | BOX_EXPR
BREAK_EXPR => DynAttrsOwner { syntax }, | BREAK_EXPR
CALL_EXPR => DynAttrsOwner { syntax }, | CALL_EXPR
CAST_EXPR => DynAttrsOwner { syntax }, | CAST_EXPR
CLOSURE_EXPR => DynAttrsOwner { syntax }, | CLOSURE_EXPR
CONTINUE_EXPR => DynAttrsOwner { syntax }, | CONTINUE_EXPR
EFFECT_EXPR => DynAttrsOwner { syntax }, | EFFECT_EXPR
FIELD_EXPR => DynAttrsOwner { syntax }, | FIELD_EXPR
FOR_EXPR => DynAttrsOwner { syntax }, | FOR_EXPR
IF_EXPR => DynAttrsOwner { syntax }, | IF_EXPR
INDEX_EXPR => DynAttrsOwner { syntax }, | INDEX_EXPR
LITERAL => DynAttrsOwner { syntax }, | LITERAL
LOOP_EXPR => DynAttrsOwner { syntax }, | LOOP_EXPR
MATCH_EXPR => DynAttrsOwner { syntax }, | MATCH_EXPR
METHOD_CALL_EXPR => DynAttrsOwner { syntax }, | METHOD_CALL_EXPR
PAREN_EXPR => DynAttrsOwner { syntax }, | PAREN_EXPR
PATH_EXPR => DynAttrsOwner { syntax }, | PATH_EXPR
PREFIX_EXPR => DynAttrsOwner { syntax }, | PREFIX_EXPR
RANGE_EXPR => DynAttrsOwner { syntax }, | RANGE_EXPR
REF_EXPR => DynAttrsOwner { syntax }, | REF_EXPR
RETURN_EXPR => DynAttrsOwner { syntax }, | RETURN_EXPR
TRY_EXPR => DynAttrsOwner { syntax }, | TRY_EXPR
TUPLE_EXPR => DynAttrsOwner { syntax }, | TUPLE_EXPR
WHILE_EXPR => DynAttrsOwner { syntax }, | WHILE_EXPR
YIELD_EXPR => DynAttrsOwner { syntax }, | YIELD_EXPR
RECORD_EXPR_FIELD_LIST => DynAttrsOwner { syntax }, | RECORD_EXPR_FIELD_LIST
RECORD_EXPR_FIELD => DynAttrsOwner { syntax }, | RECORD_EXPR_FIELD
MATCH_ARM_LIST => DynAttrsOwner { syntax }, | MATCH_ARM_LIST
MATCH_ARM => DynAttrsOwner { syntax }, | MATCH_ARM
IDENT_PAT => DynAttrsOwner { syntax }, | IDENT_PAT
RECORD_PAT_FIELD => DynAttrsOwner { syntax }, | RECORD_PAT_FIELD => Some(DynAttrsOwner { syntax }),
_ => return None, _ => None,
}; }
Some(res)
} }
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
@ -3791,17 +3788,12 @@ impl AstNode for DynGenericParamsOwner {
} }
} }
fn cast(syntax: SyntaxNode) -> Option<Self> { fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() { match syntax.kind() {
ENUM => DynGenericParamsOwner { syntax }, ENUM | FN | IMPL | STRUCT | TRAIT | TYPE_ALIAS | UNION => {
FN => DynGenericParamsOwner { syntax }, Some(DynGenericParamsOwner { syntax })
IMPL => DynGenericParamsOwner { syntax }, }
STRUCT => DynGenericParamsOwner { syntax }, _ => None,
TRAIT => DynGenericParamsOwner { syntax }, }
TYPE_ALIAS => DynGenericParamsOwner { syntax },
UNION => DynGenericParamsOwner { syntax },
_ => return None,
};
Some(res)
} }
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
@ -3819,13 +3811,10 @@ impl AstNode for DynLoopBodyOwner {
} }
} }
fn cast(syntax: SyntaxNode) -> Option<Self> { fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() { match syntax.kind() {
FOR_EXPR => DynLoopBodyOwner { syntax }, FOR_EXPR | LOOP_EXPR | WHILE_EXPR => Some(DynLoopBodyOwner { syntax }),
LOOP_EXPR => DynLoopBodyOwner { syntax }, _ => None,
WHILE_EXPR => DynLoopBodyOwner { syntax }, }
_ => return None,
};
Some(res)
} }
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
@ -3843,13 +3832,10 @@ impl AstNode for DynModuleItemOwner {
} }
} }
fn cast(syntax: SyntaxNode) -> Option<Self> { fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() { match syntax.kind() {
MACRO_ITEMS => DynModuleItemOwner { syntax }, MACRO_ITEMS | SOURCE_FILE | ITEM_LIST => Some(DynModuleItemOwner { syntax }),
SOURCE_FILE => DynModuleItemOwner { syntax }, _ => None,
ITEM_LIST => DynModuleItemOwner { syntax }, }
_ => return None,
};
Some(res)
} }
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
@ -3869,28 +3855,12 @@ impl AstNode for DynNameOwner {
} }
} }
fn cast(syntax: SyntaxNode) -> Option<Self> { fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() { match syntax.kind() {
CONST => DynNameOwner { syntax }, CONST | ENUM | FN | MACRO_RULES | MACRO_DEF | MODULE | STATIC | STRUCT | TRAIT
ENUM => DynNameOwner { syntax }, | TYPE_ALIAS | UNION | RENAME | SELF_PARAM | RECORD_FIELD | VARIANT | CONST_PARAM
FN => DynNameOwner { syntax }, | TYPE_PARAM | IDENT_PAT => Some(DynNameOwner { syntax }),
MACRO_RULES => DynNameOwner { syntax }, _ => None,
MACRO_DEF => DynNameOwner { syntax }, }
MODULE => DynNameOwner { syntax },
STATIC => DynNameOwner { syntax },
STRUCT => DynNameOwner { syntax },
TRAIT => DynNameOwner { syntax },
TYPE_ALIAS => DynNameOwner { syntax },
UNION => DynNameOwner { syntax },
RENAME => DynNameOwner { syntax },
SELF_PARAM => DynNameOwner { syntax },
RECORD_FIELD => DynNameOwner { syntax },
VARIANT => DynNameOwner { syntax },
CONST_PARAM => DynNameOwner { syntax },
TYPE_PARAM => DynNameOwner { syntax },
IDENT_PAT => DynNameOwner { syntax },
_ => return None,
};
Some(res)
} }
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
@ -3908,16 +3878,12 @@ impl AstNode for DynTypeBoundsOwner {
} }
} }
fn cast(syntax: SyntaxNode) -> Option<Self> { fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() { match syntax.kind() {
ASSOC_TYPE_ARG => DynTypeBoundsOwner { syntax }, ASSOC_TYPE_ARG | TRAIT | TYPE_ALIAS | LIFETIME_PARAM | TYPE_PARAM | WHERE_PRED => {
TRAIT => DynTypeBoundsOwner { syntax }, Some(DynTypeBoundsOwner { syntax })
TYPE_ALIAS => DynTypeBoundsOwner { syntax }, }
LIFETIME_PARAM => DynTypeBoundsOwner { syntax }, _ => None,
TYPE_PARAM => DynTypeBoundsOwner { syntax }, }
WHERE_PRED => DynTypeBoundsOwner { syntax },
_ => return None,
};
Some(res)
} }
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }
@ -3938,27 +3904,13 @@ impl AstNode for DynVisibilityOwner {
} }
} }
fn cast(syntax: SyntaxNode) -> Option<Self> { fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() { match syntax.kind() {
CONST => DynVisibilityOwner { syntax }, CONST | ENUM | EXTERN_CRATE | FN | IMPL | MACRO_RULES | MACRO_DEF | MODULE | STATIC
ENUM => DynVisibilityOwner { syntax }, | STRUCT | TRAIT | TYPE_ALIAS | UNION | USE | RECORD_FIELD | TUPLE_FIELD | VARIANT => {
EXTERN_CRATE => DynVisibilityOwner { syntax }, Some(DynVisibilityOwner { syntax })
FN => DynVisibilityOwner { syntax }, }
IMPL => DynVisibilityOwner { syntax }, _ => None,
MACRO_RULES => DynVisibilityOwner { syntax }, }
MACRO_DEF => DynVisibilityOwner { syntax },
MODULE => DynVisibilityOwner { syntax },
STATIC => DynVisibilityOwner { syntax },
STRUCT => DynVisibilityOwner { syntax },
TRAIT => DynVisibilityOwner { syntax },
TYPE_ALIAS => DynVisibilityOwner { syntax },
UNION => DynVisibilityOwner { syntax },
USE => DynVisibilityOwner { syntax },
RECORD_FIELD => DynVisibilityOwner { syntax },
TUPLE_FIELD => DynVisibilityOwner { syntax },
VARIANT => DynVisibilityOwner { syntax },
_ => return None,
};
Some(res)
} }
fn syntax(&self) -> &SyntaxNode { &self.syntax } fn syntax(&self) -> &SyntaxNode { &self.syntax }
} }

View file

@ -572,16 +572,7 @@ impl ast::Variant {
impl ast::Item { impl ast::Item {
pub fn generic_param_list(&self) -> Option<ast::GenericParamList> { pub fn generic_param_list(&self) -> Option<ast::GenericParamList> {
match self { ast::DynGenericParamsOwner::cast(self.syntax().clone())?.generic_param_list()
ast::Item::Enum(it) => it.generic_param_list(),
ast::Item::Fn(it) => it.generic_param_list(),
ast::Item::Impl(it) => it.generic_param_list(),
ast::Item::Struct(it) => it.generic_param_list(),
ast::Item::Trait(it) => it.generic_param_list(),
ast::Item::TypeAlias(it) => it.generic_param_list(),
ast::Item::Union(it) => it.generic_param_list(),
_ => None,
}
} }
} }

View file

@ -250,13 +250,11 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> String {
} }
} }
fn cast(syntax: SyntaxNode) -> Option<Self> { fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() { match syntax.kind() {
#( #(#kinds)|* => Some(#name { syntax }),
#kinds => #name { syntax }, _ => None,
)* }
_ => return None,
};
Some(res)
} }
fn syntax(&self) -> &SyntaxNode { fn syntax(&self) -> &SyntaxNode {
&self.syntax &self.syntax