Fix NewKotlinTypeChecker for case SimpleType with CapturedTypeConstructor.

Usually, we have only CapturedType with such constructor.
We should prevent creation such types in the future (KT-16147).
Also added test for KT-14740 where this problem originally appears.
This commit is contained in:
Stanislav Erokhin
2017-02-03 03:13:35 +03:00
parent 7147008e21
commit cc429cd865
4 changed files with 65 additions and 5 deletions
@@ -0,0 +1,20 @@
// FILE: Foo.java
public abstract class Foo {
public interface Transformer<T, R> {}
public interface LifecycleTransformer<T> extends Transformer<T, T> {}
public class Observable<T> {
public <R> Observable<R> compose(Transformer<? super T, ? extends R> transformer) {
return null;
}
}
public final <T> LifecycleTransformer<T> bindToLifecycle() {
return null;
}
}
// FILE: 1.kt
fun <T> Foo.Observable<T>.bindTo(a: Foo): Foo.Observable<T> = compose(a.bindToLifecycle())
@@ -0,0 +1,31 @@
package
public fun </*0*/ T> Foo.Observable<T>.bindTo(/*0*/ a: Foo): Foo.Observable<T>
public abstract class Foo {
public constructor Foo()
public final fun </*0*/ T : kotlin.Any!> bindToLifecycle(): Foo.LifecycleTransformer<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
public interface LifecycleTransformer</*0*/ T : kotlin.Any!> : Foo.Transformer<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
}
public open inner class Observable</*0*/ T : kotlin.Any!> {
public constructor Observable</*0*/ T : kotlin.Any!>()
public open fun </*0*/ R : kotlin.Any!> compose(/*0*/ transformer: Foo.Transformer<in T!, out R!>!): Foo.Observable<R!>!
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 Transformer</*0*/ T : kotlin.Any!, /*1*/ R : kotlin.Any!> {
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
}
}
@@ -16111,6 +16111,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("kt14740.kt")
public void testKt14740() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/regressions/kt14740.kt");
doTest(fileName);
}
@TestMetadata("kt1489_1728.kt")
public void testKt1489_1728() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/regressions/kt1489_1728.kt");
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.resolve.calls.inference.CapturedType
import org.jetbrains.kotlin.resolve.calls.inference.CapturedTypeConstructor
import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstructor
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.TypeCheckerContext.SupertypesPolicy
@@ -101,14 +102,16 @@ object NewKotlinTypeChecker : KotlinTypeChecker {
}
fun transformToNewType(type: SimpleType): SimpleType {
if (type is CapturedType) {
val lowerType = type.typeProjection.check { it.projectionKind == Variance.IN_VARIANCE }?.type?.unwrap()
// Type itself can be just SimpleTypeImpl, not CapturedType. see KT-16147
if (type.constructor is CapturedTypeConstructor) {
val constructor = type.constructor as CapturedTypeConstructor
val lowerType = constructor.typeProjection.check { it.projectionKind == Variance.IN_VARIANCE }?.type?.unwrap()
// it is incorrect calculate this type directly because of recursive star projections
if (type.constructor.newTypeConstructor == null) {
type.constructor.newTypeConstructor = NewCapturedTypeConstructor(type.typeProjection, type.constructor.supertypes.map { it.unwrap() })
if (constructor.newTypeConstructor == null) {
constructor.newTypeConstructor = NewCapturedTypeConstructor(constructor.typeProjection, constructor.supertypes.map { it.unwrap() })
}
val newCapturedType = NewCapturedType(CaptureStatus.FOR_SUBTYPING, type.constructor.newTypeConstructor!!,
val newCapturedType = NewCapturedType(CaptureStatus.FOR_SUBTYPING, constructor.newTypeConstructor!!,
lowerType, type.annotations, type.isMarkedNullable)
return newCapturedType
}