[KAPT+JVM_IR] Do not generate initializers for delegated properties.

^KT-54870 Fixed
This commit is contained in:
Mads Ager
2022-11-07 13:13:55 +01:00
committed by Alexander Udalov
parent 9dab8637a8
commit 30002e29d1
10 changed files with 342 additions and 10 deletions
@@ -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(
@@ -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 {
@@ -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;
}
}
@@ -0,0 +1,14 @@
package test
open class C<T>(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<String>("z")
val y by object: C<String>("y") {}
val a by lazy { C<String>("a") }
val b by lazy { object: C<String>("b") {} }
}
@@ -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<java.lang.String> getA() {
return null;
}
@org.jetbrains.annotations.NotNull()
public final test.C<java.lang.String> getB() {
return null;
}
}
////////////////////
package test;
import java.lang.System;
@kotlin.Metadata()
public class C<T extends java.lang.Object> {
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;
}
}
@@ -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<java.lang.String> getA() {
return null;
}
@org.jetbrains.annotations.NotNull()
public final test.C<java.lang.String> 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<T extends java.lang.Object> {
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;
}
}
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
class HomeFragment {
@Suppress("TOO_MANY_ARGUMENTS", "DELEGATE_SPECIAL_FUNCTION_MISSING")
private val categoryNewsListPresenter by moxyPresenter {
@@ -0,0 +1,56 @@
import java.lang.System;
@kotlin.Metadata()
public final class GroupedNewsListDelegateAdapter {
public GroupedNewsListDelegateAdapter(@org.jetbrains.annotations.NotNull()
kotlin.jvm.functions.Function0<kotlin.Unit> 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() {
}
}
@@ -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 {
@@ -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 {