From 6eb070d6613644b6698a5ce6454d006662c84d8a Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Tue, 26 Feb 2019 11:35:57 +0200 Subject: Add new trait TypeAscriptionOwner This trait should be implemented for nodes which have an ascribed type, e.g. thing : Type. Such as let, const, static, param, named struct fields. --- crates/ra_syntax/src/ast.rs | 6 ++++++ crates/ra_syntax/src/ast/generated.rs | 6 ++++++ crates/ra_syntax/src/grammar.ron | 33 ++++++++++++++++++++++++--------- 3 files changed, 36 insertions(+), 9 deletions(-) (limited to 'crates') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 56fb7c20c..81c709bfb 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -31,6 +31,12 @@ pub trait AstToken: AstNode { } } +pub trait TypeAscriptionOwner: AstNode { + fn ascribed_type(&self) -> Option<&TypeRef> { + child_opt(self) + } +} + pub trait NameOwner: AstNode { fn name(&self) -> Option<&Name> { child_opt(self) diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 4e2705d09..6db2f74ef 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -628,6 +628,7 @@ impl ast::NameOwner for ConstDef {} impl ast::TypeParamsOwner for ConstDef {} impl ast::AttrsOwner for ConstDef {} impl ast::DocCommentsOwner for ConstDef {} +impl ast::TypeAscriptionOwner for ConstDef {} impl ConstDef { pub fn type_ref(&self) -> Option<&TypeRef> { super::child_opt(self) @@ -1767,6 +1768,7 @@ impl ToOwned for LetStmt { } +impl ast::TypeAscriptionOwner for LetStmt {} impl LetStmt { pub fn pat(&self) -> Option<&Pat> { super::child_opt(self) @@ -2592,6 +2594,7 @@ impl ast::VisibilityOwner for NamedFieldDef {} impl ast::NameOwner for NamedFieldDef {} impl ast::AttrsOwner for NamedFieldDef {} impl ast::DocCommentsOwner for NamedFieldDef {} +impl ast::TypeAscriptionOwner for NamedFieldDef {} impl NamedFieldDef { pub fn type_ref(&self) -> Option<&TypeRef> { super::child_opt(self) @@ -2774,6 +2777,7 @@ impl ToOwned for Param { } +impl ast::TypeAscriptionOwner for Param {} impl Param { pub fn pat(&self) -> Option<&Pat> { super::child_opt(self) @@ -3685,6 +3689,7 @@ impl ToOwned for SelfParam { } +impl ast::TypeAscriptionOwner for SelfParam {} impl SelfParam { pub fn type_ref(&self) -> Option<&TypeRef> { super::child_opt(self) @@ -3820,6 +3825,7 @@ impl ast::NameOwner for StaticDef {} impl ast::TypeParamsOwner for StaticDef {} impl ast::AttrsOwner for StaticDef {} impl ast::DocCommentsOwner for StaticDef {} +impl ast::TypeAscriptionOwner for StaticDef {} impl StaticDef { pub fn type_ref(&self) -> Option<&TypeRef> { super::child_opt(self) diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index c7acbbd6c..a3bfc74a7 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -271,7 +271,7 @@ Grammar( ] ), "NamedFieldDefList": (collections: [["fields", "NamedFieldDef"]]), - "NamedFieldDef": ( traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner"], options: ["TypeRef"] ), + "NamedFieldDef": ( traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner", "TypeAscriptionOwner"], options: ["TypeRef"] ), "PosFieldDefList": (collections: [["fields", "PosFieldDef"]]), "PosFieldDef": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]), "EnumDef": ( traits: [ @@ -298,7 +298,8 @@ Grammar( "NameOwner", "TypeParamsOwner", "AttrsOwner", - "DocCommentsOwner" + "DocCommentsOwner", + "TypeAscriptionOwner", ], options: ["TypeRef"] ), @@ -308,7 +309,8 @@ Grammar( "NameOwner", "TypeParamsOwner", "AttrsOwner", - "DocCommentsOwner" + "DocCommentsOwner", + "TypeAscriptionOwner", ], options: ["TypeRef"] ), @@ -569,11 +571,16 @@ Grammar( "ExprStmt": ( options: [ ["expr", "Expr"] ] ), - "LetStmt": ( options: [ - ["pat", "Pat"], - ["type_ref", "TypeRef"], - ["initializer", "Expr"], - ]), + "LetStmt": ( + options: [ + ["pat", "Pat"], + ["type_ref", "TypeRef"], + ["initializer", "Expr"], + ], + traits: [ + "TypeAscriptionOwner", + ] + ), "Condition": ( options: [ "Pat", "Expr" ] ), @@ -595,10 +602,18 @@ Grammar( ["params", "Param"] ] ), - "SelfParam": (options: ["TypeRef", "SelfKw"]), + "SelfParam": ( + options: ["TypeRef", "SelfKw"], + traits: [ + "TypeAscriptionOwner", + ] + ), "SelfKw": (), "Param": ( options: [ "Pat", "TypeRef" ], + traits: [ + "TypeAscriptionOwner", + ] ), "UseItem": ( traits: ["AttrsOwner"], -- cgit v1.2.3