Type<*> is inferred now if Type<A> and Type<B> common supertype is Type<X> and X is not within parameter upper bound. #KT-7585 Fixed. #EA-68943 Fixed.

It provides also a fix for KT-7585 (empty type intersection assertion).
A set of relevant tests, one fixed test
This commit is contained in:
Mikhail Glukhikh
2015-04-27 11:27:57 +03:00
parent c90bcb41f3
commit 754f8af3fc
12 changed files with 418 additions and 2 deletions
@@ -1,6 +1,6 @@
package
internal val x: kotlin.Enum<out kotlin.Enum<out kotlin.Enum<out kotlin.Enum<out kotlin.Enum<out kotlin.Any?>>>>>
internal val x: kotlin.Enum<*>
internal final enum class A : kotlin.Enum<A> {
enum entry A
@@ -0,0 +1,20 @@
open class A
class E
abstract class Wrapper<T: A>(protected val t: T)
class MyWrapper(a: A): Wrapper<A>(a)
// This wrapper is not legal
class TheirWrapper(e: E): Wrapper<<!UPPER_BOUND_VIOLATED, UPPER_BOUND_VIOLATED!>E<!>>(e)
data class Pair<out T>(val a: T, val b: T)
fun foo(): String {
val matrix: Pair<Wrapper<*>>
// It's not legal to do such a thing because E is not derived from A
// But we should not have assertion errors because of it!
matrix = Pair(MyWrapper(A()), TheirWrapper(E()))
return matrix.toString()
}
@@ -0,0 +1,53 @@
package
internal fun foo(): kotlin.String
internal open class A {
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
}
internal final class E {
public constructor E()
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 MyWrapper : Wrapper<A> {
public constructor MyWrapper(/*0*/ a: A)
protected final override /*1*/ /*fake_override*/ val t: 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
}
kotlin.data() internal final class Pair</*0*/ out T> {
public constructor Pair</*0*/ out T>(/*0*/ a: T, /*1*/ b: T)
internal final val a: T
internal final val b: T
internal final /*synthesized*/ fun component1(): T
internal final /*synthesized*/ fun component2(): T
public final /*synthesized*/ fun copy(/*0*/ a: T = ..., /*1*/ b: T = ...): Pair<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 final class TheirWrapper : Wrapper<E> {
public constructor TheirWrapper(/*0*/ e: E)
protected final override /*1*/ /*fake_override*/ val t: E
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 abstract class Wrapper</*0*/ T : A> {
public constructor Wrapper</*0*/ T : A>(/*0*/ t: T)
protected final val 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
}
@@ -0,0 +1,30 @@
// FILE: A.java
class A {}
// FILE: Wrapper.java
abstract class Wrapper<T extends A> {
protected T t;
Wrapper(T t) { this.t = t; }
}
// FILE: kt7585.kt
class E
class MyWrapper(a: A): Wrapper<A>(a)
// This wrapper is not legal
class TheirWrapper(e: E): Wrapper<<!UPPER_BOUND_VIOLATED, UPPER_BOUND_VIOLATED!>E<!>>(e)
data class Pair<out T>(val a: T, val b: T)
fun foo(): String {
val matrix: Pair<Wrapper<*>>
// It's not legal to do such a thing because E is not derived from A
// But we should not have assertion errors because of it!
matrix = Pair(MyWrapper(A()), TheirWrapper(E()))
return matrix.toString()
}
@@ -0,0 +1,53 @@
package
internal fun foo(): kotlin.String
public/*package*/ open class A {
public/*package*/ 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
}
internal final class E {
public constructor E()
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 MyWrapper : Wrapper<A> {
public constructor MyWrapper(/*0*/ a: A)
protected/*protected and package*/ final override /*1*/ /*fake_override*/ var t: 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
}
kotlin.data() internal final class Pair</*0*/ out T> {
public constructor Pair</*0*/ out T>(/*0*/ a: T, /*1*/ b: T)
internal final val a: T
internal final val b: T
internal final /*synthesized*/ fun component1(): T
internal final /*synthesized*/ fun component2(): T
public final /*synthesized*/ fun copy(/*0*/ a: T = ..., /*1*/ b: T = ...): Pair<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 final class TheirWrapper : Wrapper<E> {
public constructor TheirWrapper(/*0*/ e: E)
protected/*protected and package*/ final override /*1*/ /*fake_override*/ var t: E!
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/*package*/ abstract class Wrapper</*0*/ T : A!> {
public/*package*/ constructor Wrapper</*0*/ T : A!>(/*0*/ t: T!)
protected/*protected and package*/ final var 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
}
@@ -0,0 +1,16 @@
interface X {
fun foo(): Int = 42
}
interface Y
class A: X, Y
class B: X, Y
class Out<out T: X>(val x: T)
fun bar(a: Out<A>, b: Out<B>, f: Boolean): Int {
val x = if (f) a else b
return x.x.foo()
}
@@ -0,0 +1,40 @@
package
internal fun bar(/*0*/ a: Out<A>, /*1*/ b: Out<B>, /*2*/ f: kotlin.Boolean): kotlin.Int
internal final class A : X, Y {
public constructor A()
public open override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
internal open override /*1*/ /*fake_override*/ fun foo(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
}
internal final class B : X, Y {
public constructor B()
public open override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
internal open override /*1*/ /*fake_override*/ fun foo(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
}
internal final class Out</*0*/ out T : X> {
public constructor Out</*0*/ out T : X>(/*0*/ x: T)
internal final val x: 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 interface X {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
internal open fun foo(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
internal interface Y {
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
}
@@ -0,0 +1,73 @@
// FILE: Base.java
interface Base {}
// FILE: Other.java
interface Other {}
// FILE: Derived.java
final class Derived<T> implements Base, Other {}
// FILE: Exotic.java
final class Exotic implements Base, Other {
int x;
Exotic(int x) {
this.x = x;
}
}
// FILE: Properties.java
import kotlin.jvm.functions.Function0;
class Val<T> {
Function0<T> initializer;
Val(Function0<T> initializer) {
this.initializer = initializer;
}
T get(Object instance, Object metadata) {
return initializer.invoke();
}
}
class Properties {
static <T> Val<T> calcVal(Function0<T> initializer) {
return new Val<T>(initializer);
}
}
// FILE: My.kt
open class Wrapper<out T: Base>(val v: T)
class DerivedWrapper(v: Derived<*>): Wrapper<Derived<*>>(v)
class ExoticWrapper(v: Exotic): Wrapper<Exotic>(v)
object MyBase {
fun derived() = Derived<String>()
fun exotic(x: Int) = Exotic(x)
fun derivedWrapper() = DerivedWrapper(derived())
fun exoticWrapper(x: Int) = ExoticWrapper(exotic(x))
}
class My(val x: Int) {
val wrapper/*: Wrapper<*>*/ by Properties.calcVal {
val y = x + 1
when {
y > 0 -> MyBase.derivedWrapper()
x < 0 -> MyBase.exoticWrapper(x)
else -> throw java.lang.NullPointerException("")
}
}
}
@@ -0,0 +1,82 @@
package
public/*package*/ interface Base {
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/*package*/ final class Derived</*0*/ T : kotlin.Any!> : Base, Other {
public/*package*/ constructor Derived</*0*/ T : kotlin.Any!>()
public open override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
}
internal final class DerivedWrapper : Wrapper<Derived<*>> {
public constructor DerivedWrapper(/*0*/ v: Derived<*>)
internal final override /*1*/ /*fake_override*/ val v: Derived<*>
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/*package*/ final class Exotic : Base, Other {
public/*package*/ constructor Exotic(/*0*/ x: kotlin.Int)
public/*package*/ final var x: kotlin.Int
public open override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
}
internal final class ExoticWrapper : Wrapper<Exotic> {
public constructor ExoticWrapper(/*0*/ v: Exotic)
internal final override /*1*/ /*fake_override*/ val v: Exotic
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 My {
public constructor My(/*0*/ x: kotlin.Int)
internal final val wrapper: Wrapper<*>!
internal final val x: kotlin.Int
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 object MyBase {
private constructor MyBase()
internal final fun derived(): Derived<kotlin.String>
internal final fun derivedWrapper(): DerivedWrapper
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
internal final fun exotic(/*0*/ x: kotlin.Int): Exotic
internal final fun exoticWrapper(/*0*/ x: kotlin.Int): ExoticWrapper
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public/*package*/ interface Other {
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/*package*/ open class Properties {
public/*package*/ constructor Properties()
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
// Static members
public/*package*/ open fun </*0*/ T : kotlin.Any!> calcVal(/*0*/ initializer: (() -> T!)!): Val<T!>!
}
internal open class Wrapper</*0*/ out T : Base> {
public constructor Wrapper</*0*/ out T : Base>(/*0*/ v: T)
internal final val v: 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
}
@@ -11711,6 +11711,33 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/regressions/WrongTraceInCallResolver.kt");
doTest(fileName);
}
@TestMetadata("compiler/testData/diagnostics/tests/regressions/kt7585")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Kt7585 extends AbstractJetDiagnosticsTest {
public void testAllFilesPresentInKt7585() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/regressions/kt7585"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("base.kt")
public void testBase() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/regressions/kt7585/base.kt");
doTest(fileName);
}
@TestMetadata("java.kt")
public void testJava() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/regressions/kt7585/java.kt");
doTest(fileName);
}
@TestMetadata("twoparents.kt")
public void testTwoparents() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/regressions/kt7585/twoparents.kt");
doTest(fileName);
}
}
}
@TestMetadata("compiler/testData/diagnostics/tests/resolve")
@@ -590,6 +590,21 @@ public class JetDiagnosticsTestWithStdLibGenerated extends AbstractJetDiagnostic
}
}
@TestMetadata("compiler/testData/diagnostics/testsWithStdLib/kt7585")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Kt7585 extends AbstractJetDiagnosticsTestWithStdLib {
public void testAllFilesPresentInKt7585() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/kt7585"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("delegate.kt")
public void testDelegate() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/kt7585/delegate.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/diagnostics/testsWithStdLib/native")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.resolve.scopes.JetScope;
import org.jetbrains.kotlin.types.checker.JetTypeChecker;
import org.jetbrains.kotlin.types.typeUtil.TypeUtilPackage;
import java.util.*;
@@ -297,7 +298,13 @@ public class CommonSupertypes {
if (outs != null) {
Variance projectionKind = variance == OUT_VARIANCE ? Variance.INVARIANT : OUT_VARIANCE;
return new TypeProjectionImpl(projectionKind, findCommonSupertype(outs, recursionDepth + 1, maxDepth));
JetType superType = findCommonSupertype(outs, recursionDepth + 1, maxDepth);
for (JetType upperBound: parameterDescriptor.getUpperBounds()) {
if (!TypeUtilPackage.isSubtypeOf(superType, upperBound)) {
return new StarProjectionImpl(parameterDescriptor);
}
}
return new TypeProjectionImpl(projectionKind, superType);
}
if (ins != null) {
JetType intersection = TypeIntersector.intersectTypes(getBuiltIns(parameterDescriptor), JetTypeChecker.DEFAULT, ins);