From 30002e29d15eeb3227fe74e25cb5a575db0109ae Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Mon, 7 Nov 2022 13:13:55 +0100 Subject: [PATCH] [KAPT+JVM_IR] Do not generate initializers for delegated properties. ^KT-54870 Fixed --- .../generators/DelegatedPropertyGenerator.kt | 38 ++++++-- .../testData/converter/anonymousDelegate.kt | 2 - .../converter/anonymousDelegate_ir.txt | 86 +++++++++++++++++++ .../testData/converter/delegatedProperties.kt | 14 +++ .../converter/delegatedProperties.txt | 71 +++++++++++++++ .../converter/delegatedProperties_ir.txt | 71 +++++++++++++++ .../testData/converter/incorrectDelegate.kt | 2 - .../converter/incorrectDelegate_ir.txt | 56 ++++++++++++ ...ileToSourceStubConverterTestGenerated.java | 6 ++ ...ileToSourceStubConverterTestGenerated.java | 6 ++ 10 files changed, 342 insertions(+), 10 deletions(-) create mode 100644 plugins/kapt3/kapt3-compiler/testData/converter/anonymousDelegate_ir.txt create mode 100644 plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties.kt create mode 100644 plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties.txt create mode 100644 plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties_ir.txt create mode 100644 plugins/kapt3/kapt3-compiler/testData/converter/incorrectDelegate_ir.txt diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/DelegatedPropertyGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/DelegatedPropertyGenerator.kt index b2c95585f4b..15d8d517332 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/DelegatedPropertyGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/DelegatedPropertyGenerator.kt @@ -35,6 +35,7 @@ import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol import org.jetbrains.kotlin.ir.util.declareSimpleFunctionWithOverrides import org.jetbrains.kotlin.psi.KtElement +import org.jetbrains.kotlin.psi.KtObjectLiteralExpression import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.psi.KtPropertyDelegate import org.jetbrains.kotlin.psi.psiUtil.endOffset @@ -42,6 +43,7 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffsetSkippingComments import org.jetbrains.kotlin.psi2ir.intermediate.* import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections class DelegatedPropertyGenerator(declarationGenerator: DeclarationGenerator) : DeclarationGeneratorExtension(declarationGenerator) { constructor(context: GeneratorContext) : this(DeclarationGenerator(context)) @@ -143,10 +145,12 @@ class DelegatedPropertyGenerator(declarationGenerator: DeclarationGenerator) : D metadata = DescriptorMetadataSource.Property(propertyDescriptor) } }.also { irDelegate -> - irDelegate.initializer = generateInitializerBodyForPropertyDelegate( - propertyDescriptor, kPropertyType, ktDelegate, - irDelegate.symbol - ) + if (context.configuration.generateBodies) { + irDelegate.initializer = generateInitializerBodyForPropertyDelegate( + propertyDescriptor, kPropertyType, ktDelegate, + irDelegate.symbol + ) + } } } } @@ -315,10 +319,32 @@ class DelegatedPropertyGenerator(declarationGenerator: DeclarationGenerator) : D ktDelegate: KtPropertyDelegate ): KotlinType { val provideDelegateResolvedCall = get(BindingContext.PROVIDE_DELEGATE_RESOLVED_CALL, delegatedPropertyDescriptor) - return if (provideDelegateResolvedCall != null) + val delegateType = if (provideDelegateResolvedCall != null) { provideDelegateResolvedCall.resultingDescriptor.returnType!! - else + } else { getTypeInferredByFrontendOrFail(ktDelegate.expression!!) + } + // For KAPT stub generation, we approximate the type of the delegate. + // Since we are not generating bodies, we could end up with unbound + // symbols in cases such as: + // + // val x by object: Serializable {} + // + // val x by lazy { object: Serializable {} } + // + // For the first case, we approximate to the super type for delegation + // to anonymous objects. + // + // For the second case, we erase type parameters from types that are + // not anonymous objects. This avoids using anonymous object types, + // and the types will be raw in the output anyway. + return if (context.configuration.generateBodies) { + delegateType + } else if (ktDelegate.expression is KtObjectLiteralExpression) { + delegateType.constructor.supertypes.first() + } else { + delegateType.replaceArgumentsWithStarProjections() + } } private fun generateInitializerForLocalDelegatedPropertyDelegate( diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/anonymousDelegate.kt b/plugins/kapt3/kapt3-compiler/testData/converter/anonymousDelegate.kt index 84450e258fb..344039696dd 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/anonymousDelegate.kt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/anonymousDelegate.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // CORRECT_ERROR_TYPES import kotlin.reflect.KProperty @@ -33,7 +32,6 @@ class Test { } val concreteDelegate: Int by ConcreteDelegate() - } var delegate by object { diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/anonymousDelegate_ir.txt b/plugins/kapt3/kapt3-compiler/testData/converter/anonymousDelegate_ir.txt new file mode 100644 index 00000000000..1da85d6ff27 --- /dev/null +++ b/plugins/kapt3/kapt3-compiler/testData/converter/anonymousDelegate_ir.txt @@ -0,0 +1,86 @@ +import kotlin.reflect.KProperty; + +@kotlin.Metadata() +public final class AnonymousDelegateKt { + + public AnonymousDelegateKt() { + super(); + } + @org.jetbrains.annotations.NotNull() + private static final java.lang.Object delegate$delegate = null; + + @org.jetbrains.annotations.NotNull() + public static final java.lang.Object getDelegate() { + return null; + } + + public static final void setDelegate(@org.jetbrains.annotations.NotNull() + java.lang.Object p0) { + } +} + +//////////////////// + + +import kotlin.reflect.KProperty; + +@kotlin.Metadata() +public final class ConcreteDelegate { + + public ConcreteDelegate() { + super(); + } + + public final int getValue(@org.jetbrains.annotations.Nullable() + java.lang.Object t, @org.jetbrains.annotations.NotNull() + kotlin.reflect.KProperty p) { + return 0; + } +} + +//////////////////// + + +import kotlin.reflect.KProperty; + +@kotlin.Metadata() +public final class Test { + @org.jetbrains.annotations.NotNull() + private final java.lang.Object broken$delegate = null; + @org.jetbrains.annotations.NotNull() + private final java.io.Serializable overridden$delegate = null; + @org.jetbrains.annotations.NotNull() + private final kotlin.Lazy lazyProp$delegate = null; + @org.jetbrains.annotations.NotNull() + private final ConcreteDelegate concreteDelegate$delegate = null; + + public Test() { + super(); + } + + @org.jetbrains.annotations.NotNull() + public final java.lang.Object getBroken() { + return null; + } + + public final void setBroken(@org.jetbrains.annotations.NotNull() + java.lang.Object p0) { + } + + @org.jetbrains.annotations.NotNull() + public final java.lang.Object getOverridden() { + return null; + } + + public final void setOverridden(@org.jetbrains.annotations.NotNull() + java.lang.Object p0) { + } + + private final java.lang.Runnable getLazyProp() { + return null; + } + + public final int getConcreteDelegate() { + return 0; + } +} diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties.kt b/plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties.kt new file mode 100644 index 00000000000..09ed876fdd8 --- /dev/null +++ b/plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties.kt @@ -0,0 +1,14 @@ +package test + +open class C(v: T) { + operator fun getValue(p1: Any?, p2: Any?): T = v +} + +class A { + @Suppress("UNRESOLVED_REFERENCE") + val x by lazy { Unresolved } + val z by C("z") + val y by object: C("y") {} + val a by lazy { C("a") } + val b by lazy { object: C("b") {} } +} diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties.txt b/plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties.txt new file mode 100644 index 00000000000..8872a27a94d --- /dev/null +++ b/plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties.txt @@ -0,0 +1,71 @@ +package test; + +import java.lang.System; + +@kotlin.Metadata() +public final class A { + @org.jetbrains.annotations.NotNull() + private final kotlin.Lazy x$delegate = null; + @org.jetbrains.annotations.NotNull() + private final test.C z$delegate = null; + @org.jetbrains.annotations.NotNull() + private final test.C y$delegate = null; + @org.jetbrains.annotations.NotNull() + private final kotlin.Lazy a$delegate = null; + @org.jetbrains.annotations.NotNull() + private final kotlin.Lazy b$delegate = null; + + public A() { + super(); + } + + @org.jetbrains.annotations.NotNull() + public final kotlin.Unit getX() { + return null; + } + + @kotlin.Suppress(names = {"UNRESOLVED_REFERENCE"}) + @java.lang.Deprecated() + public static void getX$annotations() { + } + + @org.jetbrains.annotations.NotNull() + public final java.lang.String getZ() { + return null; + } + + @org.jetbrains.annotations.NotNull() + public final java.lang.String getY() { + return null; + } + + @org.jetbrains.annotations.NotNull() + public final test.C getA() { + return null; + } + + @org.jetbrains.annotations.NotNull() + public final test.C getB() { + return null; + } +} + +//////////////////// + +package test; + +import java.lang.System; + +@kotlin.Metadata() +public class C { + + public C(T v) { + super(); + } + + public final T getValue(@org.jetbrains.annotations.Nullable() + java.lang.Object p1, @org.jetbrains.annotations.Nullable() + java.lang.Object p2) { + return null; + } +} diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties_ir.txt b/plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties_ir.txt new file mode 100644 index 00000000000..2d7bb92a0b8 --- /dev/null +++ b/plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties_ir.txt @@ -0,0 +1,71 @@ +package test; + +import java.lang.System; + +@kotlin.Metadata() +public final class A { + @org.jetbrains.annotations.NotNull() + private final kotlin.Lazy x$delegate = null; + @org.jetbrains.annotations.NotNull() + private final test.C z$delegate = null; + @org.jetbrains.annotations.NotNull() + private final test.C y$delegate = null; + @org.jetbrains.annotations.NotNull() + private final kotlin.Lazy a$delegate = null; + @org.jetbrains.annotations.NotNull() + private final kotlin.Lazy b$delegate = null; + + public A() { + super(); + } + + @org.jetbrains.annotations.NotNull() + public final kotlin.Unit getX() { + return null; + } + + @org.jetbrains.annotations.NotNull() + public final java.lang.String getZ() { + return null; + } + + @org.jetbrains.annotations.NotNull() + public final java.lang.String getY() { + return null; + } + + @org.jetbrains.annotations.NotNull() + public final test.C getA() { + return null; + } + + @org.jetbrains.annotations.NotNull() + public final test.C getB() { + return null; + } + + @kotlin.Suppress(names = {"UNRESOLVED_REFERENCE"}) + @java.lang.Deprecated() + public static void getX$annotations() { + } +} + +//////////////////// + +package test; + +import java.lang.System; + +@kotlin.Metadata() +public class C { + + public C(T v) { + super(); + } + + public final T getValue(@org.jetbrains.annotations.Nullable() + java.lang.Object p1, @org.jetbrains.annotations.Nullable() + java.lang.Object p2) { + return null; + } +} diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/incorrectDelegate.kt b/plugins/kapt3/kapt3-compiler/testData/converter/incorrectDelegate.kt index 362358dcf23..21948c12a71 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/incorrectDelegate.kt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/incorrectDelegate.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: JVM_IR - class HomeFragment { @Suppress("TOO_MANY_ARGUMENTS", "DELEGATE_SPECIAL_FUNCTION_MISSING") private val categoryNewsListPresenter by moxyPresenter { diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/incorrectDelegate_ir.txt b/plugins/kapt3/kapt3-compiler/testData/converter/incorrectDelegate_ir.txt new file mode 100644 index 00000000000..7c164e632c2 --- /dev/null +++ b/plugins/kapt3/kapt3-compiler/testData/converter/incorrectDelegate_ir.txt @@ -0,0 +1,56 @@ +import java.lang.System; + +@kotlin.Metadata() +public final class GroupedNewsListDelegateAdapter { + + public GroupedNewsListDelegateAdapter(@org.jetbrains.annotations.NotNull() + kotlin.jvm.functions.Function0 onWiFiClickListener) { + super(); + } +} + +//////////////////// + + +import java.lang.System; + +@kotlin.Metadata() +public final class HomeFragment { + @org.jetbrains.annotations.NotNull() + private final kotlin.Unit categoryNewsListPresenter$delegate = null; + @org.jetbrains.annotations.NotNull() + private final kotlin.Lazy groupedNewsListAdapter$delegate = null; + + public HomeFragment() { + super(); + } + + private final error.NonExistentClass getCategoryNewsListPresenter() { + return null; + } + + private final GroupedNewsListDelegateAdapter getGroupedNewsListAdapter() { + return null; + } + + @kotlin.Suppress(names = {"TOO_MANY_ARGUMENTS", "DELEGATE_SPECIAL_FUNCTION_MISSING"}) + @java.lang.Deprecated() + private static void getCategoryNewsListPresenter$annotations() { + } +} + +//////////////////// + + +import java.lang.System; + +@kotlin.Metadata() +public final class IncorrectDelegateKt { + + public IncorrectDelegateKt() { + super(); + } + + public static final void moxyPresenter() { + } +} diff --git a/plugins/kapt3/kapt3-compiler/tests-gen/org/jetbrains/kotlin/kapt3/test/runners/ClassFileToSourceStubConverterTestGenerated.java b/plugins/kapt3/kapt3-compiler/tests-gen/org/jetbrains/kotlin/kapt3/test/runners/ClassFileToSourceStubConverterTestGenerated.java index a427250a6eb..018987d64bb 100644 --- a/plugins/kapt3/kapt3-compiler/tests-gen/org/jetbrains/kotlin/kapt3/test/runners/ClassFileToSourceStubConverterTestGenerated.java +++ b/plugins/kapt3/kapt3-compiler/tests-gen/org/jetbrains/kotlin/kapt3/test/runners/ClassFileToSourceStubConverterTestGenerated.java @@ -151,6 +151,12 @@ public class ClassFileToSourceStubConverterTestGenerated extends AbstractClassFi runTest("plugins/kapt3/kapt3-compiler/testData/converter/delegateCorrectErrorTypes.kt"); } + @Test + @TestMetadata("delegatedProperties.kt") + public void testDelegatedProperties() throws Exception { + runTest("plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties.kt"); + } + @Test @TestMetadata("deprecated.kt") public void testDeprecated() throws Exception { diff --git a/plugins/kapt3/kapt3-compiler/tests-gen/org/jetbrains/kotlin/kapt3/test/runners/IrClassFileToSourceStubConverterTestGenerated.java b/plugins/kapt3/kapt3-compiler/tests-gen/org/jetbrains/kotlin/kapt3/test/runners/IrClassFileToSourceStubConverterTestGenerated.java index 642ee43361f..72ea17d7064 100644 --- a/plugins/kapt3/kapt3-compiler/tests-gen/org/jetbrains/kotlin/kapt3/test/runners/IrClassFileToSourceStubConverterTestGenerated.java +++ b/plugins/kapt3/kapt3-compiler/tests-gen/org/jetbrains/kotlin/kapt3/test/runners/IrClassFileToSourceStubConverterTestGenerated.java @@ -151,6 +151,12 @@ public class IrClassFileToSourceStubConverterTestGenerated extends AbstractIrCla runTest("plugins/kapt3/kapt3-compiler/testData/converter/delegateCorrectErrorTypes.kt"); } + @Test + @TestMetadata("delegatedProperties.kt") + public void testDelegatedProperties() throws Exception { + runTest("plugins/kapt3/kapt3-compiler/testData/converter/delegatedProperties.kt"); + } + @Test @TestMetadata("deprecated.kt") public void testDeprecated() throws Exception {