aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-02-27 12:18:55 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-02-27 12:18:55 +0000
commit2e2a6dd2fbeb4da16e602fa1902ab6bbd850b442 (patch)
treeb42e6faf8618c3625e41d5a2011b4ffd145f22de /crates
parent1927eb088ac9aa3851f77bb929296873ccb4faed (diff)
parentd3ce69aee3297e683691ec0123f5a2584a8075a0 (diff)
Merge #900
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 <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/adt.rs4
-rw-r--r--crates/ra_hir/src/code_model_impl/function.rs6
-rw-r--r--crates/ra_hir/src/code_model_impl/konst.rs13
-rw-r--r--crates/ra_hir/src/expr.rs6
-rw-r--r--crates/ra_hir/src/type_ref.rs4
-rw-r--r--crates/ra_ide_api_light/src/structure.rs14
-rw-r--r--crates/ra_syntax/src/ast.rs6
-rw-r--r--crates/ra_syntax/src/ast/generated.rs36
-rw-r--r--crates/ra_syntax/src/grammar.ron44
9 files changed, 72 insertions, 61 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs
index 6d917bb1b..325f1d7b6 100644
--- a/crates/ra_hir/src/adt.rs
+++ b/crates/ra_hir/src/adt.rs
@@ -6,7 +6,7 @@ use std::sync::Arc;
6use ra_arena::{RawId, Arena, impl_arena_id}; 6use ra_arena::{RawId, Arena, impl_arena_id};
7use ra_syntax::{ 7use ra_syntax::{
8 TreeArc, 8 TreeArc,
9 ast::{self, NameOwner, StructFlavor} 9 ast::{self, NameOwner, StructFlavor, TypeAscriptionOwner}
10}; 10};
11 11
12use crate::{ 12use crate::{
@@ -164,7 +164,7 @@ impl VariantData {
164 .fields() 164 .fields()
165 .map(|fd| StructFieldData { 165 .map(|fd| StructFieldData {
166 name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), 166 name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
167 type_ref: TypeRef::from_ast_opt(fd.type_ref()), 167 type_ref: TypeRef::from_ast_opt(fd.ascribed_type()),
168 }) 168 })
169 .collect(); 169 .collect();
170 VariantDataInner::Struct(fields) 170 VariantDataInner::Struct(fields)
diff --git a/crates/ra_hir/src/code_model_impl/function.rs b/crates/ra_hir/src/code_model_impl/function.rs
index c401528c6..c1654b069 100644
--- a/crates/ra_hir/src/code_model_impl/function.rs
+++ b/crates/ra_hir/src/code_model_impl/function.rs
@@ -1,6 +1,6 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use ra_syntax::ast::{self, NameOwner}; 3use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
4 4
5use crate::{ 5use crate::{
6 Name, AsName, Function, FnSignature, 6 Name, AsName, Function, FnSignature,
@@ -19,7 +19,7 @@ impl FnSignature {
19 let mut has_self_param = false; 19 let mut has_self_param = false;
20 if let Some(param_list) = node.param_list() { 20 if let Some(param_list) = node.param_list() {
21 if let Some(self_param) = param_list.self_param() { 21 if let Some(self_param) = param_list.self_param() {
22 let self_type = if let Some(type_ref) = self_param.type_ref() { 22 let self_type = if let Some(type_ref) = self_param.ascribed_type() {
23 TypeRef::from_ast(type_ref) 23 TypeRef::from_ast(type_ref)
24 } else { 24 } else {
25 let self_type = TypeRef::Path(Name::self_type().into()); 25 let self_type = TypeRef::Path(Name::self_type().into());
@@ -37,7 +37,7 @@ impl FnSignature {
37 has_self_param = true; 37 has_self_param = true;
38 } 38 }
39 for param in param_list.params() { 39 for param in param_list.params() {
40 let type_ref = TypeRef::from_ast_opt(param.type_ref()); 40 let type_ref = TypeRef::from_ast_opt(param.ascribed_type());
41 params.push(type_ref); 41 params.push(type_ref);
42 } 42 }
43 } 43 }
diff --git a/crates/ra_hir/src/code_model_impl/konst.rs b/crates/ra_hir/src/code_model_impl/konst.rs
index ecf4c8122..8b861a81f 100644
--- a/crates/ra_hir/src/code_model_impl/konst.rs
+++ b/crates/ra_hir/src/code_model_impl/konst.rs
@@ -1,6 +1,6 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use ra_syntax::ast::{self, NameOwner}; 3use ra_syntax::ast::{NameOwner, TypeAscriptionOwner};
4 4
5use crate::{ 5use crate::{
6 Name, AsName, Const, ConstSignature, Static, 6 Name, AsName, Const, ConstSignature, Static,
@@ -8,12 +8,9 @@ use crate::{
8 PersistentHirDatabase, 8 PersistentHirDatabase,
9}; 9};
10 10
11fn const_signature_for<N: NameOwner>( 11fn const_signature_for<N: NameOwner + TypeAscriptionOwner>(node: &N) -> Arc<ConstSignature> {
12 node: &N,
13 type_ref: Option<&ast::TypeRef>,
14) -> Arc<ConstSignature> {
15 let name = node.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); 12 let name = node.name().map(|n| n.as_name()).unwrap_or_else(Name::missing);
16 let type_ref = TypeRef::from_ast_opt(type_ref); 13 let type_ref = TypeRef::from_ast_opt(node.ascribed_type());
17 let sig = ConstSignature { name, type_ref }; 14 let sig = ConstSignature { name, type_ref };
18 Arc::new(sig) 15 Arc::new(sig)
19} 16}
@@ -24,7 +21,7 @@ impl ConstSignature {
24 konst: Const, 21 konst: Const,
25 ) -> Arc<ConstSignature> { 22 ) -> Arc<ConstSignature> {
26 let (_, node) = konst.source(db); 23 let (_, node) = konst.source(db);
27 const_signature_for(&*node, node.type_ref()) 24 const_signature_for(&*node)
28 } 25 }
29 26
30 pub(crate) fn static_signature_query( 27 pub(crate) fn static_signature_query(
@@ -32,6 +29,6 @@ impl ConstSignature {
32 konst: Static, 29 konst: Static,
33 ) -> Arc<ConstSignature> { 30 ) -> Arc<ConstSignature> {
34 let (_, node) = konst.source(db); 31 let (_, node) = konst.source(db);
35 const_signature_for(&*node, node.type_ref()) 32 const_signature_for(&*node)
36 } 33 }
37} 34}
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index e9db8282f..aa39d28ed 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -6,7 +6,7 @@ use rustc_hash::FxHashMap;
6use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; 6use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
7use ra_syntax::{ 7use ra_syntax::{
8 SyntaxNodePtr, AstNode, 8 SyntaxNodePtr, AstNode,
9 ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralFlavor} 9 ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralFlavor, TypeAscriptionOwner}
10}; 10};
11 11
12use crate::{ 12use crate::{
@@ -709,7 +709,7 @@ impl ExprCollector {
709 if let Some(pl) = e.param_list() { 709 if let Some(pl) = e.param_list() {
710 for param in pl.params() { 710 for param in pl.params() {
711 let pat = self.collect_pat_opt(param.pat()); 711 let pat = self.collect_pat_opt(param.pat());
712 let type_ref = param.type_ref().map(TypeRef::from_ast); 712 let type_ref = param.ascribed_type().map(TypeRef::from_ast);
713 args.push(pat); 713 args.push(pat);
714 arg_types.push(type_ref); 714 arg_types.push(type_ref);
715 } 715 }
@@ -790,7 +790,7 @@ impl ExprCollector {
790 .map(|s| match s.kind() { 790 .map(|s| match s.kind() {
791 ast::StmtKind::LetStmt(stmt) => { 791 ast::StmtKind::LetStmt(stmt) => {
792 let pat = self.collect_pat_opt(stmt.pat()); 792 let pat = self.collect_pat_opt(stmt.pat());
793 let type_ref = stmt.type_ref().map(TypeRef::from_ast); 793 let type_ref = stmt.ascribed_type().map(TypeRef::from_ast);
794 let initializer = stmt.initializer().map(|e| self.collect_expr(e)); 794 let initializer = stmt.initializer().map(|e| self.collect_expr(e));
795 Statement::Let { pat, type_ref, initializer } 795 Statement::Let { pat, type_ref, initializer }
796 } 796 }
diff --git a/crates/ra_hir/src/type_ref.rs b/crates/ra_hir/src/type_ref.rs
index ee8b7376a..8aa807648 100644
--- a/crates/ra_hir/src/type_ref.rs
+++ b/crates/ra_hir/src/type_ref.rs
@@ -1,7 +1,7 @@
1//! HIR for references to types. Paths in these are not yet resolved. They can 1//! HIR for references to types. Paths in these are not yet resolved. They can
2//! be directly created from an ast::TypeRef, without further queries. 2//! be directly created from an ast::TypeRef, without further queries.
3 3
4use ra_syntax::ast; 4use ra_syntax::ast::{self, TypeAscriptionOwner};
5 5
6use crate::Path; 6use crate::Path;
7 7
@@ -81,7 +81,7 @@ impl TypeRef {
81 FnPointerType(inner) => { 81 FnPointerType(inner) => {
82 let ret_ty = TypeRef::from_ast_opt(inner.ret_type().and_then(|rt| rt.type_ref())); 82 let ret_ty = TypeRef::from_ast_opt(inner.ret_type().and_then(|rt| rt.type_ref()));
83 let mut params = if let Some(pl) = inner.param_list() { 83 let mut params = if let Some(pl) = inner.param_list() {
84 pl.params().map(|p| p.type_ref()).map(TypeRef::from_ast_opt).collect() 84 pl.params().map(|p| p.ascribed_type()).map(TypeRef::from_ast_opt).collect()
85 } else { 85 } else {
86 Vec::new() 86 Vec::new()
87 }; 87 };
diff --git a/crates/ra_ide_api_light/src/structure.rs b/crates/ra_ide_api_light/src/structure.rs
index dea494daa..ec2c9bbc6 100644
--- a/crates/ra_ide_api_light/src/structure.rs
+++ b/crates/ra_ide_api_light/src/structure.rs
@@ -2,7 +2,7 @@ use crate::TextRange;
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 algo::visit::{visitor, Visitor}, 4 algo::visit::{visitor, Visitor},
5 ast::{self, AttrsOwner, NameOwner, TypeParamsOwner}, 5 ast::{self, AttrsOwner, NameOwner, TypeParamsOwner, TypeAscriptionOwner},
6 AstNode, SourceFile, SyntaxKind, SyntaxNode, WalkEvent, 6 AstNode, SourceFile, SyntaxKind, SyntaxNode, WalkEvent,
7}; 7};
8 8
@@ -45,6 +45,12 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
45 decl_with_detail(node, None) 45 decl_with_detail(node, None)
46 } 46 }
47 47
48 fn decl_with_ascription<N: NameOwner + AttrsOwner + TypeAscriptionOwner>(
49 node: &N,
50 ) -> Option<StructureNode> {
51 decl_with_type_ref(node, node.ascribed_type())
52 }
53
48 fn decl_with_type_ref<N: NameOwner + AttrsOwner>( 54 fn decl_with_type_ref<N: NameOwner + AttrsOwner>(
49 node: &N, 55 node: &N,
50 type_ref: Option<&ast::TypeRef>, 56 type_ref: Option<&ast::TypeRef>,
@@ -107,14 +113,14 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
107 decl_with_detail(fn_def, Some(detail)) 113 decl_with_detail(fn_def, Some(detail))
108 }) 114 })
109 .visit(decl::<ast::StructDef>) 115 .visit(decl::<ast::StructDef>)
110 .visit(|nfd: &ast::NamedFieldDef| decl_with_type_ref(nfd, nfd.type_ref()))
111 .visit(decl::<ast::EnumDef>) 116 .visit(decl::<ast::EnumDef>)
112 .visit(decl::<ast::EnumVariant>) 117 .visit(decl::<ast::EnumVariant>)
113 .visit(decl::<ast::TraitDef>) 118 .visit(decl::<ast::TraitDef>)
114 .visit(decl::<ast::Module>) 119 .visit(decl::<ast::Module>)
115 .visit(|td: &ast::TypeAliasDef| decl_with_type_ref(td, td.type_ref())) 120 .visit(|td: &ast::TypeAliasDef| decl_with_type_ref(td, td.type_ref()))
116 .visit(|cd: &ast::ConstDef| decl_with_type_ref(cd, cd.type_ref())) 121 .visit(decl_with_ascription::<ast::NamedFieldDef>)
117 .visit(|sd: &ast::StaticDef| decl_with_type_ref(sd, sd.type_ref())) 122 .visit(decl_with_ascription::<ast::ConstDef>)
123 .visit(decl_with_ascription::<ast::StaticDef>)
118 .visit(|im: &ast::ImplBlock| { 124 .visit(|im: &ast::ImplBlock| {
119 let target_type = im.target_type()?; 125 let target_type = im.target_type()?;
120 let target_trait = im.target_trait(); 126 let target_trait = im.target_trait();
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 {
31 } 31 }
32} 32}
33 33
34pub trait TypeAscriptionOwner: AstNode {
35 fn ascribed_type(&self) -> Option<&TypeRef> {
36 child_opt(self)
37 }
38}
39
34pub trait NameOwner: AstNode { 40pub trait NameOwner: AstNode {
35 fn name(&self) -> Option<&Name> { 41 fn name(&self) -> Option<&Name> {
36 child_opt(self) 42 child_opt(self)
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index 4e2705d09..7572225b8 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -628,11 +628,8 @@ impl ast::NameOwner for ConstDef {}
628impl ast::TypeParamsOwner for ConstDef {} 628impl ast::TypeParamsOwner for ConstDef {}
629impl ast::AttrsOwner for ConstDef {} 629impl ast::AttrsOwner for ConstDef {}
630impl ast::DocCommentsOwner for ConstDef {} 630impl ast::DocCommentsOwner for ConstDef {}
631impl ConstDef { 631impl ast::TypeAscriptionOwner for ConstDef {}
632 pub fn type_ref(&self) -> Option<&TypeRef> { 632impl ConstDef {}
633 super::child_opt(self)
634 }
635}
636 633
637// ContinueExpr 634// ContinueExpr
638#[derive(Debug, PartialEq, Eq, Hash)] 635#[derive(Debug, PartialEq, Eq, Hash)]
@@ -1767,15 +1764,12 @@ impl ToOwned for LetStmt {
1767} 1764}
1768 1765
1769 1766
1767impl ast::TypeAscriptionOwner for LetStmt {}
1770impl LetStmt { 1768impl LetStmt {
1771 pub fn pat(&self) -> Option<&Pat> { 1769 pub fn pat(&self) -> Option<&Pat> {
1772 super::child_opt(self) 1770 super::child_opt(self)
1773 } 1771 }
1774 1772
1775 pub fn type_ref(&self) -> Option<&TypeRef> {
1776 super::child_opt(self)
1777 }
1778
1779 pub fn initializer(&self) -> Option<&Expr> { 1773 pub fn initializer(&self) -> Option<&Expr> {
1780 super::child_opt(self) 1774 super::child_opt(self)
1781 } 1775 }
@@ -2592,11 +2586,8 @@ impl ast::VisibilityOwner for NamedFieldDef {}
2592impl ast::NameOwner for NamedFieldDef {} 2586impl ast::NameOwner for NamedFieldDef {}
2593impl ast::AttrsOwner for NamedFieldDef {} 2587impl ast::AttrsOwner for NamedFieldDef {}
2594impl ast::DocCommentsOwner for NamedFieldDef {} 2588impl ast::DocCommentsOwner for NamedFieldDef {}
2595impl NamedFieldDef { 2589impl ast::TypeAscriptionOwner for NamedFieldDef {}
2596 pub fn type_ref(&self) -> Option<&TypeRef> { 2590impl NamedFieldDef {}
2597 super::child_opt(self)
2598 }
2599}
2600 2591
2601// NamedFieldDefList 2592// NamedFieldDefList
2602#[derive(Debug, PartialEq, Eq, Hash)] 2593#[derive(Debug, PartialEq, Eq, Hash)]
@@ -2774,14 +2765,11 @@ impl ToOwned for Param {
2774} 2765}
2775 2766
2776 2767
2768impl ast::TypeAscriptionOwner for Param {}
2777impl Param { 2769impl Param {
2778 pub fn pat(&self) -> Option<&Pat> { 2770 pub fn pat(&self) -> Option<&Pat> {
2779 super::child_opt(self) 2771 super::child_opt(self)
2780 } 2772 }
2781
2782 pub fn type_ref(&self) -> Option<&TypeRef> {
2783 super::child_opt(self)
2784 }
2785} 2773}
2786 2774
2787// ParamList 2775// ParamList
@@ -3685,11 +3673,8 @@ impl ToOwned for SelfParam {
3685} 3673}
3686 3674
3687 3675
3676impl ast::TypeAscriptionOwner for SelfParam {}
3688impl SelfParam { 3677impl SelfParam {
3689 pub fn type_ref(&self) -> Option<&TypeRef> {
3690 super::child_opt(self)
3691 }
3692
3693 pub fn self_kw(&self) -> Option<&SelfKw> { 3678 pub fn self_kw(&self) -> Option<&SelfKw> {
3694 super::child_opt(self) 3679 super::child_opt(self)
3695 } 3680 }
@@ -3820,11 +3805,8 @@ impl ast::NameOwner for StaticDef {}
3820impl ast::TypeParamsOwner for StaticDef {} 3805impl ast::TypeParamsOwner for StaticDef {}
3821impl ast::AttrsOwner for StaticDef {} 3806impl ast::AttrsOwner for StaticDef {}
3822impl ast::DocCommentsOwner for StaticDef {} 3807impl ast::DocCommentsOwner for StaticDef {}
3823impl StaticDef { 3808impl ast::TypeAscriptionOwner for StaticDef {}
3824 pub fn type_ref(&self) -> Option<&TypeRef> { 3809impl StaticDef {}
3825 super::child_opt(self)
3826 }
3827}
3828 3810
3829// Stmt 3811// Stmt
3830#[derive(Debug, PartialEq, Eq, Hash)] 3812#[derive(Debug, PartialEq, Eq, Hash)]
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index c7acbbd6c..b7a2d1c01 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -271,7 +271,15 @@ Grammar(
271 ] 271 ]
272 ), 272 ),
273 "NamedFieldDefList": (collections: [["fields", "NamedFieldDef"]]), 273 "NamedFieldDefList": (collections: [["fields", "NamedFieldDef"]]),
274 "NamedFieldDef": ( traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner"], options: ["TypeRef"] ), 274 "NamedFieldDef": (
275 traits: [
276 "VisibilityOwner",
277 "NameOwner",
278 "AttrsOwner",
279 "DocCommentsOwner",
280 "TypeAscriptionOwner"
281 ]
282 ),
275 "PosFieldDefList": (collections: [["fields", "PosFieldDef"]]), 283 "PosFieldDefList": (collections: [["fields", "PosFieldDef"]]),
276 "PosFieldDef": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]), 284 "PosFieldDef": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]),
277 "EnumDef": ( traits: [ 285 "EnumDef": ( traits: [
@@ -298,9 +306,9 @@ Grammar(
298 "NameOwner", 306 "NameOwner",
299 "TypeParamsOwner", 307 "TypeParamsOwner",
300 "AttrsOwner", 308 "AttrsOwner",
301 "DocCommentsOwner" 309 "DocCommentsOwner",
310 "TypeAscriptionOwner",
302 ], 311 ],
303 options: ["TypeRef"]
304 ), 312 ),
305 "StaticDef": ( 313 "StaticDef": (
306 traits: [ 314 traits: [
@@ -308,9 +316,9 @@ Grammar(
308 "NameOwner", 316 "NameOwner",
309 "TypeParamsOwner", 317 "TypeParamsOwner",
310 "AttrsOwner", 318 "AttrsOwner",
311 "DocCommentsOwner" 319 "DocCommentsOwner",
320 "TypeAscriptionOwner",
312 ], 321 ],
313 options: ["TypeRef"]
314 ), 322 ),
315 "TypeAliasDef": ( 323 "TypeAliasDef": (
316 traits: [ 324 traits: [
@@ -569,11 +577,15 @@ Grammar(
569 "ExprStmt": ( 577 "ExprStmt": (
570 options: [ ["expr", "Expr"] ] 578 options: [ ["expr", "Expr"] ]
571 ), 579 ),
572 "LetStmt": ( options: [ 580 "LetStmt": (
573 ["pat", "Pat"], 581 options: [
574 ["type_ref", "TypeRef"], 582 ["pat", "Pat"],
575 ["initializer", "Expr"], 583 ["initializer", "Expr"],
576 ]), 584 ],
585 traits: [
586 "TypeAscriptionOwner",
587 ]
588 ),
577 "Condition": ( 589 "Condition": (
578 options: [ "Pat", "Expr" ] 590 options: [ "Pat", "Expr" ]
579 ), 591 ),
@@ -595,10 +607,18 @@ Grammar(
595 ["params", "Param"] 607 ["params", "Param"]
596 ] 608 ]
597 ), 609 ),
598 "SelfParam": (options: ["TypeRef", "SelfKw"]), 610 "SelfParam": (
611 options: ["SelfKw"],
612 traits: [
613 "TypeAscriptionOwner",
614 ]
615 ),
599 "SelfKw": (), 616 "SelfKw": (),
600 "Param": ( 617 "Param": (
601 options: [ "Pat", "TypeRef" ], 618 options: [ "Pat" ],
619 traits: [
620 "TypeAscriptionOwner",
621 ]
602 ), 622 ),
603 "UseItem": ( 623 "UseItem": (
604 traits: ["AttrsOwner"], 624 traits: ["AttrsOwner"],