From 6cb1d2e3f72318779a75c64cf8ed6dc2c1a97677 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Mon, 25 Aug 2014 17:51:02 +0400 Subject: [PATCH] Lower bound must be a subtype of the upper bound. The change in CommonSupertypes: We used to say that commonSupertype(Inv, Inv) = Inv --- compiler/testData/type-checker-test.kt | 6 +++++- .../org/jetbrains/jet/types/JetTypeCheckerTest.java | 2 ++ .../jetbrains/jet/lang/types/CommonSupertypes.java | 11 ++++------- .../src/org/jetbrains/jet/lang/types/flexibleTypes.kt | 5 +++++ 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/compiler/testData/type-checker-test.kt b/compiler/testData/type-checker-test.kt index 8852ce71e27..b578f212933 100644 --- a/compiler/testData/type-checker-test.kt +++ b/compiler/testData/type-checker-test.kt @@ -36,4 +36,8 @@ open class ArrayList() : Any, AbstractList, List fun f() : Unit {} fun f(a : Int) : Int {a} fun f(a : Float, b : Int) : Float {a} -fun f(a : Float) : T {a} \ No newline at end of file +fun f(a : Float) : T {a} + +trait Parent +trait A: Parent +trait B: Parent \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/types/JetTypeCheckerTest.java b/compiler/tests/org/jetbrains/jet/types/JetTypeCheckerTest.java index 828559d0bff..268ca44b7a2 100644 --- a/compiler/tests/org/jetbrains/jet/types/JetTypeCheckerTest.java +++ b/compiler/tests/org/jetbrains/jet/types/JetTypeCheckerTest.java @@ -200,6 +200,8 @@ public class JetTypeCheckerTest extends JetLiteFixture { assertCommonSupertype("Base_T", "Derived_T", "Base_T"); assertCommonSupertype("Base_T", "Derived_T", "Base_T"); assertCommonSupertype("Base_T<*>", "Base_T", "Base_T<*>"); + + assertCommonSupertype("Base_T", "Base_T", "Base_T"); } public void testIntersect() throws Exception { diff --git a/core/descriptors/src/org/jetbrains/jet/lang/types/CommonSupertypes.java b/core/descriptors/src/org/jetbrains/jet/lang/types/CommonSupertypes.java index cdadce5904e..30d326dc325 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/types/CommonSupertypes.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/CommonSupertypes.java @@ -237,21 +237,18 @@ public class CommonSupertypes { } } + if (outs != null) { + Variance projectionKind = variance == OUT_VARIANCE ? Variance.INVARIANT : OUT_VARIANCE; + return new TypeProjectionImpl(projectionKind, commonSupertype(outs)); + } if (ins != null) { JetType intersection = TypeUtils.intersect(JetTypeChecker.DEFAULT, ins); if (intersection == null) { - if (outs != null) { - return new TypeProjectionImpl(OUT_VARIANCE, commonSupertype(outs)); - } return new TypeProjectionImpl(OUT_VARIANCE, commonSupertype(parameterDescriptor.getUpperBounds())); } Variance projectionKind = variance == IN_VARIANCE ? Variance.INVARIANT : IN_VARIANCE; return new TypeProjectionImpl(projectionKind, intersection); } - else if (outs != null) { - Variance projectionKind = variance == OUT_VARIANCE ? Variance.INVARIANT : OUT_VARIANCE; - return new TypeProjectionImpl(projectionKind, commonSupertype(outs)); - } else { Variance projectionKind = variance == OUT_VARIANCE ? Variance.INVARIANT : OUT_VARIANCE; return new TypeProjectionImpl(projectionKind, commonSupertype(parameterDescriptor.getUpperBounds())); diff --git a/core/descriptors/src/org/jetbrains/jet/lang/types/flexibleTypes.kt b/core/descriptors/src/org/jetbrains/jet/lang/types/flexibleTypes.kt index 2d4996ccc13..21f85007c81 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/types/flexibleTypes.kt +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/flexibleTypes.kt @@ -16,6 +16,8 @@ package org.jetbrains.jet.lang.types +import org.jetbrains.jet.lang.types.checker.JetTypeChecker + public trait FlexibleType : JetType { public val lowerBound: JetType public val upperBound: JetType @@ -33,6 +35,9 @@ public open class DelegatingFlexibleType( { assert (!lowerBound.isFlexible()) { "Lower bound of a flexible type can not be flexible: $lowerBound" } assert (!upperBound.isFlexible()) { "Upper bound of a flexible type can not be flexible: $upperBound" } + assert (JetTypeChecker.DEFAULT.isSubtypeOf(lowerBound, upperBound)) { + "Lower bound $lowerBound of a flexible type must be a subtype of the upper bound $upperBound" + } } override fun getDelegate() = lowerBound