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 {
fn name(&self) -> Option<&Name> {
child_opt(self)

View file

@ -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)

View file

@ -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"],