Kapt+JVM_IR: fix ordering of properties in generated stubs

Apparently it depended on the ordering of the synthetic `$annotations`
methods generated for properties. And those methods were always
generated at the end of a Java stub by kapt because they lacked PSI
element, and thus `ClassFileToSourceStubConverter.convertClass` could
not compute their source position, and it placed them at the end as all
other synthetic methods.

The solution is to provide PSI element for `$annotations` methods in
JvmDeclarationOrigin. This origin is then collected in kapt in
`OriginCollectingClassBuilderFactory` and used for sorting, as in the
case of the old JVM backend.

 #KT-56360 Fixed
This commit is contained in:
Alexander Udalov
2023-02-02 16:28:44 +01:00
committed by Space Team
parent 67c4dc3cce
commit 1844869e8a
10 changed files with 90 additions and 251 deletions
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.backend.jvm.codegen
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.backend.common.lower.ANNOTATION_IMPLEMENTATION
import org.jetbrains.kotlin.backend.common.psi.PsiSourceManager
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
@@ -38,7 +39,10 @@ import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrSetFieldImpl
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.types.getArrayElementType
import org.jetbrains.kotlin.ir.types.isArray
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.kotlin.load.kotlin.TypeMappingMode
@@ -525,15 +529,7 @@ class ClassCodegen private constructor(
private val IrDeclaration.descriptorOrigin: JvmDeclarationOrigin
get() {
val psiElement = PsiSourceManager.findPsiElement(this).let { element ->
// Offsets for accessors and field of delegated property in IR point to the 'by' keyword, so the closest PSI element is the
// KtPropertyDelegate (`by ...` expression). However, old JVM backend passed the PSI element of the property instead.
// This is important for example in case of KAPT stub generation in the "correct error types" mode, which tries to find the
// PSI element for each declaration with unresolved types and tries to heuristically "resolve" those unresolved types to
// generate them into the Java stub. In case of delegated property accessors, it should look for the property declaration,
// since the type can only be provided there, and not in the `by ...` expression.
if (element is KtPropertyDelegate) element.parent else element
}
val psiElement = findPsiElementForDeclarationOrigin()
return when {
origin == IrDeclarationOrigin.FILE_CLASS ->
JvmDeclarationOrigin(JvmDeclarationOriginKind.PACKAGE_PART, psiElement, toIrBasedDescriptor())
@@ -545,6 +541,27 @@ class ClassCodegen private constructor(
}
}
private fun IrDeclaration.findPsiElementForDeclarationOrigin(): PsiElement? {
// For synthetic $annotations methods for properties, use the PSI for the property or the constructor parameter.
// It's used in KAPT stub generation to sort the properties correctly based on their source position (see KT-44130).
if (this is IrFunction && name.asString().endsWith("\$annotations")) {
val metadata = metadata as? DescriptorMetadataSource.Property
if (metadata != null) {
return metadata.descriptor.psiElement
}
}
val element = PsiSourceManager.findPsiElement(this)
// Offsets for accessors and field of delegated property in IR point to the 'by' keyword, so the closest PSI element is the
// KtPropertyDelegate (`by ...` expression). However, old JVM backend passed the PSI element of the property instead.
// This is important for example in case of KAPT stub generation in the "correct error types" mode, which tries to find the
// PSI element for each declaration with unresolved types and tries to heuristically "resolve" those unresolved types to
// generate them into the Java stub. In case of delegated property accessors, it should look for the property declaration,
// since the type can only be provided there, and not in the `by ...` expression.
return if (element is KtPropertyDelegate) element.parent else element
}
private fun storeSerializedIr(serializedIr: ByteArray) {
val av = visitor.newAnnotation(JvmAnnotationNames.SERIALIZED_IR_DESC, true)
val partsVisitor = av.visitArray(JvmAnnotationNames.SERIALIZED_IR_BYTES_FIELD_NAME)
@@ -113,14 +113,14 @@ public final class TestAnno2 {
return null;
}
@Anno3(value = "property")
@java.lang.Deprecated()
public static void getB$annotations() {
}
@Anno3(value = "setter")
public final void setB(@Anno3(value = "setparam")
@org.jetbrains.annotations.NotNull()
java.lang.String p0) {
}
@Anno3(value = "property")
@java.lang.Deprecated()
public static void getB$annotations() {
}
}
@@ -26,16 +26,16 @@ public final class AnnotationsTest {
java.lang.String $this$topLevelFun) {
}
@Anno(value = "top-level-val")
@java.lang.Deprecated()
public static void getTopLevelVal$annotations(int p0) {
}
@org.jetbrains.annotations.NotNull()
public static final java.lang.String getTopLevelVal(@Anno(value = "top-level-val-receiver")
int $this$topLevelVal) {
return null;
}
@Anno(value = "top-level-val")
@java.lang.Deprecated()
public static void getTopLevelVal$annotations(int p0) {
}
}
////////////////////
@@ -88,6 +88,11 @@ public abstract class Test {
return null;
}
@Anno(value = "v-property")
@java.lang.Deprecated()
public static void getV$annotations() {
}
@Anno(value = "v-set")
public final void setV(@Anno(value = "v-setparam")
@org.jetbrains.annotations.NotNull()
@@ -105,9 +110,4 @@ public abstract class Test {
@java.lang.Deprecated()
public static void getAbstractVal$annotations() {
}
@Anno(value = "v-property")
@java.lang.Deprecated()
public static void getV$annotations() {
}
}
@@ -169,26 +169,57 @@ public final class MyActivity {
return 0;
}
@Bind(id = lib.R.id.textView)
@java.lang.Deprecated()
public static void getA$annotations() {
}
public final int getB() {
return 0;
}
@Bind(id = lib.R.id.textView)
@java.lang.Deprecated()
public static void getB$annotations() {
}
public final int getC() {
return 0;
}
@Bind(id = app.R.layout.mainActivity)
@java.lang.Deprecated()
public static void getC$annotations() {
}
public final int getD() {
return 0;
}
@Bind(id = app.R.layout.mainActivity)
@java.lang.Deprecated()
public static void getD$annotations() {
}
public final int getE() {
return 0;
}
@Bind(id = app.R2.layout.mainActivity)
@Anno(a1 = app.B.a1, a2 = app.B.a2, a3 = app.B.a3, a4 = app.B.a4, a5 = app.B.a5, a6 = app.B.a6, a7 = app.B.a7, a8 = app.B.a8, a9 = "A")
@java.lang.Deprecated()
public static void getE$annotations() {
}
public final int getF() {
return 0;
}
@Bind(id = app.B.id.textView)
@java.lang.Deprecated()
public static void getF$annotations() {
}
@Bind(id = lib.R.id.textView)
public final void foo() {
}
@@ -260,37 +291,6 @@ public final class MyActivity {
public final int getPropF() {
return 0;
}
@Bind(id = lib.R.id.textView)
@java.lang.Deprecated()
public static void getA$annotations() {
}
@Bind(id = lib.R.id.textView)
@java.lang.Deprecated()
public static void getB$annotations() {
}
@Bind(id = app.R.layout.mainActivity)
@java.lang.Deprecated()
public static void getC$annotations() {
}
@Bind(id = app.R.layout.mainActivity)
@java.lang.Deprecated()
public static void getD$annotations() {
}
@Bind(id = app.R2.layout.mainActivity)
@Anno(a1 = app.B.a1, a2 = app.B.a2, a3 = app.B.a3, a4 = app.B.a4, a5 = app.B.a5, a6 = app.B.a6, a7 = app.B.a7, a8 = app.B.a8, a9 = "A")
@java.lang.Deprecated()
public static void getE$annotations() {
}
@Bind(id = app.B.id.textView)
@java.lang.Deprecated()
public static void getF$annotations() {
}
}
////////////////////
@@ -92,6 +92,14 @@ public final class Test {
return null;
}
/**
* prop2.
*/
@Anno()
@java.lang.Deprecated()
public static void getProp2$annotations() {
}
/**
* get.
*/
@@ -106,11 +114,6 @@ public final class Test {
public final void setProp3(@org.jetbrains.annotations.NotNull()
java.lang.String v) {
}
@Anno()
@java.lang.Deprecated()
public static void getProp2$annotations() {
}
}
////////////////////
@@ -66,6 +66,11 @@ public final class Test {
return null;
}
@Anno()
@java.lang.Deprecated()
public static void getProp2$annotations() {
}
@org.jetbrains.annotations.NotNull()
public final java.lang.String getProp3() {
return null;
@@ -74,11 +79,6 @@ public final class Test {
public final void setProp3(@org.jetbrains.annotations.NotNull()
java.lang.String v) {
}
@Anno()
@java.lang.Deprecated()
public static void getProp2$annotations() {
}
}
////////////////////
@@ -1,67 +0,0 @@
package test;
@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;
@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,45 +0,0 @@
package deprecated;
@java.lang.annotation.Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
@kotlin.Metadata()
@java.lang.Deprecated()
public abstract @interface Anno {
}
////////////////////
package deprecated;
@Anno()
@kotlin.Metadata()
@java.lang.Deprecated()
public final class Foo {
@java.lang.Deprecated()
private final int prop = 0;
public Foo() {
super();
}
@java.lang.Deprecated()
public final void foo(int a) {
}
@java.lang.Deprecated()
public final int getProp() {
return 0;
}
@java.lang.Deprecated()
public final int getFoo() {
return 0;
}
@java.lang.Deprecated()
public final void setFoo(int value) {
}
@java.lang.Deprecated()
public static void getProp$annotations() {
}
}
@@ -25,14 +25,14 @@ public final class HomeFragment {
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() {
}
private final GroupedNewsListDelegateAdapter getGroupedNewsListAdapter() {
return null;
}
}
////////////////////
@@ -1,69 +0,0 @@
package test.another;
@java.lang.annotation.Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
@kotlin.Metadata()
public abstract @interface Anno {
public abstract java.lang.String value();
}
////////////////////
package test.another;
@kotlin.Metadata()
public final class TopLevelKt {
public TopLevelKt() {
super();
}
private static final int topLevelProperty = 2;
public static final int topLevelConstProperty = 2;
@org.jetbrains.annotations.Nullable()
public static final java.lang.String topLevelFunction() {
return null;
}
@org.jetbrains.annotations.Nullable()
public static final <X extends java.lang.CharSequence, T extends java.util.List<? extends X>>T topLevelGenericFunction() {
return null;
}
public static final int getTopLevelProperty() {
return 0;
}
@org.jetbrains.annotations.NotNull()
public static final java.lang.String getTopLevelProperty2() {
return null;
}
public static final void extensionFunction(@Anno(value = "rec")
@org.jetbrains.annotations.NotNull()
java.lang.String $this$extensionFunction, @Anno(value = "1")
@org.jetbrains.annotations.NotNull()
java.lang.String a, @Anno(value = "2")
@org.jetbrains.annotations.NotNull()
java.lang.String b) {
}
@org.jetbrains.annotations.NotNull()
public static final <T extends java.lang.Object>java.lang.String getExtensionProperty(@Anno(value = "propRec")
@org.jetbrains.annotations.NotNull()
T $this$extensionProperty) {
return null;
}
public static final <T extends java.lang.Object>void setExtensionProperty(@Anno(value = "propRec")
@org.jetbrains.annotations.NotNull()
T $this$extensionProperty, @Anno(value = "setparam")
@org.jetbrains.annotations.NotNull()
java.lang.String setParamName) {
}
@Anno(value = "extpr")
@java.lang.Deprecated()
public static void getExtensionProperty$annotations(java.lang.Object p0) {
}
}