From 52145e062369bf42c542ac5c33a906765eb83267 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Tue, 16 Nov 2021 16:22:47 +0300 Subject: [PATCH] FIR: Respect nullability when substituting stub types --- ...nosisCompilerFirTestdataTestGenerated.java | 6 ++++ .../delegates/nullableTypeDelegate.fir.txt | 32 +++++++++++++++++++ .../delegates/nullableTypeDelegate.kt | 20 ++++++++++++ .../runners/FirDiagnosticTestGenerated.java | 6 ++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 ++++ .../FirDelegatedPropertyInferenceSession.kt | 2 +- 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/nullableTypeDelegate.fir.txt create mode 100644 compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/nullableTypeDelegate.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java index 935780b6698..d5e3cef98d0 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java @@ -4973,6 +4973,12 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/kt41917.kt"); } + @Test + @TestMetadata("nullableTypeDelegate.kt") + public void testNullableTypeDelegate() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/nullableTypeDelegate.kt"); + } + @Test @TestMetadata("propertyWithFunctionalType.kt") public void testPropertyWithFunctionalType() throws Exception { diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/nullableTypeDelegate.fir.txt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/nullableTypeDelegate.fir.txt new file mode 100644 index 00000000000..99338510e24 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/nullableTypeDelegate.fir.txt @@ -0,0 +1,32 @@ +FILE: nullableTypeDelegate.kt + public abstract interface ReadWriteProperty : R|kotlin/Any| { + public abstract operator fun getValue(thisRef: R|R|, prop: R|kotlin/Any|): R|T| + + public abstract operator fun setValue(thisRef: R|R|, prop: R|kotlin/Any|, value: R|T|): R|kotlin/Unit| + + } + public abstract interface Delegate : R|ReadWriteProperty| { + } + public abstract interface DatabaseEntity : R|kotlin/Any| { + } + public final fun R|Self|.directed(clazz: R|java/lang/Class|): R|Delegate| { + ^directed Null(null)!! + } + public final class MyClassSome : R|DatabaseEntity| { + public constructor(): R|MyClassSome| { + super() + } + + public final var other: R|MyClassSome?|by this@R|/MyClassSome|.R|/directed|((Q|MyClassSome|).R|kotlin/jvm/java|) + public get(): R|MyClassSome?| { + ^ this@R|/MyClassSome|.D|/MyClassSome.other|.R|SubstitutionOverride|(this@R|/MyClassSome|, ::R|/MyClassSome.other|) + } + public set(: R|MyClassSome?|): R|kotlin/Unit| { + this@R|/MyClassSome|.D|/MyClassSome.other|.R|SubstitutionOverride|(this@R|/MyClassSome|, ::R|/MyClassSome.other|, R|/other|) + } + + public final fun set(link: R|MyClassSome?|): R|kotlin/Unit| { + this@R|/MyClassSome|.R|/MyClassSome.other| = R|/link| + } + + } diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/nullableTypeDelegate.kt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/nullableTypeDelegate.kt new file mode 100644 index 00000000000..970b11d0d6e --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/nullableTypeDelegate.kt @@ -0,0 +1,20 @@ +interface ReadWriteProperty { + operator fun getValue(thisRef: R, prop: Any): T + operator fun setValue(thisRef: R, prop: Any, value: T) +} + + +interface Delegate : ReadWriteProperty + +interface DatabaseEntity + + +fun Self.directed(clazz: Class): + Delegate = null!! + +class MyClassSome : DatabaseEntity { + var other by directed(MyClassSome::class.java) + fun set(link: MyClassSome?) { + other = link + } +} diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index 039b391527f..cd1e0a4c819 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -4973,6 +4973,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/kt41917.kt"); } + @Test + @TestMetadata("nullableTypeDelegate.kt") + public void testNullableTypeDelegate() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/nullableTypeDelegate.kt"); + } + @Test @TestMetadata("propertyWithFunctionalType.kt") public void testPropertyWithFunctionalType() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index 4eb05a205fd..1b901f1795c 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -4973,6 +4973,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/kt41917.kt"); } + @Test + @TestMetadata("nullableTypeDelegate.kt") + public void testNullableTypeDelegate() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/delegates/nullableTypeDelegate.kt"); + } + @Test @TestMetadata("propertyWithFunctionalType.kt") public void testPropertyWithFunctionalType() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt index 0dc1216b47c..35170e97dfa 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt @@ -116,7 +116,7 @@ class FirDelegatedPropertyInferenceSession( return object : AbstractConeSubstitutor(typeContext) { override fun substituteType(type: ConeKotlinType): ConeKotlinType? { if (type !is ConeStubType) return null - return bindings[type.constructor] + return bindings[type.constructor].updateNullabilityIfNeeded(type) } } }