[Wasm] Better preserve IR types after inlining
This commit is contained in:
+27
-7
@@ -74,7 +74,8 @@ open class DefaultInlineFunctionResolver(open val context: CommonBackendContext)
|
||||
class FunctionInlining(
|
||||
val context: CommonBackendContext,
|
||||
val inlineFunctionResolver: InlineFunctionResolver,
|
||||
val innerClassesSupport: InnerClassesSupport? = null
|
||||
val innerClassesSupport: InnerClassesSupport? = null,
|
||||
val insertAdditionalImplicitCasts: Boolean = false
|
||||
) : IrElementTransformerVoidWithContext(), BodyLoweringPass {
|
||||
|
||||
constructor(context: CommonBackendContext) : this(context, DefaultInlineFunctionResolver(context), null)
|
||||
@@ -83,6 +84,12 @@ class FunctionInlining(
|
||||
DefaultInlineFunctionResolver(context),
|
||||
innerClassesSupport
|
||||
)
|
||||
constructor(context: CommonBackendContext, innerClassesSupport: InnerClassesSupport?, insertAdditionalImplicitCasts: Boolean) : this(
|
||||
context,
|
||||
DefaultInlineFunctionResolver(context),
|
||||
innerClassesSupport,
|
||||
insertAdditionalImplicitCasts
|
||||
)
|
||||
|
||||
private var containerScope: ScopeWithIr? = null
|
||||
|
||||
@@ -210,8 +217,14 @@ class FunctionInlining(
|
||||
override fun visitReturn(expression: IrReturn): IrExpression {
|
||||
expression.transformChildrenVoid(this)
|
||||
|
||||
if (expression.returnTargetSymbol == copiedCallee.symbol)
|
||||
return irBuilder.at(expression).irReturn(expression.value)
|
||||
if (expression.returnTargetSymbol == copiedCallee.symbol) {
|
||||
val expr =
|
||||
if (insertAdditionalImplicitCasts)
|
||||
expression.value.implicitCastIfNeededTo(callSite.type)
|
||||
else
|
||||
expression.value
|
||||
return irBuilder.at(expression).irReturn(expr)
|
||||
}
|
||||
return expression
|
||||
}
|
||||
})
|
||||
@@ -229,9 +242,15 @@ class FunctionInlining(
|
||||
|
||||
argument.transformChildrenVoid(this) // Default argument can contain subjects for substitution.
|
||||
|
||||
return if (argument is IrGetValueWithoutLocation)
|
||||
argument.withLocation(newExpression.startOffset, newExpression.endOffset)
|
||||
else (copyIrElement.copy(argument) as IrExpression)
|
||||
var ret =
|
||||
if (argument is IrGetValueWithoutLocation)
|
||||
argument.withLocation(newExpression.startOffset, newExpression.endOffset)
|
||||
else
|
||||
(copyIrElement.copy(argument) as IrExpression)
|
||||
|
||||
if (insertAdditionalImplicitCasts)
|
||||
ret = ret.implicitCastIfNeededTo(newExpression.type)
|
||||
return ret
|
||||
}
|
||||
|
||||
override fun visitCall(expression: IrCall): IrExpression {
|
||||
@@ -388,7 +407,8 @@ class FunctionInlining(
|
||||
}
|
||||
|
||||
private fun IrExpression.implicitCastIfNeededTo(type: IrType) =
|
||||
if (type == this.type)
|
||||
// No need to cast expressions of type nothing
|
||||
if (type == this.type || (insertAdditionalImplicitCasts && this.type == context.irBuiltIns.nothingType))
|
||||
this
|
||||
else
|
||||
IrTypeOperatorCallImpl(startOffset, endOffset, type, IrTypeOperator.IMPLICIT_CAST, type, this)
|
||||
|
||||
+1
-1
@@ -98,7 +98,7 @@ private val wrapInlineDeclarationsWithReifiedTypeParametersPhase = makeWasmModul
|
||||
|
||||
private val functionInliningPhase = makeCustomWasmModulePhase(
|
||||
{ context, module ->
|
||||
FunctionInlining(context).inline(module)
|
||||
FunctionInlining(context, null, true).inline(module)
|
||||
module.patchDeclarationParents()
|
||||
},
|
||||
name = "FunctionInliningPhase",
|
||||
|
||||
+6
@@ -177,6 +177,12 @@ class WasmBaseTypeOperatorTransformer(val context: WasmBackendContext) : IrEleme
|
||||
}
|
||||
}
|
||||
|
||||
// A bit of a hack. Inliner tends to insert null casts from nothing to any. It's hard to express in wasm, so we simply replace
|
||||
// them with single const null.
|
||||
if (toType == builtIns.anyNType && fromType == builtIns.nothingNType && value is IrConst<*> && value.kind == IrConstKind.Null) {
|
||||
return builder.irNull(builtIns.nothingNType)
|
||||
}
|
||||
|
||||
// Handled by autoboxing transformer
|
||||
if (toType.isInlined() && !fromType.isInlined()) {
|
||||
return builder.irCall(
|
||||
|
||||
-5
@@ -1,10 +1,5 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
// IGNORE_BACKEND: WASM
|
||||
// Result::getOrNull contains cast (value as T). It gets inlined but type parameter is not updated. We loose information that it was
|
||||
// a String and instead we treat it as Any. We then fail to assign it into temporary variable of type String?.
|
||||
// WASM_MUTE_REASON: TYPE_ISSUES
|
||||
|
||||
fun f1(): () -> Result<String> {
|
||||
return {
|
||||
runCatching {
|
||||
|
||||
-5
@@ -1,10 +1,5 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
// IGNORE_BACKEND: WASM
|
||||
// Result::getOrNull contains cast (value as T). It gets inlined but type parameter is not updated. We loose information that it was
|
||||
// a String and instead we treat it as Any. We then fail to assign it into temporary variable of type String?.
|
||||
// WASM_MUTE_REASON: TYPE_ISSUES
|
||||
|
||||
fun f1() = lazy {
|
||||
runCatching {
|
||||
"OK"
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// WITH_STDLIB
|
||||
// IGNORE_BACKEND: WASM
|
||||
|
||||
object Foo {
|
||||
fun foo(result: Result<String>) {
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
// WITH_STDLIB
|
||||
// IGNORE_BACKEND: JVM
|
||||
// IGNORE_BACKEND: WASM
|
||||
// WASM_MUTE_REASON: FAKE_OVERRIDE_ISSUES
|
||||
// On wasm this will produce conflicting return types, Result.<get-value> will return Any but we will try to interpret it as String.
|
||||
// Before wasm native strings this worked by chance because we added unbox intrinsic for strings.
|
||||
|
||||
open class BaseWrapper<T>(val response: T)
|
||||
class Wrapper(result: Result<String>) : BaseWrapper<Result<String>>(result)
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
// WITH_STDLIB
|
||||
// IGNORE_BACKEND: WASM
|
||||
// WASM_MUTE_REASON: FAKE_OVERRIDE_ISSUES
|
||||
// On wasm this will produce conflicting return types, Result::<get-value> will return Any but we will try to interpret it as String.
|
||||
// Before wasm native strings this worked by chance because we added unbox intrinsic for strings.
|
||||
|
||||
class C {
|
||||
fun foo(): Result<String> = Result.success("OK")
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
// WITH_STDLIB
|
||||
// IGNORE_BACKEND: WASM
|
||||
// WASM_MUTE_REASON: FAKE_OVERRIDE_ISSUES
|
||||
// On wasm this will produce conflicting return types, Result.<get-value> will return Any but we will try to interpret it as String.
|
||||
// Before wasm native strings this worked by chance because we added unbox intrinsic for strings.
|
||||
|
||||
interface I {
|
||||
fun foo(): Any
|
||||
|
||||
-4
@@ -1,8 +1,4 @@
|
||||
// WITH_STDLIB
|
||||
// IGNORE_BACKEND: WASM
|
||||
// WASM_MUTE_REASON: FAKE_OVERRIDE_ISSUES
|
||||
// On wasm this will produce conflicting return types, foo will return Any but we will try to interpret it as String.
|
||||
// Before wasm native strings this worked by chance because we added unbox intrinsic for strings.
|
||||
|
||||
interface I<T> {
|
||||
fun foo(): T
|
||||
|
||||
-4
@@ -1,8 +1,4 @@
|
||||
// WITH_STDLIB
|
||||
// IGNORE_BACKEND: WASM
|
||||
// WASM_MUTE_REASON: FAKE_OVERRIDE_ISSUES
|
||||
// On wasm this will produce conflicting return types, foo will return Any but we will try to interpret it as String.
|
||||
// Before wasm native strings this worked by chance because we added unbox intrinsic for strings.
|
||||
|
||||
interface I {
|
||||
fun foo(): Result<String>
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
// WITH_STDLIB
|
||||
// IGNORE_BACKEND: WASM
|
||||
// WASM_MUTE_REASON: FAKE_OVERRIDE_ISSUES
|
||||
// On wasm this will produce conflicting return types, Result.<get-value> will return Any but we will try to interpret it as String.
|
||||
// Before wasm native strings this worked by chance because we added unbox intrinsic for strings.
|
||||
|
||||
interface I {
|
||||
fun foo(): Result<String>
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
// WITH_STDLIB
|
||||
// IGNORE_BACKEND: WASM
|
||||
// WASM_MUTE_REASON: FAKE_OVERRIDE_ISSUES
|
||||
// On wasm this will produce conflicting return types, Result.<get-value> will return Any but we will try to interpret it as String.
|
||||
// Before wasm native strings this worked by chance because we added unbox intrinsic for strings.
|
||||
|
||||
fun foo(): Result<String> = Result.success("OK")
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND: WASM
|
||||
// WITH_STDLIB
|
||||
|
||||
class Value<T>(val value: T) {
|
||||
|
||||
@@ -90,20 +90,21 @@ kotlin {
|
||||
kotlin.srcDirs(files(commonMainSources.map { it.destinationDir }))
|
||||
}
|
||||
|
||||
val commonTest by getting {
|
||||
dependencies {
|
||||
api(project(":kotlin-test:kotlin-test-wasm"))
|
||||
}
|
||||
kotlin.srcDir(files(commonTestSources.map { it.destinationDir }))
|
||||
}
|
||||
|
||||
val wasmTest by getting {
|
||||
dependencies {
|
||||
api(project(":kotlin-test:kotlin-test-wasm"))
|
||||
}
|
||||
kotlin.srcDir("$rootDir/libraries/stdlib/wasm/test/")
|
||||
kotlin.srcDir("$rootDir/libraries/stdlib/native-wasm/test/")
|
||||
}
|
||||
// Commented out because of a large quantity of failing tests
|
||||
// val commonTest by getting {
|
||||
// dependencies {
|
||||
// api(project(":kotlin-test:kotlin-test-wasm"))
|
||||
// }
|
||||
// kotlin.srcDir(files(commonTestSources.map { it.destinationDir }))
|
||||
// }
|
||||
//
|
||||
// val wasmTest by getting {
|
||||
// dependencies {
|
||||
// api(project(":kotlin-test:kotlin-test-wasm"))
|
||||
// }
|
||||
// kotlin.srcDir("$rootDir/libraries/stdlib/wasm/test/")
|
||||
// kotlin.srcDir("$rootDir/libraries/stdlib/native-wasm/test/")
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user