From 642f8ecaf8e212a3e704ba340a950171ef438890 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 16 Apr 2019 16:02:10 +0300 Subject: [PATCH] [NI] remove redundant replacing receiver in lambda function descriptor #KT-30927 Fixed #KT-31057 Fixed --- .../fir/FirDiagnosticsSmokeTestGenerated.java | 5 ++ .../calls/tower/ResolvedAtomCompleter.kt | 8 +-- .../diagnostics/tests/smartCasts/kt30927.kt | 51 +++++++++++++++++++ .../diagnostics/tests/smartCasts/kt30927.txt | 6 +++ .../checkers/DiagnosticsTestGenerated.java | 5 ++ .../DiagnosticsUsingJavacTestGenerated.java | 5 ++ .../scopes/receivers/ExtensionReceiver.java | 16 ++++++ 7 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/kt30927.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/kt30927.txt diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java index cf1ea12de31..e61aaa2addb 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java @@ -19539,6 +19539,11 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok runTest("compiler/testData/diagnostics/tests/smartCasts/kt30826.kt"); } + @TestMetadata("kt30927.kt") + public void testKt30927() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/kt30927.kt"); + } + @TestMetadata("kt3224.kt") public void testKt3224() throws Exception { runTest("compiler/testData/diagnostics/tests/smartCasts/kt3224.kt"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt index e822d288e77..804f89eccf8 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt @@ -208,9 +208,11 @@ class ResolvedAtomCompleter( val newReceiverValue = receiver.value.replaceType(newValueType) - functionDescriptor.setExtensionReceiverParameter( - ReceiverParameterDescriptorImpl(receiver.containingDeclaration, newReceiverValue, receiver.annotations) - ) + if (newReceiverValue != receiver.value) { + functionDescriptor.setExtensionReceiverParameter( + ReceiverParameterDescriptorImpl(receiver.containingDeclaration, newReceiverValue, receiver.annotations) + ) + } } } diff --git a/compiler/testData/diagnostics/tests/smartCasts/kt30927.kt b/compiler/testData/diagnostics/tests/smartCasts/kt30927.kt new file mode 100644 index 00000000000..bb9958bb433 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/kt30927.kt @@ -0,0 +1,51 @@ +// !DIAGNOSTICS: -UNUSED_EXPRESSION +// !WITH_NEW_INFERENCE +// !CHECK_TYPE + +fun case_0() { + val z: Any? = 10 + val y = z.run { + this as Int + this // error in NI: required Int, found Any?; just inferred to Any? in OI + } + y checkType { _() } + y checkType { _() } +} + +fun case_1(z: Any?) { + val y = z.run { + when (this) { + is String -> return@run this // type mismatch in the new inference (required String, found Any?) + is Float -> "" + else -> return@run "" + } + } + y checkType { _() } + y checkType { _() } + // y is inferred to Any? +} + +fun case_2(z: Any?) { + val y = z.run { + when (this) { + is String -> this + is Float -> "" + else -> return@run "" + } + } + y checkType { _() } + // y is inferred to String +} + +fun case_3(z: Any?) { + val y = z.let { + when (it) { + is String -> return@let it + is Float -> "" + else -> return@let "" + } + } + y checkType { _() } + y checkType { _() } + // y is inferred to String +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/smartCasts/kt30927.txt b/compiler/testData/diagnostics/tests/smartCasts/kt30927.txt new file mode 100644 index 00000000000..6bb3fb6d837 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/kt30927.txt @@ -0,0 +1,6 @@ +package + +public fun case_0(): kotlin.Unit +public fun case_1(/*0*/ z: kotlin.Any?): kotlin.Unit +public fun case_2(/*0*/ z: kotlin.Any?): kotlin.Unit +public fun case_3(/*0*/ z: kotlin.Any?): kotlin.Unit diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 451456d2fad..dc3ce81cf79 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -19616,6 +19616,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/smartCasts/kt30826.kt"); } + @TestMetadata("kt30927.kt") + public void testKt30927() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/kt30927.kt"); + } + @TestMetadata("kt3224.kt") public void testKt3224() throws Exception { runTest("compiler/testData/diagnostics/tests/smartCasts/kt3224.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 5352e99d1f7..1f76c10cd73 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -19541,6 +19541,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/smartCasts/kt30826.kt"); } + @TestMetadata("kt30927.kt") + public void testKt30927() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/kt30927.kt"); + } + @TestMetadata("kt3224.kt") public void testKt3224() throws Exception { runTest("compiler/testData/diagnostics/tests/smartCasts/kt3224.kt"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/receivers/ExtensionReceiver.java b/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/receivers/ExtensionReceiver.java index 81a0c1ee6c2..0d764bcdd71 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/receivers/ExtensionReceiver.java +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/receivers/ExtensionReceiver.java @@ -20,7 +20,20 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.descriptors.CallableDescriptor; import org.jetbrains.kotlin.types.KotlinType; +import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker; +import java.util.Objects; + +/** + * [ExtensionReceiver] is a receiver for pure `this` call inside lambda with receiver + * + * x.run { + * this // ExtensionReceiver + * this as String // ExtensionReceiver + * this.toString() // ImplicitClassReceiver + * toString() // ImplicitClassReceiver + * } + */ public class ExtensionReceiver extends AbstractReceiverValue implements ImplicitReceiver { private final CallableDescriptor descriptor; @@ -43,6 +56,9 @@ public class ExtensionReceiver extends AbstractReceiverValue implements Implicit @NotNull @Override public ReceiverValue replaceType(@NotNull KotlinType newType) { + if (NewKotlinTypeChecker.INSTANCE.equalTypes(receiverType, newType)) { + return this; + } return new ExtensionReceiver(descriptor, newType, getOriginal()); }