From 3d44471659e14ab877178c556075b84507b2d062 Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Wed, 23 Dec 2015 17:48:42 +0300 Subject: [PATCH] KT-10483 Missing smart cast on variable in variable as function call #KT-10483 Fixed --- .../calls/tower/NewResolveOldInference.kt | 4 ++- .../diagnostics/tests/smartCasts/kt10483.kt | 17 +++++++++++++ .../diagnostics/tests/smartCasts/kt10483.txt | 25 +++++++++++++++++++ .../checkers/DiagnosticsTestGenerated.java | 6 +++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/kt10483.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/kt10483.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolveOldInference.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolveOldInference.kt index bcca3de9c5e..66ea19d5b2d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolveOldInference.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/NewResolveOldInference.kt @@ -296,7 +296,9 @@ class NewResolveOldInference( val basicCallResolutionContext = basicCallContext.replaceBindingTrace(variable.resolvedCall.trace) .replaceCall(functionCall) .replaceContextDependency(ContextDependency.DEPENDENT) // todo - val newContext = Context(scopeTower, OperatorNameConventions.INVOKE, basicCallResolutionContext, tracingForInvoke) + + val newScopeTower = ScopeTowerImpl(basicCallResolutionContext, scopeTower.dynamicScope, scopeTower.syntheticScopes, scopeTower.location) + val newContext = Context(newScopeTower, OperatorNameConventions.INVOKE, basicCallResolutionContext, tracingForInvoke) return variableReceiver to newContext } diff --git a/compiler/testData/diagnostics/tests/smartCasts/kt10483.kt b/compiler/testData/diagnostics/tests/smartCasts/kt10483.kt new file mode 100644 index 00000000000..a832f21e662 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/kt10483.kt @@ -0,0 +1,17 @@ +interface A +class B : A { + operator fun invoke() = this +} + +class C : A + +operator fun C.invoke(): B = B() + +fun foo(arg: A): B? { + if (arg is B) return arg() + + if (arg is C) return arg() + + return null +} + diff --git a/compiler/testData/diagnostics/tests/smartCasts/kt10483.txt b/compiler/testData/diagnostics/tests/smartCasts/kt10483.txt new file mode 100644 index 00000000000..abd84cba9b4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/kt10483.txt @@ -0,0 +1,25 @@ +package + +public fun foo(/*0*/ arg: A): B? +public operator fun C.invoke(): B + +public interface A { + 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 +} + +public final class B : A { + public constructor B() + 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 final operator fun invoke(): B + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class C : A { + public constructor C() + 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/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 42b6e9fac9f..7534e751c4d 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -15303,6 +15303,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("kt10483.kt") + public void testKt10483() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/kt10483.kt"); + doTest(fileName); + } + @TestMetadata("kt1461.kt") public void testKt1461() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/kt1461.kt");