From 5309e774ac7659ae3d4af8162604f2491bbc59bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Mon, 6 Jan 2020 16:34:00 +0100 Subject: [PATCH] JVM IR: Remove FAKE_OWNER from MethodSignatureMapper FAKE_OWNER is a hack for when we encounter IrFunctions with non-class parents during codegen. This can only happen for unhandled intrinsic functions and resolving them to FAKE_OWNER can cause codegen to succeed while producing broken bytecode. --- .../backend/jvm/codegen/ExpressionCodegen.kt | 5 +++-- .../backend/jvm/codegen/MethodSignatureMapper.kt | 14 +------------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt index 1923fa8d8c9..8bd18bef580 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt @@ -367,8 +367,9 @@ class ExpressionCodegen( classCodegen.context.irIntrinsics.getIntrinsic(expression.symbol) ?.invoke(expression, this, data)?.let { return it } - val callable = methodSignatureMapper.mapToCallableMethod(irFunction, expression) val callee = expression.symbol.owner + require(callee.parent is IrClass) { "Unhandled intrinsic in ExpressionCodegen: ${callee.render()}" } + val callable = methodSignatureMapper.mapToCallableMethod(irFunction, expression) val callGenerator = getOrCreateCallGenerator(expression, data, callable.signature) val asmType = if (expression is IrConstructorCall) typeMapper.mapTypeAsDeclaration(expression.type) else expression.asmType @@ -1022,7 +1023,7 @@ class ExpressionCodegen( } } - val methodOwner = callee.parent.safeAs()?.let(typeMapper::mapClass) ?: MethodSignatureMapper.FAKE_OWNER_TYPE + val methodOwner = typeMapper.mapClass(callee.parentAsClass) val sourceCompiler = IrSourceCompilerForInline(state, element, callee, this, data) val reifiedTypeInliner = ReifiedTypeInliner(mappings, object : ReifiedTypeInliner.IntrinsicsSupport { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/MethodSignatureMapper.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/MethodSignatureMapper.kt index a8d083e34a3..27bc878ef3f 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/MethodSignatureMapper.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/MethodSignatureMapper.kt @@ -285,15 +285,7 @@ class MethodSignatureMapper(private val context: JvmBackendContext) { fun mapToCallableMethod(caller: IrFunction, expression: IrFunctionAccessExpression): IrCallableMethod { val callee = expression.symbol.owner.getOrCreateSuspendFunctionViewIfNeeded(context) - val calleeParent = callee.parent - if (calleeParent !is IrClass) { - // Non-class parent is only possible for intrinsics created in IrBuiltIns, such as dataClassArrayMemberHashCode. In that case, - // we still need to return some IrCallableMethod with some owner instance, but that owner will be ignored at the call site. - // Here we return a fake type, but this needs to be refactored so that we never call mapToCallableMethod on intrinsics. - // TODO: get rid of fake owner here - return IrCallableMethod(FAKE_OWNER_TYPE, Opcodes.INVOKESTATIC, mapSignatureSkipGeneric(callee), false) - } - + val calleeParent = callee.parentAsClass val owner = typeMapper.mapClass(calleeParent) if (callee !is IrSimpleFunction) { @@ -351,8 +343,4 @@ class MethodSignatureMapper(private val context: JvmBackendContext) { } return current } - - companion object { - val FAKE_OWNER_TYPE = Type.getObjectType("kotlin/internal/ir/Intrinsic") - } }