From cdecb9d8c43663fd69ba4ec4cb4f190cc1a2712b Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Wed, 19 Nov 2014 19:36:35 +0300 Subject: [PATCH] Test for using dynamic types in type inference --- .../lang/types/expressions/DataFlowUtils.java | 3 +- .../tests/dynamicTypes/inference.kt | 29 +++++++++++++++++++ .../tests/dynamicTypes/inference.txt | 27 +++++++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 6 ++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/dynamicTypes/inference.kt create mode 100644 compiler/testData/diagnostics/tests/dynamicTypes/inference.txt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/DataFlowUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/DataFlowUtils.java index 8921224d88e..40f76c8297d 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/DataFlowUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/DataFlowUtils.java @@ -269,7 +269,8 @@ public class DataFlowUtils { @Nullable public static JetType checkImplicitCast(@Nullable JetType expressionType, @NotNull JetExpression expression, @NotNull ExpressionTypingContext context, boolean isStatement) { if (expressionType != null && context.expectedType == NO_EXPECTED_TYPE && context.contextDependency == INDEPENDENT && !isStatement - && (KotlinBuiltIns.getInstance().isUnit(expressionType) || KotlinBuiltIns.getInstance().isAnyOrNullableAny(expressionType))) { + && (KotlinBuiltIns.getInstance().isUnit(expressionType) || KotlinBuiltIns.getInstance().isAnyOrNullableAny(expressionType)) + && !TypesPackage.isDynamic(expressionType)) { context.trace.report(IMPLICIT_CAST_TO_UNIT_OR_ANY.on(expression, expressionType)); } return expressionType; diff --git a/compiler/testData/diagnostics/tests/dynamicTypes/inference.kt b/compiler/testData/diagnostics/tests/dynamicTypes/inference.kt new file mode 100644 index 00000000000..f1c1405f3e4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/dynamicTypes/inference.kt @@ -0,0 +1,29 @@ +// !MARK_DYNAMIC_CALLS +// !DIAGNOSTICS: -UNUSED_PARAMETER + +// MODULE[js]: m1 +// FILE: k.kt + +fun foo(t1: T, t2: T): T = t1 + +trait Tr +class C: Tr +fun foo1(t1: T, t2: T): T = t1 + +fun test(d: dynamic, b: Boolean, n: String?) { + foo(d, "").foo() + foo1(d, C()).foo() + + val fromIf = if (b) d else "" + fromIf.doo() + + val fromElvis = n ?: d + fromElvis.doo() +} + +class In(t: T) +fun contra(a: In, b: In): T = null!! + +fun testContra(d: dynamic) { + contra(In(d), In("")).get(0) // not a dynamic call +} diff --git a/compiler/testData/diagnostics/tests/dynamicTypes/inference.txt b/compiler/testData/diagnostics/tests/dynamicTypes/inference.txt new file mode 100644 index 00000000000..9f23d38fdf0 --- /dev/null +++ b/compiler/testData/diagnostics/tests/dynamicTypes/inference.txt @@ -0,0 +1,27 @@ +package + +internal fun contra(/*0*/ a: In, /*1*/ b: In): T +internal fun foo(/*0*/ t1: T, /*1*/ t2: T): T +internal fun foo1(/*0*/ t1: T, /*1*/ t2: T): T +internal fun test(/*0*/ d: dynamic, /*1*/ b: kotlin.Boolean, /*2*/ n: kotlin.String?): kotlin.Unit +internal fun testContra(/*0*/ d: dynamic): kotlin.Unit + +internal final class C : Tr { + 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 +} + +internal final class In { + public constructor In(/*0*/ t: T) + 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 +} + +internal trait Tr { + 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/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 949dcbc043f..47117984c73 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -3758,6 +3758,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("inference.kt") + public void testInference() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/dynamicTypes/inference.kt"); + doTest(fileName); + } + @TestMetadata("membersOfAny.kt") public void testMembersOfAny() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/dynamicTypes/membersOfAny.kt");