From 15f401a4732fb6edbb1bfe9f7b6b670c5fe084ef Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Fri, 4 Aug 2017 15:32:58 +0200 Subject: [PATCH] Copy annotation and attributes on class transformation during inline --- .../codegen/inline/MethodBodyVisitor.kt | 8 +++++-- .../kotlin/codegen/inline/MethodInliner.kt | 8 +++++-- .../kotlin/codegen/inline/RemapVisitor.java | 5 ++-- .../boxInline/anonymousObject/kt19434.kt | 23 ++++++++++++++++++ .../boxInline/anonymousObject/kt19434_2.kt | 24 +++++++++++++++++++ .../IrBlackBoxInlineCodegenTestGenerated.java | 12 ++++++++++ ...otlinAgainstInlineKotlinTestGenerated.java | 12 ++++++++++ .../BlackBoxInlineCodegenTestGenerated.java | 12 ++++++++++ ...otlinAgainstInlineKotlinTestGenerated.java | 12 ++++++++++ 9 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/codegen/boxInline/anonymousObject/kt19434.kt create mode 100644 compiler/testData/codegen/boxInline/anonymousObject/kt19434_2.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodBodyVisitor.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodBodyVisitor.kt index ce92df9159b..15a582bafef 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodBodyVisitor.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodBodyVisitor.kt @@ -24,7 +24,9 @@ import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter open class MethodBodyVisitor(mv: MethodVisitor, private val visitAnnotationsAndAttributes: Boolean = false) : InstructionAdapter(API, mv) { - override fun visitParameter(name: String, access: Int) {} + override fun visitParameter(name: String, access: Int) { + if (visitAnnotationsAndAttributes) super.visitParameter(name, access) + } override fun visitAnnotationDefault(): AnnotationVisitor? = if (visitAnnotationsAndAttributes) super.visitAnnotationDefault() else null @@ -42,7 +44,9 @@ open class MethodBodyVisitor(mv: MethodVisitor, private val visitAnnotationsAndA if (visitAnnotationsAndAttributes) super.visitAttribute(attr) } - override fun visitCode() {} + override fun visitCode() { + if (visitAnnotationsAndAttributes) super.visitCode() + } override fun visitMaxs(maxStack: Int, maxLocals: Int) {} diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt index 07c59a0d515..60888288b7b 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInliner.kt @@ -90,7 +90,11 @@ class MethodInliner( API, transformedNode.access, transformedNode.name, transformedNode.desc, transformedNode.signature, transformedNode.exceptions?.toTypedArray() ) - val visitor = RemapVisitor(resultNode, remapper, nodeRemapper) + val visitor = RemapVisitor( + resultNode, remapper, nodeRemapper, + /*copy annotation and attributes*/ + nodeRemapper is RegeneratedLambdaFieldRemapper + ) try { transformedNode.accept(visitor) } @@ -109,7 +113,7 @@ class MethodInliner( processReturns(resultNode, labelOwner, remapReturn, end) //flush transformed node to output - resultNode.accept(MethodBodyVisitor(adapter)) + resultNode.accept(MethodBodyVisitor(adapter, true)) sourceMapper.endMapping() return result diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/RemapVisitor.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/RemapVisitor.java index c55ded016fc..45360e34291 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/RemapVisitor.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/RemapVisitor.java @@ -34,9 +34,10 @@ public class RemapVisitor extends MethodBodyVisitor { public RemapVisitor( @NotNull MethodVisitor mv, @NotNull LocalVarRemapper remapper, - @NotNull FieldRemapper nodeRemapper + @NotNull FieldRemapper nodeRemapper, + boolean copyAnnotationsAndAttributes ) { - super(mv); + super(mv, copyAnnotationsAndAttributes); this.instructionAdapter = new InstructionAdapter(mv); this.remapper = remapper; this.nodeRemapper = nodeRemapper; diff --git a/compiler/testData/codegen/boxInline/anonymousObject/kt19434.kt b/compiler/testData/codegen/boxInline/anonymousObject/kt19434.kt new file mode 100644 index 00000000000..1ac01af5003 --- /dev/null +++ b/compiler/testData/codegen/boxInline/anonymousObject/kt19434.kt @@ -0,0 +1,23 @@ +// FILE: 1.kt +//WITH_RUNTIME +package test + +annotation class MethodAnnotation + +inline fun reproduceIssue(crossinline s: () -> String): String { + val obj = object { + @MethodAnnotation fun annotatedMethod(): String { + return s() + } + } + val annotatedMethod = obj::class.java.declaredMethods.first { it.name == "annotatedMethod" } + if (annotatedMethod.annotations.isEmpty()) return "fail: can't find annotated method" + return obj.annotatedMethod() +} + +// FILE: 2.kt +import test.* + +fun box(): String { + return reproduceIssue { "OK" } +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/anonymousObject/kt19434_2.kt b/compiler/testData/codegen/boxInline/anonymousObject/kt19434_2.kt new file mode 100644 index 00000000000..dea98563dd2 --- /dev/null +++ b/compiler/testData/codegen/boxInline/anonymousObject/kt19434_2.kt @@ -0,0 +1,24 @@ +// FILE: 1.kt +//WITH_RUNTIME +package test + +annotation class FieldAnnotation + +inline fun reproduceIssue(crossinline s: () -> String): String { + val obj = object { + @field:FieldAnnotation val annotatedField = "O" + fun method(): String { + return annotatedField + s() + } + } + val annotatedMethod = obj::class.java.declaredFields.first { it.name == "annotatedField" } + if (annotatedMethod.annotations.isEmpty()) return "fail: can't find annotated field" + return obj.method() +} + +// FILE: 2.kt +import test.* + +fun box(): String { + return reproduceIssue { "K" } +} \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java index f3042a2ba2a..5d845756dad 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java @@ -212,6 +212,18 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli doTest(fileName); } + @TestMetadata("kt19434.kt") + public void testKt19434() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt19434.kt"); + doTest(fileName); + } + + @TestMetadata("kt19434_2.kt") + public void testKt19434_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt19434_2.kt"); + doTest(fileName); + } + @TestMetadata("kt6552.kt") public void testKt6552() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt6552.kt"); diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java index c16a2266a2d..8c7ca0e8819 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java @@ -212,6 +212,18 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC doTest(fileName); } + @TestMetadata("kt19434.kt") + public void testKt19434() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt19434.kt"); + doTest(fileName); + } + + @TestMetadata("kt19434_2.kt") + public void testKt19434_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt19434_2.kt"); + doTest(fileName); + } + @TestMetadata("kt6552.kt") public void testKt6552() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt6552.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java index f04ef4b57d0..e5114598332 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -212,6 +212,18 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo doTest(fileName); } + @TestMetadata("kt19434.kt") + public void testKt19434() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt19434.kt"); + doTest(fileName); + } + + @TestMetadata("kt19434_2.kt") + public void testKt19434_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt19434_2.kt"); + doTest(fileName); + } + @TestMetadata("kt6552.kt") public void testKt6552() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt6552.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index d3b88ed88a7..6146888223c 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -212,6 +212,18 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi doTest(fileName); } + @TestMetadata("kt19434.kt") + public void testKt19434() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt19434.kt"); + doTest(fileName); + } + + @TestMetadata("kt19434_2.kt") + public void testKt19434_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt19434_2.kt"); + doTest(fileName); + } + @TestMetadata("kt6552.kt") public void testKt6552() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt6552.kt");