Repository: intellij-rust/intellij-rust
Issue: java.lang.Exception. Outdated stub in index. Cached PSI class org.rust.lang.core.psi.RsFile

## Problem description
Completion works, but exception occurs, when I try to call it from #4780 case

## Environment

* **IntelliJ Rust plugin version:** 0.4.179.4887-222-beta
* **Rust toolchain version:** 1.63.0 (4b91a6ea7 2022-08-08) x86_64-apple-darwin
* **IDE name and version:** CLion 2022.2.3 (CL-222.4167.35)
* **Operating system:** macOS 11.5.1
* **Macro expansion:** enabled


<details> 

```
Outdated stub in index: 
file:///Users/jamilya.shurukhova/learning-rust/chapter7/tests/integration_tests.rs 
indexing timestamp = 1663675980536, binary = false, byte size = 377, char size = 375, doc=DocumentImpl[file:///Users/jamilya.shurukhova/learning-rust/chapter7/tests/integration_tests.rs], docSaved=true, wasIndexedAlready=true, queried at 1663675980536
doc length=377
file length=377
cached PSI class org.rust.lang.core.psi.RsFile
PSI length=377
projects with file: 1
physical file exists; length = 377

java.lang.Exception
	at com.intellij.psi.stubs.StubTreeLoaderImpl.diagnoseLengthMismatch(StubTreeLoaderImpl.java:191)
	at com.intellij.psi.stubs.StubTreeLoaderImpl.checkLengthMatch(StubTreeLoaderImpl.java:157)
	at com.intellij.psi.stubs.StubTreeLoaderImpl.readFromVFile(StubTreeLoaderImpl.java:124)
	at com.intellij.psi.stubs.StubTreeLoaderImpl.readOrBuild(StubTreeLoaderImpl.java:49)
	at org.rust.lang.core.resolve2.ModCollectorKt.getOrBuildFileStub(ModCollector.kt:504)
	at org.rust.lang.core.resolve2.FileModificationTrackerKt.isFileChanged(FileModificationTracker.kt:55)
	at org.rust.lang.core.resolve2.CrateModificationTrackerKt.processChangedFiles(CrateModificationTracker.kt:77)
	at org.rust.lang.core.resolve2.CrateModificationTrackerKt.getShouldRebuild(CrateModificationTracker.kt:43)
	at org.rust.lang.core.resolve2.DefMapHolder.updateShouldRebuild(DefMapService.kt:122)
	at org.rust.lang.core.resolve2.DefMapUpdater$findCratesToUpdate$cratesToUpdate$1$1.invoke(FacadeUpdateDefMap.kt:206)
	at org.rust.lang.core.resolve2.DefMapUpdater$findCratesToUpdate$cratesToUpdate$1$1.invoke(FacadeUpdateDefMap.kt:205)
	at org.rust.openapiext.UtilsKt$computeInReadActionWithWriteActionPriority$success$1.invoke(utils.kt:347)
	at org.rust.openapiext.UtilsKt$computeInReadActionWithWriteActionPriority$success$1.invoke(utils.kt:346)
	at org.rust.openapiext.UtilsKt.runInReadActionWithWriteActionPriority$lambda-13(utils.kt:342)
	at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1154)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.lambda$runInReadActionWithWriteActionPriority$0(ProgressIndicatorUtils.java:75)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runActionAndCancelBeforeWrite(ProgressIndicatorUtils.java:158)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.lambda$runWithWriteActionPriority$1(ProgressIndicatorUtils.java:115)
	at com.intellij.openapi.progress.ProgressManager.lambda$runProcess$0(ProgressManager.java:66)
	at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$2(CoreProgressManager.java:188)
	at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$executeProcessUnderProgress$12(CoreProgressManager.java:608)
	at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:683)
	at com.intellij.openapi.progress.impl.CoreProgressManager.computeUnderProgress(CoreProgressManager.java:639)
	at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:607)
	at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:60)
	at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:175)
	at com.intellij.openapi.progress.ProgressManager.runProcess(ProgressManager.java:66)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runWithWriteActionPriority(ProgressIndicatorUtils.java:112)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runInReadActionWithWriteActionPriority(ProgressIndicatorUtils.java:75)
	at org.rust.openapiext.UtilsKt.runInReadActionWithWriteActionPriority(utils.kt:342)
	at org.rust.openapiext.UtilsKt.computeInReadActionWithWriteActionPriority(utils.kt:346)
	at org.rust.lang.core.resolve2.DefMapUpdater$findCratesToUpdate$cratesToUpdate$1.invoke(FacadeUpdateDefMap.kt:205)
	at org.rust.lang.core.resolve2.DefMapUpdater$findCratesToUpdate$cratesToUpdate$1.invoke(FacadeUpdateDefMap.kt:204)
	at org.rust.lang.core.resolve2.FacadeUpdateDefMapKt.filterAsync$lambda-8(FacadeUpdateDefMap.kt:268)
	at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1395)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
```
</details>



Changed files:
src/main/kotlin/org/rust/lang/core/resolve2/ModCollector.kt

diff --git a/src/main/kotlin/org/rust/lang/core/resolve2/ModCollector.kt b/src/main/kotlin/org/rust/lang/core/resolve2/ModCollector.kt
index 993c219ed..282f5fa19 100644
--- a/src/main/kotlin/org/rust/lang/core/resolve2/ModCollector.kt
+++ b/src/main/kotlin/org/rust/lang/core/resolve2/ModCollector.kt
@@ -14,7 +14,6 @@ import com.intellij.openapi.vfs.newvfs.persistent.PersistentFS
 import com.intellij.psi.PsiAnchor
 import com.intellij.psi.StubBasedPsiElement
 import com.intellij.psi.stubs.StubElement
-import com.intellij.psi.stubs.StubTreeLoader
 import com.intellij.util.containers.map2Array
 import org.rust.lang.RsConstants
 import org.rust.lang.RsFileType
@@ -515,10 +514,14 @@ private fun RsItemsOwner.getOrBuildStub(): StubElement<out RsItemsOwner>? {
 }
 
 fun RsFileBase.getOrBuildFileStub(): RsFileStub? {
-    val virtualFile = viewProvider.virtualFile
-    val stubTree = greenStubTree ?: StubTreeLoader.getInstance().readOrBuild(project, virtualFile, this)
-    val stub = stubTree?.root as? RsFileStub
-    if (stub == null) RESOLVE_LOG.error("No stub for file ${virtualFile.path}")
+    val greenStubTree = greenStubTree
+    val stub = when {
+        greenStubTree != null -> greenStubTree.root
+        this is RsFile -> calcStubTree().root
+        // We don't attach [stub] to PSI for [RsReplCodeFragment]
+        else -> RsFileStub.Type.builder.buildStubTree(this)
+    } as? RsFileStub
+    if (stub == null) RESOLVE_LOG.error("No stub for file ${viewProvider.virtualFile.path}")
     return stub
 }