aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r--crates/ra_hir/src/ty/method_resolution.rs12
-rw-r--r--crates/ra_hir/src/ty/snapshots/tests__infer_impl_generics_with_autoderef.snap16
-rw-r--r--crates/ra_hir/src/ty/tests.rs20
3 files changed, 43 insertions, 5 deletions
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index 8d1076774..94b757af2 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -118,11 +118,13 @@ impl Ty {
118 // TODO: cache this as a query? 118 // TODO: cache this as a query?
119 // - if so, what signature? (TyFingerprint, Name)? 119 // - if so, what signature? (TyFingerprint, Name)?
120 // - or maybe cache all names and def_ids of methods per fingerprint? 120 // - or maybe cache all names and def_ids of methods per fingerprint?
121 pub fn lookup_method(self, db: &impl HirDatabase, name: &Name) -> Option<Function> { 121 /// Look up the method with the given name, returning the actual autoderefed
122 self.iterate_methods(db, |f| { 122 /// receiver type (but without autoref applied yet).
123 pub fn lookup_method(self, db: &impl HirDatabase, name: &Name) -> Option<(Ty, Function)> {
124 self.iterate_methods(db, |ty, f| {
123 let sig = f.signature(db); 125 let sig = f.signature(db);
124 if sig.name() == name && sig.has_self_param() { 126 if sig.name() == name && sig.has_self_param() {
125 Some(f) 127 Some((ty.clone(), f))
126 } else { 128 } else {
127 None 129 None
128 } 130 }
@@ -134,7 +136,7 @@ impl Ty {
134 pub fn iterate_methods<T>( 136 pub fn iterate_methods<T>(
135 self, 137 self,
136 db: &impl HirDatabase, 138 db: &impl HirDatabase,
137 mut callback: impl FnMut(Function) -> Option<T>, 139 mut callback: impl FnMut(&Ty, Function) -> Option<T>,
138 ) -> Option<T> { 140 ) -> Option<T> {
139 // For method calls, rust first does any number of autoderef, and then one 141 // For method calls, rust first does any number of autoderef, and then one
140 // autoref (i.e. when the method takes &self or &mut self). We just ignore 142 // autoref (i.e. when the method takes &self or &mut self). We just ignore
@@ -156,7 +158,7 @@ impl Ty {
156 for item in impl_block.items(db) { 158 for item in impl_block.items(db) {
157 match item { 159 match item {
158 ImplItem::Method(f) => { 160 ImplItem::Method(f) => {
159 if let Some(result) = callback(f) { 161 if let Some(result) = callback(&derefed_ty, f) {
160 return Some(result); 162 return Some(result);
161 } 163 }
162 } 164 }
diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_impl_generics_with_autoderef.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_impl_generics_with_autoderef.snap
new file mode 100644
index 000000000..f609eaf7c
--- /dev/null
+++ b/crates/ra_hir/src/ty/snapshots/tests__infer_impl_generics_with_autoderef.snap
@@ -0,0 +1,16 @@
1---
2created: "2019-02-17T13:35:06.385679926Z"
3creator: [email protected]
4source: crates/ra_hir/src/ty/tests.rs
5expression: "&result"
6---
7[78; 82) 'self': &Option<T>
8[98; 100) '{}': ()
9[111; 112) 'o': Option<u32>
10[127; 165) '{ ...f(); }': ()
11[133; 146) '(&o).as_ref()': Option<&u32>
12[134; 136) '&o': &Option<u32>
13[135; 136) 'o': Option<u32>
14[152; 153) 'o': Option<u32>
15[152; 162) 'o.as_ref()': Option<&u32>
16
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 1eca68dc5..5eb9c4f5b 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -539,6 +539,26 @@ fn test() -> i128 {
539} 539}
540 540
541#[test] 541#[test]
542fn infer_impl_generics_with_autoderef() {
543 check_inference(
544 "infer_impl_generics_with_autoderef",
545 r#"
546enum Option<T> {
547 Some(T),
548 None,
549}
550impl<T> Option<T> {
551 fn as_ref(&self) -> Option<&T> {}
552}
553fn test(o: Option<u32>) {
554 (&o).as_ref();
555 o.as_ref();
556}
557"#,
558 );
559}
560
561#[test]
542fn infer_generic_chain() { 562fn infer_generic_chain() {
543 check_inference( 563 check_inference(
544 "infer_generic_chain", 564 "infer_generic_chain",