Repository: intellij-rust/intellij-rust
Issue: "java.lang.Throwable: Target RsTraitItemImpl(TRAIT_ITEM) of path 'Foo' is inside dummy holder" moving traits

## Environment

* **IntelliJ Rust plugin version:** 0.3.137.3507-203-nightly
* **Rust toolchain version:** 1.48.0-nightly (8876ffc92 2020-10-02) x86_64-apple-darwin
* **IDE name and version:** CLion 2021.1 Snapshot (CL-211.2132)
* **Operating system:** macOS 10.15.6
* **Macro expansion engine:** new
* **Name resolution engine:** new

## Problem description

Got an exception moving a trait and it's impl

<details>
  <summary>java.lang.Throwable: Target RsTraitItemImpl(TRAIT_ITEM) of path 'Foo' is inside dummy holder</summary>

```stacktrace
java.lang.Throwable: Target RsTraitItemImpl(TRAIT_ITEM) of path 'Foo' is inside dummy holder
	at com.intellij.openapi.diagnostic.Logger.error(Logger.java:159)
	at org.rust.ide.refactoring.move.common.RsMoveUtil.resolvesToAndAccessible(RsMoveUtil.kt:186)
	at org.rust.ide.refactoring.move.common.RsMoveRetargetReferencesProcessor.retargetReferences(RsMoveRetargetReferencesProcessor.kt:37)
	at org.rust.ide.refactoring.move.common.RsMoveCommonProcessor.performRefactoring(RsMoveCommonProcessor.kt:369)
	at org.rust.ide.refactoring.move.RsMoveTopLevelItemsProcessor.performRefactoring(RsMoveTopLevelItemsProcessor.kt:70)
	at com.intellij.refactoring.BaseRefactoringProcessor.lambda$doRefactoring$9(BaseRefactoringProcessor.java:516)
	at com.intellij.refactoring.BaseRefactoringProcessor.callPerformRefactoring(BaseRefactoringProcessor.java:569)
	at com.intellij.refactoring.BaseRefactoringProcessor.lambda$doRefactoring$10(BaseRefactoringProcessor.java:515)
	at com.intellij.openapi.application.impl.ApplicationImpl.lambda$runEdtProgressWriteAction$11(ApplicationImpl.java:938)
	at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$2(CoreProgressManager.java:178)
	at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:658)
	at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:610)
	at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:63)
	at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:165)
	at com.intellij.openapi.progress.util.PotemkinProgress.runInSwingThread(PotemkinProgress.java:191)
	at com.intellij.openapi.application.impl.ApplicationImpl.lambda$runEdtProgressWriteAction$12(ApplicationImpl.java:938)
	at com.intellij.openapi.application.impl.ApplicationImpl.runWriteActionWithClass(ApplicationImpl.java:962)
	at com.intellij.openapi.application.impl.ApplicationImpl.runEdtProgressWriteAction(ApplicationImpl.java:936)
	at com.intellij.openapi.application.impl.ApplicationImpl.runWriteActionWithNonCancellableProgressInDispatchThread(ApplicationImpl.java:917)
	at com.intellij.refactoring.BaseRefactoringProcessor.doRefactoring(BaseRefactoringProcessor.java:514)
	at com.intellij.refactoring.BaseRefactoringProcessor.lambda$execute$2(BaseRefactoringProcessor.java:331)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:220)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:177)
	at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:167)
	at com.intellij.refactoring.BaseRefactoringProcessor.execute(BaseRefactoringProcessor.java:329)
	at com.intellij.refactoring.BaseRefactoringProcessor.doRun(BaseRefactoringProcessor.java:252)
	at com.intellij.refactoring.BaseRefactoringProcessor.run(BaseRefactoringProcessor.java:644)
	at com.intellij.refactoring.ui.RefactoringDialog.invokeRefactoring(RefactoringDialog.java:241)
	at org.rust.ide.refactoring.move.RsMoveTopLevelItemsDialog.doAction(RsMoveTopLevelItemsDialog.kt:139)
	at com.intellij.refactoring.ui.RefactoringDialog.doRefactorAction(RefactoringDialog.java:136)
	at com.intellij.refactoring.ui.RefactoringDialog$RefactorAction.actionPerformed(RefactoringDialog.java:219)
	at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
	at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
	at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
	at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
	at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:270)
	at java.desktop/java.awt.Component.processMouseEvent(Component.java:6652)
	at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3345)
	at java.desktop/java.awt.Component.processEvent(Component.java:6417)
	at java.desktop/java.awt.Container.processEvent(Container.java:2263)
	at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5027)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4859)
	at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
	at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
	at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
	at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4859)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:778)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:727)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:751)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:749)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:748)
	at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:973)
	at com.intellij.ide.IdeEventQueue.dispatchMouseEvent(IdeEventQueue.java:908)
	at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:837)
	at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$8(IdeEventQueue.java:451)
	at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:762)
	at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$9(IdeEventQueue.java:450)
	at com.intellij.openapi.application.impl.ApplicationImpl.runIntendedWriteActionOnCurrentThread(ApplicationImpl.java:796)
	at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:504)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:117)
	at java.desktop/java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:190)
	at java.desktop/java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:235)
	at java.desktop/java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:233)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.desktop/java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:233)
	at java.desktop/java.awt.Dialog.show(Dialog.java:1063)
	at com.intellij.openapi.ui.impl.DialogWrapperPeerImpl$MyDialog.show(DialogWrapperPeerImpl.java:694)
	at com.intellij.openapi.ui.impl.DialogWrapperPeerImpl.show(DialogWrapperPeerImpl.java:435)
	at com.intellij.openapi.ui.DialogWrapper.doShow(DialogWrapper.java:1715)
	at com.intellij.openapi.ui.DialogWrapper.show(DialogWrapper.java:1674)
	at com.intellij.refactoring.ui.RefactoringDialog.show(RefactoringDialog.java:103)
	at org.rust.ide.refactoring.move.RsMoveTopLevelItemsHandler.doMove(RsMoveTopLevelItemsHandler.kt:86)
	at org.rust.ide.refactoring.move.RsMoveTopLevelItemsHandler.tryToMove(RsMoveTopLevelItemsHandler.kt:64)
	at com.intellij.refactoring.move.MoveHandler.invoke(MoveHandler.java:71)
	at com.intellij.refactoring.actions.BaseRefactoringAction.performRefactoringAction(BaseRefactoringAction.java:156)
	at com.intellij.refactoring.actions.BaseRefactoringAction.actionPerformed(BaseRefactoringAction.java:108)
	at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:281)
	at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.lambda$actionPerformed$0(ActionMenuItem.java:310)
	at com.intellij.openapi.wm.impl.FocusManagerImpl.runOnOwnContext(FocusManagerImpl.java:286)
	at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.runOnOwnContext(IdeFocusManagerImpl.java:77)
	at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.actionPerformed(ActionMenuItem.java:299)
	at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
	at com.intellij.openapi.actionSystem.impl.ActionMenuItem.lambda$fireActionPerformed$0(ActionMenuItem.java:110)
	at com.intellij.openapi.application.TransactionGuardImpl.performUserActivity(TransactionGuardImpl.java:96)
	at com.intellij.openapi.actionSystem.impl.ActionMenuItem.fireActionPerformed(ActionMenuItem.java:110)
	at com.intellij.ui.plaf.beg.BegMenuItemUI.doClick(BegMenuItemUI.java:515)
	at com.intellij.ui.plaf.beg.BegMenuItemUI$MyMouseInputHandler.mouseReleased(BegMenuItemUI.java:545)
	at java.desktop/java.awt.Component.processMouseEvent(Component.java:6652)
	at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3345)
	at java.desktop/java.awt.Component.processEvent(Component.java:6417)
	at java.desktop/java.awt.Container.processEvent(Container.java:2263)
	at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5027)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4859)
	at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
	at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
	at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
	at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4859)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:778)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:727)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:751)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:749)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:748)
	at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:973)
	at com.intellij.ide.IdeEventQueue.dispatchMouseEvent(IdeEventQueue.java:908)
	at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:837)
	at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$8(IdeEventQueue.java:451)
	at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:773)
	at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$9(IdeEventQueue.java:450)
	at com.intellij.openapi.application.impl.ApplicationImpl.runIntendedWriteActionOnCurrentThread(ApplicationImpl.java:796)
	at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:504)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
```
</details>

