[JS IR] Enable StringTrimLowering for JS IR backend

This commit is contained in:
Sergej Jaskiewicz
2021-10-06 17:19:22 +03:00
committed by Space
parent ca2f37f6eb
commit 9ee8f1e961
10 changed files with 197 additions and 0 deletions
@@ -41752,6 +41752,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/strings/surrogatePair.kt");
}
@Test
@TestMetadata("trimOptimization.kt")
public void testTrimOptimization() throws Exception {
runTest("compiler/testData/codegen/box/strings/trimOptimization.kt");
}
@Test
@TestMetadata("twoArgumentNullableStringOperatorPlus.kt")
public void testTwoArgumentNullableStringOperatorPlus() throws Exception {
@@ -423,6 +423,12 @@ private val foldConstantLoweringPhase = makeBodyLoweringPhase(
prerequisite = setOf(propertyAccessorInlinerLoweringPhase)
)
private val computeStringTrimPhase = makeJsModulePhase(
::StringTrimLowering,
name = "StringTrimLowering",
description = "Compute trimIndent and trimMargin operations on constant strings"
).toModuleLowering()
private val localDelegatedPropertiesLoweringPhase = makeBodyLoweringPhase(
{ LocalDelegatedPropertiesLowering() },
name = "LocalDelegatedPropertiesLowering",
@@ -816,6 +822,7 @@ private val loweringList = listOf<Lowering>(
copyPropertyAccessorBodiesLoweringPass,
booleanPropertyInExternalLowering,
foldConstantLoweringPhase,
computeStringTrimPhase,
privateMembersLoweringPhase,
privateMemberUsagesLoweringPhase,
exportedDefaultParameterStubPhase,
@@ -0,0 +1,147 @@
// WITH_RUNTIME
// IGNORE_BACKEND: JS
import kotlin.test.assertEquals
// CHECK_CONTAINS_NO_CALLS: trimIndentConstant
fun trimIndentConstant(): String {
return """
Hello,
World
""".trimIndent()
}
// CHECK_CONTAINS_NO_CALLS: trimIndentInterpolatedUsingConstant
private const val HAS_INDENT = """Hello,
World"""
fun trimIndentInterpolatedUsingConstant(): String {
return """
Hello,
$HAS_INDENT
World
""".trimIndent()
}
// CHECK_CONTAINS_NO_CALLS: trimIndentReliesOnNestedStringBuilderFlatteningAndConstantConcatenation
private const val SPACES = " "
private const val HELLO = "Hello"
private const val WORLD = "World"
fun trimIndentReliesOnNestedStringBuilderFlatteningAndConstantConcatenation(): String {
return ("" + '\n' + SPACES + "${SPACES}Hey" + """
${HELLO + HELLO},
${WORLD + WORLD}
""" + SPACES).trimIndent()
}
// CHECK_CALLED_IN_SCOPE: function=trimIndent scope=trimIndentNotConstant
fun trimIndentNotConstant(arg: String): String {
return arg.trimIndent()
}
// CHECK_CALLED_IN_SCOPE: function=trimIndent scope=trimIndentInterpolated
fun trimIndentInterpolated(arg: Int): String {
return """
Hello,
$arg
""".trimIndent()
}
// CHECK_CONTAINS_NO_CALLS: trimMarginConstant
fun trimMarginConstant(): String {
return """
|Hello,
|World
""".trimMargin()
}
// CHECK_CONTAINS_NO_CALLS: trimMarginInterpolatedUsingConstant
private const val HAS_MARGIN = """Hello,
|World"""
fun trimMarginInterpolatedUsingConstant(): String {
return """
|Hello,
|$HAS_MARGIN
|World
""".trimMargin()
}
// CHECK_CONTAINS_NO_CALLS: trimMarginReliesOnNestedStringBuilderFlatteningAndConstantConcatenation
fun trimMarginReliesOnNestedStringBuilderFlatteningAndConstantConcatenation(): String {
return ("" + '\n' + SPACES + "${SPACES}|Hey" + """
|${HELLO + HELLO},
|${WORLD + WORLD}
""" + SPACES).trimMargin()
}
// CHECK_CONTAINS_NO_CALLS: trimMarginConstantCustomPrefix
fun trimMarginConstantCustomPrefix(): String {
return """
###Hello,
###World
""".trimMargin(marginPrefix = "###")
}
// CHECK_CONTAINS_NO_CALLS: trimMarginConstantCustomPrefixInterpolatedUsingConstant
private const val OCTOTHORPE = '#'
fun trimMarginConstantCustomPrefixInterpolatedUsingConstant(): String {
return """
#@#Hello,
#@#World
""".trimMargin(marginPrefix = "$OCTOTHORPE@$OCTOTHORPE")
}
// CHECK_CALLED_IN_SCOPE: function=trimMargin$default scope=trimMarginNotConstant
fun trimMarginNotConstant(arg: String): String {
return arg.trimMargin()
}
// CHECK_CALLED_IN_SCOPE: function=trimMargin scope=trimMarginNotConstantCustomPrefix
fun trimMarginNotConstantCustomPrefix(arg: String): String {
return arg.trimMargin("###")
}
// CHECK_CALLED_IN_SCOPE: function=trimMargin$default scope=trimMarginInterpolated
fun trimMarginInterpolated(arg: Int): String {
return """
|Hello,
|$arg
""".trimMargin()
}
// CHECK_CALLED_IN_SCOPE: function=trimMargin scope=trimMarginConstantWithNonConstantCustomPrefix
fun trimMarginConstantWithNonConstantCustomPrefix(arg: String): String {
return """
|Hello,
|World
""".trimMargin(arg)
}
fun box(): String {
assertEquals("Hello,\nWorld", trimIndentConstant())
assertEquals("Hello,\nHello,\nWorld\nWorld", trimIndentInterpolatedUsingConstant())
assertEquals("Hey\nHelloHello,\nWorldWorld", trimIndentReliesOnNestedStringBuilderFlatteningAndConstantConcatenation())
assertEquals("Hello,\nWorld", trimIndentNotConstant("""
Hello,
World
"""))
assertEquals("Hello,\n42", trimIndentInterpolated(42))
assertEquals("Hello,\nWorld", trimMarginConstant())
assertEquals("Hello,\nHello,\nWorld\nWorld", trimMarginInterpolatedUsingConstant())
assertEquals("Hey\nHelloHello,\nWorldWorld", trimMarginReliesOnNestedStringBuilderFlatteningAndConstantConcatenation())
assertEquals("Hello,\nWorld", trimMarginConstantCustomPrefix())
assertEquals("Hello,\nWorld", trimMarginConstantCustomPrefixInterpolatedUsingConstant())
assertEquals("Hello,\nWorld", trimMarginNotConstant("""
|Hello,
|World
"""))
assertEquals("Hello,\nWorld", trimMarginNotConstantCustomPrefix("""
###Hello,
###World
"""))
assertEquals("Hello,\n42", trimMarginInterpolated(42))
assertEquals("Hello,\nWorld", trimMarginConstantWithNonConstantCustomPrefix("|"))
return "OK"
}
@@ -41590,6 +41590,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/strings/surrogatePair.kt");
}
@Test
@TestMetadata("trimOptimization.kt")
public void testTrimOptimization() throws Exception {
runTest("compiler/testData/codegen/box/strings/trimOptimization.kt");
}
@Test
@TestMetadata("twoArgumentNullableStringOperatorPlus.kt")
public void testTwoArgumentNullableStringOperatorPlus() throws Exception {
@@ -41752,6 +41752,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/strings/surrogatePair.kt");
}
@Test
@TestMetadata("trimOptimization.kt")
public void testTrimOptimization() throws Exception {
runTest("compiler/testData/codegen/box/strings/trimOptimization.kt");
}
@Test
@TestMetadata("twoArgumentNullableStringOperatorPlus.kt")
public void testTwoArgumentNullableStringOperatorPlus() throws Exception {
@@ -33434,6 +33434,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/strings/surrogatePair.kt");
}
@TestMetadata("trimOptimization.kt")
public void testTrimOptimization() throws Exception {
runTest("compiler/testData/codegen/box/strings/trimOptimization.kt");
}
@TestMetadata("twoArgumentNullableStringOperatorPlus.kt")
public void testTwoArgumentNullableStringOperatorPlus() throws Exception {
runTest("compiler/testData/codegen/box/strings/twoArgumentNullableStringOperatorPlus.kt");
@@ -27924,6 +27924,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/strings/surrogatePair.kt");
}
@TestMetadata("trimOptimization.kt")
public void testTrimOptimization() throws Exception {
runTest("compiler/testData/codegen/box/strings/trimOptimization.kt");
}
@TestMetadata("twoArgumentNullableStringOperatorPlus.kt")
public void testTwoArgumentNullableStringOperatorPlus() throws Exception {
runTest("compiler/testData/codegen/box/strings/twoArgumentNullableStringOperatorPlus.kt");
@@ -27330,6 +27330,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/strings/surrogatePair.kt");
}
@TestMetadata("trimOptimization.kt")
public void testTrimOptimization() throws Exception {
runTest("compiler/testData/codegen/box/strings/trimOptimization.kt");
}
@TestMetadata("twoArgumentNullableStringOperatorPlus.kt")
public void testTwoArgumentNullableStringOperatorPlus() throws Exception {
runTest("compiler/testData/codegen/box/strings/twoArgumentNullableStringOperatorPlus.kt");
@@ -27240,6 +27240,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/strings/surrogatePair.kt");
}
@TestMetadata("trimOptimization.kt")
public void testTrimOptimization() throws Exception {
runTest("compiler/testData/codegen/box/strings/trimOptimization.kt");
}
@TestMetadata("twoArgumentNullableStringOperatorPlus.kt")
public void testTwoArgumentNullableStringOperatorPlus() throws Exception {
runTest("compiler/testData/codegen/box/strings/twoArgumentNullableStringOperatorPlus.kt");
@@ -21164,6 +21164,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/strings/surrogatePair.kt");
}
@TestMetadata("trimOptimization.kt")
public void testTrimOptimization() throws Exception {
runTest("compiler/testData/codegen/box/strings/trimOptimization.kt");
}
@TestMetadata("twoArgumentNullableStringOperatorPlus.kt")
public void testTwoArgumentNullableStringOperatorPlus() throws Exception {
runTest("compiler/testData/codegen/box/strings/twoArgumentNullableStringOperatorPlus.kt");