diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/Renderers.kt b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/Renderers.kt index 76a9a5f5341..258e4c58a18 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/Renderers.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/Renderers.kt @@ -489,7 +489,7 @@ object Renderers { val typeParameter = typeVariableWithCapturedConstraint.originalTypeParameter val upperBound = TypeIntersector.getUpperBoundsAsType(typeParameter) - assert(!KotlinBuiltIns.isNullableAny(upperBound) && capturedTypeConstructor.typeProjection.projectionKind == Variance.IN_VARIANCE) { + assert(!KotlinBuiltIns.isNullableAny(upperBound) && capturedTypeConstructor.projection.projectionKind == Variance.IN_VARIANCE) { "There is the only reason to report TYPE_INFERENCE_CANNOT_CAPTURE_TYPES" } @@ -504,7 +504,7 @@ object Renderers { newText().normal( typeParameter.name.wrapIntoQuotes() + " cannot capture " + - "${capturedTypeConstructor.typeProjection.toString().wrapIntoQuotes()}. " + + "${capturedTypeConstructor.projection.toString().wrapIntoQuotes()}. " + explanation ) ) diff --git a/compiler/testData/diagnostics/tests/generics/projectionsScope/starNullabilityRecursive.kt b/compiler/testData/diagnostics/tests/generics/projectionsScope/starNullabilityRecursive.kt index cebe9e5b231..7a5f0dc7fdd 100644 --- a/compiler/testData/diagnostics/tests/generics/projectionsScope/starNullabilityRecursive.kt +++ b/compiler/testData/diagnostics/tests/generics/projectionsScope/starNullabilityRecursive.kt @@ -5,5 +5,5 @@ interface A?> { fun foo(): T? } fun testA(a: A<*>) { - a.foo() checkType { _?>() } + a.foo() checkType { _?>() } } diff --git a/compiler/testData/diagnostics/tests/generics/projectionsScope/typeMismatchInLambda.kt b/compiler/testData/diagnostics/tests/generics/projectionsScope/typeMismatchInLambda.kt index a5af6b880ff..adca804ce06 100644 --- a/compiler/testData/diagnostics/tests/generics/projectionsScope/typeMismatchInLambda.kt +++ b/compiler/testData/diagnostics/tests/generics/projectionsScope/typeMismatchInLambda.kt @@ -14,14 +14,14 @@ class A { fun test(a: A, z: Out) { a.foo { val x: String = 1 // Should be no TYPE_MISMATCH_DUE_TO_TYPE_PROJECTIONS - "" + "" } a.bar { Out() } a.bar { Out() } a.bar { z.id() } a.foo { - z.foobar(if (1 > 2) return@foo "" else "") - "" + z.foobar(if (1 > 2) return@foo "" else "") + "" } } diff --git a/compiler/testData/diagnostics/tests/generics/projectionsScope/typeParameterBounds.kt b/compiler/testData/diagnostics/tests/generics/projectionsScope/typeParameterBounds.kt index 90794e1a9d5..ebe893a9bd3 100644 --- a/compiler/testData/diagnostics/tests/generics/projectionsScope/typeParameterBounds.kt +++ b/compiler/testData/diagnostics/tests/generics/projectionsScope/typeParameterBounds.kt @@ -13,15 +13,15 @@ class A { } fun foo2(a: A, b: A) { - a.foo1(Out()) - a.foo1<Out>(Out()) + a.foo1(Out()) + a.foo1<Out>(Out()) a.foo1(Out()) a.foo1(Out()) - a.foo2(Inv()) - a.foo2(Inv()) - a.foo2<Inv>(Inv()) + a.foo2(Inv()) + a.foo2(Inv()) + a.foo2<Inv>(Inv()) a.foo3(In()) a.foo3(In()) @@ -31,13 +31,13 @@ fun foo2(a: A, b: A) { b.foo1(Out()) b.foo1>(Out()) - b.foo2(Inv()) - b.foo2(Inv()) - b.foo2<Inv>(Inv()) + b.foo2(Inv()) + b.foo2(Inv()) + b.foo2<Inv>(Inv()) - b.foo3(In()) - b.foo3<In>(In()) + b.foo3(In()) + b.foo3<In>(In()) b.foo3(In()) b.foo3(In()) diff --git a/compiler/testData/diagnostics/tests/generics/starProjections/invalid.kt b/compiler/testData/diagnostics/tests/generics/starProjections/invalid.kt index 55910af194f..54377f61c05 100644 --- a/compiler/testData/diagnostics/tests/generics/starProjections/invalid.kt +++ b/compiler/testData/diagnostics/tests/generics/starProjections/invalid.kt @@ -13,9 +13,9 @@ class Inv2<T : Inv2>(val x: T) fun main(a: A<*>, j: JavaClass<*>, i2: Inv2<*>) { // Probably it's too restrictive to suppose star projection type here as Any?, // but looks like we can refine it later - a.foo() checkType { _() } - j.foo() checkType { _() } - i2.x checkType { _() } + a.foo() checkType { _() } + j.foo() checkType { _() } + i2.x checkType { _() } j.bar(1, 2, Any()) j.bar(null) diff --git a/compiler/testData/diagnostics/tests/generics/varProjection/setterNotProjectedOutAssign.kt b/compiler/testData/diagnostics/tests/generics/varProjection/setterNotProjectedOutAssign.kt index 14c58b8bce7..b72c5e14314 100644 --- a/compiler/testData/diagnostics/tests/generics/varProjection/setterNotProjectedOutAssign.kt +++ b/compiler/testData/diagnostics/tests/generics/varProjection/setterNotProjectedOutAssign.kt @@ -5,6 +5,6 @@ interface Tr { } fun test(t: Tr<*>) { - t.v = t + t.v = t t.v checkType { _>() } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/generics/varProjection/setterProjectedOutAssign.kt b/compiler/testData/diagnostics/tests/generics/varProjection/setterProjectedOutAssign.kt index 93212e0bebc..d2d740e37ec 100644 --- a/compiler/testData/diagnostics/tests/generics/varProjection/setterProjectedOutAssign.kt +++ b/compiler/testData/diagnostics/tests/generics/varProjection/setterProjectedOutAssign.kt @@ -8,7 +8,7 @@ interface Tr { fun test(t: Tr<*>) { t.v = null!! - t.v = "" - t.v = null + t.v = "" + t.v = null t.v checkType { _() } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/generics/varProjection/setterProjectedOutNoPlusAssign.kt b/compiler/testData/diagnostics/tests/generics/varProjection/setterProjectedOutNoPlusAssign.kt index 373364973a8..b2046304041 100644 --- a/compiler/testData/diagnostics/tests/generics/varProjection/setterProjectedOutNoPlusAssign.kt +++ b/compiler/testData/diagnostics/tests/generics/varProjection/setterProjectedOutNoPlusAssign.kt @@ -7,5 +7,5 @@ interface Tr { fun test(t: Tr) { // resolved as t.v = t.v + null!!, where type of right operand is String, // so TYPE_MISMATCH: String is not <: of Captured(out String) - t.v += null!! + t.v += null!! } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/kt13401.kt b/compiler/testData/diagnostics/tests/kt13401.kt index 3989d8d1d37..e9ccffb6966 100644 --- a/compiler/testData/diagnostics/tests/kt13401.kt +++ b/compiler/testData/diagnostics/tests/kt13401.kt @@ -14,5 +14,5 @@ class Owner { fun getT() = u } - fun foo(arg: Inner<*>) = arg.getT() + fun foo(arg: Inner<*>) = arg.getT() } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/kt13401.ni.txt b/compiler/testData/diagnostics/tests/kt13401.ni.txt index 0d16cd16c1c..879c71b95a9 100644 --- a/compiler/testData/diagnostics/tests/kt13401.ni.txt +++ b/compiler/testData/diagnostics/tests/kt13401.ni.txt @@ -3,7 +3,7 @@ package public final class Owner { public constructor Owner() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - public final fun foo(/*0*/ arg: Owner.Inner<*>): T + public final fun foo(/*0*/ arg: Owner.Inner<*>): kotlin.Any? public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String @@ -26,7 +26,7 @@ public interface Rec> { public interface Super { public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - public open fun foo(/*0*/ p: Rec<*>): Rec>>>> + public open fun foo(/*0*/ p: Rec<*>): Rec<*> public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } diff --git a/compiler/testData/diagnostics/tests/substitutions/starProjections.kt b/compiler/testData/diagnostics/tests/substitutions/starProjections.kt index 932d99dc1d3..d09276c0c03 100644 --- a/compiler/testData/diagnostics/tests/substitutions/starProjections.kt +++ b/compiler/testData/diagnostics/tests/substitutions/starProjections.kt @@ -17,9 +17,9 @@ interface B, T>> { } fun testB(b: B<*, *>) { - b.r().checkType { _() } - b.t().checkType { _, *>>() } + b.r().checkType { _() } + b.t().checkType { _, *>>() } - b.t().r().size + b.t().r().size } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/CapturedTypeConstructor.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/CapturedTypeConstructor.kt index 4f578fab562..c2b2bea8205 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/CapturedTypeConstructor.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/calls/inference/CapturedTypeConstructor.kt @@ -27,22 +27,26 @@ import org.jetbrains.kotlin.types.Variance.OUT_VARIANCE import org.jetbrains.kotlin.types.checker.NewCapturedTypeConstructor import org.jetbrains.kotlin.types.typeUtil.builtIns -class CapturedTypeConstructor( - val typeProjection: TypeProjection -) : TypeConstructor { +interface CapturedTypeConstructor : TypeConstructor { + val projection: TypeProjection +} + +class CapturedTypeConstructorImpl( + override val projection: TypeProjection +) : CapturedTypeConstructor { var newTypeConstructor: NewCapturedTypeConstructor? = null init { - assert(typeProjection.projectionKind != Variance.INVARIANT) { - "Only nontrivial projections can be captured, not: $typeProjection" + assert(projection.projectionKind != Variance.INVARIANT) { + "Only nontrivial projections can be captured, not: $projection" } } override fun getParameters(): List = listOf() override fun getSupertypes(): Collection { - val superType = if (typeProjection.projectionKind == Variance.OUT_VARIANCE) - typeProjection.type + val superType = if (projection.projectionKind == Variance.OUT_VARIANCE) + projection.type else builtIns.nullableAnyType return listOf(superType) @@ -54,14 +58,14 @@ class CapturedTypeConstructor( override fun getDeclarationDescriptor() = null - override fun toString() = "CapturedTypeConstructor($typeProjection)" + override fun toString() = "CapturedTypeConstructor($projection)" - override fun getBuiltIns(): KotlinBuiltIns = typeProjection.type.constructor.builtIns + override fun getBuiltIns(): KotlinBuiltIns = projection.type.constructor.builtIns } class CapturedType( val typeProjection: TypeProjection, - override val constructor: CapturedTypeConstructor = CapturedTypeConstructor(typeProjection), + override val constructor: CapturedTypeConstructor = CapturedTypeConstructorImpl(typeProjection), override val isMarkedNullable: Boolean = false, override val annotations: Annotations = Annotations.EMPTY ) : SimpleType(), SubtypingRepresentatives { diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/CapturedTypeApproximation.kt b/core/descriptors/src/org/jetbrains/kotlin/types/CapturedTypeApproximation.kt index e5517443612..8498af270b8 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/CapturedTypeApproximation.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/CapturedTypeApproximation.kt @@ -98,10 +98,10 @@ private fun substituteCapturedTypesWithProjections(typeProjection: TypeProjectio val typeSubstitutor = TypeSubstitutor.create(object : TypeConstructorSubstitution() { override fun get(key: TypeConstructor): TypeProjection? { val capturedTypeConstructor = key as? CapturedTypeConstructor ?: return null - if (capturedTypeConstructor.typeProjection.isStarProjection) { - return TypeProjectionImpl(Variance.OUT_VARIANCE, capturedTypeConstructor.typeProjection.type) + if (capturedTypeConstructor.projection.isStarProjection) { + return TypeProjectionImpl(Variance.OUT_VARIANCE, capturedTypeConstructor.projection.type) } - return capturedTypeConstructor.typeProjection + return capturedTypeConstructor.projection } }) return typeSubstitutor.substituteWithoutApproximation(typeProjection) @@ -127,7 +127,7 @@ fun approximateCapturedTypes(type: KotlinType): ApproximationBounds val typeConstructor = type.constructor if (type.isCaptured()) { - val typeProjection = (typeConstructor as CapturedTypeConstructor).typeProjection + val typeProjection = (typeConstructor as CapturedTypeConstructor).projection fun KotlinType.makeNullableIfNeeded() = TypeUtils.makeNullableIfNeeded(this, type.isMarkedNullable) val bound = typeProjection.type.makeNullableIfNeeded() diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewCapturedType.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewCapturedType.kt index 23265171942..695e293d80d 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewCapturedType.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewCapturedType.kt @@ -20,6 +20,7 @@ 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.inference.CapturedTypeConstructor import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.typeUtil.asTypeProjection @@ -145,7 +146,7 @@ class NewCapturedType( NewCapturedType(captureStatus, constructor, lowerType, annotations, newNullability) } -class NewCapturedTypeConstructor(val projection: TypeProjection, private var supertypes: List? = null) : TypeConstructor { +class NewCapturedTypeConstructor(override val projection: TypeProjection, private var supertypes: List? = null) : CapturedTypeConstructor { fun initializeSupertypes(supertypes: List) { assert(this.supertypes == null) { "Already initialized! oldValue = ${this.supertypes}, newValue = $supertypes" diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewKotlinTypeChecker.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewKotlinTypeChecker.kt index f3986ffce5a..4f31b189fc0 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewKotlinTypeChecker.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/NewKotlinTypeChecker.kt @@ -22,7 +22,7 @@ import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor import org.jetbrains.kotlin.descriptors.isFinalClass import org.jetbrains.kotlin.resolve.calls.inference.CapturedType -import org.jetbrains.kotlin.resolve.calls.inference.CapturedTypeConstructor +import org.jetbrains.kotlin.resolve.calls.inference.CapturedTypeConstructorImpl import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstructor import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.TypeCheckerContext.LowerCapturedTypePolicy.* @@ -136,12 +136,12 @@ object NewKotlinTypeChecker : KotlinTypeChecker { val constructor = type.constructor when (constructor) { // Type itself can be just SimpleTypeImpl, not CapturedType. see KT-16147 - is CapturedTypeConstructor -> { - val lowerType = constructor.typeProjection.takeIf { it.projectionKind == Variance.IN_VARIANCE }?.type?.unwrap() + is CapturedTypeConstructorImpl -> { + val lowerType = constructor.projection.takeIf { it.projectionKind == Variance.IN_VARIANCE }?.type?.unwrap() // it is incorrect calculate this type directly because of recursive star projections if (constructor.newTypeConstructor == null) { - constructor.newTypeConstructor = NewCapturedTypeConstructor(constructor.typeProjection, constructor.supertypes.map { it.unwrap() }) + constructor.newTypeConstructor = NewCapturedTypeConstructor(constructor.projection, constructor.supertypes.map { it.unwrap() }) } return NewCapturedType(CaptureStatus.FOR_SUBTYPING, constructor.newTypeConstructor!!, lowerType, type.annotations, type.isMarkedNullable)