Skip to content

Commit 851ecfe

Browse files
vlad20012rrevenantt
authored andcommitted
Fix intellij-rust#3764 possible NPE in VFS batch (macro expansion)
I don't sure what exactly causes this NPS, but I added more validity checks, so it should help
1 parent a6f3bd1 commit 851ecfe

File tree

5 files changed

+14
-8
lines changed

5 files changed

+14
-8
lines changed

src/191/main/kotlin/org/rust/lang/core/macros/MacroExpansionVfsBatch.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ private class EventBasedVfsBatch191 : EventBasedVfsBatch() {
3232
VFileContentChangeEvent(null, vFile, vFile.modificationStamp, -1, true)
3333
}
3434
is Event.Delete -> {
35-
val vFile = LocalFileSystem.getInstance().findFileByPath(file.toString())!!
36-
VFileDeleteEvent(null, vFile, true)
35+
val vFile = LocalFileSystem.getInstance().findFileByPath(file.toString())
36+
// skip if file is already deleted (not sure how this can happen)
37+
if (vFile == null) null else VFileDeleteEvent(null, vFile, true)
3738
}
3839
}
3940
}

src/main/kotlin/org/rust/lang/core/macros/ExpandedMacroStorage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ class ExpandedMacroInfo(
498498
val callHash: HashCode?,
499499
var stubIndex: Int = -1
500500
) {
501-
val expansionFileUrl: String? get() = expansionFile?.url
501+
private val expansionFileUrl: String? get() = expansionFile?.url
502502
val fileId: Int get() = expansionFile?.fileId ?: -1
503503

504504
@Volatile

src/main/kotlin/org/rust/lang/core/macros/MacroExpansionManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ private class MacroExpansionServiceImplInner(
309309
val toRemove = mutableListOf<ExpandedMacroInfo>()
310310
runReadAction {
311311
storage.processExpandedMacroInfos { info ->
312-
if (info.expansionFileUrl != null && info.expansionFile?.isValid != true) {
312+
if (info.expansionFile != null && !info.expansionFile.isValid) {
313313
toRemove.add(info)
314314
}
315315
}

src/main/kotlin/org/rust/lang/core/macros/MacroExpansionTask.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@ object InvalidationPipeline {
266266

267267
class Stage2(val info: ExpandedMacroInfo) : Pipeline.Stage2WriteToFs {
268268
override fun writeExpansionToFs(fs: MacroExpansionVfsBatch): Pipeline.Stage3SaveToStorage {
269-
info.expansionFile?.let { fs.deleteFile(fs.resolve(it)) }
269+
val expansionFile = info.expansionFile
270+
if (expansionFile != null && expansionFile.isValid) {
271+
fs.deleteFile(fs.resolve(expansionFile))
272+
}
270273
return Stage3(info)
271274
}
272275
}
@@ -344,7 +347,9 @@ object ExpansionPipeline {
344347
) : Pipeline.Stage2WriteToFs {
345348
override fun writeExpansionToFs(fs: MacroExpansionVfsBatch): Pipeline.Stage3SaveToStorage {
346349
val oldExpansionFile = info.expansionFile
347-
oldExpansionFile?.let { fs.deleteFile(fs.resolve(it)) }
350+
if (oldExpansionFile != null && oldExpansionFile.isValid) {
351+
fs.deleteFile(fs.resolve(oldExpansionFile))
352+
}
348353
return Stage3(call, info, def, null)
349354
}
350355
}

src/main/kotlin/org/rust/lang/core/macros/MacroExpansionVfsBatch.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ abstract class EventBasedVfsBatch : VfsBatch() {
158158
manager.fireBeforeRefreshStart(/*asynchronous = */ false)
159159
try {
160160
val events = mutableListOf<VFileEvent>()
161-
while (!dirEvents.isEmpty()) {
161+
while (dirEvents.isNotEmpty()) {
162162
val iter = dirEvents.iterator()
163163
while (iter.hasNext()) {
164164
val event = iter.next().toVFileEvent()
@@ -173,7 +173,7 @@ abstract class EventBasedVfsBatch : VfsBatch() {
173173
}
174174

175175
if (fileEvents.isNotEmpty()) {
176-
PersistentFS.getInstance().processEvents(fileEvents.map { it.toVFileEvent()!! })
176+
PersistentFS.getInstance().processEvents(fileEvents.mapNotNull { it.toVFileEvent() })
177177
fileEvents.clear()
178178
}
179179
} finally {

0 commit comments

Comments
 (0)