diff --git a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/TestConfiguration.kt b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/TestConfiguration.kt index 39c7578e2ad..3c0b5c35fd6 100644 --- a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/TestConfiguration.kt +++ b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/TestConfiguration.kt @@ -6,14 +6,11 @@ package org.jetbrains.kotlin.test import com.intellij.openapi.Disposable -import org.jetbrains.kotlin.test.directives.ConfigurationDirectives -import org.jetbrains.kotlin.test.directives.LanguageSettingsDirectives import org.jetbrains.kotlin.test.directives.model.DirectivesContainer import org.jetbrains.kotlin.test.directives.model.RegisteredDirectives import org.jetbrains.kotlin.test.model.* import org.jetbrains.kotlin.test.services.MetaTestConfigurator import org.jetbrains.kotlin.test.services.ModuleStructureExtractor -import org.jetbrains.kotlin.test.services.SourceFilePreprocessor import org.jetbrains.kotlin.test.services.TestServices typealias Constructor = (TestServices) -> T diff --git a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/TestRunner.kt b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/TestRunner.kt index fe9b7ccb97f..bfd22272781 100644 --- a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/TestRunner.kt +++ b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/TestRunner.kt @@ -49,7 +49,7 @@ class TestRunner(private val testConfiguration: TestConfiguration) { var failedException: Throwable? = null try { for (module in modules) { - processModule(module, dependencyProvider, moduleStructure) + processModule(services, module, dependencyProvider, moduleStructure) } } catch (e: Throwable) { failedException = e @@ -81,6 +81,7 @@ class TestRunner(private val testConfiguration: TestConfiguration) { } private fun processModule( + services: TestServices, module: TestModule, dependencyProvider: DependencyProviderImpl, moduleStructure: TestModuleStructure @@ -99,7 +100,7 @@ class TestRunner(private val testConfiguration: TestConfiguration) { } } - val backendKind = module.backendKind + val backendKind = services.backendKindExtractor.backendKind(module.targetBackend) if (!backendKind.shouldRunAnalysis) return val backendInputInfo = testConfiguration.getFacade(frontendKind, backendKind) diff --git a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/directives/ModuleStructureDirectives.kt b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/directives/ModuleStructureDirectives.kt index db1c132c7dd..de9b0d1e39c 100644 --- a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/directives/ModuleStructureDirectives.kt +++ b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/directives/ModuleStructureDirectives.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.test.directives +import org.jetbrains.kotlin.test.TargetBackend import org.jetbrains.kotlin.test.directives.model.SimpleDirectivesContainer object ModuleStructureDirectives : SimpleDirectivesContainer() { @@ -45,7 +46,7 @@ object ModuleStructureDirectives : SimpleDirectivesContainer() { """.trimIndent() ) - val TARGET_BACKEND_KIND by stringDirective( + val TARGET_BACKEND_KIND by enumDirective( """ Usage: // TARGET_BACKEND: {Backend} Declares backend for analyzing current module diff --git a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/model/Modules.kt b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/model/Modules.kt index e645417efda..2d751ec9ea2 100644 --- a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/model/Modules.kt +++ b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/model/Modules.kt @@ -7,14 +7,15 @@ package org.jetbrains.kotlin.test.model import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.platform.TargetPlatform +import org.jetbrains.kotlin.test.TargetBackend import org.jetbrains.kotlin.test.directives.model.RegisteredDirectives import java.io.File data class TestModule( val name: String, val targetPlatform: TargetPlatform, + val targetBackend: TargetBackend?, val frontendKind: FrontendKind<*>, - val backendKind: BackendKind<*>, val files: List, val dependencies: List, val directives: RegisteredDirectives, diff --git a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/BackendKindExtractor.kt b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/BackendKindExtractor.kt new file mode 100644 index 00000000000..95b947e8c5e --- /dev/null +++ b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/BackendKindExtractor.kt @@ -0,0 +1,15 @@ +/* + * Copyright 2010-2020 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.services + +import org.jetbrains.kotlin.test.TargetBackend +import org.jetbrains.kotlin.test.model.BackendKind + +abstract class BackendKindExtractor(protected val testServices: TestServices) : TestService { + abstract fun backendKind(targetBackend: TargetBackend?): BackendKind<*> +} + +val TestServices.backendKindExtractor: BackendKindExtractor by TestServices.testServiceAccessor() diff --git a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/DefaultsProvider.kt b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/DefaultsProvider.kt index 78c23446c93..54310fdb033 100644 --- a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/DefaultsProvider.kt +++ b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/DefaultsProvider.kt @@ -7,8 +7,8 @@ package org.jetbrains.kotlin.test.services import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.platform.TargetPlatform +import org.jetbrains.kotlin.test.TargetBackend import org.jetbrains.kotlin.test.builders.LanguageVersionSettingsBuilder -import org.jetbrains.kotlin.test.model.BackendKind import org.jetbrains.kotlin.test.model.DependencyKind import org.jetbrains.kotlin.test.model.FrontendKind @@ -18,11 +18,11 @@ import org.jetbrains.kotlin.test.model.FrontendKind * - default libraries */ class DefaultsProvider( - val defaultBackend: BackendKind<*>, val defaultFrontend: FrontendKind<*>, val defaultLanguageSettings: LanguageVersionSettings, private val defaultLanguageSettingsBuilder: LanguageVersionSettingsBuilder, val defaultPlatform: TargetPlatform, + val defaultTargetBackend: TargetBackend?, val defaultDependencyKind: DependencyKind ) : TestService { fun newLanguageSettingsBuilder(): LanguageVersionSettingsBuilder { diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/BlackBoxCodegenSuppressor.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/BlackBoxCodegenSuppressor.kt index 47f9befff9a..3af0577e4d0 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/BlackBoxCodegenSuppressor.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/backend/BlackBoxCodegenSuppressor.kt @@ -25,7 +25,7 @@ class BlackBoxCodegenSuppressor(testServices: TestServices) : AfterAnalysisCheck val moduleStructure = testServices.moduleStructure val ignoredBackends = moduleStructure.modules.flatMap { it.directives[CodegenTestDirectives.IGNORE_BACKEND] } if (ignoredBackends.isEmpty()) return failedAssertions - val targetBackends = moduleStructure.modules.flatMap { it.targetBackends } + val targetBackends = moduleStructure.modules.map { it.targetBackend } val matchedBackend = ignoredBackends.intersect(targetBackends) if (ignoredBackends.contains(TargetBackend.ANY)) { return processAssertions(failedAssertions) @@ -50,19 +50,4 @@ class BlackBoxCodegenSuppressor(testServices: TestServices) : AfterAnalysisCheck listOf(AssertionError(message)) } } - - private val TestModule.targetBackends: List - get() = when (backendKind) { - BackendKinds.ClassicBackend -> when { - targetPlatform.isJvm() -> listOf(TargetBackend.JVM, TargetBackend.JVM_OLD) - targetPlatform.isJs() -> listOf(TargetBackend.JS) - else -> emptyList() - } - BackendKinds.IrBackend -> when { - targetPlatform.isJvm() -> listOf(TargetBackend.JVM_IR) - targetPlatform.isJs() -> listOf(TargetBackend.JS_IR) - else -> emptyList() - } - else -> emptyList() - } } diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/builders/DefaultsProviderBuilder.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/builders/DefaultsProviderBuilder.kt index e46324d3713..5a99cc25118 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/builders/DefaultsProviderBuilder.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/builders/DefaultsProviderBuilder.kt @@ -11,16 +11,16 @@ import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.config.LanguageVersionSettingsImpl import org.jetbrains.kotlin.fir.PrivateForInline import org.jetbrains.kotlin.platform.TargetPlatform +import org.jetbrains.kotlin.test.TargetBackend import org.jetbrains.kotlin.test.services.DefaultsDsl import org.jetbrains.kotlin.test.services.DefaultsProvider -import org.jetbrains.kotlin.test.model.BackendKind import org.jetbrains.kotlin.test.model.DependencyKind import org.jetbrains.kotlin.test.model.FrontendKind @DefaultsDsl class DefaultsProviderBuilder { - lateinit var backend: BackendKind<*> lateinit var frontend: FrontendKind<*> + var targetBackend: TargetBackend? = null lateinit var targetPlatform: TargetPlatform lateinit var dependencyKind: DependencyKind @@ -40,11 +40,11 @@ class DefaultsProviderBuilder { @OptIn(PrivateForInline::class) fun build(): DefaultsProvider { return DefaultsProvider( - backend, frontend, languageVersionSettings ?: LanguageVersionSettingsImpl(LanguageVersion.LATEST_STABLE, ApiVersion.LATEST_STABLE), languageVersionSettingsBuilder ?: LanguageVersionSettingsBuilder(), targetPlatform, + targetBackend, dependencyKind ) } diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/builders/TestConfigurationBuilder.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/builders/TestConfigurationBuilder.kt index fac2eca53fd..49a1486f04f 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/builders/TestConfigurationBuilder.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/builders/TestConfigurationBuilder.kt @@ -37,6 +37,15 @@ class TestConfigurationBuilder { val defaultRegisteredDirectivesBuilder: RegisteredDirectivesBuilder = RegisteredDirectivesBuilder() private val configurationsByTestDataCondition: MutableList Unit>> = mutableListOf() + private val additionalServices: MutableList = mutableListOf() + + inline fun useAdditionalService(noinline serviceConstructor: (TestServices) -> T) { + useAdditionalService(service(serviceConstructor)) + } + + fun useAdditionalService(serviceRegistrationData: ServiceRegistrationData) { + additionalServices += serviceRegistrationData + } fun forTestsMatching(pattern: String, configuration: TestConfigurationBuilder.() -> Unit) { val regex = pattern.toMatchingRegexString().toRegex() @@ -140,7 +149,8 @@ class TestConfigurationBuilder { afterAnalysisCheckers, metaInfoHandlerEnabled, directives, - defaultRegisteredDirectivesBuilder.build() + defaultRegisteredDirectivesBuilder.build(), + additionalServices ) } } diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/impl/TestConfigurationImpl.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/impl/TestConfigurationImpl.kt index e82d9f6534e..ceb321cb223 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/impl/TestConfigurationImpl.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/impl/TestConfigurationImpl.kt @@ -35,11 +35,16 @@ class TestConfigurationImpl( override val metaInfoHandlerEnabled: Boolean, directives: List, - override val defaultRegisteredDirectives: RegisteredDirectives + override val defaultRegisteredDirectives: RegisteredDirectives, + additionalServices: List ) : TestConfiguration() { override val rootDisposable: Disposable = TestDisposable() override val testServices: TestServices = TestServices() + init { + additionalServices.forEach { testServices.register(it) } + } + private val allDirectives = directives.toMutableSet() override val directives: DirectivesContainer by lazy { when (allDirectives.size) { diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractKotlinCompilerTest.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractKotlinCompilerTest.kt index 89db141e50f..e9b40727887 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractKotlinCompilerTest.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractKotlinCompilerTest.kt @@ -12,8 +12,10 @@ import org.jetbrains.kotlin.test.builders.testRunner import org.jetbrains.kotlin.test.directives.ConfigurationDirectives import org.jetbrains.kotlin.test.directives.LanguageSettingsDirectives import org.jetbrains.kotlin.test.preprocessors.MetaInfosCleanupPreprocessor +import org.jetbrains.kotlin.test.services.BackendKindExtractor import org.jetbrains.kotlin.test.services.JUnit5Assertions import org.jetbrains.kotlin.test.services.SourceFilePreprocessor +import org.jetbrains.kotlin.test.services.impl.BackendKindExtractorImpl import org.jetbrains.kotlin.types.AbstractTypeChecker import org.jetbrains.kotlin.types.FlexibleTypeImpl @@ -31,6 +33,7 @@ abstract class AbstractKotlinCompilerTest { private val configuration: TestConfigurationBuilder.() -> Unit = { assertions = JUnit5Assertions + useAdditionalService(::BackendKindExtractorImpl) useSourcePreprocessor(*defaultPreprocessors.toTypedArray()) useDirectives(*defaultDirectiveContainers.toTypedArray()) configureDebugFlags() diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/impl/BackendKindExtractorImpl.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/impl/BackendKindExtractorImpl.kt new file mode 100644 index 00000000000..8ddb190714d --- /dev/null +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/impl/BackendKindExtractorImpl.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2010-2020 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.services.impl + +import org.jetbrains.kotlin.test.TargetBackend +import org.jetbrains.kotlin.test.model.BackendKind +import org.jetbrains.kotlin.test.model.BackendKinds +import org.jetbrains.kotlin.test.services.BackendKindExtractor +import org.jetbrains.kotlin.test.services.TestServices + +class BackendKindExtractorImpl(testServices: TestServices) : BackendKindExtractor(testServices) { + override fun backendKind(targetBackend: TargetBackend?): BackendKind<*> { + return when (targetBackend) { + TargetBackend.ANY, + TargetBackend.JVM, + TargetBackend.JVM_OLD, + TargetBackend.ANDROID, + TargetBackend.JVM_MULTI_MODULE_OLD_AGAINST_IR -> BackendKinds.ClassicBackend + + TargetBackend.JVM_IR, + TargetBackend.JVM_MULTI_MODULE_IR_AGAINST_OLD, + TargetBackend.JS, + TargetBackend.JS_IR, + TargetBackend.JS_IR_ES6, + TargetBackend.WASM -> BackendKinds.IrBackend + + null -> BackendKind.NoBackend + } + } +} diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/impl/ModuleStructureExtractorImpl.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/impl/ModuleStructureExtractorImpl.kt index e2724ea52a7..759e606dd84 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/impl/ModuleStructureExtractorImpl.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/impl/ModuleStructureExtractorImpl.kt @@ -11,6 +11,7 @@ import org.jetbrains.kotlin.platform.js.JsPlatforms import org.jetbrains.kotlin.platform.jvm.JvmPlatforms import org.jetbrains.kotlin.platform.konan.NativePlatforms import org.jetbrains.kotlin.test.Assertions +import org.jetbrains.kotlin.test.TargetBackend import org.jetbrains.kotlin.test.builders.LanguageVersionSettingsBuilder import org.jetbrains.kotlin.test.directives.ModuleStructureDirectives import org.jetbrains.kotlin.test.directives.model.ComposedRegisteredDirectives @@ -61,7 +62,7 @@ class ModuleStructureExtractorImpl( private var currentModuleName: String? = null private var currentModuleTargetPlatform: TargetPlatform? = null private var currentModuleFrontendKind: FrontendKind<*>? = null - private var currentModuleBackendKind: BackendKind<*>? = null + private var currentModuleTargetBackend: TargetBackend? = null private var currentModuleLanguageVersionSettingsBuilder: LanguageVersionSettingsBuilder = initLanguageSettingsBuilder() private var dependenciesOfCurrentModule = mutableListOf() private var filesOfCurrentModule = mutableListOf() @@ -171,12 +172,7 @@ class ModuleStructureExtractorImpl( } } ModuleStructureDirectives.TARGET_BACKEND_KIND -> { - val name = values.singleOrNull() as? String ?: assertions.fail { - "Target backend specified incorrectly\nUsage: ${directive.description}" - } - currentModuleBackendKind = BackendKinds.fromString(name) ?: assertions.fail { - "Unknown backend: $name" - } + currentModuleTargetBackend = values.single() as TargetBackend } ModuleStructureDirectives.FILE -> { if (currentFileName != null) { @@ -215,8 +211,8 @@ class ModuleStructureExtractorImpl( val testModule = TestModule( name = moduleName, targetPlatform = currentModuleTargetPlatform ?: parseModulePlatformByName(moduleName) ?: defaultsProvider.defaultPlatform, + targetBackend = currentModuleTargetBackend ?: defaultsProvider.defaultTargetBackend, frontendKind = currentModuleFrontendKind ?: defaultsProvider.defaultFrontend, - backendKind = currentModuleBackendKind ?: defaultsProvider.defaultBackend, files = filesOfCurrentModule, dependencies = dependenciesOfCurrentModule, directives = moduleDirectives, @@ -271,8 +267,8 @@ class ModuleStructureExtractorImpl( firstFileInModule = true currentModuleName = null currentModuleTargetPlatform = null + currentModuleTargetBackend = null currentModuleFrontendKind = null - currentModuleBackendKind = null currentModuleLanguageVersionSettingsBuilder = initLanguageSettingsBuilder() filesOfCurrentModule = mutableListOf() dependenciesOfCurrentModule = mutableListOf()