aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/generics.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/generics.rs')
-rw-r--r--crates/ra_hir/src/generics.rs38
1 files changed, 21 insertions, 17 deletions
diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs
index e10b85ec9..df23871c7 100644
--- a/crates/ra_hir/src/generics.rs
+++ b/crates/ra_hir/src/generics.rs
@@ -7,7 +7,7 @@ use std::sync::Arc;
7 7
8use ra_syntax::ast::{self, AstNode, NameOwner, TypeParamsOwner}; 8use ra_syntax::ast::{self, AstNode, NameOwner, TypeParamsOwner};
9 9
10use crate::{db::HirDatabase, DefId, Name, AsName, Function, Struct}; 10use crate::{db::HirDatabase, DefId, Name, AsName, Function, Struct, Enum};
11 11
12/// Data about a generic parameter (to a function, struct, impl, ...). 12/// Data about a generic parameter (to a function, struct, impl, ...).
13#[derive(Clone, PartialEq, Eq, Debug)] 13#[derive(Clone, PartialEq, Eq, Debug)]
@@ -26,6 +26,7 @@ pub struct GenericParams {
26pub enum GenericDef { 26pub enum GenericDef {
27 Function(Function), 27 Function(Function),
28 Struct(Struct), 28 Struct(Struct),
29 Enum(Enum),
29 Def(DefId), 30 Def(DefId),
30} 31}
31 32
@@ -36,8 +37,14 @@ impl From<Function> for GenericDef {
36} 37}
37 38
38impl From<Struct> for GenericDef { 39impl From<Struct> for GenericDef {
39 fn from(func: Struct) -> GenericDef { 40 fn from(s: Struct) -> GenericDef {
40 GenericDef::Struct(func) 41 GenericDef::Struct(s)
42 }
43}
44
45impl From<Enum> for GenericDef {
46 fn from(e: Enum) -> GenericDef {
47 GenericDef::Enum(e)
41 } 48 }
42} 49}
43 50
@@ -54,22 +61,13 @@ impl GenericParams {
54 ) -> Arc<GenericParams> { 61 ) -> Arc<GenericParams> {
55 let mut generics = GenericParams::default(); 62 let mut generics = GenericParams::default();
56 match def { 63 match def {
57 GenericDef::Function(func) => { 64 GenericDef::Function(it) => generics.fill(&*it.source(db).1),
58 let (_, fn_def) = func.source(db); 65 GenericDef::Struct(it) => generics.fill(&*it.source(db).1),
59 if let Some(type_param_list) = fn_def.type_param_list() { 66 GenericDef::Enum(it) => generics.fill(&*it.source(db).1),
60 generics.fill(type_param_list)
61 }
62 }
63 GenericDef::Struct(s) => {
64 let (_, struct_def) = s.source(db);
65 if let Some(type_param_list) = struct_def.type_param_list() {
66 generics.fill(type_param_list)
67 }
68 }
69 GenericDef::Def(def_id) => { 67 GenericDef::Def(def_id) => {
70 let (_file_id, node) = def_id.source(db); 68 let (_file_id, node) = def_id.source(db);
71 if let Some(type_param_list) = node.children().find_map(ast::TypeParamList::cast) { 69 if let Some(type_param_list) = node.children().find_map(ast::TypeParamList::cast) {
72 generics.fill(type_param_list) 70 generics.fill_params(type_param_list)
73 } 71 }
74 } 72 }
75 } 73 }
@@ -77,7 +75,13 @@ impl GenericParams {
77 Arc::new(generics) 75 Arc::new(generics)
78 } 76 }
79 77
80 fn fill(&mut self, params: &ast::TypeParamList) { 78 fn fill(&mut self, node: &impl TypeParamsOwner) {
79 if let Some(params) = node.type_param_list() {
80 self.fill_params(params)
81 }
82 }
83
84 fn fill_params(&mut self, params: &ast::TypeParamList) {
81 for (idx, type_param) in params.type_params().enumerate() { 85 for (idx, type_param) in params.type_params().enumerate() {
82 let name = type_param 86 let name = type_param
83 .name() 87 .name()