diff --git a/compiler/testData/diagnostics/tests/platformTypes/methodCall/inferenceWithBound.kt b/compiler/testData/diagnostics/tests/platformTypes/methodCall/inferenceWithBound.kt new file mode 100644 index 00000000000..5c428db0487 --- /dev/null +++ b/compiler/testData/diagnostics/tests/platformTypes/methodCall/inferenceWithBound.kt @@ -0,0 +1,41 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +// FILE: p/Super.java + +package p; + +public interface Super { + +} + +// FILE: p/Sub.java + +package p; + +public interface Sub extends Super {} + +// FILE: p/Other.java + +package p; + +import java.util.*; + +public class Other { + + public static Sub sub; + + public static Collection subs; + public static Collection supers; +} + +// FILE: k.kt + +import p.* + +fun test() { + val col = if (1 < 2) Other.subs else Other.supers + col.foo() +} + +fun Collection.foo(): T = null!! +fun listOf(t: T): List = null!! \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 0831d64801c..babe241e902 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -7742,6 +7742,11 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/platformTypes/methodCall"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("inferenceWithBound.kt") + public void testInferenceWithBound() throws Exception { + doTest("compiler/testData/diagnostics/tests/platformTypes/methodCall/inferenceWithBound.kt"); + } + @TestMetadata("int.kt") public void testInt() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/methodCall/int.kt"); 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 a68e95f5f44..cdadce5904e 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/types/CommonSupertypes.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/CommonSupertypes.java @@ -47,6 +47,28 @@ public class CommonSupertypes { @NotNull public static JetType commonSupertype(@NotNull Collection types) { + boolean hasFlexible = false; + List upper = new ArrayList(types.size()); + List lower = new ArrayList(types.size()); + for (JetType type : types) { + if (TypesPackage.isFlexible(type)) { + hasFlexible = true; + FlexibleType flexibleType = (FlexibleType) type; + upper.add(flexibleType.getUpperBound()); + lower.add(flexibleType.getLowerBound()); + } + else { + upper.add(type); + lower.add(type); + } + } + + if (!hasFlexible) return commonSuperTypeForInflexible(types); + return new DelegatingFlexibleType(commonSuperTypeForInflexible(lower), commonSuperTypeForInflexible(upper)); + } + + @NotNull + private static JetType commonSuperTypeForInflexible(@NotNull Collection types) { assert !types.isEmpty(); Collection typeSet = new HashSet(types); if (typeSet.size() == 1) return typeSet.iterator().next(); @@ -57,6 +79,7 @@ public class CommonSupertypes { for (Iterator iterator = typeSet.iterator(); iterator.hasNext();) { JetType type = iterator.next(); assert type != null; + assert !TypesPackage.isFlexible(type) : "Flexible type " + type + " passed to commonSuperTypeForInflexible"; if (KotlinBuiltIns.getInstance().isNothingOrNullableNothing(type)) { iterator.remove(); } diff --git a/core/descriptors/src/org/jetbrains/jet/lang/types/CustomTypeVariable.kt b/core/descriptors/src/org/jetbrains/jet/lang/types/CustomTypeVariable.kt index 3653fc4ff94..30e933a0e3d 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/types/CustomTypeVariable.kt +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/CustomTypeVariable.kt @@ -22,14 +22,14 @@ import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor // even if it turns out that the type an instance represents is not actually a type variable // (i.e. it is not derived from a type parameter), see isTypeVariable public trait CustomTypeVariable : JetType { - val isTypeVariable: Boolean + public val isTypeVariable: Boolean // If typeParameterDescriptor != null <=> isTypeVariable == true, this is not a type variable - val typeParameterDescriptor: TypeParameterDescriptor? + public val typeParameterDescriptor: TypeParameterDescriptor? // Throws an exception when isTypeVariable == false - fun substitutionResult(replacement: JetType): JetType + public fun substitutionResult(replacement: JetType): JetType } fun JetType.isCustomTypeVariable() = (this as? CustomTypeVariable)?.isTypeVariable ?: false \ No newline at end of file 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 1d3b221532a..2d4996ccc13 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/types/flexibleTypes.kt +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/flexibleTypes.kt @@ -17,8 +17,8 @@ package org.jetbrains.jet.lang.types public trait FlexibleType : JetType { - val lowerBound: JetType - val upperBound: JetType + public val lowerBound: JetType + public val upperBound: JetType } public fun JetType.isFlexible(): Boolean = this is FlexibleType