Repository: intellij-rust/intellij-rust
Issue: Auto-complete and GoTo definition does not work

## Environment

* **Intellij-Rust plugin version:** Rust (v0.2.98.2125-191)
* **Rust toolchain version:** rustc 1.35.0 (3c235d560 2019-05-20)
* **IDE name and version:** IntelliJ IDEA 2019.1.3 (Community Edition) Build #IC-191.7479.19, built on May 28, 2019
* **Operating system:** Windows 10 64bit

## Problem description
Auto-completion and GoTo implementation does not work

## Steps to reproduce
```
use std::io::Read;

trait FooTrait {
    fn foo(&mut self) {}
}

impl<R: Read> FooTrait for R {}

struct FooStruct<R> {
    input: R,
}

impl<R: Read> FooStruct<R> {
    fn bar(&mut self) {
        self.input.foo() //auto-completion and GoTo implementation for 'foo'  does not work
    }
}

fn main() {}
```


Changed files:
src/main/kotlin/org/rust/lang/core/resolve/NameResolution.kt, src/test/kotlin/org/rust/lang/core/resolve/RsTypeAwareGenericResolveTest.kt, src/main/kotlin/org/rust/lang/core/resolve/ImplLookup.kt

diff --git a/src/main/kotlin/org/rust/lang/core/resolve/ImplLookup.kt b/src/main/kotlin/org/rust/lang/core/resolve/ImplLookup.kt
index c3b7616d2..414f65325 100644
--- a/src/main/kotlin/org/rust/lang/core/resolve/ImplLookup.kt
+++ b/src/main/kotlin/org/rust/lang/core/resolve/ImplLookup.kt
@@ -224,6 +224,10 @@ class ImplLookup(
                     .mapTo(implsAndTraits) { TraitImplSource.ExplicitImpl(it) }
             }
             is TyUnknown -> Unit
+            is TyTypeParameter -> {
+                RsImplIndex.findFreeImpls(project)
+                    .mapTo(implsAndTraits) { TraitImplSource.ExplicitImpl(it) }
+            }
             else -> {
                 implsAndTraits += findDerivedTraits(ty).map { TraitImplSource.Derived(it) }
                 implsAndTraits += findSimpleImpls(ty).map { TraitImplSource.ExplicitImpl(it) }
diff --git a/src/main/kotlin/org/rust/lang/core/resolve/NameResolution.kt b/src/main/kotlin/org/rust/lang/core/resolve/NameResolution.kt
index 2254dd627..c29d3fe75 100644
--- a/src/main/kotlin/org/rust/lang/core/resolve/NameResolution.kt
+++ b/src/main/kotlin/org/rust/lang/core/resolve/NameResolution.kt
@@ -530,7 +530,15 @@ private fun processTypeQualifiedPathResolveVariants(
             return if (isAppropriateTrait) processor(e) else false
         }
     } else {
-        processor
+        fun(e: AssocItemScopeEntry): Boolean {
+            return if (e.element is RsTypeAlias && baseTy is TyTypeParameter &&
+                e.source is TraitImplSource.ExplicitImpl) {
+                NameResolutionTestmarks.skipAssocTypeFromImpl.hit()
+                false
+            } else {
+                processor(e)
+            }
+        }
     }
     val selfSubst = if (baseTy !is TyTraitObject) {
         mapOf(TyTypeParameter.self() to baseTy).toTypeSubst()
@@ -1362,6 +1370,7 @@ object NameResolutionTestmarks {
     val modDeclExplicitPathInInlineModule = Testmark("modDeclExplicitPathInInlineModule")
     val modDeclExplicitPathInNonInlineModule = Testmark("modDeclExplicitPathInNonInlineModule")
     val selfRelatedTypeSpecialCase = Testmark("selfRelatedTypeSpecialCase")
+    val skipAssocTypeFromImpl = Testmark("skipAssocTypeFromImpl")
 }
 
 private data class ImplicitStdlibCrate(val name: String, val crateRoot: RsFile)
diff --git a/src/test/kotlin/org/rust/lang/core/resolve/RsTypeAwareGenericResolveTest.kt b/src/test/kotlin/org/rust/lang/core/resolve/RsTypeAwareGenericResolveTest.kt
index 22cbba705..c01fb6e71 100644
--- a/src/test/kotlin/org/rust/lang/core/resolve/RsTypeAwareGenericResolveTest.kt
+++ b/src/test/kotlin/org/rust/lang/core/resolve/RsTypeAwareGenericResolveTest.kt
@@ -1004,4 +1004,33 @@ class RsTypeAwareGenericResolveTest : RsResolveTestBase() {
             0.my_add(0).my_add(0);
         }             //^ unresolved
     """)
+
+    fun `test method in "impl for generic type" at type parameter with bound`() = checkByCode("""
+        trait Bound {}
+        trait Tr {
+            fn foo(&self) {}
+        }    //X
+        impl<A: Bound> Tr for A {}
+        fn foo<B: Bound>(b: B) {
+            b.foo();
+        }   //^
+    """)
+
+    fun `test "impl for generic type" is NOT used for associated type resolve`() = checkByCode("""
+        trait Bound {}
+        trait Tr { type Item; }
+        impl<A: Bound> Tr for A { type Item = (); }
+        fn foo<B: Bound>(b: B) {
+            let a: B::Item;
+        }           //^ unresolved
+    """, NameResolutionTestmarks.skipAssocTypeFromImpl)
+
+    fun `test "impl for generic type" is USED for associated type resolve UFCS`() = checkByCode("""
+        trait Bound {}
+        trait Tr { type Item; }
+        impl<A: Bound> Tr for A { type Item = (); }
+        fn foo<B: Bound>(b: B) {     //X
+            let a: <B as Tr>::Item;
+        }                   //^
+    """)
 }