[NI] Approximate intersection type in type argument to star if it's necessary

#KT-32196 Fixed
This commit is contained in:
Dmitriy Novozhilov
2019-12-19 14:27:58 +03:00
parent 26f7bf1c21
commit e730965bc5
15 changed files with 288 additions and 1 deletions
@@ -9848,6 +9848,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/inference/intersectionTypeMultipleBoundsAsReceiver.kt");
}
@TestMetadata("intersectionWithEnum.kt")
public void testIntersectionWithEnum() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/intersectionWithEnum.kt");
}
@TestMetadata("invokeLambdaAsFunction.kt")
public void testInvokeLambdaAsFunction() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/invokeLambdaAsFunction.kt");
@@ -510,6 +510,24 @@ abstract class AbstractTypeApproximator(val ctx: TypeSystemInferenceExtensionCon
}
} ?: continue@loop
if (
conf.intersection != ALLOWED &&
effectiveVariance == TypeVariance.OUT &&
argumentType.typeConstructor().isIntersection()
) {
var shouldReplaceWithStar = false
for (upperBoundIndex in 0 until parameter.upperBoundCount()) {
if (!AbstractTypeChecker.isSubtypeOf(ctx, approximatedArgument, parameter.getUpperBound(upperBoundIndex))) {
shouldReplaceWithStar = true
break
}
}
if (shouldReplaceWithStar) {
newArguments[index] = createStarProjection(parameter)
continue@loop
}
}
if (parameter.getVariance() == TypeVariance.INV) {
newArguments[index] = createTypeArgument(approximatedArgument, effectiveVariance)
} else {
@@ -1,4 +1,6 @@
// !WITH_NEW_INFERENCE
// NI_EXPECTED_FILE
val test1 = { if (true) 1 else "" }
val test2 = { { if (true) 1 else "" } }
@@ -1,4 +1,6 @@
// !WITH_NEW_INFERENCE
// NI_EXPECTED_FILE
val test1 = { if (true) <!OI;IMPLICIT_CAST_TO_ANY!>1<!> else <!OI;IMPLICIT_CAST_TO_ANY!>""<!> }
val test2 = { { if (true) <!OI;IMPLICIT_CAST_TO_ANY!>1<!> else <!OI;IMPLICIT_CAST_TO_ANY!>""<!> } }
@@ -0,0 +1,6 @@
package
public val test1: () -> kotlin.Any
public val test2: () -> () -> kotlin.Any
public val test3: (kotlin.Boolean) -> kotlin.Any
public val test4: (kotlin.Boolean) -> kotlin.Any?
@@ -1,4 +1,6 @@
// !WITH_NEW_INFERENCE
// NI_EXPECTED_FILE
val test1 = { when (true) { true -> 1; else -> "" } }
val test2 = { { when (true) { true -> 1; else -> "" } } }
@@ -1,4 +1,6 @@
// !WITH_NEW_INFERENCE
// NI_EXPECTED_FILE
val test1 = { when (true) { true -> <!OI;IMPLICIT_CAST_TO_ANY!>1<!>; else -> <!OI;IMPLICIT_CAST_TO_ANY!>""<!> } }
val test2 = { { when (true) { true -> <!OI;IMPLICIT_CAST_TO_ANY!>1<!>; else -> <!OI;IMPLICIT_CAST_TO_ANY!>""<!> } } }
@@ -0,0 +1,8 @@
package
public val test1: () -> kotlin.Any
public val test2: () -> () -> kotlin.Any
public val test3: (kotlin.Boolean) -> kotlin.Any
public val test4: (kotlin.Boolean) -> kotlin.Any?
public val test5: () -> kotlin.Unit
public fun println(): kotlin.Unit
@@ -0,0 +1,26 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
// NI_EXPECTED_FILE
// ISSUE: KT-32383
class Inv<T>
class Out<out T>
class In<in T>
fun <T> invOf(vararg t: T): Inv<T> = null!!
fun <T> outOf(vararg t: T): Out<T> = null!!
fun <T> inOf(vararg t: T): In<T> = null!!
interface Foo
abstract class Bar<out TFoo : Foo>
enum class AFoo : Foo
object A : Bar<AFoo>()
enum class BFoo : Foo
object B : Bar<BFoo>()
val invs = invOf(A, B)
val outs = outOf(A, B)
val ins = inOf(A, B)
@@ -0,0 +1,26 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
// NI_EXPECTED_FILE
// ISSUE: KT-32383
class Inv<T>
class Out<out T>
class In<in T>
fun <T> invOf(vararg t: T): Inv<T> = null!!
fun <T> outOf(vararg t: T): Out<T> = null!!
fun <T> inOf(vararg t: T): In<T> = null!!
interface Foo
abstract class Bar<out TFoo : Foo>
enum class AFoo : Foo
object A : Bar<AFoo>()
enum class BFoo : Foo
object B : Bar<BFoo>()
val invs = invOf(A, B)
val outs = outOf(A, B)
val ins = inOf(A, B)
@@ -0,0 +1,90 @@
package
public val ins: In<Bar<kotlin.Nothing>>
public val invs: Inv<out Bar<*>>
public val outs: Out<Bar<*>>
public fun </*0*/ T> inOf(/*0*/ vararg t: T /*kotlin.Array<out T>*/): In<T>
public fun </*0*/ T> invOf(/*0*/ vararg t: T /*kotlin.Array<out T>*/): Inv<T>
public fun </*0*/ T> outOf(/*0*/ vararg t: T /*kotlin.Array<out T>*/): Out<T>
public object A : Bar<AFoo> {
private 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
}
public final enum class AFoo : kotlin.Enum<AFoo>, Foo {
private constructor AFoo()
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: AFoo): kotlin.Int
public final override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<AFoo!>!
public final override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): AFoo
public final /*synthesized*/ fun values(): kotlin.Array<AFoo>
}
public object B : Bar<BFoo> {
private constructor B()
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 final enum class BFoo : kotlin.Enum<BFoo>, Foo {
private constructor BFoo()
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: BFoo): kotlin.Int
public final override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<BFoo!>!
public final override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): BFoo
public final /*synthesized*/ fun values(): kotlin.Array<BFoo>
}
public abstract class Bar</*0*/ out TFoo : Foo> {
public constructor Bar</*0*/ out TFoo : Foo>()
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 Foo {
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 final class In</*0*/ in T> {
public constructor In</*0*/ in 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 final class Inv</*0*/ T> {
public constructor Inv</*0*/ 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 final class Out</*0*/ out T> {
public constructor Out</*0*/ out 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,90 @@
package
public val ins: In<Bar<*>>
public val invs: Inv<Bar<*>>
public val outs: Out<Bar<*>>
public fun </*0*/ T> inOf(/*0*/ vararg t: T /*kotlin.Array<out T>*/): In<T>
public fun </*0*/ T> invOf(/*0*/ vararg t: T /*kotlin.Array<out T>*/): Inv<T>
public fun </*0*/ T> outOf(/*0*/ vararg t: T /*kotlin.Array<out T>*/): Out<T>
public object A : Bar<AFoo> {
private 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
}
public final enum class AFoo : kotlin.Enum<AFoo>, Foo {
private constructor AFoo()
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: AFoo): kotlin.Int
public final override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<AFoo!>!
public final override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): AFoo
public final /*synthesized*/ fun values(): kotlin.Array<AFoo>
}
public object B : Bar<BFoo> {
private constructor B()
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 final enum class BFoo : kotlin.Enum<BFoo>, Foo {
private constructor BFoo()
public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: BFoo): kotlin.Int
public final override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<BFoo!>!
public final override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): BFoo
public final /*synthesized*/ fun values(): kotlin.Array<BFoo>
}
public abstract class Bar</*0*/ out TFoo : Foo> {
public constructor Bar</*0*/ out TFoo : Foo>()
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 Foo {
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 final class In</*0*/ in T> {
public constructor In</*0*/ in 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 final class Inv</*0*/ T> {
public constructor Inv</*0*/ 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 final class Out</*0*/ out T> {
public constructor Out</*0*/ out 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
}
@@ -39,7 +39,7 @@ public final class ExoticWrapper : Wrapper<Exotic> {
public final class My {
public constructor My(/*0*/ x: kotlin.Int)
public final val wrapper: Wrapper<kotlin.Any>!
public final val wrapper: Wrapper<*>!
public 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
@@ -9855,6 +9855,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/inference/intersectionTypeMultipleBoundsAsReceiver.kt");
}
@TestMetadata("intersectionWithEnum.kt")
public void testIntersectionWithEnum() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/intersectionWithEnum.kt");
}
@TestMetadata("invokeLambdaAsFunction.kt")
public void testInvokeLambdaAsFunction() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/invokeLambdaAsFunction.kt");
@@ -9850,6 +9850,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/inference/intersectionTypeMultipleBoundsAsReceiver.kt");
}
@TestMetadata("intersectionWithEnum.kt")
public void testIntersectionWithEnum() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/intersectionWithEnum.kt");
}
@TestMetadata("invokeLambdaAsFunction.kt")
public void testInvokeLambdaAsFunction() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/invokeLambdaAsFunction.kt");