JVM IR: disable SAM conversion in case type has 'in' projection

... and the corresponding type parameter has a non-trivial (i.e.
non-`Any?`) upper bound.

The best solution here would be to get rid of
`removeExternalProjections` completely, and just use the type of the
argument at the call site, but see KT-52428.

 #KT-51868 Fixed
This commit is contained in:
Alexander Udalov
2022-04-22 00:15:11 +02:00
parent 62c12864f3
commit 584b70719e
14 changed files with 138 additions and 51 deletions
@@ -6,10 +6,7 @@
package org.jetbrains.kotlin.backend.jvm
import org.jetbrains.kotlin.backend.common.serialization.signature.PublicIdSignatureComputer
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.config.JvmSerializeIrMode
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
@@ -53,7 +50,7 @@ import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
open class JvmGeneratorExtensionsImpl(
configuration: CompilerConfiguration,
private val configuration: CompilerConfiguration,
private val generateFacades: Boolean = true,
) : GeneratorExtensions(), JvmGeneratorExtensions {
override val classNameOverride: MutableMap<IrClass, JvmClassName> = mutableMapOf()
@@ -62,15 +59,14 @@ open class JvmGeneratorExtensionsImpl(
override val cachedFields = CachedFieldsForObjectInstances(IrFactoryImpl, configuration.languageVersionSettings)
override val samConversion: SamConversion
get() = JvmSamConversion
open class JvmSamConversion : SamConversion() {
override val samConversion: SamConversion = JvmSamConversion()
inner class JvmSamConversion : SamConversion() {
override fun isPlatformSamType(type: KotlinType): Boolean =
JavaSingleAbstractMethodUtils.isSamType(type)
companion object Instance : JvmSamConversion()
override fun isCarefulApproximationOfContravariantProjection(): Boolean =
configuration.get(JVMConfigurationKeys.SAM_CONVERSIONS) != JvmClosureGenerationScheme.CLASS
}
override fun getContainerSource(descriptor: DeclarationDescriptor): DeserializedContainerSource? {
@@ -737,8 +737,9 @@ fun StatementGenerator.generateSamConversionForValueArgumentsIfRequired(call: Ca
}
private fun StatementGenerator.getSamTypeForValueParameter(valueParameter: ValueParameterDescriptor): KotlinType? {
val approximatedSamType = context.samTypeApproximator.getSamTypeForValueParameter(valueParameter)
?: return null
val approximatedSamType = context.samTypeApproximator.getSamTypeForValueParameter(
valueParameter, context.extensions.samConversion.isCarefulApproximationOfContravariantProjection(),
) ?: return null
if (!context.extensions.samConversion.isSamType(approximatedSamType))
return null
val classDescriptor = approximatedSamType.constructor.declarationDescriptor
@@ -23,6 +23,8 @@ open class GeneratorExtensions : StubGeneratorExtensions() {
open class SamConversion {
open fun isPlatformSamType(type: KotlinType): Boolean = false
open fun isCarefulApproximationOfContravariantProjection(): Boolean = false
companion object Instance : SamConversion()
}