Skip to content

Commit

Permalink
Support intrinsifying of serializer() function when it is seen inside…
Browse files Browse the repository at this point in the history
… multifile class part

It's needed to be able to correctly compile kotlinx-serialization-core itself,
as compiler sees real function, not one from a facade.

#KT-56244 Fixed
  • Loading branch information
sandwwraith committed Jan 27, 2023
1 parent 2d1e8db commit 46ddcac
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.
*/

Expand All @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.codegen.inline.ReifiedTypeInliner.Companion.pluginIn
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.descriptors.findClassAcrossModuleDependencies
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
Expand Down Expand Up @@ -114,9 +115,21 @@ class SerializationJvmIrIntrinsicSupport(val jvmBackendContext: JvmBackendContex

}

/**
* Method for intrinsification `kotlinx.serialization.serializer` is a top-level function.
* For the rest of the world, it is located in the facade `kotlinx.serialization.SerializersKt`.
* However, when we compile `kotlinx-serialization-core` itself, facade contains only synthetic bridges.
* Real function is contained in IR class with `SerializersKt__SerializersKt` name.
* (as we have `@file:JvmMultifileClass @file:JvmName("SerializersKt")` on both common Serializers.kt and a platform-specific SerializersJvm.kt files)
*/
private fun IrFunction.isTargetMethod(): Boolean {
val fqName = fqNameWhenAvailable?.asString() ?: return false
return fqName == "kotlinx.serialization.SerializersKt.serializer" || fqName == "kotlinx.serialization.SerializersKt__SerializersKt.serializer"
}

override fun getIntrinsic(symbol: IrFunctionSymbol): IntrinsicMethod? {
val method = symbol.owner
if (method.fqNameWhenAvailable?.asString() != "kotlinx.serialization.SerializersKt.serializer"
if (!method.isTargetMethod()
|| method.dispatchReceiverParameter != null
|| method.typeParameters.size != 1
|| method.valueParameters.isNotEmpty()
Expand Down

0 comments on commit 46ddcac

Please sign in to comment.