[FIR Plugin] Reproduce KT-56173
This commit is contained in:
committed by
Space Team
parent
083f54aceb
commit
81da96fed3
+45
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.ir.plugin
|
||||
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
|
||||
import org.jetbrains.kotlin.ir.deepCopyWithVariables
|
||||
import org.jetbrains.kotlin.ir.expressions.IrReturn
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
|
||||
class BodyWithDefaultValueReplacer : IrElementVisitorVoid {
|
||||
companion object {
|
||||
val NAME = Name.identifier("replaceMeWithDefault")
|
||||
}
|
||||
|
||||
override fun visitFile(declaration: IrFile) {
|
||||
declaration.acceptChildrenVoid(this)
|
||||
}
|
||||
|
||||
override fun visitClass(declaration: IrClass) {
|
||||
declaration.acceptChildrenVoid(this)
|
||||
}
|
||||
|
||||
override fun visitSimpleFunction(declaration: IrSimpleFunction) {
|
||||
if (declaration.name != NAME) return
|
||||
val defaultValue = declaration.valueParameters.first().defaultValue ?: return
|
||||
val bodyReplacer = object : IrElementVisitorVoid {
|
||||
override fun visitElement(element: IrElement) {
|
||||
element.acceptChildrenVoid(this)
|
||||
}
|
||||
|
||||
override fun visitReturn(expression: IrReturn) {
|
||||
expression.value = defaultValue.expression.deepCopyWithVariables()
|
||||
}
|
||||
}
|
||||
declaration.body?.acceptChildrenVoid(bodyReplacer)
|
||||
}
|
||||
}
|
||||
+1
@@ -20,6 +20,7 @@ class GeneratedDeclarationsIrBodyFiller : IrGenerationExtension {
|
||||
AllPropertiesConstructorIrGenerator(pluginContext),
|
||||
TransformerForAddingAnnotations(pluginContext),
|
||||
ComposableFunctionsTransformer(pluginContext),
|
||||
BodyWithDefaultValueReplacer(),
|
||||
)
|
||||
|
||||
for (transformer in transformers) {
|
||||
|
||||
Vendored
+26
@@ -0,0 +1,26 @@
|
||||
Module: common
|
||||
FILE: common.kt
|
||||
public final expect fun replaceMeWithDefault(s: R|kotlin/String| = String(OK)): R|kotlin/String|
|
||||
public final fun commonBox(): R|kotlin/String| {
|
||||
^commonBox R|/replaceMeWithDefault|(String(Fail))
|
||||
}
|
||||
FILE: main.kt
|
||||
public final actual fun replaceMeWithDefault(s: R|kotlin/String|): R|kotlin/String| {
|
||||
^replaceMeWithDefault R|<local>/s|
|
||||
}
|
||||
public final fun box(): R|kotlin/String| {
|
||||
^box R|/commonBox|()
|
||||
}
|
||||
Module: jvm
|
||||
FILE: common.kt
|
||||
public final expect fun replaceMeWithDefault(s: R|kotlin/String| = String(OK)): R|kotlin/String|
|
||||
public final fun commonBox(): R|kotlin/String| {
|
||||
^commonBox R|/replaceMeWithDefault|(String(Fail))
|
||||
}
|
||||
FILE: main.kt
|
||||
public final actual fun replaceMeWithDefault(s: R|kotlin/String|): R|kotlin/String| {
|
||||
^replaceMeWithDefault R|<local>/s|
|
||||
}
|
||||
public final fun box(): R|kotlin/String| {
|
||||
^box R|/commonBox|()
|
||||
}
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// IGNORE_BACKEND: JVM_IR
|
||||
// LANGUAGE: +MultiPlatformProjects
|
||||
// ISSUE: KT-56173
|
||||
|
||||
// MODULE: common
|
||||
// TARGET_PLATFORM: Common
|
||||
// FILE: common.kt
|
||||
|
||||
expect fun replaceMeWithDefault(s: String = "OK"): String
|
||||
|
||||
fun commonBox(): String = replaceMeWithDefault("Fail")
|
||||
|
||||
// MODULE: jvm()()(common)
|
||||
// TARGET_PLATFORM: JVM
|
||||
// FILE: main.kt
|
||||
|
||||
actual fun replaceMeWithDefault(s: String): String = s
|
||||
|
||||
fun box(): String {
|
||||
return commonBox()
|
||||
}
|
||||
+6
@@ -79,6 +79,12 @@ public class FirLightTreePluginBlackBoxCodegenTestGenerated extends AbstractFirL
|
||||
runTest("plugins/fir-plugin-prototype/testData/box/newSupertype.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("replaceActualFunctionBodyWitExpectDefaultValue.kt")
|
||||
public void testReplaceActualFunctionBodyWitExpectDefaultValue() throws Exception {
|
||||
runTest("plugins/fir-plugin-prototype/testData/box/replaceActualFunctionBodyWitExpectDefaultValue.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("serializer.kt")
|
||||
public void testSerializer() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user