Generate special bridges for removeAt/getOrDefault

In case we extending some Map specialization with non-trivial type arguments,
e.g. Map<String, String> from Kotlin point-of-view it has
"remove(String, String)" signature while in Java it's "remove(Object, Object)".

So, we generate an override "remove(String, String)" in first Kotlin class of the hierarchy,
which body delegates to "super.remove(Object, Object)"

Also, we generate a final override for "remove(Object, Object)" to allow
for Java inheritors choose only the version with String while overriding.

The main idea of the fix is to make mayBeUsedAsSuperImplementation
return true in case of PlatformDependent annotations.
Otherwise, we weren't able to choose the impl from the java.util.Map
as a delegate in our bridge.

Another part of the fix is overriding `isDeclaration`:
it was necessary because otherwise bridge-generation algorithm
was assuming that there's already an actual declaration
in the first sub-class (TestMap) in the test and we need to
delegate to the latter instead of the method from the interface

^KT-26069 Fixed
This commit is contained in:
Denis Zharkov
2018-12-14 12:57:50 +03:00
parent 949214c10b
commit 254380d418
6 changed files with 132 additions and 5 deletions
@@ -5,13 +5,13 @@
package org.jetbrains.kotlin.codegen
import org.jetbrains.kotlin.backend.common.bridges.Bridge
import org.jetbrains.kotlin.backend.common.bridges.DescriptorBasedFunctionHandle
import org.jetbrains.kotlin.backend.common.bridges.generateBridges
import org.jetbrains.kotlin.backend.common.bridges.*
import org.jetbrains.kotlin.config.JvmTarget
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.jvm.annotations.hasJvmDefaultAnnotation
import org.jetbrains.kotlin.resolve.jvm.annotations.hasPlatformDependentAnnotation
class DescriptorBasedFunctionHandleForJvm(
descriptor: FunctionDescriptor,
@@ -27,14 +27,32 @@ class DescriptorBasedFunctionHandleForJvm(
the method in the interface is abstract so we must not generate bridges for such cases.
*/
override val isAbstract: Boolean = super.isAbstract || isAbstractOnJvmIgnoringActualModality(jvmTarget, descriptor)
override val mayBeUsedAsSuperImplementation: Boolean =
super.mayBeUsedAsSuperImplementation || descriptor.isJvmDefaultOrPlatformDependent()
override val isDeclaration: Boolean =
descriptor.kind.isReal || needToGenerateDelegationToDefaultImpls(descriptor)
}
private fun CallableMemberDescriptor.isJvmDefaultOrPlatformDependent() =
hasJvmDefaultAnnotation() || hasPlatformDependentAnnotation()
private fun needToGenerateDelegationToDefaultImpls(descriptor: FunctionDescriptor): Boolean {
if (findInterfaceImplementation(descriptor) == null) return false
val overriddenFromInterface = findImplementationFromInterface(descriptor) ?: return false
return !overriddenFromInterface.isJvmDefaultOrPlatformDependent()
}
/**
* @return return true for interface method not annotated with @JvmDefault
* @return return true for interface method not annotated with @JvmDefault or @PlatformDependent
*/
fun isAbstractOnJvmIgnoringActualModality(jvmTarget: JvmTarget, descriptor: FunctionDescriptor): Boolean {
if (!DescriptorUtils.isInterface(descriptor.containingDeclaration)) return false
return jvmTarget == JvmTarget.JVM_1_6 || !descriptor.hasJvmDefaultAnnotation()
if (jvmTarget == JvmTarget.JVM_1_6) return true
return !descriptor.isJvmDefaultOrPlatformDependent()
}
fun <Signature> generateBridgesForFunctionDescriptorForJvm(