JVM IR: erase parameter default value when copying it to bridge

#KT-48391 Fixed
This commit is contained in:
Alexander Udalov
2021-09-02 23:39:44 +02:00
parent bcafece28e
commit a06fc20680
12 changed files with 73 additions and 14 deletions
@@ -12840,6 +12840,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/defaultArguments/kt47073_nested.kt");
}
@Test
@TestMetadata("kt48391.kt")
public void testKt48391() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt");
}
@Test
@TestMetadata("kt6382.kt")
public void testKt6382() throws Exception {
@@ -563,11 +563,15 @@ internal class BridgeLowering(val context: JvmBackendContext) : FileLoweringPass
substitutedType: IrType? = null
): IrValueParameter = copyTo(
target, IrDeclarationOrigin.BRIDGE,
startOffset = target.startOffset,
endOffset = target.endOffset,
type = (substitutedType?.eraseToScope(visibleTypeParameters) ?: type.eraseTypeParameters()),
// Currently there are no special bridge methods with vararg parameters, so we don't track substituted vararg element types.
varargElementType = varargElementType?.eraseToScope(visibleTypeParameters),
startOffset = target.startOffset,
endOffset = target.endOffset,
// If the parameter has a default value, replace it with a stub, as if this function is coming from an external dependency.
// Otherwise it can lead to all sorts of problems, for example this default value can reference private functions from another
// file, which would rightfully make SyntheticAccessorLowering fail.
defaultValue = if (defaultValue != null) createStubDefaultValue() else null,
)
private fun IrBuilderWithScope.delegatingCall(
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.lazy.*
import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.expressions.impl.IrErrorExpressionImpl
import org.jetbrains.kotlin.ir.linkage.IrProvider
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol
@@ -241,13 +240,7 @@ abstract class DeclarationStubGenerator(
varargElementType?.toIrType(), isCrossinline, isNoinline, isHidden = false, isAssignable = false
).also { irValueParameter ->
if (descriptor.declaresDefaultValue()) {
irValueParameter.defaultValue =
irValueParameter.factory.createExpressionBody(
IrErrorExpressionImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, descriptor.type.toIrType(),
"Stub expression for default value of ${descriptor.name}"
)
)
irValueParameter.defaultValue = irValueParameter.createStubDefaultValue()
}
}
}
@@ -6,10 +6,7 @@
package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
@@ -174,6 +171,11 @@ fun IrExpression.coerceToUnitIfNeeded(valueType: IrType, irBuiltIns: IrBuiltIns,
fun IrFunctionAccessExpression.usesDefaultArguments(): Boolean =
symbol.owner.valueParameters.any { this.getValueArgument(it.index) == null }
fun IrValueParameter.createStubDefaultValue(): IrExpressionBody =
factory.createExpressionBody(
IrErrorExpressionImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, type, "Stub expression for default value of $name")
)
val IrClass.functions: Sequence<IrSimpleFunction>
get() = declarations.asSequence().filterIsInstance<IrSimpleFunction>()
@@ -0,0 +1,17 @@
// FILE: 1.kt
class B : A<String>
fun box(): String =
B().f()!!
// FILE: 2.kt
interface A<T> {
fun f(s: String = RESULT): T? =
s as T
companion object {
private val RESULT = "OK"
}
}
@@ -12768,6 +12768,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/defaultArguments/kt47073_nested.kt");
}
@Test
@TestMetadata("kt48391.kt")
public void testKt48391() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt");
}
@Test
@TestMetadata("kt6382.kt")
public void testKt6382() throws Exception {
@@ -12840,6 +12840,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/defaultArguments/kt47073_nested.kt");
}
@Test
@TestMetadata("kt48391.kt")
public void testKt48391() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt");
}
@Test
@TestMetadata("kt6382.kt")
public void testKt6382() throws Exception {
@@ -10335,6 +10335,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/defaultArguments/kt47073_nested.kt");
}
@TestMetadata("kt48391.kt")
public void testKt48391() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt");
}
@TestMetadata("kt6382.kt")
public void testKt6382() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt6382.kt");
@@ -9189,6 +9189,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/defaultArguments/kt47073_nested.kt");
}
@TestMetadata("kt48391.kt")
public void testKt48391() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt");
}
@TestMetadata("kt6382.kt")
public void testKt6382() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt6382.kt");
@@ -8595,6 +8595,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/defaultArguments/kt47073_nested.kt");
}
@TestMetadata("kt48391.kt")
public void testKt48391() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt");
}
@TestMetadata("kt6382.kt")
public void testKt6382() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt6382.kt");
@@ -8575,6 +8575,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/defaultArguments/kt47073_nested.kt");
}
@TestMetadata("kt48391.kt")
public void testKt48391() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt");
}
@TestMetadata("kt6382.kt")
public void testKt6382() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt6382.kt");
@@ -4100,6 +4100,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/defaultArguments/kt47073_nested.kt");
}
@TestMetadata("kt48391.kt")
public void testKt48391() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt48391.kt");
}
@TestMetadata("kt6382.kt")
public void testKt6382() throws Exception {
runTest("compiler/testData/codegen/box/defaultArguments/kt6382.kt");