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.
This commit is contained in:
Ville Penttinen 2019-02-26 11:35:57 +02:00
parent 7c9acf2f83
commit 6eb070d661
3 changed files with 36 additions and 9 deletions

View File

@ -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 { pub trait NameOwner: AstNode {
fn name(&self) -> Option<&Name> { fn name(&self) -> Option<&Name> {
child_opt(self) child_opt(self)

View File

@ -628,6 +628,7 @@ impl ast::NameOwner for ConstDef {}
impl ast::TypeParamsOwner for ConstDef {} impl ast::TypeParamsOwner for ConstDef {}
impl ast::AttrsOwner for ConstDef {} impl ast::AttrsOwner for ConstDef {}
impl ast::DocCommentsOwner for ConstDef {} impl ast::DocCommentsOwner for ConstDef {}
impl ast::TypeAscriptionOwner for ConstDef {}
impl ConstDef { impl ConstDef {
pub fn type_ref(&self) -> Option<&TypeRef> { pub fn type_ref(&self) -> Option<&TypeRef> {
super::child_opt(self) super::child_opt(self)
@ -1767,6 +1768,7 @@ impl ToOwned for LetStmt {
} }
impl ast::TypeAscriptionOwner for LetStmt {}
impl LetStmt { impl LetStmt {
pub fn pat(&self) -> Option<&Pat> { pub fn pat(&self) -> Option<&Pat> {
super::child_opt(self) super::child_opt(self)
@ -2592,6 +2594,7 @@ impl ast::VisibilityOwner for NamedFieldDef {}
impl ast::NameOwner for NamedFieldDef {} impl ast::NameOwner for NamedFieldDef {}
impl ast::AttrsOwner for NamedFieldDef {} impl ast::AttrsOwner for NamedFieldDef {}
impl ast::DocCommentsOwner for NamedFieldDef {} impl ast::DocCommentsOwner for NamedFieldDef {}
impl ast::TypeAscriptionOwner for NamedFieldDef {}
impl NamedFieldDef { impl NamedFieldDef {
pub fn type_ref(&self) -> Option<&TypeRef> { pub fn type_ref(&self) -> Option<&TypeRef> {
super::child_opt(self) super::child_opt(self)
@ -2774,6 +2777,7 @@ impl ToOwned for Param {
} }
impl ast::TypeAscriptionOwner for Param {}
impl Param { impl Param {
pub fn pat(&self) -> Option<&Pat> { pub fn pat(&self) -> Option<&Pat> {
super::child_opt(self) super::child_opt(self)
@ -3685,6 +3689,7 @@ impl ToOwned for SelfParam {
} }
impl ast::TypeAscriptionOwner for SelfParam {}
impl SelfParam { impl SelfParam {
pub fn type_ref(&self) -> Option<&TypeRef> { pub fn type_ref(&self) -> Option<&TypeRef> {
super::child_opt(self) super::child_opt(self)
@ -3820,6 +3825,7 @@ impl ast::NameOwner for StaticDef {}
impl ast::TypeParamsOwner for StaticDef {} impl ast::TypeParamsOwner for StaticDef {}
impl ast::AttrsOwner for StaticDef {} impl ast::AttrsOwner for StaticDef {}
impl ast::DocCommentsOwner for StaticDef {} impl ast::DocCommentsOwner for StaticDef {}
impl ast::TypeAscriptionOwner for StaticDef {}
impl StaticDef { impl StaticDef {
pub fn type_ref(&self) -> Option<&TypeRef> { pub fn type_ref(&self) -> Option<&TypeRef> {
super::child_opt(self) super::child_opt(self)

View File

@ -271,7 +271,7 @@ Grammar(
] ]
), ),
"NamedFieldDefList": (collections: [["fields", "NamedFieldDef"]]), "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"]]), "PosFieldDefList": (collections: [["fields", "PosFieldDef"]]),
"PosFieldDef": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]), "PosFieldDef": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]),
"EnumDef": ( traits: [ "EnumDef": ( traits: [
@ -298,7 +298,8 @@ Grammar(
"NameOwner", "NameOwner",
"TypeParamsOwner", "TypeParamsOwner",
"AttrsOwner", "AttrsOwner",
"DocCommentsOwner" "DocCommentsOwner",
"TypeAscriptionOwner",
], ],
options: ["TypeRef"] options: ["TypeRef"]
), ),
@ -308,7 +309,8 @@ Grammar(
"NameOwner", "NameOwner",
"TypeParamsOwner", "TypeParamsOwner",
"AttrsOwner", "AttrsOwner",
"DocCommentsOwner" "DocCommentsOwner",
"TypeAscriptionOwner",
], ],
options: ["TypeRef"] options: ["TypeRef"]
), ),
@ -569,11 +571,16 @@ Grammar(
"ExprStmt": ( "ExprStmt": (
options: [ ["expr", "Expr"] ] options: [ ["expr", "Expr"] ]
), ),
"LetStmt": ( options: [ "LetStmt": (
["pat", "Pat"], options: [
["type_ref", "TypeRef"], ["pat", "Pat"],
["initializer", "Expr"], ["type_ref", "TypeRef"],
]), ["initializer", "Expr"],
],
traits: [
"TypeAscriptionOwner",
]
),
"Condition": ( "Condition": (
options: [ "Pat", "Expr" ] options: [ "Pat", "Expr" ]
), ),
@ -595,10 +602,18 @@ Grammar(
["params", "Param"] ["params", "Param"]
] ]
), ),
"SelfParam": (options: ["TypeRef", "SelfKw"]), "SelfParam": (
options: ["TypeRef", "SelfKw"],
traits: [
"TypeAscriptionOwner",
]
),
"SelfKw": (), "SelfKw": (),
"Param": ( "Param": (
options: [ "Pat", "TypeRef" ], options: [ "Pat", "TypeRef" ],
traits: [
"TypeAscriptionOwner",
]
), ),
"UseItem": ( "UseItem": (
traits: ["AttrsOwner"], traits: ["AttrsOwner"],