Do subtyping between self types with captured type in special way

This commit is contained in:
Victor Petukhov
2021-03-24 15:30:16 +03:00
parent 7f2c5cde55
commit bbf5c4412e
14 changed files with 185 additions and 11 deletions
@@ -11765,6 +11765,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
runTest("compiler/testData/diagnostics/tests/inference/capturedInProjectedFlexibleType.kt");
}
@Test
@TestMetadata("capturedTypesInSelfType.kt")
public void testCapturedTypesInSelfType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/capturedTypesInSelfType.kt");
}
@Test
@TestMetadata("coerceFunctionLiteralToSuspend.kt")
public void testCoerceFunctionLiteralToSuspend() throws Exception {
@@ -314,8 +314,8 @@ class ConeStubType(val variable: ConeTypeVariable, override val nullability: Con
}
}
open class ConeTypeVariable(name: String) : TypeVariableMarker {
val typeConstructor = ConeTypeVariableTypeConstructor(name)
open class ConeTypeVariable(name: String, originalTypeParameter: TypeParameterMarker? = null) : TypeVariableMarker {
val typeConstructor = ConeTypeVariableTypeConstructor(name, originalTypeParameter)
val defaultType = ConeTypeVariableType(ConeNullability.NOT_NULL, typeConstructor)
override fun toString(): String {
@@ -323,7 +323,10 @@ open class ConeTypeVariable(name: String) : TypeVariableMarker {
}
}
class ConeTypeVariableTypeConstructor(val debugName: String) : ConeClassifierLookupTag(), TypeVariableTypeConstructorMarker {
class ConeTypeVariableTypeConstructor(
val debugName: String,
val originalTypeParameter: TypeParameterMarker?
) : ConeClassifierLookupTag(), TypeVariableTypeConstructorMarker {
override val name: Name get() = Name.identifier(debugName)
var isContainedInInvariantOrContravariantPositions: Boolean = false
@@ -9,4 +9,4 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol
import org.jetbrains.kotlin.fir.types.ConeTypeVariable
class ConeTypeParameterBasedTypeVariable(val typeParameterSymbol: FirTypeParameterSymbol) :
ConeTypeVariable(typeParameterSymbol.name.identifier)
ConeTypeVariable(typeParameterSymbol.name.identifier, typeParameterSymbol.toLookupTag())
@@ -47,6 +47,12 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty
return classId.isLocal
}
override val TypeVariableTypeConstructorMarker.typeParameter: TypeParameterMarker?
get() {
require(this is ConeTypeVariableTypeConstructor)
return this.originalTypeParameter
}
override fun SimpleTypeMarker.possibleIntegerTypes(): Collection<KotlinTypeMarker> {
return (this as? ConeIntegerLiteralType)?.possibleTypes ?: emptyList()
}
@@ -274,6 +280,14 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty
return this
}
override fun TypeParameterMarker.doesFormSelfType(selfConstructor: TypeConstructorMarker): Boolean {
require(this is ConeTypeParameterLookupTag)
return this.typeParameterSymbol.fir.bounds.any { typeRef ->
typeRef.coneType.contains { it.typeConstructor() == this.getTypeConstructor() }
&& typeRef.coneType.typeConstructor() == selfConstructor
}
}
override fun areEqualTypeConstructors(c1: TypeConstructorMarker, c2: TypeConstructorMarker): Boolean {
if (c1 is ErrorTypeConstructor || c2 is ErrorTypeConstructor) return false
return c1 == c2
@@ -168,6 +168,28 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
override fun TypeParameterMarker.getTypeConstructor() = this as IrTypeParameterSymbol
private fun KotlinTypeMarker.containsTypeConstructor(constructor: TypeConstructorMarker): Boolean {
if (this.typeConstructor() == constructor) return true
for (i in 0 until this.argumentsCount()) {
val typeArgument = this.getArgument(i).takeIf { !it.isStarProjection() } ?: continue
if (typeArgument.getType().containsTypeConstructor(constructor)) return true
}
return false
}
override fun TypeParameterMarker.doesFormSelfType(selfConstructor: TypeConstructorMarker): Boolean {
for (i in 0 until this.upperBoundCount()) {
val upperBound = this.getUpperBound(i)
if (upperBound.containsTypeConstructor(selfConstructor) && upperBound.typeConstructor() == selfConstructor) {
return true
}
}
return false
}
override fun areEqualTypeConstructors(c1: TypeConstructorMarker, c2: TypeConstructorMarker): Boolean =
if (c1 is IrClassifierSymbol && c2 is IrClassifierSymbol) {
FqNameEqualityChecker.areEqual(c1 , c2)
@@ -277,6 +299,9 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
return this.owner.classId?.isLocal == true
}
override val TypeVariableTypeConstructorMarker.typeParameter: TypeParameterMarker?
get() = error("Type variables is unsupported in IR")
override fun createFlexibleType(lowerBound: SimpleTypeMarker, upperBound: SimpleTypeMarker): KotlinTypeMarker {
require(lowerBound.isNothing())
require(upperBound is IrType && upperBound.isNullableAny())
@@ -20,8 +20,6 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.calls.model.CallableReferenceKotlinCallArgument
import org.jetbrains.kotlin.resolve.calls.model.LambdaKotlinCallArgument
import org.jetbrains.kotlin.resolve.calls.model.PostponableKotlinCallArgument
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.descriptorUtil.hasOnlyInputTypesAnnotation
@@ -36,7 +34,11 @@ import org.jetbrains.kotlin.types.model.TypeVariableTypeConstructorMarker
import org.jetbrains.kotlin.types.refinement.TypeRefinement
class TypeVariableTypeConstructor(private val builtIns: KotlinBuiltIns, val debugName: String) : TypeConstructor,
class TypeVariableTypeConstructor(
private val builtIns: KotlinBuiltIns,
val debugName: String,
override val originalTypeParameter: TypeParameterDescriptor?
) : TypeConstructor,
NewTypeVariableConstructor, TypeVariableTypeConstructorMarker {
override fun getParameters(): List<TypeParameterDescriptor> = emptyList()
override fun getSupertypes(): Collection<KotlinType> = emptyList()
@@ -54,8 +56,12 @@ class TypeVariableTypeConstructor(private val builtIns: KotlinBuiltIns, val debu
var isContainedInInvariantOrContravariantPositions: Boolean = false
}
sealed class NewTypeVariable(builtIns: KotlinBuiltIns, name: String) : TypeVariableMarker {
val freshTypeConstructor = TypeVariableTypeConstructor(builtIns, name)
sealed class NewTypeVariable(
builtIns: KotlinBuiltIns,
name: String,
originalTypeParameter: TypeParameterDescriptor? = null
) : TypeVariableMarker {
val freshTypeConstructor = TypeVariableTypeConstructor(builtIns, name, originalTypeParameter)
// member scope is used if we have receiver with type TypeVariable(T)
// todo add to member scope methods from supertypes for type variable
@@ -75,7 +81,7 @@ fun TypeConstructor.typeForTypeVariable(): SimpleType {
class TypeVariableFromCallableDescriptor(
val originalTypeParameter: TypeParameterDescriptor
) : NewTypeVariable(originalTypeParameter.builtIns, originalTypeParameter.name.identifier) {
) : NewTypeVariable(originalTypeParameter.builtIns, originalTypeParameter.name.identifier, originalTypeParameter) {
override fun hasOnlyInputTypesAnnotation(): Boolean = originalTypeParameter.hasOnlyInputTypesAnnotation()
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// !DIAGNOSTICS: -UNUSED_VARIABLE
class Foo<T : Enum<T>>(val values: Array<T>)
fun foo(x: Array<out Enum<*>>) {
val y = <!INAPPLICABLE_CANDIDATE!>Foo<!>(x)
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// !DIAGNOSTICS: -UNUSED_VARIABLE
class Foo<T : Enum<T>>(val values: Array<T>)
fun foo(x: Array<out Enum<*>>) {
val y = Foo(x)
}
@@ -0,0 +1,11 @@
package
public fun foo(/*0*/ x: kotlin.Array<out kotlin.Enum<*>>): kotlin.Unit
public final class Foo</*0*/ T : kotlin.Enum<T>> {
public constructor Foo</*0*/ T : kotlin.Enum<T>>(/*0*/ values: kotlin.Array<T>)
public final val values: kotlin.Array<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
}
@@ -11771,6 +11771,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/inference/capturedInProjectedFlexibleType.kt");
}
@Test
@TestMetadata("capturedTypesInSelfType.kt")
public void testCapturedTypesInSelfType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/capturedTypesInSelfType.kt");
}
@Test
@TestMetadata("coerceFunctionLiteralToSuspend.kt")
public void testCoerceFunctionLiteralToSuspend() throws Exception {