From 4b6b35dadf68a9088c58c14ed3d49f9d307ff4d0 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 7 Jun 2016 19:14:16 +0300 Subject: [PATCH] Type parameter is considered capable of taking any value for the purpose of cast possibility check #KT-6611 Fixed --- .../jetbrains/kotlin/types/CastDiagnosticsUtil.kt | 5 +++++ .../diagnostics/tests/cast/AsWithOtherParameter.kt | 14 ++++++++++++++ .../tests/cast/AsWithOtherParameter.txt | 14 ++++++++++++++ .../qualifiedJavaClassLiteralInKClassExtension.kt | 2 +- .../kotlin/checkers/DiagnosticsTestGenerated.java | 6 ++++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.kt create mode 100644 compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt index 2fcd219728e..400f1d4204d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/CastDiagnosticsUtil.kt @@ -40,6 +40,11 @@ object CastDiagnosticsUtil { // This is an oversimplification (which does not render the method incomplete): // we consider any type parameter capable of taking any value, which may be made more precise if we considered bounds if (TypeUtils.isTypeParameter(lhsType) || TypeUtils.isTypeParameter(rhsType)) return true + if (lhsType.constructor == rhsType.constructor) { + if (lhsType.arguments.all { TypeUtils.isTypeParameter(it.type) }) return true + if (rhsType.arguments.all { TypeUtils.isTypeParameter(it.type) }) return true + } + if (isFinal(lhsType) || isFinal(rhsType)) return false if (isTrait(lhsType) || isTrait(rhsType)) return true return false diff --git a/compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.kt b/compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.kt new file mode 100644 index 00000000000..6c225909d01 --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.kt @@ -0,0 +1,14 @@ +// See also: KT-6611 (cast can never succeed: Class -> Class) + +class Class(val name: String, val instance: T) + +fun test(clazz: Class) { + println((clazz as Class).name) +} + +fun use() { + test(Class("String", "")) +} + +fun println(s: String) = s + diff --git a/compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.txt b/compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.txt new file mode 100644 index 00000000000..4ff48442c2d --- /dev/null +++ b/compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.txt @@ -0,0 +1,14 @@ +package + +public fun println(/*0*/ s: kotlin.String): kotlin.String +public fun test(/*0*/ clazz: Class): kotlin.Unit +public fun use(): kotlin.Unit + +public final class Class { + public constructor Class(/*0*/ name: kotlin.String, /*1*/ instance: T) + public final val instance: T + public final val name: 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/sourceCompatibility/noBoundCallableReferences/qualifiedJavaClassLiteralInKClassExtension.kt b/compiler/testData/diagnostics/tests/sourceCompatibility/noBoundCallableReferences/qualifiedJavaClassLiteralInKClassExtension.kt index a315491b898..843819e62de 100644 --- a/compiler/testData/diagnostics/tests/sourceCompatibility/noBoundCallableReferences/qualifiedJavaClassLiteralInKClassExtension.kt +++ b/compiler/testData/diagnostics/tests/sourceCompatibility/noBoundCallableReferences/qualifiedJavaClassLiteralInKClassExtension.kt @@ -1,5 +1,5 @@ // !LANGUAGE: -BoundCallableReferences -// !DIAGNOSTICS: -CAST_NEVER_SUCCEEDS +// !DIAGNOSTICS: -UNCHECKED_CAST import kotlin.reflect.KClass diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index a25e896730c..918e6be9825 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -2328,6 +2328,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("AsWithOtherParameter.kt") + public void testAsWithOtherParameter() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.kt"); + doTest(fileName); + } + @TestMetadata("constants.kt") public void testConstants() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/constants.kt");