Skip to content

Commit 409dc09

Browse files
dima74yopox
authored andcommitted
RES: Fix some cases when named import should shadow glob-import
1 parent 62f2c48 commit 409dc09

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

src/main/kotlin/org/rust/lang/core/resolve2/DefCollector.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class DefCollector(
7373
// two cfg-disabled mods with same name (first one will be shadowed).
7474
// See [RsCfgAttrResolveTest.`test import inside expanded shadowed mod 1`].
7575
removeInvalidImportsAndMacroCalls(defMap, context)
76+
sortImports(unresolvedImports)
7677

7778
resolveImports()
7879
val changed = expandMacros()
@@ -87,11 +88,11 @@ class DefCollector(
8788
private fun resolveImports() {
8889
do {
8990
var hasChangedIndeterminateImports = false
90-
val hasResolvedImports = unresolvedImports.inPlaceRemoveIf { import ->
91+
val hasResolvedImports = unresolvedImports.removeIf { import ->
9192
ProgressManager.checkCanceled()
9293
when (val status = resolveImport(import)) {
9394
is Indeterminate -> {
94-
if (import.status is Indeterminate && import.status == status) return@inPlaceRemoveIf false
95+
if (import.status is Indeterminate && import.status == status) return@removeIf false
9596

9697
import.status = status
9798
val changed = recordResolvedImport(import)
@@ -510,6 +511,20 @@ private fun removeInvalidImportsAndMacroCalls(defMap: CrateDefMap, context: Coll
510511
context.macroCalls.removeIf { it.containingMod !in allMods }
511512
}
512513

514+
/**
515+
* This is a workaround for some real-project cases. See:
516+
* - [RsUseResolveTest.`test import adds same name as existing`]
517+
* - https://github.com/rust-lang/cargo/blob/875e0123259b0b6299903fe4aea0a12ecde9324f/src/cargo/util/mod.rs#L23
518+
*/
519+
private fun sortImports(imports: MutableList<Import>) {
520+
imports.sortWith(
521+
compareBy<Import> { it.visibility === Visibility.CfgDisabled } // cfg-enabled imports first
522+
.thenBy { it.isGlob } // named imports first
523+
.thenByDescending { it.nameInScope in it.containingMod.visibleItems }
524+
.thenByDescending { it.containingMod.path.segments.size } // imports from nested modules first
525+
)
526+
}
527+
513528
/**
514529
* Faster alternative to [java.util.Collection.removeIf] that doesn't preserve element order.
515530
* [filter] is allowed to append to [this].

src/main/kotlin/org/rust/lang/core/resolve2/FacadeBuildDefMap.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ private fun buildDefMapContainingExplicitItems(
108108
val modCollectorContext = ModCollectorContext(defMap, crateRootData, context)
109109
collectFileAndCalculateHash(crateRoot, crateRootData, crateRootData.macroIndex, modCollectorContext)
110110

111-
sortImports(context.imports)
112111
return defMap
113112
}
114113

@@ -189,20 +188,6 @@ private fun createExternCrateStdImport(defMap: CrateDefMap): Import? {
189188
)
190189
}
191190

192-
/**
193-
* This is a workaround for some real-project cases. See:
194-
* - [RsUseResolveTest.`test import adds same name as existing`]
195-
* - https://github.com/rust-lang/cargo/blob/875e0123259b0b6299903fe4aea0a12ecde9324f/src/cargo/util/mod.rs#L23
196-
*/
197-
private fun sortImports(imports: MutableList<Import>) {
198-
imports.sortWith(
199-
// TODO: Profile & optimize
200-
compareByDescending<Import> { it.nameInScope in it.containingMod.visibleItems }
201-
.thenBy { it.isGlob }
202-
.thenByDescending { it.containingMod.path.segments.size } // imports from nested modules first
203-
)
204-
}
205-
206191
private fun CrateDefMap.afterBuilt() {
207192
root.visitDescendants {
208193
it.isShadowedByOtherFile = false

src/test/kotlin/org/rust/lang/core/resolve/RsPackageLibraryResolveTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,4 +1014,26 @@ class RsPackageLibraryResolveTest : RsResolveTestBase() {
10141014
//^ unresolved
10151015
}
10161016
""")
1017+
1018+
// https://github.com/intellij-rust/intellij-rust/issues/7215
1019+
@MockEdition(CargoWorkspace.Edition.EDITION_2018)
1020+
fun `test usual import override glob-import`() = stubOnlyResolve("""
1021+
//- lib.rs
1022+
pub mod header {
1023+
pub struct HeaderMap;
1024+
}
1025+
//- main.rs
1026+
use http::HeaderMap;
1027+
//^ main.rs
1028+
pub mod header {
1029+
pub use test_package::header::*;
1030+
pub use map::HeaderMap;
1031+
mod map {
1032+
pub struct HeaderMap;
1033+
} //x
1034+
}
1035+
pub mod http {
1036+
pub use crate::header::HeaderMap;
1037+
}
1038+
""")
10171039
}

0 commit comments

Comments
 (0)