900: Add new trait ast::TypeAscriptionOwner r=vipentti a=vipentti

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.

In addition, we update some places where previously we used node + node.type_ref() with `TypeAscriptionOwner` in the trait bounds.

Co-authored-by: Ville Penttinen <villem.penttinen@gmail.com>
This commit is contained in:
bors[bot] 2019-02-27 12:18:55 +00:00
commit 2e2a6dd2fb
9 changed files with 72 additions and 61 deletions

View file

@ -6,7 +6,7 @@ use std::sync::Arc;
use ra_arena::{RawId, Arena, impl_arena_id};
use ra_syntax::{
TreeArc,
ast::{self, NameOwner, StructFlavor}
ast::{self, NameOwner, StructFlavor, TypeAscriptionOwner}
};
use crate::{
@ -164,7 +164,7 @@ impl VariantData {
.fields()
.map(|fd| StructFieldData {
name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
type_ref: TypeRef::from_ast_opt(fd.type_ref()),
type_ref: TypeRef::from_ast_opt(fd.ascribed_type()),
})
.collect();
VariantDataInner::Struct(fields)

View file

@ -1,6 +1,6 @@
use std::sync::Arc;
use ra_syntax::ast::{self, NameOwner};
use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
use crate::{
Name, AsName, Function, FnSignature,
@ -19,7 +19,7 @@ impl FnSignature {
let mut has_self_param = false;
if let Some(param_list) = node.param_list() {
if let Some(self_param) = param_list.self_param() {
let self_type = if let Some(type_ref) = self_param.type_ref() {
let self_type = if let Some(type_ref) = self_param.ascribed_type() {
TypeRef::from_ast(type_ref)
} else {
let self_type = TypeRef::Path(Name::self_type().into());
@ -37,7 +37,7 @@ impl FnSignature {
has_self_param = true;
}
for param in param_list.params() {
let type_ref = TypeRef::from_ast_opt(param.type_ref());
let type_ref = TypeRef::from_ast_opt(param.ascribed_type());
params.push(type_ref);
}
}

View file

@ -1,6 +1,6 @@
use std::sync::Arc;
use ra_syntax::ast::{self, NameOwner};
use ra_syntax::ast::{NameOwner, TypeAscriptionOwner};
use crate::{
Name, AsName, Const, ConstSignature, Static,
@ -8,12 +8,9 @@ use crate::{
PersistentHirDatabase,
};
fn const_signature_for<N: NameOwner>(
node: &N,
type_ref: Option<&ast::TypeRef>,
) -> Arc<ConstSignature> {
fn const_signature_for<N: NameOwner + TypeAscriptionOwner>(node: &N) -> Arc<ConstSignature> {
let name = node.name().map(|n| n.as_name()).unwrap_or_else(Name::missing);
let type_ref = TypeRef::from_ast_opt(type_ref);
let type_ref = TypeRef::from_ast_opt(node.ascribed_type());
let sig = ConstSignature { name, type_ref };
Arc::new(sig)
}
@ -24,7 +21,7 @@ impl ConstSignature {
konst: Const,
) -> Arc<ConstSignature> {
let (_, node) = konst.source(db);
const_signature_for(&*node, node.type_ref())
const_signature_for(&*node)
}
pub(crate) fn static_signature_query(
@ -32,6 +29,6 @@ impl ConstSignature {
konst: Static,
) -> Arc<ConstSignature> {
let (_, node) = konst.source(db);
const_signature_for(&*node, node.type_ref())
const_signature_for(&*node)
}
}

View file

@ -6,7 +6,7 @@ use rustc_hash::FxHashMap;
use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
use ra_syntax::{
SyntaxNodePtr, AstNode,
ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralFlavor}
ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralFlavor, TypeAscriptionOwner}
};
use crate::{
@ -709,7 +709,7 @@ impl ExprCollector {
if let Some(pl) = e.param_list() {
for param in pl.params() {
let pat = self.collect_pat_opt(param.pat());
let type_ref = param.type_ref().map(TypeRef::from_ast);
let type_ref = param.ascribed_type().map(TypeRef::from_ast);
args.push(pat);
arg_types.push(type_ref);
}
@ -790,7 +790,7 @@ impl ExprCollector {
.map(|s| match s.kind() {
ast::StmtKind::LetStmt(stmt) => {
let pat = self.collect_pat_opt(stmt.pat());
let type_ref = stmt.type_ref().map(TypeRef::from_ast);
let type_ref = stmt.ascribed_type().map(TypeRef::from_ast);
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
Statement::Let { pat, type_ref, initializer }
}

View file

@ -1,7 +1,7 @@
//! HIR for references to types. Paths in these are not yet resolved. They can
//! be directly created from an ast::TypeRef, without further queries.
use ra_syntax::ast;
use ra_syntax::ast::{self, TypeAscriptionOwner};
use crate::Path;
@ -81,7 +81,7 @@ impl TypeRef {
FnPointerType(inner) => {
let ret_ty = TypeRef::from_ast_opt(inner.ret_type().and_then(|rt| rt.type_ref()));
let mut params = if let Some(pl) = inner.param_list() {
pl.params().map(|p| p.type_ref()).map(TypeRef::from_ast_opt).collect()
pl.params().map(|p| p.ascribed_type()).map(TypeRef::from_ast_opt).collect()
} else {
Vec::new()
};

View file

@ -2,7 +2,7 @@ use crate::TextRange;
use ra_syntax::{
algo::visit::{visitor, Visitor},
ast::{self, AttrsOwner, NameOwner, TypeParamsOwner},
ast::{self, AttrsOwner, NameOwner, TypeParamsOwner, TypeAscriptionOwner},
AstNode, SourceFile, SyntaxKind, SyntaxNode, WalkEvent,
};
@ -45,6 +45,12 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
decl_with_detail(node, None)
}
fn decl_with_ascription<N: NameOwner + AttrsOwner + TypeAscriptionOwner>(
node: &N,
) -> Option<StructureNode> {
decl_with_type_ref(node, node.ascribed_type())
}
fn decl_with_type_ref<N: NameOwner + AttrsOwner>(
node: &N,
type_ref: Option<&ast::TypeRef>,
@ -107,14 +113,14 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
decl_with_detail(fn_def, Some(detail))
})
.visit(decl::<ast::StructDef>)
.visit(|nfd: &ast::NamedFieldDef| decl_with_type_ref(nfd, nfd.type_ref()))
.visit(decl::<ast::EnumDef>)
.visit(decl::<ast::EnumVariant>)
.visit(decl::<ast::TraitDef>)
.visit(decl::<ast::Module>)
.visit(|td: &ast::TypeAliasDef| decl_with_type_ref(td, td.type_ref()))
.visit(|cd: &ast::ConstDef| decl_with_type_ref(cd, cd.type_ref()))
.visit(|sd: &ast::StaticDef| decl_with_type_ref(sd, sd.type_ref()))
.visit(decl_with_ascription::<ast::NamedFieldDef>)
.visit(decl_with_ascription::<ast::ConstDef>)
.visit(decl_with_ascription::<ast::StaticDef>)
.visit(|im: &ast::ImplBlock| {
let target_type = im.target_type()?;
let target_trait = im.target_trait();

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,11 +628,8 @@ impl ast::NameOwner for ConstDef {}
impl ast::TypeParamsOwner for ConstDef {}
impl ast::AttrsOwner for ConstDef {}
impl ast::DocCommentsOwner for ConstDef {}
impl ConstDef {
pub fn type_ref(&self) -> Option<&TypeRef> {
super::child_opt(self)
}
}
impl ast::TypeAscriptionOwner for ConstDef {}
impl ConstDef {}
// ContinueExpr
#[derive(Debug, PartialEq, Eq, Hash)]
@ -1767,15 +1764,12 @@ impl ToOwned for LetStmt {
}
impl ast::TypeAscriptionOwner for LetStmt {}
impl LetStmt {
pub fn pat(&self) -> Option<&Pat> {
super::child_opt(self)
}
pub fn type_ref(&self) -> Option<&TypeRef> {
super::child_opt(self)
}
pub fn initializer(&self) -> Option<&Expr> {
super::child_opt(self)
}
@ -2592,11 +2586,8 @@ impl ast::VisibilityOwner for NamedFieldDef {}
impl ast::NameOwner for NamedFieldDef {}
impl ast::AttrsOwner for NamedFieldDef {}
impl ast::DocCommentsOwner for NamedFieldDef {}
impl NamedFieldDef {
pub fn type_ref(&self) -> Option<&TypeRef> {
super::child_opt(self)
}
}
impl ast::TypeAscriptionOwner for NamedFieldDef {}
impl NamedFieldDef {}
// NamedFieldDefList
#[derive(Debug, PartialEq, Eq, Hash)]
@ -2774,14 +2765,11 @@ impl ToOwned for Param {
}
impl ast::TypeAscriptionOwner for Param {}
impl Param {
pub fn pat(&self) -> Option<&Pat> {
super::child_opt(self)
}
pub fn type_ref(&self) -> Option<&TypeRef> {
super::child_opt(self)
}
}
// ParamList
@ -3685,11 +3673,8 @@ impl ToOwned for SelfParam {
}
impl ast::TypeAscriptionOwner for SelfParam {}
impl SelfParam {
pub fn type_ref(&self) -> Option<&TypeRef> {
super::child_opt(self)
}
pub fn self_kw(&self) -> Option<&SelfKw> {
super::child_opt(self)
}
@ -3820,11 +3805,8 @@ impl ast::NameOwner for StaticDef {}
impl ast::TypeParamsOwner for StaticDef {}
impl ast::AttrsOwner for StaticDef {}
impl ast::DocCommentsOwner for StaticDef {}
impl StaticDef {
pub fn type_ref(&self) -> Option<&TypeRef> {
super::child_opt(self)
}
}
impl ast::TypeAscriptionOwner for StaticDef {}
impl StaticDef {}
// Stmt
#[derive(Debug, PartialEq, Eq, Hash)]

View file

@ -271,7 +271,15 @@ Grammar(
]
),
"NamedFieldDefList": (collections: [["fields", "NamedFieldDef"]]),
"NamedFieldDef": ( traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner"], options: ["TypeRef"] ),
"NamedFieldDef": (
traits: [
"VisibilityOwner",
"NameOwner",
"AttrsOwner",
"DocCommentsOwner",
"TypeAscriptionOwner"
]
),
"PosFieldDefList": (collections: [["fields", "PosFieldDef"]]),
"PosFieldDef": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]),
"EnumDef": ( traits: [
@ -298,9 +306,9 @@ Grammar(
"NameOwner",
"TypeParamsOwner",
"AttrsOwner",
"DocCommentsOwner"
"DocCommentsOwner",
"TypeAscriptionOwner",
],
options: ["TypeRef"]
),
"StaticDef": (
traits: [
@ -308,9 +316,9 @@ Grammar(
"NameOwner",
"TypeParamsOwner",
"AttrsOwner",
"DocCommentsOwner"
"DocCommentsOwner",
"TypeAscriptionOwner",
],
options: ["TypeRef"]
),
"TypeAliasDef": (
traits: [
@ -569,11 +577,15 @@ Grammar(
"ExprStmt": (
options: [ ["expr", "Expr"] ]
),
"LetStmt": ( options: [
["pat", "Pat"],
["type_ref", "TypeRef"],
["initializer", "Expr"],
]),
"LetStmt": (
options: [
["pat", "Pat"],
["initializer", "Expr"],
],
traits: [
"TypeAscriptionOwner",
]
),
"Condition": (
options: [ "Pat", "Expr" ]
),
@ -595,10 +607,18 @@ Grammar(
["params", "Param"]
]
),
"SelfParam": (options: ["TypeRef", "SelfKw"]),
"SelfParam": (
options: ["SelfKw"],
traits: [
"TypeAscriptionOwner",
]
),
"SelfKw": (),
"Param": (
options: [ "Pat", "TypeRef" ],
options: [ "Pat" ],
traits: [
"TypeAscriptionOwner",
]
),
"UseItem": (
traits: ["AttrsOwner"],