Skip to content

Commit

Permalink
RES: Fix some cases when named import should shadow glob-import
Browse files Browse the repository at this point in the history
  • Loading branch information
dima74 committed Sep 14, 2021
1 parent 51e3cf3 commit 8169fb9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
19 changes: 17 additions & 2 deletions src/main/kotlin/org/rust/lang/core/resolve2/DefCollector.kt
Expand Up @@ -63,6 +63,7 @@ class DefCollector(
// two cfg-disabled mods with same name (first one will be shadowed).
// See [RsCfgAttrResolveTest.`test import inside expanded shadowed mod 1`].
removeInvalidImportsAndMacroCalls(defMap, context)
sortImports(unresolvedImports)

resolveImports()
val changed = expandMacros()
Expand All @@ -77,11 +78,11 @@ class DefCollector(
private fun resolveImports() {
do {
var hasChangedIndeterminateImports = false
val hasResolvedImports = unresolvedImports.inPlaceRemoveIf { import ->
val hasResolvedImports = unresolvedImports.removeIf { import ->
ProgressManager.checkCanceled()
when (val status = resolveImport(import)) {
is Indeterminate -> {
if (import.status is Indeterminate && import.status == status) return@inPlaceRemoveIf false
if (import.status is Indeterminate && import.status == status) return@removeIf false

import.status = status
val changed = recordResolvedImport(import)
Expand Down Expand Up @@ -455,6 +456,20 @@ private fun removeInvalidImportsAndMacroCalls(defMap: CrateDefMap, context: Coll
context.macroCalls.removeIf { it.containingMod !in allMods }
}

/**
* This is a workaround for some real-project cases. See:
* - [RsUseResolveTest.`test import adds same name as existing`]
* - https://github.com/rust-lang/cargo/blob/875e0123259b0b6299903fe4aea0a12ecde9324f/src/cargo/util/mod.rs#L23
*/
private fun sortImports(imports: MutableList<Import>) {
imports.sortWith(
compareBy<Import> { it.visibility === Visibility.CfgDisabled } // cfg-enabled imports first
.thenBy { it.isGlob } // named imports first
.thenByDescending { it.nameInScope in it.containingMod.visibleItems }
.thenByDescending { it.containingMod.path.segments.size } // imports from nested modules first
)
}

/**
* Faster alternative to [java.util.Collection.removeIf] that doesn't preserve element order.
* [filter] is allowed to append to [this].
Expand Down
15 changes: 0 additions & 15 deletions src/main/kotlin/org/rust/lang/core/resolve2/FacadeBuildDefMap.kt
Expand Up @@ -97,7 +97,6 @@ private fun buildDefMapContainingExplicitItems(
val modCollectorContext = ModCollectorContext(defMap, crateRootData, context)
collectFileAndCalculateHash(crateRoot, crateRootData, crateRootData.macroIndex, modCollectorContext)

sortImports(context.imports)
return defMap
}

Expand Down Expand Up @@ -169,20 +168,6 @@ private fun createExternCrateStdImport(defMap: CrateDefMap): Import? {
)
}

/**
* This is a workaround for some real-project cases. See:
* - [RsUseResolveTest.`test import adds same name as existing`]
* - https://github.com/rust-lang/cargo/blob/875e0123259b0b6299903fe4aea0a12ecde9324f/src/cargo/util/mod.rs#L23
*/
private fun sortImports(imports: MutableList<Import>) {
imports.sortWith(
// TODO: Profile & optimize
compareByDescending<Import> { it.nameInScope in it.containingMod.visibleItems }
.thenBy { it.isGlob }
.thenByDescending { it.containingMod.path.segments.size } // imports from nested modules first
)
}

private fun CrateDefMap.afterBuilt() {
root.visitDescendants {
it.isShadowedByOtherFile = false
Expand Down
Expand Up @@ -1014,4 +1014,26 @@ class RsPackageLibraryResolveTest : RsResolveTestBase() {
//^ unresolved
}
""")

// https://github.com/intellij-rust/intellij-rust/issues/7215
@MockEdition(CargoWorkspace.Edition.EDITION_2018)
fun `test usual import override glob-import`() = stubOnlyResolve("""
//- lib.rs
pub mod header {
pub struct HeaderMap;
}
//- main.rs
use http::HeaderMap;
//^ main.rs
pub mod header {
pub use test_package::header::*;
pub use map::HeaderMap;
mod map {
pub struct HeaderMap;
} //x
}
pub mod http {
pub use crate::header::HeaderMap;
}
""")
}

0 comments on commit 8169fb9

Please sign in to comment.