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
@@ -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>()