Skip to content

Commit f943cad

Browse files
jDramaixcopybara-github
authored andcommitted
[KT] Upgrade callable references before function inlining.
The J2clUpgradeCallableReferences pass is now run earlier in the lowering pipeline to ensure callable references are in the correct form before other passes, including function inlining. Having `IrRichCallableReference` nodes in the IR tree before inlining allows for the removal of J2clInlineCallableReferenceToLambdaPhase and WrapInlineDeclarationsWithReifiedTypeParametersLowering. We also need to run explicitly J2clUpgradeCallableReferences on inline function before the inlining. The serialized IR coming from Kotlin/JVM contains the old node to represent lambda and break their correct inlining. PiperOrigin-RevId: 886825091
1 parent 3f3d378 commit f943cad

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

transpiler/java/com/google/j2cl/transpiler/frontend/kotlin/lower/FunctionInlining.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ constructor(
9797
if (actualCallee.body == null) {
9898
return expression
9999
}
100+
101+
// MODIFIED BY GOOGLE.
102+
// The serialized IR from Kotlin/JVM compilation does not fully support
103+
// `IrRichCallableReference` nodes. Run `J2clUpgradeCallableReferences` to ensure the inline
104+
// function body uses the correct node types expected by the inliner.
105+
J2clUpgradeCallableReferences(context).lower(actualCallee)
106+
// END OF MODIFICATIONS.
107+
100108
actualCallee.body?.transformChildren(this, actualCallee)
101109
actualCallee.parameters.forEachIndexed { index, param ->
102110
if (expression.arguments[index] == null) {
@@ -597,10 +605,9 @@ private class CallInlining(
597605
argumentExpression.isLambdaBlock())
598606

599607
val isImmutableVariableLoad: Boolean
600-
get() =
601-
argumentExpression.let { argument ->
602-
argument is IrGetValue && !argument.symbol.owner.let { it is IrVariable && it.isVar }
603-
}
608+
get() = argumentExpression.let { argument ->
609+
argument is IrGetValue && !argument.symbol.owner.let { it is IrVariable && it.isVar }
610+
}
604611
}
605612

606613
private fun ParameterToArgument.allOuterClasses(): List<ParameterToArgument> {

transpiler/java/com/google/j2cl/transpiler/frontend/kotlin/lower/J2clUpgradeCallableReferences.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.j2cl.transpiler.frontend.kotlin.lower
1818

19+
import org.jetbrains.kotlin.backend.common.LoweringContext
1920
import org.jetbrains.kotlin.backend.common.lower.UpgradeCallableReferences
2021
import org.jetbrains.kotlin.ir.declarations.IrFile
2122
import org.jetbrains.kotlin.ir.expressions.IrExpression
@@ -28,7 +29,7 @@ import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
2829
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
2930

3031
/** Upgrades property and function reference-like nodes to the new IrRichCallableReference node. */
31-
class J2clUpgradeCallableReferences(context: J2clBackendContext) :
32+
class J2clUpgradeCallableReferences(context: LoweringContext) :
3233
UpgradeCallableReferences(
3334
context,
3435
upgradeFunctionReferencesAndLambdas = true,

transpiler/java/com/google/j2cl/transpiler/frontend/kotlin/lower/LoweringPasses.kt

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ private val loweringPhase = loweringPhase {
7373
// Invent names for local classes and anonymous objects. Later passes may require all classes
7474
// to have a name for computing function signature.
7575
perFileLowering(jvmInventNamesForLocalClassesFactory)
76-
// Transform all callable reference (including defaults) to inline lambdas, mark inline lambdas
77-
// for later passes.
78-
perFileLowering(::J2clInlineCallableReferenceToLambdaPhase)
7976
// Rewrites `Array(size) { index -> value }` using type-specific initializer lambdas.
8077
perFileLowering(::ArrayConstructorLowering)
78+
// TODO(dramaix) : This should be run as the first pass. But our inlining pass is adding function
79+
// references and function expression.
80+
moduleLowering(::J2clUpgradeCallableReferences)
8181
// Create nullable backing fields and insert nullability checks for lateinit properties and
8282
// variables. Must run before JvmPropertiesLowering.
8383
perFileLowering(::LateinitLowering)
@@ -89,21 +89,8 @@ private val loweringPhase = loweringPhase {
8989
perFileLowering(::LocalClassesExtractionFromInlineFunctionsLowering.asPostfix())
9090
// Create public bridge for private top level function called from inline functions.
9191
perFileLowering(::SyntheticAccessorLowering)
92-
// TODO(b/449125803) : remove that pass and use `UpgradeCallableReferences` instead.
93-
// Replace reference to inline function with reified parameter with reference to a synthetic
94-
// non-inline function where types parameters have been substituted so there is no reference to
95-
// inline functions anymore.
96-
// ex:
97-
// inline fun <reified T> castTo(param: Any): T = param as T
98-
// reference like `::castTo<String>` is replaced with `::castTo$wrap` where
99-
// fun castTo$wrap(param: Any) String = castTo<String>(param)
100-
perFileLowering(::WrapInlineDeclarationsWithReifiedTypeParametersLowering)
10192
// Perform function inlining.
10293
moduleLowering(::J2clFunctionInlining)
103-
// TODO(dramaix) : This should be run as the first pass. But our inlining pass is adding function
104-
// references and function expression.
105-
moduleLowering(::J2clUpgradeCallableReferences)
106-
10794
// Remove inline functions with reified type parameters as these functions cannot be called from
10895
// Java
10996
perFileLowering(::RemoveInlineDeclarationsWithReifiedTypeParametersLowering)

transpiler/javatests/com/google/j2cl/readable/kotlin/localfunction/output_closure/LocalFunctionKt.impl.java.js.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ class LocalFunctionKt extends j_l_Object {
9090
}
9191
/** @nodts @return {string} */
9292
static m_localFunctionInInlinedLambda__java_lang_String() {
93-
let tmp0__this_ = StringBuilder.$create__();
94-
let this_1 = tmp0__this_;
95-
let appendTwoDigits = (/** !Appendable */ this_2, /** number */ number_1) =>{
93+
let anonymous_$appendTwoDigit = (/** !Appendable */ this_1, /** !StringBuilder */ $this_apply, /** number */ number_1) =>{
9694
if (number_1 < 10) {
97-
this_2.m_append__char__java_lang_Appendable(48 /* '0' */);
95+
this_1.m_append__char__java_lang_Appendable(48 /* '0' */);
9896
}
99-
this_1.m_append__int__java_lang_StringBuilder(number_1);
97+
$this_apply.m_append__int__java_lang_StringBuilder(number_1);
10098
};
101-
appendTwoDigits(this_1, 1);
102-
return InternalPreconditions.m_checkNotNull__java_lang_Object__java_lang_Object(this_1.toString());
99+
let tmp0__this_ = StringBuilder.$create__();
100+
let this_2 = tmp0__this_;
101+
anonymous_$appendTwoDigit(this_2, this_2, 1);
102+
return InternalPreconditions.m_checkNotNull__java_lang_Object__java_lang_Object(this_2.toString());
103103
}
104104
/** @nodts @template T @return {string} */
105105
static m_localFunctionWithGenericType__java_lang_Object__java_lang_String(/** T */ t) {

0 commit comments

Comments
 (0)