[JVM IR] Fix lambdaInLocalFunction.kt by adding helper methods that

return the target backend under test.
This commit is contained in:
Mark Punzalan
2020-01-15 01:01:52 -08:00
committed by Alexander Udalov
parent beb3165839
commit 7329a1641a
4 changed files with 34 additions and 11 deletions
@@ -1,8 +1,10 @@
// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM // TARGET_BACKEND: JVM
// WITH_REFLECT // WITH_REFLECT
// WITH_HELPERS
import helpers.*
fun box(): String { fun box(): String {
fun foo(): Any { fun foo(): Any {
@@ -10,11 +12,16 @@ fun box(): String {
} }
val javaClass = foo().javaClass val javaClass = foo().javaClass
val enclosingMethod = javaClass.getEnclosingMethod()
if (enclosingMethod?.getName() != "invoke") return "method: $enclosingMethod"
val enclosingClass = javaClass.getEnclosingClass()!!.getName() // The enclosing method is a local function, which are in a separate class (implementing FunctionN) for non-IR, and are static methods
if (enclosingClass != "LambdaInLocalFunctionKt\$box$1") return "enclosing class: $enclosingClass" // in the enclosing class for IR.
val actualEnclosingMethod = javaClass.getEnclosingMethod()!!.getName()
val expectedEnclosingMethod = if (isIR()) "box\$foo" else "invoke"
if (actualEnclosingMethod != expectedEnclosingMethod) return "method: $actualEnclosingMethod"
val actualEnclosingClass = javaClass.getEnclosingClass()!!.getName()
val expectedEnclosingClass = if (isIR()) "LambdaInLocalFunctionKt" else "LambdaInLocalFunctionKt\$box\$1"
if (actualEnclosingClass != expectedEnclosingClass) return "enclosing class: $actualEnclosingClass"
val declaringClass = javaClass.getDeclaringClass() val declaringClass = javaClass.getDeclaringClass()
if (declaringClass != null) return "anonymous function has a declaring class: $declaringClass" if (declaringClass != null) return "anonymous function has a declaring class: $declaringClass"
@@ -6,8 +6,10 @@
package org.jetbrains.kotlin package org.jetbrains.kotlin
import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.test.TargetBackend
fun createTextForHelpers(isReleaseCoroutines: Boolean, checkStateMachine: Boolean, checkTailCallOptimization: Boolean): String { // Add the directive `// WITH_COROUTINES` to use these helpers in codegen tests (see TestFiles.java).
fun createTextForCoroutineHelpers(isReleaseCoroutines: Boolean, checkStateMachine: Boolean, checkTailCallOptimization: Boolean): String {
val coroutinesPackage = val coroutinesPackage =
if (isReleaseCoroutines) if (isReleaseCoroutines)
DescriptorUtils.COROUTINES_PACKAGE_FQ_NAME_RELEASE.asString() DescriptorUtils.COROUTINES_PACKAGE_FQ_NAME_RELEASE.asString()
@@ -188,3 +190,11 @@ fun createTextForHelpers(isReleaseCoroutines: Boolean, checkStateMachine: Boolea
|${if (checkTailCallOptimization) checkTailCallOptimizationString else ""} |${if (checkTailCallOptimization) checkTailCallOptimizationString else ""}
""".trimMargin() """.trimMargin()
} }
// Add the directive `// WITH_HELPERS` to use these helpers in codegen tests (see CodegenTestCase.java).
fun createTextForCodegenTestHelpers(backend: TargetBackend) =
"""
|package helpers
|
|fun isIR() = ${backend.isIR}
""".trimMargin()
@@ -16,6 +16,7 @@ import kotlin.io.FilesKt;
import kotlin.text.Charsets; import kotlin.text.Charsets;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.TestHelperGeneratorKt;
import org.jetbrains.kotlin.TestsCompilerError; import org.jetbrains.kotlin.TestsCompilerError;
import org.jetbrains.kotlin.TestsCompiletimeError; import org.jetbrains.kotlin.TestsCompiletimeError;
import org.jetbrains.kotlin.backend.common.output.OutputFile; import org.jetbrains.kotlin.backend.common.output.OutputFile;
@@ -783,7 +784,7 @@ public abstract class CodegenTestCase extends KtUsefulTestCase {
expectedText = expectedText.replace("COROUTINES_PACKAGE", coroutinesPackage); expectedText = expectedText.replace("COROUTINES_PACKAGE", coroutinesPackage);
} }
List<TestFile> testFiles = createTestFiles(file, expectedText, coroutinesPackage); List<TestFile> testFiles = createTestFiles(file, expectedText);
doMultiFileTest(file, testFiles); doMultiFileTest(file, testFiles);
} }
@@ -794,14 +795,18 @@ public abstract class CodegenTestCase extends KtUsefulTestCase {
} }
@NotNull @NotNull
private static List<TestFile> createTestFiles(File file, String expectedText, String coroutinesPackage) { private List<TestFile> createTestFiles(File file, String expectedText) {
return TestFiles.createTestFiles(file.getName(), expectedText, new TestFiles.TestFileFactoryNoModules<TestFile>() { List testFiles = TestFiles.createTestFiles(file.getName(), expectedText, new TestFiles.TestFileFactoryNoModules<TestFile>() {
@NotNull @NotNull
@Override @Override
public TestFile create(@NotNull String fileName, @NotNull String text, @NotNull Map<String, String> directives) { public TestFile create(@NotNull String fileName, @NotNull String text, @NotNull Map<String, String> directives) {
return new TestFile(fileName, text); return new TestFile(fileName, text);
} }
}, coroutinesPackage); }, coroutinesPackage);
if (InTextDirectivesUtils.isDirectiveDefined(expectedText, "WITH_HELPERS")) {
testFiles.add(new TestFile("CodegenTestHelpers.kt", TestHelperGeneratorKt.createTextForCodegenTestHelpers(getBackend())));
}
return testFiles;
} }
@NotNull @NotNull
@@ -8,7 +8,7 @@ package org.jetbrains.kotlin.test;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.CoroutineTestUtilKt; import org.jetbrains.kotlin.TestHelperGeneratorKt;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -113,7 +113,8 @@ public class TestFiles {
factory.createFile( factory.createFile(
supportModule, supportModule,
"CoroutineUtil.kt", "CoroutineUtil.kt",
CoroutineTestUtilKt.createTextForHelpers(isReleaseCoroutines, checkStateMachine, checkTailCallOptimization), TestHelperGeneratorKt.createTextForCoroutineHelpers(
isReleaseCoroutines, checkStateMachine, checkTailCallOptimization),
directives directives
)); ));
} }