Lower fun interface children's body during inline class lowering

#KT-51121 Fixed
This commit is contained in:
Ilmir Usmanov
2022-02-21 02:29:16 +01:00
committed by teamcity
parent 72532f6f03
commit c072448d22
12 changed files with 163 additions and 10 deletions
@@ -23200,6 +23200,18 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/javaSamReturnResult.kt");
}
@Test
@TestMetadata("kt51121.kt")
public void testKt51121() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121.kt", TransformersFunctions.getReplaceOptionalJvmInlineAnnotationWithReal());
}
@Test
@TestMetadata("kt51121_2.kt")
public void testKt51121_2() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121_2.kt", TransformersFunctions.getReplaceOptionalJvmInlineAnnotationWithReal());
}
@Test
@TestMetadata("mangledSamWrappers.kt")
public void testMangledSamWrappers() throws Exception {
@@ -122,10 +122,14 @@ private class JvmInlineClassLowering(private val context: JvmBackendContext) : F
return null
}
// If fun interface methods are already mangled, do not mangle them twice.
if (function is IrSimpleFunction && function.overriddenSymbols.any { it.owner.parentAsClass.isFun } &&
function.name.asString().substringAfterLast('-') == replacement.name.asString().substringAfterLast('-')
) return null
if (function is IrSimpleFunction && function.overriddenSymbols.any { it.owner.parentAsClass.isFun }) {
// If fun interface methods are already mangled, do not mangle them twice.
val suffix = function.hashSuffix()
if (suffix != null && function.name.asString().endsWith(suffix)) {
function.transformChildrenVoid()
return null
}
}
addBindingsFor(function, replacement)
return when (function) {
@@ -135,6 +139,13 @@ private class JvmInlineClassLowering(private val context: JvmBackendContext) : F
}
}
private fun IrFunction.hashSuffix(): String? =
InlineClassAbi.hashSuffix(
this,
context.state.functionsWithInlineClassReturnTypesMangled,
context.state.useOldManglingSchemeForFunctionsWithInlineClassesInSignatures
)
private fun transformSimpleFunctionFlat(function: IrSimpleFunction, replacement: IrSimpleFunction): List<IrDeclaration> {
replacement.valueParameters.forEach {
it.transformChildrenVoid()
@@ -460,6 +471,10 @@ private class JvmInlineClassLowering(private val context: JvmBackendContext) : F
override fun visitReturn(expression: IrReturn): IrExpression {
expression.returnTargetSymbol.owner.safeAs<IrFunction>()?.let { target ->
val suffix = target.hashSuffix()
if (suffix != null && target.name.asString().endsWith(suffix))
return super.visitReturn(expression)
context.inlineClassReplacements.getReplacementFunction(target)?.let {
return context.createIrBuilder(it.symbol, expression.startOffset, expression.endOffset).irReturn(
expression.value.transform(this, null)
@@ -66,12 +66,7 @@ object InlineClassAbi {
return Name.identifier("constructor-impl")
}
val suffix = hashSuffix(
useOldMangleRules,
irFunction.fullValueParameterList.map { it.type },
irFunction.returnType.takeIf { mangleReturnTypes && irFunction.hasMangledReturnType },
irFunction.isSuspend
)
val suffix = hashSuffix(irFunction, mangleReturnTypes, useOldMangleRules)
if (suffix == null && ((irFunction.parent as? IrClass)?.isSingleFieldValueClass != true || irFunction.origin == IrDeclarationOrigin.IR_BUILTINS_STUB)) {
return irFunction.name
}
@@ -90,6 +85,14 @@ object InlineClassAbi {
return Name.identifier("$base-${suffix ?: "impl"}")
}
fun hashSuffix(irFunction: IrFunction, mangleReturnTypes: Boolean, useOldMangleRules: Boolean): String? =
hashSuffix(
useOldMangleRules,
irFunction.fullValueParameterList.map { it.type },
irFunction.returnType.takeIf { mangleReturnTypes && irFunction.hasMangledReturnType },
irFunction.isSuspend
)
fun hashSuffix(
useOldMangleRules: Boolean,
valueParameters: List<IrType>,
@@ -0,0 +1,19 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses
// IGNORE_BACKEND: JVM
// FILE: 1.kt
fun interface F {
fun foo(s: String): Z
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class Z(val value: String)
// FILE: 2.kt
val f: F = F { value -> Z(value) }
fun box(): String =
f.foo("OK").value
@@ -0,0 +1,20 @@
// WITH_STDLIB
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses
// IGNORE_BACKEND: JVM
// FILE: 1.kt
val f: F = F { value -> Z(value) }
fun box(): String =
f.foo("OK").value
// FILE: 2.kt
fun interface F {
fun foo(s: String): Z
}
OPTIONAL_JVM_INLINE_ANNOTATION
value class Z(val value: String)
@@ -22762,6 +22762,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/javaSamReturnResult.kt");
}
@Test
@TestMetadata("kt51121.kt")
public void testKt51121() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121.kt", TransformersFunctions.getReplaceOptionalJvmInlineAnnotationWithReal());
}
@Test
@TestMetadata("kt51121_2.kt")
public void testKt51121_2() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121_2.kt", TransformersFunctions.getReplaceOptionalJvmInlineAnnotationWithReal());
}
@Test
@TestMetadata("mangledSamWrappers.kt")
public void testMangledSamWrappers() throws Exception {
@@ -23200,6 +23200,18 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/javaSamReturnResult.kt");
}
@Test
@TestMetadata("kt51121.kt")
public void testKt51121() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121.kt", TransformersFunctions.getReplaceOptionalJvmInlineAnnotationWithReal());
}
@Test
@TestMetadata("kt51121_2.kt")
public void testKt51121_2() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121_2.kt", TransformersFunctions.getReplaceOptionalJvmInlineAnnotationWithReal());
}
@Test
@TestMetadata("mangledSamWrappers.kt")
public void testMangledSamWrappers() throws Exception {
@@ -18993,6 +18993,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/argumentResult.kt");
}
@TestMetadata("kt51121.kt")
public void ignoreKt51121() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121.kt", TransformersFunctions.getReplaceOptionalJvmInlineAnnotationWithReal());
}
@TestMetadata("kt51121_2.kt")
public void ignoreKt51121_2() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121_2.kt", TransformersFunctions.getReplaceOptionalJvmInlineAnnotationWithReal());
}
@TestMetadata("mangledSamWrappers.kt")
public void ignoreMangledSamWrappers() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/mangledSamWrappers.kt", TransformersFunctions.getReplaceOptionalJvmInlineAnnotationWithReal());
@@ -18198,6 +18198,18 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/argumentResult.kt");
}
@Test
@TestMetadata("kt51121.kt")
public void testKt51121() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
}
@Test
@TestMetadata("kt51121_2.kt")
public void testKt51121_2() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121_2.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
}
@Test
@TestMetadata("mangledSamWrappers.kt")
public void testMangledSamWrappers() throws Exception {
@@ -18162,6 +18162,18 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/argumentResult.kt");
}
@Test
@TestMetadata("kt51121.kt")
public void testKt51121() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
}
@Test
@TestMetadata("kt51121_2.kt")
public void testKt51121_2() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121_2.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
}
@Test
@TestMetadata("mangledSamWrappers.kt")
public void testMangledSamWrappers() throws Exception {
@@ -15354,6 +15354,16 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/argumentResult.kt");
}
@TestMetadata("kt51121.kt")
public void testKt51121() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
}
@TestMetadata("kt51121_2.kt")
public void testKt51121_2() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121_2.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
}
@TestMetadata("mangledSamWrappers.kt")
public void testMangledSamWrappers() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/mangledSamWrappers.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
@@ -20269,6 +20269,8 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
public FunInterface() {
register("compiler/testData/codegen/box/inlineClasses/funInterface/argumentIC.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
register("compiler/testData/codegen/box/inlineClasses/funInterface/argumentICGeneric.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
register("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
register("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121_2.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
register("compiler/testData/codegen/box/inlineClasses/funInterface/mangledSamWrappers.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
register("compiler/testData/codegen/box/inlineClasses/funInterface/mangledSamWrappersGeneric.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
register("compiler/testData/codegen/box/inlineClasses/funInterface/returnIC.kt", TransformersFunctions.getRemoveOptionalJvmInlineAnnotation());
@@ -20300,6 +20302,20 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/argumentResult.kt");
}
@Test
@TestMetadata("kt51121.kt")
public void testKt51121() throws Exception {
// There is a registered source transformer for the testcase: TransformersFunctions.getRemoveOptionalJvmInlineAnnotation()
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121.kt");
}
@Test
@TestMetadata("kt51121_2.kt")
public void testKt51121_2() throws Exception {
// There is a registered source transformer for the testcase: TransformersFunctions.getRemoveOptionalJvmInlineAnnotation()
runTest("compiler/testData/codegen/box/inlineClasses/funInterface/kt51121_2.kt");
}
@Test
@TestMetadata("mangledSamWrappers.kt")
public void testMangledSamWrappers() throws Exception {