JVM_IR KT-43812 erase generic arguments of SAM wrapper supertype

This commit is contained in:
Dmitry Petrov
2020-12-09 11:23:36 +03:00
parent 5daa406cdf
commit 313dfaf48c
10 changed files with 124 additions and 13 deletions
@@ -379,18 +379,21 @@ fun collectVisibleTypeParameters(scopeOwner: IrTypeParametersContainer): Set<IrT
.flatMap { it.typeParameters }
.toSet()
fun IrClassSymbol.rawType(context: JvmBackendContext): IrSimpleType {
// On the IR backend we represent raw types as star projected types with a special synthetic annotation.
// See `TypeTranslator.translateTypeAnnotations`.
val rawTypeAnnotation = IrConstructorCallImpl.fromSymbolOwner(
context.generatorExtensions.rawTypeAnnotationConstructor!!.constructedClassType,
context.generatorExtensions.rawTypeAnnotationConstructor.symbol
// On the IR backend we represent raw types as star projected types with a special synthetic annotation.
// See `TypeTranslator.translateTypeAnnotations`.
private fun JvmBackendContext.makeRawTypeAnnotation() =
IrConstructorCallImpl.fromSymbolOwner(
generatorExtensions.rawTypeAnnotationConstructor!!.constructedClassType,
generatorExtensions.rawTypeAnnotationConstructor.symbol
)
return IrSimpleTypeImpl(
fun IrClassSymbol.rawDefaultType(context: JvmBackendContext): IrType =
this.defaultType.addAnnotations(listOf(context.makeRawTypeAnnotation()))
fun IrClassSymbol.rawStarProjectedType(context: JvmBackendContext): IrSimpleType =
IrSimpleTypeImpl(
this,
hasQuestionMark = false,
arguments = owner.typeParameters.map { IrStarProjectionImpl },
annotations = listOf(rawTypeAnnotation)
annotations = listOf(context.makeRawTypeAnnotation())
)
}
@@ -10,7 +10,8 @@ import org.jetbrains.kotlin.backend.common.lower.SingleAbstractMethodLowering
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.ir.erasedUpperBound
import org.jetbrains.kotlin.backend.jvm.ir.rawType
import org.jetbrains.kotlin.backend.jvm.ir.rawDefaultType
import org.jetbrains.kotlin.backend.jvm.ir.rawStarProjectedType
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
@@ -21,7 +22,6 @@ import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.expressions.IrTypeOperatorCall
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.load.java.JavaDescriptorVisibilities
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -45,10 +45,10 @@ private class JvmSingleAbstractMethodLowering(context: JvmBackendContext) : Sing
if (inInlineFunctionScope) DescriptorVisibilities.PUBLIC else JavaDescriptorVisibilities.PACKAGE_VISIBILITY
override fun getSuperTypeForWrapper(typeOperand: IrType): IrType =
typeOperand.erasedUpperBound.defaultType
typeOperand.erasedUpperBound.symbol.rawDefaultType(context as JvmBackendContext)
override fun getWrappedFunctionType(klass: IrClass): IrType =
klass.symbol.rawType(context as JvmBackendContext)
klass.symbol.rawStarProjectedType(context as JvmBackendContext)
// The constructor of a SAM wrapper is non-synthetic and should not have line numbers.
// Otherwise the debugger will try to step into it.
@@ -0,0 +1,16 @@
// WITH_SIGNATURES
// FILE: samGenericSuperinterface.kt
fun <T> genericSam(f: () -> T): T = J.g(f)
// FILE: J.java
public class J {
static <T> T g(Sam<T> s) {
return s.get();
}
}
// FILE: Sam.java
public interface Sam<T> {
T get();
}
@@ -0,0 +1,13 @@
@kotlin.Metadata
final class<null> SamGenericSuperinterfaceKt$sam$Sam$0 {
// source: 'samGenericSuperinterface.kt'
<null> method <init>(p0: kotlin.jvm.functions.Function0): void
public synthetic final <null> method get(): java.lang.Object
private synthetic final field <null> function: kotlin.jvm.functions.Function0
}
@kotlin.Metadata
public final class<null> SamGenericSuperinterfaceKt {
// source: 'samGenericSuperinterface.kt'
public final static <<T:Ljava/lang/Object;>(Lkotlin/jvm/functions/Function0<+TT;>;)TT;> method genericSam(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.Object
}
@@ -0,0 +1,15 @@
@kotlin.Metadata
final class<null> SamGenericSuperinterfaceKt$sam$Sam$0 {
// source: 'samGenericSuperinterface.kt'
<null> method <init>(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): void
public synthetic final <null> method get(): java.lang.Object
private synthetic final field <null> function: kotlin.jvm.functions.Function0
final inner class SamGenericSuperinterfaceKt$sam$Sam$0
}
@kotlin.Metadata
public final class<null> SamGenericSuperinterfaceKt {
// source: 'samGenericSuperinterface.kt'
public final static <<T:Ljava/lang/Object;>(Lkotlin/jvm/functions/Function0<+TT;>;)TT;> method genericSam(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.Object
final inner class SamGenericSuperinterfaceKt$sam$Sam$0
}
@@ -0,0 +1,16 @@
// WITH_SIGNATURES
// FILE: samGenericSuperinterface.kt
fun specializedSam(f: () -> String) = J.g(f)
// FILE: J.java
public class J {
static <T> T g(Sam<T> s) {
return s.get();
}
}
// FILE: Sam.java
public interface Sam<T> {
T get();
}
@@ -0,0 +1,13 @@
@kotlin.Metadata
final class<null> SamGenericSuperinterfaceKt$sam$Sam$0 {
// source: 'samGenericSuperinterface.kt'
<null> method <init>(p0: kotlin.jvm.functions.Function0): void
public synthetic final <null> method get(): java.lang.Object
private synthetic final field <null> function: kotlin.jvm.functions.Function0
}
@kotlin.Metadata
public final class<null> SamGenericSuperinterfaceKt {
// source: 'samGenericSuperinterface.kt'
public final static <(Lkotlin/jvm/functions/Function0<Ljava/lang/String;>;)Ljava/lang/String;> method specializedSam(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.String
}
@@ -0,0 +1,15 @@
@kotlin.Metadata
final class<null> SamGenericSuperinterfaceKt$sam$Sam$0 {
// source: 'samGenericSuperinterface.kt'
<null> method <init>(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): void
public synthetic final <null> method get(): java.lang.Object
private synthetic final field <null> function: kotlin.jvm.functions.Function0
final inner class SamGenericSuperinterfaceKt$sam$Sam$0
}
@kotlin.Metadata
public final class<null> SamGenericSuperinterfaceKt {
// source: 'samGenericSuperinterface.kt'
public final static <(Lkotlin/jvm/functions/Function0<Ljava/lang/String;>;)Ljava/lang/String;> method specializedSam(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): java.lang.String
final inner class SamGenericSuperinterfaceKt$sam$Sam$0
}
@@ -214,6 +214,16 @@ public class BytecodeListingTestGenerated extends AbstractBytecodeListingTest {
runTest("compiler/testData/codegen/bytecodeListing/samAdapterAndInlinedOne.kt");
}
@TestMetadata("samGenericSuperinterface.kt")
public void testSamGenericSuperinterface() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/samGenericSuperinterface.kt");
}
@TestMetadata("samSpecializedGenericSuperinterface.kt")
public void testSamSpecializedGenericSuperinterface() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/samSpecializedGenericSuperinterface.kt");
}
@TestMetadata("varargsBridge.kt")
public void testVarargsBridge() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/varargsBridge.kt");
@@ -214,6 +214,16 @@ public class IrBytecodeListingTestGenerated extends AbstractIrBytecodeListingTes
runTest("compiler/testData/codegen/bytecodeListing/samAdapterAndInlinedOne.kt");
}
@TestMetadata("samGenericSuperinterface.kt")
public void testSamGenericSuperinterface() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/samGenericSuperinterface.kt");
}
@TestMetadata("samSpecializedGenericSuperinterface.kt")
public void testSamSpecializedGenericSuperinterface() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/samSpecializedGenericSuperinterface.kt");
}
@TestMetadata("varargsBridge.kt")
public void testVarargsBridge() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/varargsBridge.kt");