From 67b59fa72fed63590720a0bc7e8f7c594a5ed62a Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Fri, 18 Mar 2016 11:16:06 +0300 Subject: [PATCH] Prohibit nested intersection types in return position #KT-11490 Fixed --- .../kotlin/resolve/DeclarationsChecker.kt | 3 ++- .../tests/implicitNestedIntersection.kt | 9 +++++++ .../tests/implicitNestedIntersection.txt | 24 +++++++++++++++++++ .../checkers/DiagnosticsTestGenerated.java | 6 +++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/implicitNestedIntersection.kt create mode 100644 compiler/testData/diagnostics/tests/implicitNestedIntersection.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt index 423758ca810..1501bc1130c 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt @@ -35,6 +35,7 @@ import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.SubstitutionUtils import org.jetbrains.kotlin.types.TypeUtils import org.jetbrains.kotlin.types.checker.KotlinTypeChecker +import org.jetbrains.kotlin.types.typeUtil.contains import org.jetbrains.kotlin.types.typeUtil.isNothing import java.util.* @@ -707,7 +708,7 @@ class DeclarationsChecker( (if (declaration is KtProperty) IMPLICIT_NOTHING_PROPERTY_TYPE else IMPLICIT_NOTHING_RETURN_TYPE).on(target) ) } - if (it.constructor is IntersectionTypeConstructor) { + if (it.contains { type -> type.constructor is IntersectionTypeConstructor }) { trace.report(IMPLICIT_INTERSECTION_TYPE.on(target, it)) } } diff --git a/compiler/testData/diagnostics/tests/implicitNestedIntersection.kt b/compiler/testData/diagnostics/tests/implicitNestedIntersection.kt new file mode 100644 index 00000000000..f65fbf7af98 --- /dev/null +++ b/compiler/testData/diagnostics/tests/implicitNestedIntersection.kt @@ -0,0 +1,9 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +interface In +open class A : In +open class B : In + +fun select(x: T, y: T) = x + +fun foo2() = select(A(), B()) // Type is In is prohibited in return position diff --git a/compiler/testData/diagnostics/tests/implicitNestedIntersection.txt b/compiler/testData/diagnostics/tests/implicitNestedIntersection.txt new file mode 100644 index 00000000000..690b5058242 --- /dev/null +++ b/compiler/testData/diagnostics/tests/implicitNestedIntersection.txt @@ -0,0 +1,24 @@ +package + +public fun foo2(): In<{A & B}> +public fun select(/*0*/ x: T, /*1*/ y: T): T + +public open class A : In { + public constructor 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 open class B : In { + 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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public interface In { + 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 413f225a279..a72407035a0 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -301,6 +301,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("implicitNestedIntersection.kt") + public void testImplicitNestedIntersection() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/implicitNestedIntersection.kt"); + doTest(fileName); + } + @TestMetadata("implicitNothing.kt") public void testImplicitNothing() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/implicitNothing.kt");