From 0f0b48f87bdf815c9fe7a1c32f5be87b617e6c9a Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Tue, 14 Dec 2021 23:31:46 +0300 Subject: [PATCH] Move reporting of interpreter's backend errors tests to fir module --- ...gnosticsTestWithJvmIrBackendGenerated.java | 32 ++++++++++++ .../interpreter/checker/IrConstTransformer.kt | 7 ++- .../exceptionFromInterpreter_ir.kt | 7 +-- .../exceptionFromInterpreter_ir.txt | 0 ...gnosticsTestWithJvmIrBackendGenerated.java | 6 --- .../handlers/JvmBackendDiagnosticsHandler.kt | 16 +++++- .../AbstractDiagnosticsTestWithJvmBackend.kt | 49 ++++++++++++++++--- .../generators/GenerateJUnit5CompilerTests.kt | 4 ++ 8 files changed, 101 insertions(+), 20 deletions(-) create mode 100644 compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsTestWithJvmIrBackendGenerated.java rename compiler/testData/diagnostics/{testsWithJvmBackend => firTestWithJvmBackend}/exceptionFromInterpreter_ir.kt (73%) rename compiler/testData/diagnostics/{testsWithJvmBackend => firTestWithJvmBackend}/exceptionFromInterpreter_ir.txt (100%) diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsTestWithJvmIrBackendGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsTestWithJvmIrBackendGenerated.java new file mode 100644 index 00000000000..394a1a8ec96 --- /dev/null +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsTestWithJvmIrBackendGenerated.java @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2021 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. + */ + +package org.jetbrains.kotlin.test.runners; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.util.KtTestUtil; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link GenerateNewCompilerTests.kt}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("compiler/testData/diagnostics/firTestWithJvmBackend") +@TestDataPath("$PROJECT_ROOT") +public class FirDiagnosticsTestWithJvmIrBackendGenerated extends AbstractFirDiagnosticsTestWithJvmIrBackend { + @Test + public void testAllFilesPresentInFirTestWithJvmBackend() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/firTestWithJvmBackend"), Pattern.compile("^(.+)\\.kt$"), null, true); + } + + @Test + @TestMetadata("exceptionFromInterpreter_ir.kt") + public void testExceptionFromInterpreter_ir() throws Exception { + runTest("compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.kt"); + } +} diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrConstTransformer.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrConstTransformer.kt index a5f32338395..d3cd14f9ca2 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrConstTransformer.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/checker/IrConstTransformer.kt @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.expressions.* +import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl import org.jetbrains.kotlin.ir.expressions.impl.IrVarargImpl import org.jetbrains.kotlin.ir.interpreter.IrInterpreter import org.jetbrains.kotlin.ir.interpreter.isPrimitiveArray @@ -29,7 +30,11 @@ class IrConstTransformer( private fun IrExpression.reportIfError(original: IrExpression): IrExpression { if (this is IrErrorExpression) { onError(original, this) - return original + return when (mode) { + // need to pass any const value to be able to get some bytecode and then report error + EvaluationMode.ONLY_INTRINSIC_CONST -> IrConstImpl.int(startOffset, endOffset, type, 0) + else -> original + } } return this } diff --git a/compiler/testData/diagnostics/testsWithJvmBackend/exceptionFromInterpreter_ir.kt b/compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.kt similarity index 73% rename from compiler/testData/diagnostics/testsWithJvmBackend/exceptionFromInterpreter_ir.kt rename to compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.kt index 77431a9876f..ae057e9f45e 100644 --- a/compiler/testData/diagnostics/testsWithJvmBackend/exceptionFromInterpreter_ir.kt +++ b/compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.kt @@ -6,6 +6,7 @@ const val divideByZero = 1 / 0 const val trimMarginException = "123".trimMargin(" ") -//annotation class A(val i: Int, val b: Int) -//@A(1 / 0, 2) -//fun foo() {} \ No newline at end of file +annotation class A(val i: Int, val b: Int) + +@A(1 / 0, 2) +fun foo() {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithJvmBackend/exceptionFromInterpreter_ir.txt b/compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.txt similarity index 100% rename from compiler/testData/diagnostics/testsWithJvmBackend/exceptionFromInterpreter_ir.txt rename to compiler/testData/diagnostics/firTestWithJvmBackend/exceptionFromInterpreter_ir.txt diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithJvmIrBackendGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithJvmIrBackendGenerated.java index 90e8f7a703b..4d184255629 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithJvmIrBackendGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticsTestWithJvmIrBackendGenerated.java @@ -25,12 +25,6 @@ public class DiagnosticsTestWithJvmIrBackendGenerated extends AbstractDiagnostic KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithJvmBackend"), Pattern.compile("^(.+)\\.kts?$"), null, TargetBackend.JVM_IR, true); } - @Test - @TestMetadata("exceptionFromInterpreter_ir.kt") - public void testExceptionFromInterpreter_ir() throws Exception { - runTest("compiler/testData/diagnostics/testsWithJvmBackend/exceptionFromInterpreter_ir.kt"); - } - @Test @TestMetadata("indirectInlineCycle_ir.kt") public void testIndirectInlineCycle_ir() throws Exception { diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/JvmBackendDiagnosticsHandler.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/JvmBackendDiagnosticsHandler.kt index f178b4952f2..a87571c38b2 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/JvmBackendDiagnosticsHandler.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/handlers/JvmBackendDiagnosticsHandler.kt @@ -6,14 +6,17 @@ package org.jetbrains.kotlin.test.backend.handlers import org.jetbrains.kotlin.diagnostics.impl.BaseDiagnosticsCollector +import org.jetbrains.kotlin.fir.psi import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.test.frontend.classic.handlers.ClassicDiagnosticReporter import org.jetbrains.kotlin.test.frontend.classic.handlers.withNewInferenceModeEnabled import org.jetbrains.kotlin.test.frontend.fir.handlers.toMetaInfos import org.jetbrains.kotlin.test.model.BinaryArtifacts import org.jetbrains.kotlin.test.model.FrontendKinds +import org.jetbrains.kotlin.test.model.TestFile import org.jetbrains.kotlin.test.model.TestModule import org.jetbrains.kotlin.test.services.TestServices +import org.jetbrains.kotlin.test.services.assertions import org.jetbrains.kotlin.test.services.dependencyProvider import org.jetbrains.kotlin.test.services.globalMetadataInfoHandler @@ -21,8 +24,8 @@ class JvmBackendDiagnosticsHandler(testServices: TestServices) : JvmBinaryArtifa private val reporter = ClassicDiagnosticReporter(testServices) override fun processModule(module: TestModule, info: BinaryArtifacts.Jvm) { - val testFileToKtFileMap = testServices.dependencyProvider.getArtifact(module, FrontendKinds.ClassicFrontend).ktFiles - val ktFileToTestFileMap = testFileToKtFileMap.entries.map { it.value to it.key }.toMap() + val testFileToKtFileMap = getKtFiles(module) + val ktFileToTestFileMap = testFileToKtFileMap.entries.associate { it.value to it.key } val generationState = info.classFileFactory.generationState val diagnostics = generationState.collectedExtraJvmDiagnostics.all() val configuration = reporter.createConfiguration(module) @@ -44,5 +47,14 @@ class JvmBackendDiagnosticsHandler(testServices: TestServices) : JvmBinaryArtifa } } + private fun getKtFiles(module: TestModule): Map { + return when (module.frontendKind) { + FrontendKinds.ClassicFrontend -> testServices.dependencyProvider.getArtifact(module, FrontendKinds.ClassicFrontend).ktFiles + FrontendKinds.FIR -> testServices.dependencyProvider.getArtifact(module, FrontendKinds.FIR).firFiles.entries + .associate { it.key to (it.value.psi as KtFile) } + else -> testServices.assertions.fail { "Unknown frontend kind ${module.frontendKind}" } + } + } + override fun processAfterAllModules(someAssertionWasFailed: Boolean) {} } diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractDiagnosticsTestWithJvmBackend.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractDiagnosticsTestWithJvmBackend.kt index 352b08ee936..43da6cf0230 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractDiagnosticsTestWithJvmBackend.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractDiagnosticsTestWithJvmBackend.kt @@ -15,7 +15,6 @@ import org.jetbrains.kotlin.test.backend.ir.IrBackendInput import org.jetbrains.kotlin.test.backend.ir.JvmIrBackendFacade import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder import org.jetbrains.kotlin.test.builders.classicFrontendHandlersStep -import org.jetbrains.kotlin.test.builders.classicFrontendStep import org.jetbrains.kotlin.test.builders.jvmArtifactsHandlersStep import org.jetbrains.kotlin.test.directives.JvmEnvironmentConfigurationDirectives import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontend2ClassicBackendConverter @@ -25,21 +24,26 @@ import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontendOutputArtifact import org.jetbrains.kotlin.test.frontend.classic.handlers.ClassicDiagnosticsHandler import org.jetbrains.kotlin.test.frontend.classic.handlers.DeclarationsDumpHandler import org.jetbrains.kotlin.test.frontend.classic.handlers.OldNewInferenceMetaInfoProcessor +import org.jetbrains.kotlin.test.frontend.fir.Fir2IrResultsConverter +import org.jetbrains.kotlin.test.frontend.fir.FirFrontendFacade +import org.jetbrains.kotlin.test.frontend.fir.FirOutputArtifact import org.jetbrains.kotlin.test.model.* import org.jetbrains.kotlin.test.services.configuration.CommonEnvironmentConfigurator +import org.jetbrains.kotlin.test.services.configuration.JvmEnvironmentConfigurator import org.jetbrains.kotlin.test.services.sourceProviders.AdditionalDiagnosticsSourceFilesProvider import org.jetbrains.kotlin.test.services.sourceProviders.CoroutineHelpersSourceFilesProvider -import org.jetbrains.kotlin.test.services.configuration.JvmEnvironmentConfigurator import org.jetbrains.kotlin.test.services.configuration.ScriptingEnvironmentConfigurator -abstract class AbstractDiagnosticsTestWithJvmBackend> : AbstractKotlinCompilerTest() { +abstract class AbstractDiagnosticsTestWithJvmBackend, I : ResultingArtifact.BackendInput> : AbstractKotlinCompilerTest() { + abstract val targetFrontend: FrontendKind abstract val targetBackend: TargetBackend - abstract val converter: Constructor> + abstract val frontend: Constructor> + abstract val converter: Constructor> abstract val backendFacade: Constructor> override fun TestConfigurationBuilder.configuration() { globalDefaults { - frontend = FrontendKinds.ClassicFrontend + frontend = targetFrontend targetBackend = this@AbstractDiagnosticsTestWithJvmBackend.targetBackend targetPlatform = JvmPlatforms.defaultJvmPlatform dependencyKind = DependencyKind.Binary @@ -63,7 +67,7 @@ abstract class AbstractDiagnosticsTestWithJvmBackend() { +abstract class AbstractDiagnosticsTestWithOldJvmBackend : AbstractDiagnosticsTestWithJvmBackend() { + override val targetFrontend: FrontendKind + get() = FrontendKinds.ClassicFrontend + override val targetBackend: TargetBackend get() = TargetBackend.JVM_OLD + override val frontend: Constructor> + get() = ::ClassicFrontendFacade + override val converter: Constructor> get() = ::ClassicFrontend2ClassicBackendConverter @@ -93,13 +103,36 @@ abstract class AbstractDiagnosticsTestWithOldJvmBackend : AbstractDiagnosticsTes get() = ::ClassicJvmBackendFacade } -abstract class AbstractDiagnosticsTestWithJvmIrBackend : AbstractDiagnosticsTestWithJvmBackend() { +abstract class AbstractDiagnosticsTestWithJvmIrBackend : AbstractDiagnosticsTestWithJvmBackend() { + override val targetFrontend: FrontendKind + get() = FrontendKinds.ClassicFrontend + override val targetBackend: TargetBackend get() = TargetBackend.JVM_IR + override val frontend: Constructor> + get() = ::ClassicFrontendFacade + override val converter: Constructor> get() = ::ClassicFrontend2IrConverter override val backendFacade: Constructor> get() = ::JvmIrBackendFacade } + +abstract class AbstractFirDiagnosticsTestWithJvmIrBackend : AbstractDiagnosticsTestWithJvmBackend() { + override val targetFrontend: FrontendKind + get() = FrontendKinds.FIR + + override val targetBackend: TargetBackend + get() = TargetBackend.JVM_IR + + override val frontend: Constructor> + get() = ::FirFrontendFacade + + override val converter: Constructor> + get() = ::Fir2IrResultsConverter + + override val backendFacade: Constructor> + get() = ::JvmIrBackendFacade +} diff --git a/compiler/tests-for-compiler-generator/tests/org/jetbrains/kotlin/test/generators/GenerateJUnit5CompilerTests.kt b/compiler/tests-for-compiler-generator/tests/org/jetbrains/kotlin/test/generators/GenerateJUnit5CompilerTests.kt index 5b7b66acae1..018cf3929a9 100644 --- a/compiler/tests-for-compiler-generator/tests/org/jetbrains/kotlin/test/generators/GenerateJUnit5CompilerTests.kt +++ b/compiler/tests-for-compiler-generator/tests/org/jetbrains/kotlin/test/generators/GenerateJUnit5CompilerTests.kt @@ -247,6 +247,10 @@ fun generateJUnit5CompilerTests(args: Array) { testClass { model("ir/loweredIr") } + + testClass { + model("diagnostics/firTestWithJvmBackend") + } } testGroup(testsRoot = "compiler/fir/fir2ir/tests-gen", testDataRoot = "compiler/fir/fir2ir/testData") {