Skip to content

Commit

Permalink
Merge #9132 #9151
Browse files Browse the repository at this point in the history
9132: RES: Fix nested `include!`-ed files in different directory r=vlad20012 a=dima74

Fixes #8614

Related: #7156 (but that is for mod declaration in `include!`-ed file )

changelog: Fix resolve of items from nested `include!`-ed files in some cases


9151: RES: Fix cache invalidation after toggle "Expand macros" settings r=vlad20012 a=dima74

Currently when toggle ["Expand macros" option](#9127), we have two problems:
* `CrateDefMap`s don't rebuild
* `structureModificationTracker` doesn't increment if option was disabled but becomes enabled

---

This may lead to problems like (thanks `@neonaot):`
```rust
macro_rules! gen {
    () => { fn func() {} }
}
gen!();
fn main() {
    func();
}
```

* Disable "Expand macros" - `func` is unresolved (good)
* Enable "Expand macros" - `func` is still unresolved (bad)

changelog: Fix cache invalidation after toggle "Expand macros" settings


Co-authored-by: Dmitry Murzin <diralik@yandex.ru>
  • Loading branch information
bors[bot] and dima74 committed Aug 7, 2022
3 parents 65fe1e5 + c5ef3ab + 6905d88 commit 5372a3f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
Expand Up @@ -566,6 +566,10 @@ private class MacroExpansionServiceImplInner(
if (!isExpansionModeNew) {
cleanMacrosDirectoryAndStorage()
}
project.runWriteCommandAction {
project.defMapService.scheduleRebuildAllDefMaps()
project.rustPsiManager.incRustStructureModificationCount()
}
processUnprocessedMacros()
}

Expand Down
15 changes: 11 additions & 4 deletions src/main/kotlin/org/rust/lang/core/resolve2/DefCollector.kt
Expand Up @@ -399,21 +399,27 @@ class DefCollector(
}

private fun expandIncludeMacroCall(call: MacroCallInfo) {
val modData = call.containingMod
val containingFile = PersistentFS.getInstance().findFileById(modData.fileId ?: return) ?: return
val containingFile = PersistentFS.getInstance().findFileById(call.containingFileId ?: return) ?: return
val includePath = (call.body as? MacroCallBody.FunctionLike)?.text ?: return
val parentDirectory = containingFile.parent
val includingFile = parentDirectory.findFileByMaybeRelativePath(includePath)
val includingRsFile = includingFile?.toPsiFile(project)?.rustFile
if (includingRsFile != null) {
val context = getModCollectorContextForExpandedElements(call) ?: return
collectScope(includingRsFile, call.containingMod, context, call.macroIndex, propagateLegacyMacros = true)
collectScope(
includingRsFile,
call.containingMod,
context,
call.macroIndex,
includeMacroFile = includingFile,
propagateLegacyMacros = true
)
} else if (!context.isHangingMode) {
val filePath = parentDirectory.pathAsPath.resolve(includePath)
defMap.missedFiles.add(filePath)
}
if (includingFile != null) {
recordChildFileInUnusualLocation(modData, includingFile.fileId)
recordChildFileInUnusualLocation(call.containingMod, includingFile.fileId)
}
}

Expand Down Expand Up @@ -528,6 +534,7 @@ class MacroCallInfo(
val path: Array<String>,
val body: MacroCallBody,
val bodyHash: HashCode?, // null for `include!` macro
val containingFileId: FileId?, // needed only if this is `include!` macro
val depth: Int,
/**
* `srcOffset` - [CratePersistentId]
Expand Down
19 changes: 16 additions & 3 deletions src/main/kotlin/org/rust/lang/core/resolve2/ModCollector.kt
Expand Up @@ -58,12 +58,13 @@ fun collectScope(
context: ModCollectorContext,
modMacroIndex: MacroIndex = modData.macroIndex,
dollarCrateHelper: DollarCrateHelper? = null,
includeMacroFile: VirtualFile? = null,
propagateLegacyMacros: Boolean = false,
): LegacyMacros {
val hashCalculator = HashCalculator(modData.isEnabledByCfgInner)
.takeIf { modData.isNormalCrate }

val collector = ModCollector(modData, context, modMacroIndex, hashCalculator, dollarCrateHelper)
val collector = ModCollector(modData, context, modMacroIndex, hashCalculator, dollarCrateHelper, includeMacroFile)
collector.collectMod(scope.getOrBuildStub() ?: return emptyMap(), propagateLegacyMacros)

if (hashCalculator != null && scope is RsFile) {
Expand All @@ -80,7 +81,14 @@ fun collectExpandedElements(
context: ModCollectorContext,
dollarCrateHelper: DollarCrateHelper?
) {
val collector = ModCollector(call.containingMod, context, call.macroIndex, hashCalculator = null, dollarCrateHelper)
val collector = ModCollector(
call.containingMod,
context,
call.macroIndex,
hashCalculator = null,
dollarCrateHelper,
includeMacroFile = null
)
collector.collectMod(expandedFile, propagateLegacyMacros = true)
}

Expand All @@ -98,6 +106,8 @@ private class ModCollector(
private val parentMacroIndex: MacroIndex,
private val hashCalculator: HashCalculator?,
private val dollarCrateHelper: DollarCrateHelper?,
/** containing file, if it is `include!`-ed */
private val includeMacroFile: VirtualFile?,
) : ModVisitor {

private val defMap: CrateDefMap = context.defMap
Expand Down Expand Up @@ -255,7 +265,8 @@ private class ModCollector(
context,
childModData.macroIndex,
hashCalculator,
dollarCrateHelper
dollarCrateHelper,
includeMacroFile
)
collector.collectMod(childMod.mod)
collector.legacyMacros
Expand Down Expand Up @@ -311,6 +322,7 @@ private class ModCollector(
path,
MacroCallBody.FunctionLike(call.body),
bodyHash,
containingFileId = includeMacroFile?.fileId ?: modData.fileId,
macroDepth,
dollarCrateMap
)
Expand All @@ -335,6 +347,7 @@ private class ModCollector(
path,
body,
bodyHash,
containingFileId = null, // will not be used
macroDepth,
dollarCrateMap,
originalItem
Expand Down
Expand Up @@ -136,6 +136,17 @@ class RsIncludeMacroResolveTest : RsResolveTestBase() {
//^ lib.rs
""")

fun `test include file in included file 3`() = checkResolve("""
//- lib.rs
include!("inner/foo.rs");
fn foo(x: Foo) {}
//^ inner/bar.rs
//- inner/foo.rs
include!("bar.rs");
//- inner/bar.rs
struct Foo;
""")

@ExpandMacros
fun `test include macro in macro 1`() = checkResolve("""
//- lib.rs
Expand Down

0 comments on commit 5372a3f

Please sign in to comment.