Repository: intellij-rust/intellij-rust
Issue: New engine fails to resolve type

<!--
Hello and thank you for the issue!
If you would like to report a bug, we have added some points below that you can fill out.
Consider using "Help | Create New Issue" action that fills out all necessary information automatically.
Feel free to remove all the irrelevant text to request a new feature.
-->

## Environment

* **IntelliJ Rust plugin version:** 0.4.147.3871-211
* **Rust toolchain version:** 1.52.1,  target thumbv7em-none-eabihf
* **IDE name and version:** CLion 2021.1.1
* **Operating system:** OpenSuse Tumbleweed

## Problem description
The new engine fails to resolve some types that the old engine managed.

## Steps to reproduce

  * Clone from https://github.com/AndWass/intellij-rust-resolve-bug
  * Open project in CLion
  * `Go to declaration` of `I2CMaster7` (or any of the types imported on that line) in `src/main.rs` results in `Cannot find declaration to go to`
  * Build project works fine

<!--
Please include as much of your codebase as needed to reproduce the error.
If the relevant files are large, please provide a link to a public repository or a [Gist](https://gist.github.com/).
-->


Changed files:
src/main/kotlin/org/rust/lang/core/resolve2/DefCollector.kt, src/test/kotlin/org/rust/lang/core/resolve/RsUseResolveTest.kt

diff --git a/src/main/kotlin/org/rust/lang/core/resolve2/DefCollector.kt b/src/main/kotlin/org/rust/lang/core/resolve2/DefCollector.kt
index 4a1c2277b..e7b485e20 100644
--- a/src/main/kotlin/org/rust/lang/core/resolve2/DefCollector.kt
+++ b/src/main/kotlin/org/rust/lang/core/resolve2/DefCollector.kt
@@ -230,9 +230,8 @@ class DefCollector(
     ): Boolean {
         check(depth <= 100) { "infinite recursion in glob imports!" }
 
-        var changed = false
-        for ((name, def) in resolutions) {
-            val changedCurrent = if (name != "_") {
+        val resolutionsNew = resolutions.filter { (name, def) ->
+            if (name != "_") {
                 val defAdjusted = def.adjust(visibility, isFromNamedImport = importType == NAMED)
                     .adjustMultiresolve()
                 pushResolutionFromImport(modData, name, defAdjusted)
@@ -240,16 +239,15 @@ class DefCollector(
                 // TODO: What if `def` is not trait?
                 pushTraitResolutionFromImport(modData, def, visibility)
             }
-
-            if (changedCurrent) changed = true
         }
+        val changed = resolutionsNew.isNotEmpty()
         if (!changed) return changed
 
         val globImports = globImports[modData] ?: return changed
         for ((globImportingMod, globImportVis) in globImports) {
             // we know all resolutions have the same `visibility`, so we just need to check that once
             if (!visibility.isVisibleFromMod(globImportingMod)) continue
-            updateRecursive(globImportingMod, resolutions, globImportVis, GLOB, depth + 1)
+            updateRecursive(globImportingMod, resolutionsNew, globImportVis, GLOB, depth + 1)
         }
         return changed
     }
diff --git a/src/test/kotlin/org/rust/lang/core/resolve/RsUseResolveTest.kt b/src/test/kotlin/org/rust/lang/core/resolve/RsUseResolveTest.kt
index ee897198b..1c53acb42 100644
--- a/src/test/kotlin/org/rust/lang/core/resolve/RsUseResolveTest.kt
+++ b/src/test/kotlin/org/rust/lang/core/resolve/RsUseResolveTest.kt
@@ -805,6 +805,28 @@ class RsUseResolveTest : RsResolveTestBase() {
         }
     """)
 
+    @MockEdition(Edition.EDITION_2018)
+    fun `test complex glob imports`() = checkByCode("""
+        pub mod inner1 {
+            pub mod foo {
+                pub fn func() {}
+            }        //X
+
+            mod inner2 {
+                pub mod foo {}
+                pub mod bar {}  // this mod is important
+            }
+            macro_rules! as_is { ($($ t:tt)*) => { $($ t)* } }
+            as_is! {  // import from `inner2` should be resolved after import from `inner1`
+                pub use inner2::*;
+            }
+        }
+        pub use inner1::*;
+        fn main() {
+            foo::func();
+        }      //^
+    """)
+
     // based on https://github.com/rust-lang/cargo/blob/875e0123259b0b6299903fe4aea0a12ecde9324f/src/cargo/util/mod.rs#L23
     @MockEdition(Edition.EDITION_2018)
     fun `test import adds same name as existing 1`() = checkByCode("""