JvmDefault. Support @JvmDefaultWithCompatibility annotation

#KT-48217 Fixed
This commit is contained in:
Mikhael Bogdanov
2021-12-02 16:04:08 +01:00
parent feefaf2273
commit ea58c858d1
24 changed files with 345 additions and 4 deletions
@@ -31,6 +31,8 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils.isInterface
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyPrivateApi
import org.jetbrains.kotlin.resolve.descriptorUtil.nonSourceAnnotations
import org.jetbrains.kotlin.resolve.jvm.annotations.hasJvmDefaultNoCompatibilityAnnotation
import org.jetbrains.kotlin.resolve.jvm.annotations.hasJvmDefaultWithCompatibilityAnnotation
import org.jetbrains.kotlin.resolve.jvm.requiresFunctionNameManglingForParameterTypes
import org.jetbrains.kotlin.resolve.jvm.requiresFunctionNameManglingForReturnType
import org.jetbrains.kotlin.serialization.DescriptorSerializer
@@ -98,7 +100,8 @@ class JvmSerializerExtension @JvmOverloads constructor(
JvmProtoBuf.jvmClassFlags,
JvmFlags.getClassFlags(
jvmDefaultMode.forAllMethodsWithBody,
JvmDefaultMode.ALL_COMPATIBILITY == jvmDefaultMode
(JvmDefaultMode.ALL_COMPATIBILITY == jvmDefaultMode && !descriptor.hasJvmDefaultNoCompatibilityAnnotation()) ||
(JvmDefaultMode.ALL_INCOMPATIBLE == jvmDefaultMode && descriptor.hasJvmDefaultWithCompatibilityAnnotation())
)
)
}
@@ -25729,6 +25729,28 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/specialization/basic.kt");
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility")
@TestDataPath("$PROJECT_ROOT")
public class WithCompatibility {
@Test
public void testAllFilesPresentInWithCompatibility() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("defaultArgs.kt")
public void testDefaultArgs() throws Exception {
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility/defaultArgs.kt");
}
@Test
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility/simple.kt");
}
}
}
@Nested
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.util.findImplementationFromInterface
val JVM_DEFAULT_FQ_NAME = FqName("kotlin.jvm.JvmDefault")
val JVM_DEFAULT_NO_COMPATIBILITY_FQ_NAME = FqName("kotlin.jvm.JvmDefaultWithoutCompatibility")
val JVM_DEFAULT_WITH_COMPATIBILITY_FQ_NAME = FqName("kotlin.jvm.JvmDefaultWithCompatibility")
val JVM_OVERLOADS_FQ_NAME = FqName("kotlin.jvm.JvmOverloads")
@JvmField
@@ -77,6 +78,8 @@ fun CallableMemberDescriptor.hasJvmDefaultAnnotation(): Boolean =
fun DeclarationDescriptor.hasJvmDefaultNoCompatibilityAnnotation(): Boolean =
this.annotations.hasAnnotation(JVM_DEFAULT_NO_COMPATIBILITY_FQ_NAME)
fun DeclarationDescriptor.hasJvmDefaultWithCompatibilityAnnotation(): Boolean =
this.annotations.hasAnnotation(JVM_DEFAULT_WITH_COMPATIBILITY_FQ_NAME)
fun CallableMemberDescriptor.hasPlatformDependentAnnotation(): Boolean =
DescriptorUtils.getDirectMember(this).annotations.hasAnnotation(PLATFORM_DEPENDENT_ANNOTATION_FQ_NAME)
@@ -65,7 +65,9 @@ internal class InterfaceLowering(val context: JvmBackendContext) : IrElementTran
private fun handleInterface(irClass: IrClass) {
val jvmDefaultMode = context.state.jvmDefaultMode
val isCompatibilityMode = jvmDefaultMode.isCompatibility && !irClass.hasJvmDefaultNoCompatibilityAnnotation()
val isCompatibilityMode =
(jvmDefaultMode.isCompatibility && !irClass.hasJvmDefaultNoCompatibilityAnnotation()) ||
(jvmDefaultMode == JvmDefaultMode.ALL_INCOMPATIBLE && irClass.hasJvmDefaultWithCompatibilityAnnotation())
// There are 6 cases for functions on interfaces:
for (function in irClass.functions) {
when {
@@ -52,6 +52,7 @@ import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmProtoBufUtil
import org.jetbrains.kotlin.name.JvmNames.JVM_DEFAULT_FQ_NAME
import org.jetbrains.kotlin.name.JvmNames.JVM_DEFAULT_NO_COMPATIBILITY_FQ_NAME
import org.jetbrains.kotlin.name.JvmNames.JVM_DEFAULT_WITH_COMPATIBILITY_FQ_NAME
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.DescriptorUtils
@@ -94,6 +95,7 @@ fun IrSimpleFunction.isCompiledToJvmDefault(jvmDefaultMode: JvmDefaultMode): Boo
fun IrFunction.hasJvmDefault(): Boolean = propertyIfAccessor.hasAnnotation(JVM_DEFAULT_FQ_NAME)
fun IrClass.hasJvmDefaultNoCompatibilityAnnotation(): Boolean = hasAnnotation(JVM_DEFAULT_NO_COMPATIBILITY_FQ_NAME)
fun IrClass.hasJvmDefaultWithCompatibilityAnnotation(): Boolean = hasAnnotation(JVM_DEFAULT_WITH_COMPATIBILITY_FQ_NAME)
fun IrFunction.hasPlatformDependent(): Boolean = propertyIfAccessor.hasAnnotation(PLATFORM_DEPENDENT_ANNOTATION_FQ_NAME)
fun IrFunction.getJvmVisibilityOfDefaultArgumentStub() =
@@ -0,0 +1,22 @@
// !JVM_DEFAULT_MODE: all
// TARGET_BACKEND: JVM
// IGNORE_BACKEND: JVM
// JVM_TARGET: 1.8
// WITH_RUNTIME
// FULL_JDK
// CHECK_BYTECODE_LISTING
@JvmDefaultWithCompatibility
interface Test {
fun test(s: String ="OK"): String {
return s
}
}
class TestClass : Test {
}
fun box(): String {
return TestClass().test()
}
@@ -0,0 +1,29 @@
@kotlin.Metadata
public final class DefaultArgsKt {
// source: 'defaultArgs.kt'
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
}
@kotlin.Metadata
public final class Test$DefaultImpls {
// source: 'defaultArgs.kt'
public synthetic static method test$default(p0: Test, p1: java.lang.String, p2: int, p3: java.lang.Object): java.lang.String
public deprecated static @java.lang.Deprecated @org.jetbrains.annotations.NotNull method test(@org.jetbrains.annotations.NotNull p0: Test, @org.jetbrains.annotations.NotNull p1: java.lang.String): java.lang.String
public final inner class Test$DefaultImpls
}
@kotlin.jvm.JvmDefaultWithCompatibility
@kotlin.Metadata
public interface Test {
// source: 'defaultArgs.kt'
public synthetic static method access$test$jd(p0: Test, p1: java.lang.String): java.lang.String
public synthetic static method test$default(p0: Test, p1: java.lang.String, p2: int, p3: java.lang.Object): java.lang.String
public @org.jetbrains.annotations.NotNull method test(@org.jetbrains.annotations.NotNull p0: java.lang.String): java.lang.String
public final inner class Test$DefaultImpls
}
@kotlin.Metadata
public final class TestClass {
// source: 'defaultArgs.kt'
public method <init>(): void
}
@@ -0,0 +1,33 @@
// !JVM_DEFAULT_MODE: all
// TARGET_BACKEND: JVM
// JVM_TARGET: 1.8
// IGNORE_BACKEND: JVM
// WITH_RUNTIME
// FILE: Simple.java
public interface Simple extends KInterface {
default String test() {
return KInterface.DefaultImpls.test2(this);
}
}
// FILE: Foo.java
public class Foo implements Simple {
}
// FILE: main.kt
@JvmDefaultWithCompatibility
interface KInterface {
fun test2(): String {
return "OK"
}
}
fun box(): String {
val result = Foo().test()
if (result != "OK") return "fail 1: ${result}"
return Foo().test2()
}
@@ -25357,6 +25357,28 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/specialization/basic.kt");
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility")
@TestDataPath("$PROJECT_ROOT")
public class WithCompatibility {
@Test
public void testAllFilesPresentInWithCompatibility() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@Test
@TestMetadata("defaultArgs.kt")
public void testDefaultArgs() throws Exception {
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility/defaultArgs.kt");
}
@Test
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility/simple.kt");
}
}
}
@Nested
@@ -25729,6 +25729,28 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/specialization/basic.kt");
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility")
@TestDataPath("$PROJECT_ROOT")
public class WithCompatibility {
@Test
public void testAllFilesPresentInWithCompatibility() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("defaultArgs.kt")
public void testDefaultArgs() throws Exception {
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility/defaultArgs.kt");
}
@Test
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility/simple.kt");
}
}
}
@Nested
@@ -21385,6 +21385,29 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/specialization/basic.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class WithCompatibility extends AbstractLightAnalysisModeTest {
@TestMetadata("defaultArgs.kt")
public void ignoreDefaultArgs() throws Exception {
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility/defaultArgs.kt");
}
@TestMetadata("simple.kt")
public void ignoreSimple() throws Exception {
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility/simple.kt");
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
public void testAllFilesPresentInWithCompatibility() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
}
}
@TestMetadata("compiler/testData/codegen/box/jvm8/defaults/noDelegation")
@@ -19,6 +19,7 @@ object JvmNames {
val JVM_DEFAULT_FQ_NAME = FqName("kotlin.jvm.JvmDefault")
val JVM_DEFAULT_CLASS_ID = ClassId.topLevel(JVM_DEFAULT_FQ_NAME)
val JVM_DEFAULT_NO_COMPATIBILITY_FQ_NAME = FqName("kotlin.jvm.JvmDefaultWithoutCompatibility")
val JVM_DEFAULT_WITH_COMPATIBILITY_FQ_NAME = FqName("kotlin.jvm.JvmDefaultWithCompatibility")
val JVM_DEFAULT_NO_COMPATIBILITY_CLASS_ID = ClassId.topLevel(JVM_DEFAULT_NO_COMPATIBILITY_FQ_NAME)
val JVM_OVERLOADS_FQ_NAME = FqName("kotlin.jvm.JvmOverloads")
val JVM_OVERLOADS_CLASS_ID = ClassId.topLevel(JVM_OVERLOADS_FQ_NAME)
@@ -21,7 +21,7 @@ object JvmFlags {
fun getPropertyFlags(isMovedFromInterfaceCompanion: Boolean): Int =
IS_MOVED_FROM_INTERFACE_COMPANION.toFlags(isMovedFromInterfaceCompanion)
fun getClassFlags(isAllInterfaceBodiesInside: Boolean, isAllCompatibilityMode: Boolean): Int =
ARE_INTERFACE_METHOD_BODIES_INSIDE.toFlags(isAllInterfaceBodiesInside) or IS_ALL_COMPATIBILITY_MODE.toFlags(isAllCompatibilityMode)
fun getClassFlags(isAllInterfaceBodiesInside: Boolean, isCompatibilityMode: Boolean): Int =
ARE_INTERFACE_METHOD_BODIES_INSIDE.toFlags(isAllInterfaceBodiesInside) or IS_ALL_COMPATIBILITY_MODE.toFlags(isCompatibilityMode)
}
@@ -18411,6 +18411,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/specialization"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility")
@TestDataPath("$PROJECT_ROOT")
public class WithCompatibility {
@Test
public void testAllFilesPresentInWithCompatibility() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true);
}
}
}
@Nested
@@ -18375,6 +18375,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/specialization"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility")
@TestDataPath("$PROJECT_ROOT")
public class WithCompatibility {
@Test
public void testAllFilesPresentInWithCompatibility() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
}
}
@Nested
@@ -15712,6 +15712,19 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/specialization"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
}
@TestMetadata("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class WithCompatibility extends AbstractIrCodegenBoxWasmTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.WASM, testDataFilePath);
}
public void testAllFilesPresentInWithCompatibility() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
}
}
@TestMetadata("compiler/testData/codegen/box/jvm8/defaults/noDelegation")
@@ -48,3 +48,13 @@ annotation class JvmDefault
@RequireKotlin("1.4", versionKind = RequireKotlinVersionKind.COMPILER_VERSION)
@Target(AnnotationTarget.CLASS)
annotation class JvmDefaultWithoutCompatibility
/**
* Forces the compiler to generate compatibility accessors for the annotated interface in the `DefaultImpls` class.
*
* Used only with `-Xjvm-default=all`. For more details refer to `-Xjvm-default` documentation.
*/
@SinceKotlin("1.6")
@RequireKotlin("1.6", versionKind = RequireKotlinVersionKind.COMPILER_VERSION)
@Target(AnnotationTarget.CLASS)
annotation class JvmDefaultWithCompatibility
@@ -3227,6 +3227,9 @@ public final class kotlin/jvm/JvmClassMappingKt {
public abstract interface annotation class kotlin/jvm/JvmDefault : java/lang/annotation/Annotation {
}
public abstract interface annotation class kotlin/jvm/JvmDefaultWithCompatibility : java/lang/annotation/Annotation {
}
public abstract interface annotation class kotlin/jvm/JvmDefaultWithoutCompatibility : java/lang/annotation/Annotation {
}
@@ -140,5 +140,15 @@ public class KotlinpTestGenerated extends AbstractKotlinpTest {
public void testAllFilesPresentInJvmDefault() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("libraries/tools/kotlinp/testData/jvmDefault"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@TestMetadata("withCompatibility.kt")
public void testWithCompatibility() throws Exception {
runTest("libraries/tools/kotlinp/testData/jvmDefault/withCompatibility.kt");
}
@TestMetadata("withoutCompatibility.kt")
public void testWithoutCompatibility() throws Exception {
runTest("libraries/tools/kotlinp/testData/jvmDefault/withoutCompatibility.kt");
}
}
}
@@ -0,0 +1,8 @@
// !JVM_DEFAULT_MODE: all
@JvmDefaultWithCompatibility
interface A {
fun f() {}
}
@JvmDefaultWithCompatibility
interface B : A
@@ -0,0 +1,35 @@
// A.class
// ------------------------------------------
// requires compiler version 1.4.0 (level=ERROR)
public abstract interface A : kotlin/Any {
// signature: f()V
public open fun f(): kotlin/Unit
// module name: test-module
// has method bodies in interface
// is compiled in compatibility mode
}
// A$DefaultImpls.class
// ------------------------------------------
synthetic class
// B.class
// ------------------------------------------
// requires compiler version 1.4.0 (level=ERROR)
public abstract interface B : A {
// module name: test-module
// has method bodies in interface
// is compiled in compatibility mode
}
// B$DefaultImpls.class
// ------------------------------------------
synthetic class
// META-INF/test-module.kotlin_module
// ------------------------------------------
module {
}
@@ -0,0 +1,7 @@
// !JVM_DEFAULT_MODE: all-compatibility
@JvmDefaultWithoutCompatibility
interface A {
fun f() {}
}
interface B : A
@@ -0,0 +1,28 @@
// A.class
// ------------------------------------------
public abstract interface A : kotlin/Any {
// signature: f()V
public open fun f(): kotlin/Unit
// module name: test-module
// has method bodies in interface
}
// B.class
// ------------------------------------------
public abstract interface B : A {
// module name: test-module
// has method bodies in interface
// is compiled in compatibility mode
}
// B$DefaultImpls.class
// ------------------------------------------
synthetic class
// META-INF/test-module.kotlin_module
// ------------------------------------------
module {
}
@@ -26040,6 +26040,29 @@ public class NativeExtBlackBoxTestGenerated extends AbstractNativeBlackBoxTest {
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/specialization/basic.kt");
}
}
@Nested
@TestMetadata("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility")
@TestDataPath("$PROJECT_ROOT")
@NativeBlackBoxTestCaseGroupProvider(ExtTestCaseGroupProvider.class)
public class WithCompatibility {
@Test
public void testAllFilesPresentInWithCompatibility() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@Test
@TestMetadata("defaultArgs.kt")
public void testDefaultArgs() throws Exception {
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility/defaultArgs.kt");
}
@Test
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("compiler/testData/codegen/box/jvm8/defaults/noDefaultImpls/withCompatibility/simple.kt");
}
}
}
@Nested