From d84a15c0f61f50b2852d289104503f065332b2bf Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Tue, 27 Feb 2018 12:15:55 +0100 Subject: [PATCH] Support inline for private @JvmDefault members --- .../codegen/inline/SourceCompilerForInline.kt | 6 +++- .../codegen/java8/box/jvm8/defaults/inline.kt | 27 +++++++++++++++++ .../java8/box/jvm8/defaults/inlineProperty.kt | 29 +++++++++++++++++++ ...BlackBoxWithJava8CodegenTestGenerated.java | 12 ++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/java8/box/jvm8/defaults/inline.kt create mode 100644 compiler/testData/codegen/java8/box/jvm8/defaults/inlineProperty.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SourceCompilerForInline.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SourceCompilerForInline.kt index ec6441dc38c..662ed8b2af0 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SourceCompilerForInline.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SourceCompilerForInline.kt @@ -28,6 +28,8 @@ import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCallWithAssert +import org.jetbrains.kotlin.resolve.jvm.annotations.hasJvmFieldAnnotation +import org.jetbrains.kotlin.resolve.jvm.annotations.isCallableMemberWithJvmDefaultAnnotation import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature import org.jetbrains.kotlin.utils.addIfNotNull import org.jetbrains.org.objectweb.asm.Label @@ -445,7 +447,9 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid } is ClassDescriptor -> { val kind = - if (DescriptorUtils.isInterface(descriptor) && innerDescriptor !is ClassDescriptor) + if (DescriptorUtils.isInterface(descriptor) && + innerDescriptor !is ClassDescriptor && !innerDescriptor.isCallableMemberWithJvmDefaultAnnotation() + ) OwnerKind.DEFAULT_IMPLS else OwnerKind.IMPLEMENTATION diff --git a/compiler/testData/codegen/java8/box/jvm8/defaults/inline.kt b/compiler/testData/codegen/java8/box/jvm8/defaults/inline.kt new file mode 100644 index 00000000000..8dfea0faa09 --- /dev/null +++ b/compiler/testData/codegen/java8/box/jvm8/defaults/inline.kt @@ -0,0 +1,27 @@ +// !API_VERSION: 1.3 +// JVM_TARGET: 1.8 +// WITH_RUNTIME + +interface Test { + @kotlin.annotations.JvmDefault + fun test(): String { + return inlineFun { "O" } + } + + fun testDefaultImpls(): String { + return inlineFun { "K" } + } + + @kotlin.annotations.JvmDefault + private inline fun inlineFun(s: () -> String) = s() + +} + +class TestClass : Test { + +} + +fun box(): String { + val foo = TestClass() + return foo.test() + foo.testDefaultImpls() +} diff --git a/compiler/testData/codegen/java8/box/jvm8/defaults/inlineProperty.kt b/compiler/testData/codegen/java8/box/jvm8/defaults/inlineProperty.kt new file mode 100644 index 00000000000..930a5749090 --- /dev/null +++ b/compiler/testData/codegen/java8/box/jvm8/defaults/inlineProperty.kt @@ -0,0 +1,29 @@ +// !API_VERSION: 1.3 +// JVM_TARGET: 1.8 +// WITH_RUNTIME + +interface Test { + @kotlin.annotations.JvmDefault + fun test(): String { + return inlineProp + } + + fun testDefaultImpls(): String { + return inlineProp + } + + @kotlin.annotations.JvmDefault + private inline val inlineProp: String + get() = "OK" + +} + +class TestClass : Test { + +} + +fun box(): String { + val foo = TestClass() + if (foo.test() != "OK") return "fail: ${foo.test()}" + return foo.testDefaultImpls() +} diff --git a/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java b/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java index 1802d33295d..3063ade9c3e 100644 --- a/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java +++ b/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java @@ -395,6 +395,18 @@ public class BlackBoxWithJava8CodegenTestGenerated extends AbstractBlackBoxCodeg doTest(fileName); } + @TestMetadata("inline.kt") + public void testInline() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/box/jvm8/defaults/inline.kt"); + doTest(fileName); + } + + @TestMetadata("inlineProperty.kt") + public void testInlineProperty() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/box/jvm8/defaults/inlineProperty.kt"); + doTest(fileName); + } + @TestMetadata("kt11969.kt") public void testKt11969() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/box/jvm8/defaults/kt11969.kt");