From 40a9bb6eac98493693020e0880c1550e1fedc81b Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 18 Sep 2020 13:48:29 +0300 Subject: [PATCH] [FIR] Assume nullable types as good types for ILT approximation #KT-41982 Fixed --- .../testData/resolve/delegates/kt41982.kt | 17 ++++++++ .../testData/resolve/delegates/kt41982.txt | 40 +++++++++++++++++++ .../fir/FirDiagnosticsTestGenerated.java | 5 +++ ...DiagnosticsWithLightTreeTestGenerated.java | 5 +++ .../fir/types/ConeIntegerLiteralTypeImpl.kt | 8 ++-- .../box/annotations/annotationTargets.kt | 4 +- .../fileFacades/javaAnnotationOnFileFacade.kt | 1 - .../reflection/createAnnotation/callByJava.kt | 1 - .../createJdkAnnotationInstance.kt | 1 - .../primitivesImplicitConversions.fir.txt | 4 +- 10 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.kt create mode 100644 compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.txt diff --git a/compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.kt b/compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.kt new file mode 100644 index 00000000000..b08e94a1458 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.kt @@ -0,0 +1,17 @@ +// ISSUE: KT-41982 + +import kotlin.reflect.KProperty + +class Delegate { + operator fun getValue(thisRef: R, property: KProperty<*>): T = null!! + + operator fun setValue(thisRef: R, property: KProperty<*>, value: T) {} +} + +abstract class DelegateProvider + +fun > Type.long(initializer: (() -> Long?)? = null): Delegate = null!! + +class Test : DelegateProvider() { + var start by long { 0 } +} diff --git a/compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.txt b/compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.txt new file mode 100644 index 00000000000..311e74b6499 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.txt @@ -0,0 +1,40 @@ +FILE: kt41982.kt + public final class Delegate : R|kotlin/Any| { + public constructor(): R|Delegate| { + super() + } + + public final operator fun getValue(thisRef: R|R|, property: R|kotlin/reflect/KProperty<*>|): R|T| { + ^getValue Null(null)!! + } + + public final operator fun setValue(thisRef: R|R|, property: R|kotlin/reflect/KProperty<*>|, value: R|T|): R|kotlin/Unit| { + } + + } + public abstract class DelegateProvider : R|kotlin/Any| { + public constructor(): R|DelegateProvider| { + super() + } + + } + public final fun |> R|Type|.long(initializer: R|() -> kotlin/Long?| = Null(null)): R|Delegate| { + ^long Null(null)!! + } + public final class Test : R|DelegateProvider| { + public constructor(): R|Test| { + super|>() + } + + public final var start: R|kotlin/Long|by this@R|/Test|.R|/long|( = long@fun (): R|kotlin/Long?| { + ^ Long(0) + } + ) + public get(): R|kotlin/Long| { + ^ this@R|/Test|.D|/Test.start|.R|FakeOverride|(this@R|/Test|, ::R|/Test.start|) + } + public set(: R|kotlin/Long|): R|kotlin/Unit| { + this@R|/Test|.D|/Test.start|.R|FakeOverride|(this@R|/Test|, ::R|/Test.start|, R|/start|) + } + + } diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java index 7f6931ded25..511edbd7111 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java @@ -898,6 +898,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest { runTest("compiler/fir/analysis-tests/testData/resolve/delegates/extensionGetValueWithTypeVariableAsReceiver.kt"); } + @TestMetadata("kt41982.kt") + public void testKt41982() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.kt"); + } + @TestMetadata("provideDelegate.kt") public void testProvideDelegate() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/delegates/provideDelegate.kt"); diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java index da64e5fe21c..6f728513da8 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java @@ -898,6 +898,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolve/delegates/extensionGetValueWithTypeVariableAsReceiver.kt"); } + @TestMetadata("kt41982.kt") + public void testKt41982() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/delegates/kt41982.kt"); + } + @TestMetadata("provideDelegate.kt") public void testProvideDelegate() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/delegates/provideDelegate.kt"); diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/ConeIntegerLiteralTypeImpl.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/ConeIntegerLiteralTypeImpl.kt index 3a438afdad2..d4bf312bde6 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/ConeIntegerLiteralTypeImpl.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/ConeIntegerLiteralTypeImpl.kt @@ -64,9 +64,11 @@ class ConeIntegerLiteralTypeImpl : ConeIntegerLiteralType { } override fun getApproximatedType(expectedType: ConeKotlinType?): ConeClassLikeType { - val approximatedType = when (val expectedTypeForApproximation = expectedType?.lowerBoundIfFlexible()) { + val expectedTypeForApproximation = (expectedType?.lowerBoundIfFlexible() as? ConeClassLikeType) + ?.withNullability(ConeNullability.NOT_NULL) + val approximatedType = when (expectedTypeForApproximation) { null, !in possibleTypes -> possibleTypes.first() - else -> expectedTypeForApproximation as ConeClassLikeType + else -> expectedTypeForApproximation } return approximatedType.withNullability(nullability) } @@ -155,4 +157,4 @@ private fun ConeClassLikeType.withNullability(nullability: ConeNullability): Con is ConeClassLikeTypeImpl -> ConeClassLikeTypeImpl(lookupTag, typeArguments, nullability.isNullable) else -> error("sealed") } -} \ No newline at end of file +} diff --git a/compiler/testData/codegen/box/annotations/annotationTargets.kt b/compiler/testData/codegen/box/annotations/annotationTargets.kt index 65eaf2a613f..cbf204c1074 100644 --- a/compiler/testData/codegen/box/annotations/annotationTargets.kt +++ b/compiler/testData/codegen/box/annotations/annotationTargets.kt @@ -1,5 +1,3 @@ -// TARGET_BACKEND: JVM -// IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME // FULL_JDK @@ -20,4 +18,4 @@ fun box(): String { return targets.toList().toString() } return "OK" -} \ No newline at end of file +} diff --git a/compiler/testData/codegen/box/jvmName/fileFacades/javaAnnotationOnFileFacade.kt b/compiler/testData/codegen/box/jvmName/fileFacades/javaAnnotationOnFileFacade.kt index 5a7f299637d..34d7f33f24c 100644 --- a/compiler/testData/codegen/box/jvmName/fileFacades/javaAnnotationOnFileFacade.kt +++ b/compiler/testData/codegen/box/jvmName/fileFacades/javaAnnotationOnFileFacade.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME // FILE: StringHolder.java diff --git a/compiler/testData/codegen/box/reflection/createAnnotation/callByJava.kt b/compiler/testData/codegen/box/reflection/createAnnotation/callByJava.kt index cab0e6976a1..20cab6b3dde 100644 --- a/compiler/testData/codegen/box/reflection/createAnnotation/callByJava.kt +++ b/compiler/testData/codegen/box/reflection/createAnnotation/callByJava.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/createAnnotation/createJdkAnnotationInstance.kt b/compiler/testData/codegen/box/reflection/createAnnotation/createJdkAnnotationInstance.kt index e946480152f..13cfbf9442c 100644 --- a/compiler/testData/codegen/box/reflection/createAnnotation/createJdkAnnotationInstance.kt +++ b/compiler/testData/codegen/box/reflection/createAnnotation/createJdkAnnotationInstance.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/ir/irText/expressions/primitivesImplicitConversions.fir.txt b/compiler/testData/ir/irText/expressions/primitivesImplicitConversions.fir.txt index 37e765acb2c..f07c88dac9a 100644 --- a/compiler/testData/ir/irText/expressions/primitivesImplicitConversions.fir.txt +++ b/compiler/testData/ir/irText/expressions/primitivesImplicitConversions.fir.txt @@ -63,9 +63,9 @@ FILE fqName: fileName:/primitivesImplicitConversions.kt VAR name:test2 type:kotlin.Long [val] CONST Long type=kotlin.Long value=42 VAR name:test3 type:kotlin.Long? [val] - CONST Int type=kotlin.Int value=42 + CONST Long type=kotlin.Long value=42 VAR name:test4 type:kotlin.Long? [val] - CONST Int type=kotlin.Int value=-1 + CONST Long type=kotlin.Long value=-1 VAR name:test5 type:kotlin.Long? [val] CONST Int type=kotlin.Int value=-1 VAR name:test6 type:kotlin.Short? [val]