diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt index bed9d54df48..8cac37c8d37 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt @@ -180,7 +180,8 @@ class KotlinResolutionCallbacksImpl( override fun createReceiverWithSmartCastInfo(resolvedAtom: ResolvedCallAtom): ReceiverValueWithSmartCastInfo? { val returnType = resolvedAtom.candidateDescriptor.returnType ?: return null val psiKotlinCall = resolvedAtom.atom.psiKotlinCall - val expression = psiKotlinCall.psiCall.callElement.safeAs() ?: return null + val callElement = psiKotlinCall.psiCall.callElement.safeAs() ?: return null + val expression = findCommonParent(callElement, resolvedAtom.atom.psiKotlinCall.explicitReceiver) return transformToReceiverWithSmartCastInfo( resolvedAtom.candidateDescriptor, diff --git a/compiler/testData/diagnostics/tests/smartCasts/castchecks/smartCastOfNullableExpressionWithExpectedType.kt b/compiler/testData/diagnostics/tests/smartCasts/castchecks/smartCastOfNullableExpressionWithExpectedType.kt new file mode 100644 index 00000000000..e8e57f9ae4f --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/castchecks/smartCastOfNullableExpressionWithExpectedType.kt @@ -0,0 +1,9 @@ +// !DIAGNOSTICS: -UNUSED_VARIABLE + +class Item(val link: String?) + +fun test(item: Item) { + if (item.link != null) { + val href: String = item.link + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/smartCasts/castchecks/smartCastOfNullableExpressionWithExpectedType.txt b/compiler/testData/diagnostics/tests/smartCasts/castchecks/smartCastOfNullableExpressionWithExpectedType.txt new file mode 100644 index 00000000000..07c56461a8a --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/castchecks/smartCastOfNullableExpressionWithExpectedType.txt @@ -0,0 +1,11 @@ +package + +public fun test(/*0*/ item: Item): kotlin.Unit + +public final class Item { + public constructor Item(/*0*/ link: kotlin.String?) + public final val link: kotlin.String? + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/propertyAsCondition.kt b/compiler/testData/diagnostics/tests/smartCasts/propertyAsCondition.kt index b7ea1755df1..9a0d88c99b4 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/propertyAsCondition.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/propertyAsCondition.kt @@ -1,4 +1,3 @@ -// !WITH_NEW_INFERENCE // See also: KT-11998 data class My(val x: Boolean?) @@ -7,7 +6,7 @@ fun doIt() {} fun foo(my: My) { if (my.x != null) { // my.x should be smart-cast - if (my.x) doIt() + if (my.x) doIt() when (my.x) { true -> doIt() } diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 6333822845c..e3181df48f0 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -21469,6 +21469,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("smartCastOfNullableExpressionWithExpectedType.kt") + public void testSmartCastOfNullableExpressionWithExpectedType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/castchecks/smartCastOfNullableExpressionWithExpectedType.kt"); + doTest(fileName); + } + @TestMetadata("variables.kt") public void testVariables() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/castchecks/variables.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index adb30e1f8f0..c271d45e979 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -21469,6 +21469,12 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing doTest(fileName); } + @TestMetadata("smartCastOfNullableExpressionWithExpectedType.kt") + public void testSmartCastOfNullableExpressionWithExpectedType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/castchecks/smartCastOfNullableExpressionWithExpectedType.kt"); + doTest(fileName); + } + @TestMetadata("variables.kt") public void testVariables() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/castchecks/variables.kt");