diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemovePartsFromPropertyFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemovePartsFromPropertyFix.kt index 408f1e6611d..a484b144c4b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemovePartsFromPropertyFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemovePartsFromPropertyFix.kt @@ -21,12 +21,16 @@ import com.intellij.openapi.project.Project import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.diagnostics.Errors +import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil import org.jetbrains.kotlin.idea.intentions.SpecifyTypeExplicitlyIntention +import org.jetbrains.kotlin.idea.resolve.frontendService import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner import org.jetbrains.kotlin.types.isError +import org.jetbrains.kotlin.types.refinement.TypeRefinement open class RemovePartsFromPropertyFix( element: KtProperty, @@ -79,6 +83,18 @@ open class RemovePartsFromPropertyFix( } val replaceElement = element?.replace(newElement) as? KtProperty if (replaceElement != null && typeToAdd != null) { + // `refineType` call here is needed to avoid InvalidModuleException exception + // + // It happens because after refinement KotlinTypes may access ModuleDescriptor content + // and that module becomes invalid after replacement two lines above + // + // The actual problem is that we use a type obtained from the obsolete analysis session + // The ideal fix would be using a String that needs to be rendered instead of actual type + // + // But calling another type refinement also helps because it makes KotlinType instance using new module descriptor + @UseExperimental(TypeRefinement::class) + typeToAdd = replaceElement.getResolutionFacade().frontendService().refineType(typeToAdd) + SpecifyTypeExplicitlyIntention.addTypeAnnotation(editor, replaceElement, typeToAdd) } }