diff options
Diffstat (limited to 'crates/ra_hir/src/ty')
4 files changed, 56 insertions, 39 deletions
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 37bc3f38c..2282286b0 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs | |||
@@ -7,17 +7,16 @@ use std::sync::Arc; | |||
7 | use rustc_hash::FxHashMap; | 7 | use rustc_hash::FxHashMap; |
8 | 8 | ||
9 | use crate::{ | 9 | use crate::{ |
10 | HirDatabase, module_tree::ModuleId, Module, Crate, Name, Function, | 10 | HirDatabase, module_tree::ModuleId, Module, Crate, Name, Function, Trait, |
11 | ids::TraitId, | ||
11 | impl_block::{ImplId, ImplBlock, ImplItem}, | 12 | impl_block::{ImplId, ImplBlock, ImplItem}, |
12 | generics::GenericParams, | 13 | ty::{AdtDef, Ty}, |
13 | ty::{AdtDef, Ty} | ||
14 | }; | 14 | }; |
15 | 15 | ||
16 | /// This is used as a key for indexing impls. | 16 | /// This is used as a key for indexing impls. |
17 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | 17 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
18 | pub enum TyFingerprint { | 18 | pub enum TyFingerprint { |
19 | Adt(AdtDef), | 19 | Adt(AdtDef), // we'll also want to index impls for primitive types etc. |
20 | // we'll also want to index impls for primitive types etc. | ||
21 | } | 20 | } |
22 | 21 | ||
23 | impl TyFingerprint { | 22 | impl TyFingerprint { |
@@ -37,6 +36,7 @@ pub struct CrateImplBlocks { | |||
37 | /// To make sense of the ModuleIds, we need the source root. | 36 | /// To make sense of the ModuleIds, we need the source root. |
38 | krate: Crate, | 37 | krate: Crate, |
39 | impls: FxHashMap<TyFingerprint, Vec<(ModuleId, ImplId)>>, | 38 | impls: FxHashMap<TyFingerprint, Vec<(ModuleId, ImplId)>>, |
39 | impls_by_trait: FxHashMap<TraitId, Vec<(ModuleId, ImplId)>>, | ||
40 | } | 40 | } |
41 | 41 | ||
42 | impl CrateImplBlocks { | 42 | impl CrateImplBlocks { |
@@ -60,30 +60,46 @@ impl CrateImplBlocks { | |||
60 | }) | 60 | }) |
61 | } | 61 | } |
62 | 62 | ||
63 | pub fn lookup_impl_blocks_for_trait<'a>( | ||
64 | &'a self, | ||
65 | db: &'a impl HirDatabase, | ||
66 | tr: &Trait, | ||
67 | ) -> impl Iterator<Item = (Module, ImplBlock)> + 'a { | ||
68 | let id = tr.id; | ||
69 | self.impls_by_trait | ||
70 | .get(&id) | ||
71 | .into_iter() | ||
72 | .flat_map(|i| i.iter()) | ||
73 | .map(move |(module_id, impl_id)| { | ||
74 | let module = Module { | ||
75 | krate: self.krate, | ||
76 | module_id: *module_id, | ||
77 | }; | ||
78 | let module_impl_blocks = db.impls_in_module(module); | ||
79 | (module, ImplBlock::from_id(module_impl_blocks, *impl_id)) | ||
80 | }) | ||
81 | } | ||
82 | |||
63 | fn collect_recursive(&mut self, db: &impl HirDatabase, module: &Module) { | 83 | fn collect_recursive(&mut self, db: &impl HirDatabase, module: &Module) { |
64 | let module_impl_blocks = db.impls_in_module(module.clone()); | 84 | let module_impl_blocks = db.impls_in_module(module.clone()); |
65 | 85 | ||
66 | for (impl_id, impl_data) in module_impl_blocks.impls.iter() { | 86 | for (impl_id, _) in module_impl_blocks.impls.iter() { |
67 | let impl_block = ImplBlock::from_id(Arc::clone(&module_impl_blocks), impl_id); | 87 | let impl_block = ImplBlock::from_id(Arc::clone(&module_impl_blocks), impl_id); |
68 | 88 | ||
69 | if let Some(_target_trait) = impl_data.target_trait() { | 89 | let target_ty = impl_block.target_ty(db); |
70 | // ignore for now | 90 | |
71 | } else { | 91 | if let Some(target_ty_fp) = TyFingerprint::for_impl(&target_ty) { |
72 | // TODO provide generics of impl | 92 | self.impls |
73 | let generics = GenericParams::default(); | 93 | .entry(target_ty_fp) |
74 | let target_ty = Ty::from_hir( | 94 | .or_insert_with(Vec::new) |
75 | db, | 95 | .push((module.module_id, impl_id)); |
76 | &module, | 96 | } |
77 | Some(&impl_block), | 97 | |
78 | &generics, | 98 | if let Some(tr) = impl_block.target_trait(db) { |
79 | impl_data.target_type(), | 99 | self.impls_by_trait |
80 | ); | 100 | .entry(tr.id) |
81 | if let Some(target_ty_fp) = TyFingerprint::for_impl(&target_ty) { | 101 | .or_insert_with(Vec::new) |
82 | self.impls | 102 | .push((module.module_id, impl_id)); |
83 | .entry(target_ty_fp) | ||
84 | .or_insert_with(Vec::new) | ||
85 | .push((module.module_id, impl_id)); | ||
86 | } | ||
87 | } | 103 | } |
88 | } | 104 | } |
89 | 105 | ||
@@ -99,6 +115,7 @@ impl CrateImplBlocks { | |||
99 | let mut crate_impl_blocks = CrateImplBlocks { | 115 | let mut crate_impl_blocks = CrateImplBlocks { |
100 | krate: krate.clone(), | 116 | krate: krate.clone(), |
101 | impls: FxHashMap::default(), | 117 | impls: FxHashMap::default(), |
118 | impls_by_trait: FxHashMap::default(), | ||
102 | }; | 119 | }; |
103 | if let Some(module) = krate.root_module(db) { | 120 | if let Some(module) = krate.root_module(db) { |
104 | crate_impl_blocks.collect_recursive(db, &module); | 121 | crate_impl_blocks.collect_recursive(db, &module); |
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_function_generics.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_function_generics.snap index 8ff6e55a6..91c48897c 100644 --- a/crates/ra_hir/src/ty/snapshots/tests__infer_function_generics.snap +++ b/crates/ra_hir/src/ty/snapshots/tests__infer_function_generics.snap | |||
@@ -1,12 +1,12 @@ | |||
1 | --- | 1 | --- |
2 | created: "2019-01-26T18:16:16.530712344+00:00" | 2 | created: "2019-01-27T14:52:29.934503829+00:00" |
3 | creator: [email protected] | 3 | creator: [email protected] |
4 | expression: "&result" | 4 | expression: "&result" |
5 | source: crates/ra_hir/src/ty/tests.rs | 5 | source: crates/ra_hir/src/ty/tests.rs |
6 | --- | 6 | --- |
7 | [10; 11) 't': [unknown] | 7 | [10; 11) 't': T |
8 | [21; 26) '{ t }': [unknown] | 8 | [21; 26) '{ t }': T |
9 | [23; 24) 't': [unknown] | 9 | [23; 24) 't': T |
10 | [38; 98) '{ ...(1); }': () | 10 | [38; 98) '{ ...(1); }': () |
11 | [44; 46) 'id': fn id<u32>(T) -> T | 11 | [44; 46) 'id': fn id<u32>(T) -> T |
12 | [44; 52) 'id(1u32)': u32 | 12 | [44; 52) 'id(1u32)': u32 |
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_generic_chain.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_generic_chain.snap index f21bffa75..626f31252 100644 --- a/crates/ra_hir/src/ty/snapshots/tests__infer_generic_chain.snap +++ b/crates/ra_hir/src/ty/snapshots/tests__infer_generic_chain.snap | |||
@@ -1,5 +1,5 @@ | |||
1 | --- | 1 | --- |
2 | created: "2019-01-26T17:46:03.866825843+00:00" | 2 | created: "2019-01-27T14:52:29.938713255+00:00" |
3 | creator: [email protected] | 3 | creator: [email protected] |
4 | expression: "&result" | 4 | expression: "&result" |
5 | source: crates/ra_hir/src/ty/tests.rs | 5 | source: crates/ra_hir/src/ty/tests.rs |
@@ -8,9 +8,9 @@ source: crates/ra_hir/src/ty/tests.rs | |||
8 | [65; 87) '{ ... }': [unknown] | 8 | [65; 87) '{ ... }': [unknown] |
9 | [75; 79) 'self': A<[unknown]> | 9 | [75; 79) 'self': A<[unknown]> |
10 | [75; 81) 'self.x': [unknown] | 10 | [75; 81) 'self.x': [unknown] |
11 | [99; 100) 't': [unknown] | 11 | [99; 100) 't': T |
12 | [110; 115) '{ t }': [unknown] | 12 | [110; 115) '{ t }': T |
13 | [112; 113) 't': [unknown] | 13 | [112; 113) 't': T |
14 | [135; 261) '{ ....x() }': i128 | 14 | [135; 261) '{ ....x() }': i128 |
15 | [146; 147) 'x': i32 | 15 | [146; 147) 'x': i32 |
16 | [150; 151) '1': i32 | 16 | [150; 151) '1': i32 |
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_type_param.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_type_param.snap index a99323264..216d1e41f 100644 --- a/crates/ra_hir/src/ty/snapshots/tests__infer_type_param.snap +++ b/crates/ra_hir/src/ty/snapshots/tests__infer_type_param.snap | |||
@@ -1,15 +1,15 @@ | |||
1 | --- | 1 | --- |
2 | created: "2019-01-27T16:54:18.368427685+00:00" | 2 | created: "2019-01-27T20:38:32.153717698+00:00" |
3 | creator: [email protected] | 3 | creator: [email protected] |
4 | expression: "&result" | 4 | expression: "&result" |
5 | source: crates/ra_hir/src/ty/tests.rs | 5 | source: crates/ra_hir/src/ty/tests.rs |
6 | --- | 6 | --- |
7 | [10; 11) 'x': [unknown] | 7 | [10; 11) 'x': T |
8 | [21; 30) '{ x }': [unknown] | 8 | [21; 30) '{ x }': T |
9 | [27; 28) 'x': [unknown] | 9 | [27; 28) 'x': T |
10 | [44; 45) 'x': &[unknown] | 10 | [44; 45) 'x': &T |
11 | [56; 65) '{ x }': &[unknown] | 11 | [56; 65) '{ x }': &T |
12 | [62; 63) 'x': &[unknown] | 12 | [62; 63) 'x': &T |
13 | [77; 157) '{ ...(1); }': () | 13 | [77; 157) '{ ...(1); }': () |
14 | [87; 88) 'y': u32 | 14 | [87; 88) 'y': u32 |
15 | [91; 96) '10u32': u32 | 15 | [91; 96) '10u32': u32 |