diff --git a/idea/src/org/jetbrains/jet/plugin/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.java b/idea/src/org/jetbrains/jet/plugin/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.java index 25c861d50fe..df2f5e03574 100644 --- a/idea/src/org/jetbrains/jet/plugin/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.java +++ b/idea/src/org/jetbrains/jet/plugin/refactoring/introduce/introduceVariable/KotlinIntroduceVariableHandler.java @@ -40,6 +40,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingTraceContext; import org.jetbrains.jet.lang.resolve.ObservableBindingTrace; import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo; +import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.PackageType; @@ -465,14 +466,15 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase { element2.getParent() instanceof JetSimpleNameExpression) { JetSimpleNameExpression expr1 = (JetSimpleNameExpression)element1.getParent(); JetSimpleNameExpression expr2 = (JetSimpleNameExpression)element2.getParent(); - DeclarationDescriptor descr1 = bindingContext.get(BindingContext.REFERENCE_TARGET, expr1); - DeclarationDescriptor descr2 = bindingContext.get(BindingContext.REFERENCE_TARGET, expr2); - if (descr1 != descr2) { - return 1; - } - else { - return 0; - } + + ResolvedCall rc1 = bindingContext.get(BindingContext.RESOLVED_CALL, expr1); + ResolvedCall rc2 = bindingContext.get(BindingContext.RESOLVED_CALL, expr2); + if (rc1 == null || rc2 == null) return rc1 == rc2 ? 0 : 1; + + return rc1.getResultingDescriptor() == rc2.getResultingDescriptor() + && rc1.getExplicitReceiverKind() == rc2.getExplicitReceiverKind() + && rc1.getReceiverArgument() == rc2.getReceiverArgument() + && rc1.getThisObject() == rc2.getThisObject() ? 0 : 1; } } if (!element1.textMatches(element2)) { diff --git a/idea/testData/refactoring/introduceVariable/nonEquivalentReceivers.kt b/idea/testData/refactoring/introduceVariable/nonEquivalentReceivers.kt new file mode 100644 index 00000000000..0a4249b8520 --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/nonEquivalentReceivers.kt @@ -0,0 +1,17 @@ +class A { + val value : Int = 0 +} + +fun foo(body: A.() -> Unit) {} + +fun bar() { + foo { + print(value) + print(value) + } + + foo { + print(value) + print(value) + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/introduceVariable/nonEquivalentReceivers.kt.after b/idea/testData/refactoring/introduceVariable/nonEquivalentReceivers.kt.after new file mode 100644 index 00000000000..dd5a9b6ebf7 --- /dev/null +++ b/idea/testData/refactoring/introduceVariable/nonEquivalentReceivers.kt.after @@ -0,0 +1,18 @@ +class A { + val value : Int = 0 +} + +fun foo(body: A.() -> Unit) {} + +fun bar() { + foo { + val i = value + print(i) + print(i) + } + + foo { + print(value) + print(value) + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/refactoring/introduce/introduceVariable/JetExtractionTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/refactoring/introduce/introduceVariable/JetExtractionTestGenerated.java index ef90598b66a..61013b5988c 100644 --- a/idea/tests/org/jetbrains/jet/plugin/refactoring/introduce/introduceVariable/JetExtractionTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/refactoring/introduce/introduceVariable/JetExtractionTestGenerated.java @@ -128,6 +128,11 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest { doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/ManyOccurrences.kt"); } + @TestMetadata("nonEquivalentReceivers.kt") + public void testNonEquivalentReceivers() throws Exception { + doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/nonEquivalentReceivers.kt"); + } + @TestMetadata("ReplaceOccurence.kt") public void testReplaceOccurence() throws Exception { doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/ReplaceOccurence.kt");