diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.kt index c989f1879d8..af9d0971f83 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.kt @@ -49,7 +49,6 @@ import org.jetbrains.kotlin.idea.refactoring.* import org.jetbrains.kotlin.idea.refactoring.introduce.* import org.jetbrains.kotlin.idea.resolve.ResolutionFacade import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers -import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.util.application.executeCommand import org.jetbrains.kotlin.idea.util.application.executeWriteCommand import org.jetbrains.kotlin.idea.util.application.runWriteAction @@ -67,7 +66,10 @@ import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.resolve.source.getPsi import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.TypeConstructor import org.jetbrains.kotlin.types.checker.KotlinTypeChecker +import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker +import org.jetbrains.kotlin.types.checker.TypeCheckerContext import org.jetbrains.kotlin.utils.addIfNotNull import org.jetbrains.kotlin.utils.ifEmpty import org.jetbrains.kotlin.utils.sure @@ -82,6 +84,20 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler { private var KtExpression.isOccurrence: Boolean by NotNullablePsiCopyableUserDataProperty(Key.create("OCCURRENCE"), false) + private class TypeCheckerImpl(private val project: Project) : KotlinTypeChecker by KotlinTypeChecker.DEFAULT { + private inner class ContextImpl : TypeCheckerContext(false) { + override fun areEqualTypeConstructors(a: TypeConstructor, b: TypeConstructor): Boolean { + return compareDescriptors(project, a.declarationDescriptor, b.declarationDescriptor) + } + } + + override fun equalTypes(a: KotlinType, b: KotlinType): Boolean { + return with(NewKotlinTypeChecker) { + ContextImpl().equalTypes(a.unwrap(), b.unwrap()) + } + } + } + private class IntroduceVariableContext( private val expression: KtExpression, private val nameSuggestions: List>, @@ -495,8 +511,8 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler { val typeNoExpectedType = substringInfo?.type ?: physicalExpression.computeTypeInfoInContext(scope, physicalExpression, bindingTrace, dataFlowInfo).type val noTypeInference = expressionType != null - && typeNoExpectedType != null - && !KotlinTypeChecker.DEFAULT.equalTypes(expressionType, typeNoExpectedType) + && typeNoExpectedType != null + && !TypeCheckerImpl(project).equalTypes(expressionType, typeNoExpectedType) if (expressionType == null && bindingContext.get(BindingContext.QUALIFIER, physicalExpression) != null) { return showErrorHint(project, editor, KotlinRefactoringBundle.message("cannot.refactor.package.expression")) diff --git a/idea/testData/refactoring/introduceVariable/AnonymousType.kt b/idea/testData/refactoring/introduceVariable/AnonymousType.kt new file mode 100644 index 00000000000..81f4bf2f17a --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/AnonymousType.kt @@ -0,0 +1,9 @@ +fun foo() { + bar(object: Runnable { + override fun run() { + } + }) +} + +fun bar(runnable: Runnable) { +} \ No newline at end of file diff --git a/idea/testData/refactoring/introduceVariable/AnonymousType.kt.after b/idea/testData/refactoring/introduceVariable/AnonymousType.kt.after new file mode 100644 index 00000000000..d0d48d9a00f --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/AnonymousType.kt.after @@ -0,0 +1,10 @@ +fun foo() { + val runnable = object : Runnable { + override fun run() { + } + } + bar(runnable) +} + +fun bar(runnable: Runnable) { +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java index 16d7d95a4d8..d2283db31b2 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java @@ -27,6 +27,12 @@ public class ExtractionTestGenerated extends AbstractExtractionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/refactoring/introduceVariable"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true); } + @TestMetadata("AnonymousType.kt") + public void testAnonymousType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/AnonymousType.kt"); + doIntroduceVariableTest(fileName); + } + @TestMetadata("ArrayAccessExpr.kt") public void testArrayAccessExpr() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/ArrayAccessExpr.kt");