## Steps to reproduce

```rust
//- lib.rs
pub mod mod1 {
    pub trait Foo {}
    impl Foo for () {}
}

//- main.rs
pub mod mod2 {}
```

1. Try to move `impl` alone from `lib.rs` to `main.rs`
2. You will get an error due to #6308
3. Move `impl` with `trait` together
4. Works OK but the exception is raised in the process

Changed files:
src/main/kotlin/org/rust/ide/refactoring/move/common/RsMoveCommonProcessor.kt

diff --git a/src/main/kotlin/org/rust/ide/refactoring/move/common/RsMoveCommonProcessor.kt b/src/main/kotlin/org/rust/ide/refactoring/move/common/RsMoveCommonProcessor.kt
index 0f2efa513..0243de21e 100644
--- a/src/main/kotlin/org/rust/ide/refactoring/move/common/RsMoveCommonProcessor.kt
+++ b/src/main/kotlin/org/rust/ide/refactoring/move/common/RsMoveCommonProcessor.kt
@@ -117,6 +117,8 @@ class RsMoveCommonProcessor(
     private val traitMethodsProcessor: RsMoveTraitMethodsProcessor =
         RsMoveTraitMethodsProcessor(psiFactory, sourceMod, targetMod, pathHelper)
 
+    private lateinit var outsideReferences: List<RsMoveReferenceInfo>
+
     init {
         if (targetMod == sourceMod) {
             throw IncorrectOperationException("Source and destination modules should be different")
@@ -155,7 +157,7 @@ class RsMoveCommonProcessor(
             project.computeWithCancelableProgress(title) {
                 runReadAction {
                     // TODO: two threads
-                    val outsideReferences = collectOutsideReferences()
+                    outsideReferences = collectOutsideReferences()
                     val insideReferences = preprocessInsideReferences(usages)
                     traitMethodsProcessor.preprocessOutsideReferencesToTraitMethods(conflicts, elementsToMove)
                     traitMethodsProcessor.preprocessInsideReferencesToTraitMethods(conflicts, elementsToMove)
@@ -185,6 +187,10 @@ class RsMoveCommonProcessor(
 
         val references = mutableListOf<RsMoveReferenceInfo>()
         for (path in movedElementsDeepDescendantsOfType<RsPath>(elementsToMove)) {
+            if (path.containingFile == sourceMod.containingFile) {
+                path.putCopyableUserData(RS_PATH_OLD_BEFORE_MOVE_KEY, path)
+            }
+
             if (path.parent is RsVisRestriction) continue
             if (path.containingMod != sourceMod  // path inside nested mod of moved element
                 && !path.isAbsolute()
@@ -208,14 +214,13 @@ class RsMoveCommonProcessor(
             if (isSelfReference) continue
 
             val reference = createOutsideReferenceInfo(path, target) ?: continue
-            path.putCopyableUserData(RS_MOVE_REFERENCE_INFO_KEY, reference)
             references += reference
         }
         for (patIdent in movedElementsShallowDescendantsOfType<RsPatIdent>(elementsToMove)) {
+            patIdent.putCopyableUserData(RS_PATH_OLD_BEFORE_MOVE_KEY, patIdent)
             val target = patIdent.patBinding.reference.resolve() as? RsQualifiedNamedElement ?: continue
             if (target !is RsStructItem && target !is RsEnumVariant && target !is RsConstant) continue
             val reference = createOutsideReferenceInfo(patIdent, target) ?: continue
-            patIdent.putCopyableUserData(RS_MOVE_REFERENCE_INFO_KEY, reference)
             references += reference
         }
         return references
@@ -279,10 +284,7 @@ class RsMoveCommonProcessor(
             val target = usage.referenceInfo.target
             target.putCopyableUserData(RS_TARGET_BEFORE_MOVE_KEY, target)
 
-            val pathOldOriginal = usage.referenceInfo.pathOldOriginal
-            if (pathOldOriginal.isInsideMovedElements(elementsToMove)) {
-                pathOldOriginal.putCopyableUserData(RS_PATH_OLD_BEFORE_MOVE_KEY, pathOldOriginal)
-            }
+            /** [RS_PATH_OLD_BEFORE_MOVE_KEY] for self references is filled in [collectOutsideReferences] */
         }
         return originalReferences
     }
@@ -359,9 +361,10 @@ class RsMoveCommonProcessor(
         updateOutsideReferencesInVisRestrictions()
 
         elementsToMove = moveElements()
+        val pathMapping = createMapping(RS_PATH_OLD_BEFORE_MOVE_KEY, RsElement::class.java)
 
         val retargetReferencesProcessor = RsMoveRetargetReferencesProcessor(project, sourceMod, targetMod)
-        val outsideReferences = restoreOutsideReferenceInfosAfterMove()
+        restoreOutsideReferenceInfosAfterMove(pathMapping)
         retargetReferencesProcessor.retargetReferences(outsideReferences)
 
         traitMethodsProcessor.addTraitImportsForOutsideReferences(elementsToMove)
@@ -370,7 +373,7 @@ class RsMoveCommonProcessor(
         val insideReferences = usages
             .filterIsInstance<RsPathUsageInfo>()
             .map { it.referenceInfo }
-        updateInsideReferenceInfosIfNeeded(insideReferences)
+        updateInsideReferenceInfosIfNeeded(insideReferences, pathMapping)
         retargetReferencesProcessor.retargetReferences(insideReferences)
         retargetReferencesProcessor.optimizeImports()
     }
@@ -386,41 +389,23 @@ class RsMoveCommonProcessor(
      * After move this [RsPath] is invalidated and new [RsPath] is created.
      * We store [RsMoveReferenceInfo] for outside reference in copyable user data for this [RsPath].
      */
-    private fun restoreOutsideReferenceInfosAfterMove(): List<RsMoveReferenceInfo> {
-        return movedElementsDeepDescendantsOfType<RsElement>(elementsToMove)
-            .mapNotNullTo(mutableListOf()) { pathOldOriginal ->
-                val reference = pathOldOriginal.getCopyableUserData(RS_MOVE_REFERENCE_INFO_KEY)
-                    ?: return@mapNotNullTo null
-                pathOldOriginal.putCopyableUserData(RS_MOVE_REFERENCE_INFO_KEY, null)
-                // because after move new `RsElement`s are created
-                reference.pathOldOriginal = pathOldOriginal
-                reference.pathOld = convertFromPathOriginal(pathOldOriginal, codeFragmentFactory)
-                reference
-            }
+    private fun restoreOutsideReferenceInfosAfterMove(pathMapping: Map<RsElement, RsElement>) {
+        for (reference in outsideReferences) {
+            reference.restorePathOldAfterMove(pathMapping)
+        }
     }
 
     /**
      * After move old items are invalidated and new items ([RsElement]s) are created.
      * Thus we have to change `target` for inside references and change `pathOld` for self references.
      */
-    private fun updateInsideReferenceInfosIfNeeded(references: List<RsMoveReferenceInfo>) {
-        fun <T : RsElement> createMapping(key: Key<T>, aClass: Class<T>): Map<T, T> {
-            return movedElementsShallowDescendantsOfType(elementsToMove, aClass)
-                .mapNotNull { element ->
-                    val elementOld = element.getCopyableUserData(key) ?: return@mapNotNull null
-                    element.putCopyableUserData(key, null)
-                    elementOld to element
-                }
-                .toMap()
-        }
-
-        val pathMapping = createMapping(RS_PATH_OLD_BEFORE_MOVE_KEY, RsElement::class.java)
+    private fun updateInsideReferenceInfosIfNeeded(
+        references: List<RsMoveReferenceInfo>,
+        pathMapping: Map<RsElement, RsElement>
+    ) {
         val targetMapping = createMapping(RS_TARGET_BEFORE_MOVE_KEY, RsQualifiedNamedElement::class.java)
         for (reference in references) {
-            pathMapping[reference.pathOldOriginal]?.let { pathOldOriginal ->
-                reference.pathOldOriginal = pathOldOriginal
-                reference.pathOld = convertFromPathOriginal(pathOldOriginal, codeFragmentFactory)
-            }
+            reference.restorePathOldAfterMove(pathMapping)
 
             val targetRestored = targetMapping[reference.target]
             // it is ok if `targetRestored` is null when target was inside a file which is a submodule of moved items
@@ -434,6 +419,22 @@ class RsMoveCommonProcessor(
         }
     }
 
+    /** See [RS_PATH_OLD_BEFORE_MOVE_KEY] */
+    private fun <T : RsElement> createMapping(key: Key<T>, aClass: Class<T>): Map<T, T> {
+        return movedElementsShallowDescendantsOfType(elementsToMove, aClass)
+            .mapNotNull { element ->
+                val elementOld = element.getCopyableUserData(key) ?: return@mapNotNull null
+                element.putCopyableUserData(key, null)
+                elementOld to element
+            }
+            .toMap()
+    }
+
+    private fun RsMoveReferenceInfo.restorePathOldAfterMove(pathMapping: Map<RsElement, RsElement>) {
+        pathOldOriginal = pathMapping[pathOldOriginal] ?: return
+        pathOld = convertFromPathOriginal(pathOldOriginal, codeFragmentFactory)
+    }
+
     fun updateMovedItemVisibility(item: RsItemElement) {
         when (item.visibility) {
             is RsVisibility.Private -> {
@@ -458,15 +459,7 @@ class RsMoveCommonProcessor(
 
     companion object {
         /**
-         * Used for outside references (excluding references from nested moved files).
-         * We store [RsMoveReferenceInfo] in `copyableUserData` for [RsPath] corresponding to reference,
-         * so after move we could restore [RsMoveReferenceInfo].
-         * (We have to do it because after move new psi elements are created)
-         */
-        private val RS_MOVE_REFERENCE_INFO_KEY: Key<RsMoveReferenceInfo> = Key.create("RS_MOVE_REFERENCE_INFO_KEY")
-
-        /**
-         * Used by self references (we treat them as inside references).
+         * Used by outside references and self references (we treat them as inside references).
          * We store `pathOld` of such reference in `copyableUserData` for `pathOld` itself.
          * So after move we can create mapping between original [RsPath] in [sourceMod] and its copy in [targetMod],
          * and use this mapping to update `pathOld` in [RsMoveReferenceInfo].