KT-36143 Fix type approximation for type arguments in PSI2IR

This commit is contained in:
Dmitry Petrov
2020-02-17 23:02:06 +03:00
parent 2340a86d8d
commit 6d1da6e6d5
11 changed files with 120 additions and 30 deletions
@@ -1872,6 +1872,11 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest {
runTest("compiler/testData/ir/irText/types/intersectionType3_OI.kt");
}
@TestMetadata("kt36143.kt")
public void testKt36143() throws Exception {
runTest("compiler/testData/ir/irText/types/kt36143.kt");
}
@TestMetadata("localVariableOfIntersectionType_NI.kt")
public void testLocalVariableOfIntersectionType_NI() throws Exception {
runTest("compiler/testData/ir/irText/types/localVariableOfIntersectionType_NI.kt");
@@ -1892,6 +1897,11 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest {
runTest("compiler/testData/ir/irText/types/smartCastOnReceiverOfGenericType.kt");
}
@TestMetadata("starProjection_OI.kt")
public void testStarProjection_OI() throws Exception {
runTest("compiler/testData/ir/irText/types/starProjection_OI.kt");
}
@TestMetadata("compiler/testData/ir/irText/types/nullChecks")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -133,19 +133,26 @@ class TypeTranslator(
return properlyApproximatedType
}
private val isWithNewInference = languageVersionSettings.supportsFeature(LanguageFeature.NewInference)
private fun approximateByKotlinRules(ktType: KotlinType): KotlinType {
if (ktType.constructor.isDenotable) return ktType
return if (languageVersionSettings.supportsFeature(LanguageFeature.NewInference))
typeApproximatorForNI.approximateDeclarationType(
ktType,
local = false,
languageVersionSettings = languageVersionSettings
)
else
approximateCapturedTypes(ktType).upper
}
private fun approximateByKotlinRules(ktType: KotlinType): KotlinType =
if (isWithNewInference) {
if (ktType.constructor.isDenotable && ktType.arguments.isEmpty())
ktType
else
typeApproximatorForNI.approximateDeclarationType(
ktType,
local = false,
languageVersionSettings = languageVersionSettings
)
} else {
// Hack to preserve *-projections in arguments in OI.
// Expected to be removed as soon as OI is deprecated.
if (ktType.constructor.isDenotable)
ktType
else
approximateCapturedTypes(ktType).upper
}
}
+3 -3
View File
@@ -34,9 +34,9 @@ FILE fqName:<root> fileName:/intersectionType1_NI.kt
RETURN type=kotlin.Nothing from='public final fun foo <T> (a: kotlin.Array<<root>.In<T of <root>.foo>>, b: kotlin.Array<<root>.In<kotlin.String>>): kotlin.Boolean declared in <root>'
CALL 'public final fun ofType <K> (y: kotlin.Any?): kotlin.Boolean [inline] declared in <root>' type=kotlin.Boolean origin=null
<K>: kotlin.Any?
$receiver: CALL 'public final fun get (index: kotlin.Int): T of kotlin.Array [operator] declared in kotlin.Array' type=<root>.In<kotlin.Any?> origin=GET_ARRAY_ELEMENT
$this: CALL 'public final fun select <S> (x: S of <root>.select, y: S of <root>.select): S of <root>.select declared in <root>' type=kotlin.Array<out <root>.In<kotlin.Any?>> origin=null
<S>: kotlin.Array<out <root>.In<kotlin.Any?>>
$receiver: CALL 'public final fun get (index: kotlin.Int): T of kotlin.Array [operator] declared in kotlin.Array' type=<root>.In<kotlin.Nothing> origin=GET_ARRAY_ELEMENT
$this: CALL 'public final fun select <S> (x: S of <root>.select, y: S of <root>.select): S of <root>.select declared in <root>' type=kotlin.Array<out <root>.In<kotlin.Nothing>> origin=null
<S>: kotlin.Array<out <root>.In<kotlin.Nothing>>
x: GET_VAR 'a: kotlin.Array<<root>.In<T of <root>.foo>> declared in <root>.foo' type=kotlin.Array<<root>.In<T of <root>.foo>> origin=null
y: GET_VAR 'b: kotlin.Array<<root>.In<kotlin.String>> declared in <root>.foo' type=kotlin.Array<<root>.In<kotlin.String>> origin=null
index: CONST Int type=kotlin.Int value=0
+12 -12
View File
@@ -139,8 +139,8 @@ FILE fqName:<root> fileName:/intersectionType3_NI.kt
RETURN type=kotlin.Nothing from='public final fun testInIs1 (x: <root>.In<<root>.A>, y: <root>.In<<root>.B>): kotlin.Boolean declared in <root>'
CALL 'public final fun isT <T> (): kotlin.Boolean [inline] declared in <root>' type=kotlin.Boolean origin=null
<T>: kotlin.Any
$receiver: CALL 'public final fun sel <S> (x: S of <root>.sel, y: S of <root>.sel): S of <root>.sel declared in <root>' type=<root>.In<kotlin.Any> origin=null
<S>: <root>.In<kotlin.Any>
$receiver: CALL 'public final fun sel <S> (x: S of <root>.sel, y: S of <root>.sel): S of <root>.sel declared in <root>' type=<root>.In<kotlin.Nothing> origin=null
<S>: <root>.In<kotlin.Nothing>
x: GET_VAR 'x: <root>.In<<root>.A> declared in <root>.testInIs1' type=<root>.In<<root>.A> origin=null
y: GET_VAR 'y: <root>.In<<root>.B> declared in <root>.testInIs1' type=<root>.In<<root>.B> origin=null
FUN name:testInIs2 visibility:public modality:FINAL <> (x:<root>.In<<root>.Z1>, y:<root>.In<<root>.Z2>) returnType:kotlin.Boolean
@@ -150,8 +150,8 @@ FILE fqName:<root> fileName:/intersectionType3_NI.kt
RETURN type=kotlin.Nothing from='public final fun testInIs2 (x: <root>.In<<root>.Z1>, y: <root>.In<<root>.Z2>): kotlin.Boolean declared in <root>'
CALL 'public final fun isT <T> (): kotlin.Boolean [inline] declared in <root>' type=kotlin.Boolean origin=null
<T>: kotlin.Any
$receiver: CALL 'public final fun sel <S> (x: S of <root>.sel, y: S of <root>.sel): S of <root>.sel declared in <root>' type=<root>.In<kotlin.Any> origin=null
<S>: <root>.In<kotlin.Any>
$receiver: CALL 'public final fun sel <S> (x: S of <root>.sel, y: S of <root>.sel): S of <root>.sel declared in <root>' type=<root>.In<kotlin.Nothing> origin=null
<S>: <root>.In<kotlin.Nothing>
x: GET_VAR 'x: <root>.In<<root>.Z1> declared in <root>.testInIs2' type=<root>.In<<root>.Z1> origin=null
y: GET_VAR 'y: <root>.In<<root>.Z2> declared in <root>.testInIs2' type=<root>.In<<root>.Z2> origin=null
FUN name:testInIs3 visibility:public modality:FINAL <> (x:<root>.In<<root>.A1>, y:<root>.In<<root>.A2>) returnType:kotlin.Boolean
@@ -161,8 +161,8 @@ FILE fqName:<root> fileName:/intersectionType3_NI.kt
RETURN type=kotlin.Nothing from='public final fun testInIs3 (x: <root>.In<<root>.A1>, y: <root>.In<<root>.A2>): kotlin.Boolean declared in <root>'
CALL 'public final fun isT <T> (): kotlin.Boolean [inline] declared in <root>' type=kotlin.Boolean origin=null
<T>: <root>.A
$receiver: CALL 'public final fun sel <S> (x: S of <root>.sel, y: S of <root>.sel): S of <root>.sel declared in <root>' type=<root>.In<<root>.A> origin=null
<S>: <root>.In<<root>.A>
$receiver: CALL 'public final fun sel <S> (x: S of <root>.sel, y: S of <root>.sel): S of <root>.sel declared in <root>' type=<root>.In<kotlin.Nothing> origin=null
<S>: <root>.In<kotlin.Nothing>
x: GET_VAR 'x: <root>.In<<root>.A1> declared in <root>.testInIs3' type=<root>.In<<root>.A1> origin=null
y: GET_VAR 'y: <root>.In<<root>.A2> declared in <root>.testInIs3' type=<root>.In<<root>.A2> origin=null
FUN name:testInAs1 visibility:public modality:FINAL <> (x:<root>.In<<root>.A>, y:<root>.In<<root>.B>) returnType:kotlin.Unit
@@ -172,8 +172,8 @@ FILE fqName:<root> fileName:/intersectionType3_NI.kt
RETURN type=kotlin.Nothing from='public final fun testInAs1 (x: <root>.In<<root>.A>, y: <root>.In<<root>.B>): kotlin.Unit declared in <root>'
CALL 'public final fun asT <T> (): kotlin.Unit [inline] declared in <root>' type=kotlin.Unit origin=null
<T>: kotlin.Any
$receiver: CALL 'public final fun sel <S> (x: S of <root>.sel, y: S of <root>.sel): S of <root>.sel declared in <root>' type=<root>.In<kotlin.Any> origin=null
<S>: <root>.In<kotlin.Any>
$receiver: CALL 'public final fun sel <S> (x: S of <root>.sel, y: S of <root>.sel): S of <root>.sel declared in <root>' type=<root>.In<kotlin.Nothing> origin=null
<S>: <root>.In<kotlin.Nothing>
x: GET_VAR 'x: <root>.In<<root>.A> declared in <root>.testInAs1' type=<root>.In<<root>.A> origin=null
y: GET_VAR 'y: <root>.In<<root>.B> declared in <root>.testInAs1' type=<root>.In<<root>.B> origin=null
FUN name:testInAs2 visibility:public modality:FINAL <> (x:<root>.In<<root>.Z1>, y:<root>.In<<root>.Z2>) returnType:kotlin.Unit
@@ -183,8 +183,8 @@ FILE fqName:<root> fileName:/intersectionType3_NI.kt
RETURN type=kotlin.Nothing from='public final fun testInAs2 (x: <root>.In<<root>.Z1>, y: <root>.In<<root>.Z2>): kotlin.Unit declared in <root>'
CALL 'public final fun asT <T> (): kotlin.Unit [inline] declared in <root>' type=kotlin.Unit origin=null
<T>: kotlin.Any
$receiver: CALL 'public final fun sel <S> (x: S of <root>.sel, y: S of <root>.sel): S of <root>.sel declared in <root>' type=<root>.In<kotlin.Any> origin=null
<S>: <root>.In<kotlin.Any>
$receiver: CALL 'public final fun sel <S> (x: S of <root>.sel, y: S of <root>.sel): S of <root>.sel declared in <root>' type=<root>.In<kotlin.Nothing> origin=null
<S>: <root>.In<kotlin.Nothing>
x: GET_VAR 'x: <root>.In<<root>.Z1> declared in <root>.testInAs2' type=<root>.In<<root>.Z1> origin=null
y: GET_VAR 'y: <root>.In<<root>.Z2> declared in <root>.testInAs2' type=<root>.In<<root>.Z2> origin=null
FUN name:testInAs3 visibility:public modality:FINAL <> (x:<root>.In<<root>.A1>, y:<root>.In<<root>.A2>) returnType:kotlin.Unit
@@ -194,7 +194,7 @@ FILE fqName:<root> fileName:/intersectionType3_NI.kt
RETURN type=kotlin.Nothing from='public final fun testInAs3 (x: <root>.In<<root>.A1>, y: <root>.In<<root>.A2>): kotlin.Unit declared in <root>'
CALL 'public final fun asT <T> (): kotlin.Unit [inline] declared in <root>' type=kotlin.Unit origin=null
<T>: <root>.A
$receiver: CALL 'public final fun sel <S> (x: S of <root>.sel, y: S of <root>.sel): S of <root>.sel declared in <root>' type=<root>.In<<root>.A> origin=null
<S>: <root>.In<<root>.A>
$receiver: CALL 'public final fun sel <S> (x: S of <root>.sel, y: S of <root>.sel): S of <root>.sel declared in <root>' type=<root>.In<kotlin.Nothing> origin=null
<S>: <root>.In<kotlin.Nothing>
x: GET_VAR 'x: <root>.In<<root>.A1> declared in <root>.testInAs3' type=<root>.In<<root>.A1> origin=null
y: GET_VAR 'y: <root>.In<<root>.A2> declared in <root>.testInAs3' type=<root>.In<<root>.A2> origin=null
+7
View File
@@ -0,0 +1,7 @@
FILE fqName:<root> fileName:/kt36143.kt
FUN name:test visibility:public modality:FINAL <> ($receiver:kotlin.Array<out kotlin.String>) returnType:kotlin.Int
$receiver: VALUE_PARAMETER name:<this> type:kotlin.Array<out kotlin.String>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test (): kotlin.Int declared in <root>'
CALL 'public final fun <get-size> (): kotlin.Int declared in kotlin.Array' type=kotlin.Int origin=null
$this: GET_VAR '<this>: kotlin.Array<out kotlin.String> declared in <root>.test' type=kotlin.Array<out kotlin.String> origin=null
+1
View File
@@ -0,0 +1 @@
fun Array<out String>.test() = size
+7
View File
@@ -0,0 +1,7 @@
FILE fqName:<root> fileName:/kt36143.kt
FUN name:test visibility:public modality:FINAL <> ($receiver:kotlin.Array<out kotlin.String>) returnType:kotlin.Int
$receiver: VALUE_PARAMETER name:<this> type:kotlin.Array<out kotlin.String>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test (): kotlin.Int declared in <root>'
CALL 'public final fun <get-size> (): kotlin.Int declared in kotlin.Array' type=kotlin.Int origin=GET_PROPERTY
$this: GET_VAR '<this>: kotlin.Array<out kotlin.String> declared in <root>.test' type=kotlin.Array<out kotlin.String> origin=null
@@ -96,21 +96,21 @@ FILE fqName:<root> fileName:/localVariableOfIntersectionType_NI.kt
BLOCK_BODY
CALL 'public abstract fun foo (): kotlin.Unit declared in <root>.IA' type=kotlin.Unit origin=null
$this: CALL 'public abstract fun <get-t> (): T of <root>.Inv declared in <root>.Inv' type=kotlin.Any origin=GET_PROPERTY
$this: CALL 'public abstract fun create <T> (x: <root>.In<T of <root>.Z.create>, y: <root>.In<T of <root>.Z.create>): <root>.Inv<T of <root>.Z.create> declared in <root>.Z' type=<root>.Inv<kotlin.Any> origin=null
$this: CALL 'public abstract fun create <T> (x: <root>.In<T of <root>.Z.create>, y: <root>.In<T of <root>.Z.create>): <root>.Inv<T of <root>.Z.create> declared in <root>.Z' type=<root>.Inv<out kotlin.Any> origin=null
<T>: kotlin.Any
$this: GET_VAR 'z: <root>.Z declared in <root>.test' type=<root>.Z origin=null
x: GET_VAR 'a: <root>.In<<root>.IA> declared in <root>.test' type=<root>.In<<root>.IA> origin=null
y: GET_VAR 'b: <root>.In<<root>.IB> declared in <root>.test' type=<root>.In<<root>.IB> origin=null
CALL 'public abstract fun bar (): kotlin.Unit declared in <root>.IB' type=kotlin.Unit origin=null
$this: CALL 'public abstract fun <get-t> (): T of <root>.Inv declared in <root>.Inv' type=kotlin.Any origin=GET_PROPERTY
$this: CALL 'public abstract fun create <T> (x: <root>.In<T of <root>.Z.create>, y: <root>.In<T of <root>.Z.create>): <root>.Inv<T of <root>.Z.create> declared in <root>.Z' type=<root>.Inv<kotlin.Any> origin=null
$this: CALL 'public abstract fun create <T> (x: <root>.In<T of <root>.Z.create>, y: <root>.In<T of <root>.Z.create>): <root>.Inv<T of <root>.Z.create> declared in <root>.Z' type=<root>.Inv<out kotlin.Any> origin=null
<T>: kotlin.Any
$this: GET_VAR 'z: <root>.Z declared in <root>.test' type=<root>.Z origin=null
x: GET_VAR 'a: <root>.In<<root>.IA> declared in <root>.test' type=<root>.In<<root>.IA> origin=null
y: GET_VAR 'b: <root>.In<<root>.IB> declared in <root>.test' type=<root>.In<<root>.IB> origin=null
VAR name:t type:kotlin.Any [val]
CALL 'public abstract fun <get-t> (): T of <root>.Inv declared in <root>.Inv' type=kotlin.Any origin=GET_PROPERTY
$this: CALL 'public abstract fun create <T> (x: <root>.In<T of <root>.Z.create>, y: <root>.In<T of <root>.Z.create>): <root>.Inv<T of <root>.Z.create> declared in <root>.Z' type=<root>.Inv<kotlin.Any> origin=null
$this: CALL 'public abstract fun create <T> (x: <root>.In<T of <root>.Z.create>, y: <root>.In<T of <root>.Z.create>): <root>.Inv<T of <root>.Z.create> declared in <root>.Z' type=<root>.Inv<out kotlin.Any> origin=null
<T>: kotlin.Any
$this: GET_VAR 'z: <root>.Z declared in <root>.test' type=<root>.Z origin=null
x: GET_VAR 'a: <root>.In<<root>.IA> declared in <root>.test' type=<root>.In<<root>.IA> origin=null
@@ -0,0 +1,8 @@
// !LANGUAGE: -NewInference
// FIR_IDENTICAL
interface Continuation<in T>
abstract class C {
abstract fun dispatchResumeWithException(exception: Throwable, continuation: Continuation<*>): Boolean
}
+40
View File
@@ -0,0 +1,40 @@
FILE fqName:<root> fileName:/starProjection_OI.kt
CLASS INTERFACE name:Continuation modality:ABSTRACT visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Continuation<T of <root>.Continuation>
TYPE_PARAMETER name:T index:0 variance:in superTypes:[kotlin.Any?]
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
CLASS CLASS name:C modality:ABSTRACT visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.C
CONSTRUCTOR visibility:public <> () returnType:<root>.C [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:C modality:ABSTRACT visibility:public superTypes:[kotlin.Any]'
FUN name:dispatchResumeWithException visibility:public modality:ABSTRACT <> ($this:<root>.C, exception:kotlin.Throwable, continuation:<root>.Continuation<*>) returnType:kotlin.Boolean
$this: VALUE_PARAMETER name:<this> type:<root>.C
VALUE_PARAMETER name:exception index:0 type:kotlin.Throwable
VALUE_PARAMETER name:continuation index:1 type:<root>.Continuation<*>
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
@@ -1871,6 +1871,11 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
runTest("compiler/testData/ir/irText/types/intersectionType3_OI.kt");
}
@TestMetadata("kt36143.kt")
public void testKt36143() throws Exception {
runTest("compiler/testData/ir/irText/types/kt36143.kt");
}
@TestMetadata("localVariableOfIntersectionType_NI.kt")
public void testLocalVariableOfIntersectionType_NI() throws Exception {
runTest("compiler/testData/ir/irText/types/localVariableOfIntersectionType_NI.kt");
@@ -1891,6 +1896,11 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
runTest("compiler/testData/ir/irText/types/smartCastOnReceiverOfGenericType.kt");
}
@TestMetadata("starProjection_OI.kt")
public void testStarProjection_OI() throws Exception {
runTest("compiler/testData/ir/irText/types/starProjection_OI.kt");
}
@TestMetadata("compiler/testData/ir/irText/types/nullChecks")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)