Repository: intellij-rust/intellij-rust
Issue: Auto completion in trait impl imports unrelated paths

## Environment

* **IntelliJ Rust plugin version:** 0.4.164.4409-213
* **Rust toolchain version:** 1.58.1 (db9d1b20b 2022-01-20) x86_64-unknown-linux-gnu
* **IDE name and version:** CLion 2021.3.2 (CL-213.6461.75)
* **Operating system:** Linux 5.16.5-arch1-1
* **Macro expansion engine:** new
* **Name resolution engine:** new
* **Additional experimental features:** org.rust.cargo.features.settings.gutter, org.rust.cargo.evaluate.build.scripts, org.rust.macros.proc

## Problem description

```rust
mod test {
    use super::X;
    
    struct Z;
    
    impl X for Z {
        
    }
}

struct Y;

trait X {
    fn f() { }
    fn g(y: Y) { }
}

fn main() {
}
```

In `impl X for Z` type `fn f` and accept the suggested completion. `Y` will be imported into `test`.

Changed files:
src/test/kotlin/org/rust/lang/core/completion/RsImplTraitMemberCompletionProviderTest.kt
src/main/kotlin/org/rust/lang/core/completion/RsImplTraitMemberCompletionProvider.kt

diff --git a/src/main/kotlin/org/rust/lang/core/completion/RsImplTraitMemberCompletionProvider.kt b/src/main/kotlin/org/rust/lang/core/completion/RsImplTraitMemberCompletionProvider.kt
index fc58df433..2e6046da8 100644
--- a/src/main/kotlin/org/rust/lang/core/completion/RsImplTraitMemberCompletionProvider.kt
+++ b/src/main/kotlin/org/rust/lang/core/completion/RsImplTraitMemberCompletionProvider.kt
@@ -78,8 +78,9 @@ object RsImplTraitMemberCompletionProvider : RsCompletionProvider() {
             return
         }
 
-        val memberGenerator = MembersGenerator(RsPsiFactory(element.project), implBlock, trait)
         for (item in parentItems) {
+            // have to create new generator for each item, so that [MembersGenerator.itemsToImport] don't mix up
+            val memberGenerator = MembersGenerator(RsPsiFactory(element.project), implBlock, trait)
             val lookup = getCompletion(item, implBlock, subst, memberGenerator, keyword?.first)
             result.addElement(
                 lookup.toRsLookupElement(RsLookupElementProperties(isImplMemberFullLineCompletion = true))
diff --git a/src/test/kotlin/org/rust/lang/core/completion/RsImplTraitMemberCompletionProviderTest.kt b/src/test/kotlin/org/rust/lang/core/completion/RsImplTraitMemberCompletionProviderTest.kt
index 545927f29..31813f676 100644
--- a/src/test/kotlin/org/rust/lang/core/completion/RsImplTraitMemberCompletionProviderTest.kt
+++ b/src/test/kotlin/org/rust/lang/core/completion/RsImplTraitMemberCompletionProviderTest.kt
@@ -295,6 +295,38 @@ class RsImplTraitMemberCompletionProviderTest : RsCompletionTestBase() {
         }
     """)
 
+    fun `test auto import only completed function types`() = doFirstCompletion("""
+        mod foo {
+            pub struct S;
+            pub struct T;
+
+            pub trait Foo {
+                fn foo1();
+                fn foo2(s: S);
+            }
+        }
+
+        impl foo::Foo for () {
+            /*caret*/
+        }
+    """, """
+        mod foo {
+            pub struct S;
+            pub struct T;
+
+            pub trait Foo {
+                fn foo1();
+                fn foo2(s: S);
+            }
+        }
+
+        impl foo::Foo for () {
+            fn foo1() {
+                todo!()/*caret*/
+            }
+        }
+    """)
+
     fun `test complete full function signature`() = doFirstCompletion("""
         trait T1 {}
         trait T2 {