[Test] Migrate IrTextTestGenerated to new infrastructure

This commit is contained in:
Dmitriy Novozhilov
2021-01-18 18:00:44 +03:00
committed by TeamCityServer
parent 5ae5f660f6
commit aaa3fa5845
14 changed files with 953 additions and 370 deletions

View File

@@ -1063,9 +1063,9 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest {
runTest("compiler/testData/ir/irText/expressions/interfaceThisRef.kt");
}
@TestMetadata("javaSyntheticGenericPropretyAccess.kt")
public void testJavaSyntheticGenericPropretyAccess() throws Exception {
runTest("compiler/testData/ir/irText/expressions/javaSyntheticGenericPropretyAccess.kt");
@TestMetadata("javaSyntheticGenericPropertyAccess.kt")
public void testJavaSyntheticGenericPropertyAccess() throws Exception {
runTest("compiler/testData/ir/irText/expressions/javaSyntheticGenericPropertyAccess.kt");
}
@TestMetadata("javaSyntheticPropertyAccess.kt")

View File

@@ -44,6 +44,10 @@ class IrLazyFunction(
override val stubGenerator: DeclarationStubGenerator,
override val typeTranslator: TypeTranslator,
) : IrSimpleFunction(), IrLazyFunctionBase {
init {
@Suppress("UNUSED_VARIABLE") val x = 1
}
override var parent: IrDeclarationParent by createLazyParent()
override var annotations: List<IrConstructorCall> by createLazyAnnotations()

View File

@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.test.services.jvm.compiledClassesManager
import org.jetbrains.kotlin.test.services.sourceFileProvider
import org.jetbrains.kotlin.test.util.KtTestUtil
import java.io.File
import java.nio.file.Paths
class JavaCompilerFacade(private val testServices: TestServices) {
@OptIn(ExperimentalStdlibApi::class)
@@ -51,7 +50,8 @@ class JavaCompilerFacade(private val testServices: TestServices) {
)
val javaFiles = module.javaFiles.map { testServices.sourceFileProvider.getRealFileForSourceFile(it) }
compileJavaFiles(configuration[JVMConfigurationKeys.JVM_TARGET] ?: JvmTarget.DEFAULT, javaFiles, finalJavacOptions)
val ignoreErrors = CodegenTestDirectives.IGNORE_JAVA_ERRORS in module.directives
compileJavaFiles(configuration[JVMConfigurationKeys.JVM_TARGET] ?: JvmTarget.DEFAULT, javaFiles, finalJavacOptions, ignoreErrors)
}
@OptIn(ExperimentalStdlibApi::class)
@@ -73,10 +73,15 @@ class JavaCompilerFacade(private val testServices: TestServices) {
}
}
private fun compileJavaFiles(jvmTarget: JvmTarget, files: List<File>, javacOptions: List<String>) {
private fun compileJavaFiles(jvmTarget: JvmTarget, files: List<File>, javacOptions: List<String>, ignoreErrors: Boolean) {
val targetIsJava8OrLower = System.getProperty("java.version").startsWith("1.")
if (targetIsJava8OrLower) {
org.jetbrains.kotlin.test.compileJavaFiles(files, javacOptions, assertions = testServices.assertions)
org.jetbrains.kotlin.test.compileJavaFiles(
files,
javacOptions,
assertions = testServices.assertions,
ignoreJavaErrors = ignoreErrors
)
return
}
val jdkHome = when (jvmTarget) {

View File

@@ -0,0 +1,13 @@
/*
* 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.backend.handlers
import org.jetbrains.kotlin.test.backend.ir.IrBackendInput
import org.jetbrains.kotlin.test.model.BackendInputHandler
import org.jetbrains.kotlin.test.model.BackendKinds
import org.jetbrains.kotlin.test.services.TestServices
abstract class AbstractIrHandler(testServices: TestServices) : BackendInputHandler<IrBackendInput>(testServices, BackendKinds.IrBackend)

View File

@@ -0,0 +1,116 @@
/*
* 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.backend.handlers
import org.jetbrains.kotlin.backend.common.serialization.signature.IdSignatureDescriptor
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.findClassAcrossModuleDependencies
import org.jetbrains.kotlin.ir.IrVerifier
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsManglerDesc
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.test.backend.ir.IrBackendInput
import org.jetbrains.kotlin.test.directives.CodegenTestDirectives
import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.DUMP_EXTERNAL_CLASS
import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.DUMP_IR
import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.EXTERNAL_FILE
import org.jetbrains.kotlin.test.directives.FirDiagnosticsDirectives
import org.jetbrains.kotlin.test.directives.FirDiagnosticsDirectives.FIR_IDENTICAL
import org.jetbrains.kotlin.test.directives.model.DirectivesContainer
import org.jetbrains.kotlin.test.model.FrontendKinds
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.moduleStructure
import org.jetbrains.kotlin.test.utils.MultiModuleInfoDumperImpl
import org.jetbrains.kotlin.test.utils.withExtension
import org.jetbrains.kotlin.test.utils.withSuffixAndExtension
import java.io.File
class IrTextDumpHandler(testServices: TestServices) : AbstractIrHandler(testServices) {
override val directivesContainers: List<DirectivesContainer>
get() = listOf(CodegenTestDirectives, FirDiagnosticsDirectives)
private val baseDumper = MultiModuleInfoDumperImpl()
private val buildersForSeparateFileDumps: MutableMap<File, StringBuilder> = mutableMapOf()
@OptIn(ExperimentalStdlibApi::class)
override fun processModule(module: TestModule, info: IrBackendInput) {
if (DUMP_IR !in module.directives) return
val irFiles = info.backendInput.irModuleFragment.files
val testFileToIrFile = irFiles.mapNotNull { irFile ->
val name = irFile.fileEntry.name.removePrefix("/")
val testFile = module.files.firstOrNull { it.name == name } ?: return@mapNotNull null
testFile to irFile
}
val builder = baseDumper.builderForModule(module)
for ((testFile, irFile) in testFileToIrFile) {
if (EXTERNAL_FILE in testFile.directives) continue
val actualDump = irFile.dumpTreesFromLineNumber(lineNumber = 0, normalizeNames = true)
builder.append(actualDump)
verify(irFile)
val irFileCopy = irFile.deepCopyWithSymbols()
val dumpOfCopy = irFileCopy.dumpTreesFromLineNumber(lineNumber = 0, normalizeNames = true)
assertions.assertEquals(actualDump, dumpOfCopy) { "IR dump mismatch after deep copy with symbols" }
}
compareDumpsOfExternalClasses(module, info)
}
private fun compareDumpsOfExternalClasses(module: TestModule, info: IrBackendInput) {
val externalClassFqns = module.directives[DUMP_EXTERNAL_CLASS]
if (externalClassFqns.isEmpty()) return
// TODO: why JS one is used here in original AbstractIrTextTestCase?
val mangler = JsManglerDesc
val signaturer = IdSignatureDescriptor(mangler)
val irModule = info.backendInput.irModuleFragment
val stubGenerator = DeclarationStubGenerator(
irModule.descriptor,
SymbolTable(signaturer, IrFactoryImpl), // TODO
module.languageVersionSettings
)
val baseFile = testServices.moduleStructure.originalTestDataFiles.first()
for (externalClassFqn in externalClassFqns) {
val classDump = stubGenerator.generateExternalClass(irModule.descriptor, externalClassFqn).dump()
val expectedFile = baseFile.withSuffixAndExtension("__$externalClassFqn", module.dumpExtension)
assertions.assertEqualsToFile(expectedFile, classDump)
}
}
private fun DeclarationStubGenerator.generateExternalClass(descriptor: ModuleDescriptor, externalClassFqn: String): IrClass {
val classDescriptor =
descriptor.findClassAcrossModuleDependencies(ClassId.topLevel(FqName(externalClassFqn)))
?: throw AssertionError("Can't find a class in external dependencies: $externalClassFqn")
return generateMemberStub(classDescriptor) as IrClass
}
override fun processAfterAllModules(someAssertionWasFailed: Boolean) {
val moduleStructure = testServices.moduleStructure
val defaultExpectedFile = moduleStructure.originalTestDataFiles.first().withExtension(moduleStructure.modules.first().dumpExtension)
checkOneExpectedFile(defaultExpectedFile, baseDumper.generateResultingDump())
buildersForSeparateFileDumps.entries.forEach { (expectedFile, dump) -> checkOneExpectedFile(expectedFile, dump.toString()) }
}
private fun checkOneExpectedFile(expectedFile: File, actualDump: String) {
if (actualDump.isNotEmpty()) {
assertions.assertEqualsToFile(expectedFile, actualDump)
}
}
private fun verify(irFile: IrFile) {
IrVerifier(assertions).verifyWithAssert(irFile)
}
private val TestModule.dumpExtension: String
get() = if (frontendKind == FrontendKinds.ClassicFrontend || FIR_IDENTICAL in directives) "txt" else "fir.txt"
}

View File

@@ -6,9 +6,9 @@
package org.jetbrains.kotlin.test.directives
import org.jetbrains.kotlin.test.TargetBackend
import org.jetbrains.kotlin.test.backend.handlers.IrTextDumpHandler
import org.jetbrains.kotlin.test.backend.handlers.NoCompilationErrorsHandler
import org.jetbrains.kotlin.test.backend.ir.JvmIrBackendFacade
import org.jetbrains.kotlin.test.directives.model.DirectiveApplicability
import org.jetbrains.kotlin.test.directives.model.DirectiveApplicability.File
import org.jetbrains.kotlin.test.directives.model.DirectiveApplicability.Global
import org.jetbrains.kotlin.test.directives.model.SimpleDirectivesContainer
@@ -56,13 +56,16 @@ object CodegenTestDirectives : SimpleDirectivesContainer() {
""".trimIndent()
)
val IGNORE_JAVA_ERRORS by directive(
description = "Ignore compilation errors from java"
)
val IGNORE_FIR_DIAGNOSTICS by directive(
description = "Run backend even FIR reported some diagnostics with ERROR severity"
)
val IR_FILE by stringDirective(
description = "Specifies file name for IR text dump",
applicability = File
val DUMP_IR by directive(
description = "Dumps generated backend IR (enables ${IrTextDumpHandler::class})"
)
val DUMP_EXTERNAL_CLASS by stringDirective(
@@ -70,7 +73,7 @@ object CodegenTestDirectives : SimpleDirectivesContainer() {
)
val EXTERNAL_FILE by directive(
description = "Indicates that test file is external",
description = "Indicates that test file is external and should be skipped in ${IrTextDumpHandler::class}",
applicability = File
)
}

View File

@@ -15,7 +15,15 @@ abstract class AbstractKotlinCompilerWithTargetBackendTest(
super.configure(builder)
with(builder) {
globalDefaults {
targetBackend = this@AbstractKotlinCompilerWithTargetBackendTest.targetBackend
val targetBackendFromMarker = this@AbstractKotlinCompilerWithTargetBackendTest.targetBackend
if (targetBackend == null) {
targetBackend = this@AbstractKotlinCompilerWithTargetBackendTest.targetBackend
} else {
require(targetBackend == targetBackendFromMarker) {
"""Target backend in configuration specified to $targetBackend but in
|AbstractKotlinCompilerWithTargetBackendTest parent it is set to $targetBackendFromMarker""".trimMargin()
}
}
}
}
}

View File

@@ -0,0 +1,66 @@
/*
* 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.ir
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms
import org.jetbrains.kotlin.test.TargetBackend
import org.jetbrains.kotlin.test.backend.BlackBoxCodegenSuppressor
import org.jetbrains.kotlin.test.backend.handlers.*
import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder
import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.DUMP_IR
import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontend2IrConverter
import org.jetbrains.kotlin.test.frontend.classic.ClassicFrontendFacade
import org.jetbrains.kotlin.test.frontend.fir.Fir2IrResultsConverter
import org.jetbrains.kotlin.test.frontend.fir.FirFrontendFacade
import org.jetbrains.kotlin.test.model.*
import org.jetbrains.kotlin.test.runners.AbstractKotlinCompilerWithTargetBackendTest
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.CodegenHelpersSourceFilesProvider
import org.jetbrains.kotlin.test.services.sourceProviders.CoroutineHelpersSourceFilesProvider
abstract class AbstractIrTextTestBase(
private val frontend: FrontendKind<*>
) : AbstractKotlinCompilerWithTargetBackendTest(TargetBackend.JVM_IR) {
override fun TestConfigurationBuilder.configuration() {
globalDefaults {
frontend = this@AbstractIrTextTestBase.frontend
targetPlatform = JvmPlatforms.defaultJvmPlatform
artifactKind = BinaryKind.NoArtifact
targetBackend = TargetBackend.JVM_IR
dependencyKind = DependencyKind.Source
}
defaultDirectives {
+DUMP_IR
}
useConfigurators(
::CommonEnvironmentConfigurator,
::JvmEnvironmentConfigurator
)
useAdditionalSourceProviders(
::AdditionalDiagnosticsSourceFilesProvider,
::CoroutineHelpersSourceFilesProvider,
::CodegenHelpersSourceFilesProvider,
)
useFrontendFacades(
::ClassicFrontendFacade,
::FirFrontendFacade
)
useFrontend2BackendConverters(
::ClassicFrontend2IrConverter,
::Fir2IrResultsConverter
)
useBackendHandlers(::IrTextDumpHandler)
}
}
open class AbstractIrTextTest : AbstractIrTextTestBase(FrontendKinds.ClassicFrontend)

View File

@@ -11,23 +11,20 @@ import org.jetbrains.kotlin.backend.common.serialization.signature.IdSignatureDe
import org.jetbrains.kotlin.cli.js.loadPluginsForTests
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.findClassAcrossModuleDependencies
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsManglerDesc
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.scripting.compiler.plugin.loadScriptConfiguration
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
import org.jetbrains.kotlin.test.util.JUnit4Assertions
import org.jetbrains.kotlin.utils.fileUtils.withReplacedExtensionOrNull
import org.jetbrains.kotlin.utils.rethrow
import java.io.File
@@ -141,190 +138,7 @@ abstract class AbstractIrTextTestCase : AbstractIrGeneratorTestCase() {
}
private fun verify(irFile: IrFile) {
IrVerifier().verifyWithAssert(irFile)
}
private class IrVerifier : IrElementVisitorVoid {
private val errors = ArrayList<String>()
private val symbolForDeclaration = HashMap<IrElement, IrSymbol>()
val hasErrors get() = errors.isNotEmpty()
val errorsAsMessage get() = errors.joinToString(prefix = "IR verifier errors:\n", separator = "\n")
private fun error(message: String) {
errors.add(message)
}
private inline fun require(condition: Boolean, message: () -> String) {
if (!condition) {
errors.add(message())
}
}
private val elementsAreUniqueChecker = object : IrElementVisitorVoid {
private val elements = HashSet<IrElement>()
override fun visitElement(element: IrElement) {
require(elements.add(element)) { "Non-unique element: ${element.render()}" }
element.acceptChildrenVoid(this)
}
}
fun verifyWithAssert(irFile: IrFile) {
irFile.acceptChildrenVoid(this)
irFile.acceptChildrenVoid(elementsAreUniqueChecker)
TestCase.assertFalse(errorsAsMessage + "\n\n\n" + irFile.dump(), hasErrors)
}
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
}
@OptIn(ObsoleteDescriptorBasedAPI::class)
override fun visitDeclaration(declaration: IrDeclarationBase) {
declaration.symbol.checkBinding("decl", declaration)
require(declaration.symbol.owner == declaration) {
"Symbol is not bound to declaration: ${declaration.render()}"
}
val containingDeclarationDescriptor = declaration.descriptor.containingDeclaration
if (containingDeclarationDescriptor != null) {
val parent = declaration.parent
if (parent is IrDeclaration) {
require(parent.descriptor == containingDeclarationDescriptor) {
"In declaration ${declaration.descriptor}: " +
"Mismatching parent descriptor (${parent.descriptor}) " +
"and containing declaration descriptor ($containingDeclarationDescriptor)"
}
}
}
}
override fun visitProperty(declaration: IrProperty) {
visitDeclaration(declaration)
require((declaration.origin == IrDeclarationOrigin.FAKE_OVERRIDE) == declaration.isFakeOverride) {
"${declaration.render()}: origin: ${declaration.origin}; isFakeOverride: ${declaration.isFakeOverride}"
}
}
@OptIn(ObsoleteDescriptorBasedAPI::class)
override fun visitFunction(declaration: IrFunction) {
visitDeclaration(declaration)
val functionDescriptor = declaration.descriptor
checkTypeParameters(functionDescriptor, declaration, functionDescriptor.typeParameters)
val expectedDispatchReceiver = functionDescriptor.dispatchReceiverParameter
val actualDispatchReceiver = declaration.dispatchReceiverParameter?.descriptor
require(expectedDispatchReceiver == actualDispatchReceiver) {
"$functionDescriptor: Dispatch receiver parameter mismatch: " +
"expected $expectedDispatchReceiver, actual $actualDispatchReceiver"
}
val expectedExtensionReceiver = functionDescriptor.extensionReceiverParameter
val actualExtensionReceiver = declaration.extensionReceiverParameter?.descriptor
require(expectedExtensionReceiver == actualExtensionReceiver) {
"$functionDescriptor: Extension receiver parameter mismatch: " +
"expected $expectedExtensionReceiver, actual $actualExtensionReceiver"
}
val declaredValueParameters = declaration.valueParameters.map { it.descriptor }
val actualValueParameters = functionDescriptor.valueParameters
if (declaredValueParameters.size != actualValueParameters.size) {
error("$functionDescriptor: Value parameters mismatch: $declaredValueParameters != $actualValueParameters")
} else {
declaredValueParameters.zip(actualValueParameters).forEach { (declaredValueParameter, actualValueParameter) ->
require(declaredValueParameter == actualValueParameter) {
"$functionDescriptor: Value parameters mismatch: $declaredValueParameter != $actualValueParameter"
}
}
}
}
override fun visitSimpleFunction(declaration: IrSimpleFunction) {
visitFunction(declaration)
require((declaration.origin == IrDeclarationOrigin.FAKE_OVERRIDE) == declaration.isFakeOverride) {
"${declaration.render()}: origin: ${declaration.origin}; isFakeOverride: ${declaration.isFakeOverride}"
}
}
override fun visitDeclarationReference(expression: IrDeclarationReference) {
expression.symbol.checkBinding("ref", expression)
}
override fun visitFunctionReference(expression: IrFunctionReference) {
expression.symbol.checkBinding("ref", expression)
}
override fun visitPropertyReference(expression: IrPropertyReference) {
expression.field?.checkBinding("field", expression)
expression.getter?.checkBinding("getter", expression)
expression.setter?.checkBinding("setter", expression)
}
override fun visitLocalDelegatedPropertyReference(expression: IrLocalDelegatedPropertyReference) {
expression.delegate.checkBinding("delegate", expression)
expression.getter.checkBinding("getter", expression)
expression.setter?.checkBinding("setter", expression)
}
private fun IrSymbol.checkBinding(kind: String, irElement: IrElement) {
if (!isBound) {
error("${javaClass.simpleName} descriptor is unbound @$kind ${irElement.render()}")
} else {
val irDeclaration = owner as? IrDeclaration
if (irDeclaration != null) {
try {
irDeclaration.parent
} catch (e: Throwable) {
error("Referenced declaration has no parent: ${irDeclaration.render()}")
}
}
}
val otherSymbol = symbolForDeclaration.getOrPut(owner) { this }
if (this != otherSymbol) {
error("Multiple symbols for descriptor of @$kind ${irElement.render()}")
}
}
@OptIn(ObsoleteDescriptorBasedAPI::class)
override fun visitClass(declaration: IrClass) {
visitDeclaration(declaration)
checkTypeParameters(declaration.descriptor, declaration, declaration.descriptor.declaredTypeParameters)
}
@ObsoleteDescriptorBasedAPI
private fun checkTypeParameters(
descriptor: DeclarationDescriptor,
declaration: IrTypeParametersContainer,
expectedTypeParameters: List<TypeParameterDescriptor>
) {
val declaredTypeParameters = declaration.typeParameters.map { it.descriptor }
if (declaredTypeParameters.size != expectedTypeParameters.size) {
error("$descriptor: Type parameters mismatch: $declaredTypeParameters != $expectedTypeParameters")
} else {
declaredTypeParameters.zip(expectedTypeParameters).forEach { (declaredTypeParameter, expectedTypeParameter) ->
require(declaredTypeParameter == expectedTypeParameter) {
"$descriptor: Type parameters mismatch: $declaredTypeParameter != $expectedTypeParameter"
}
}
}
}
override fun visitTypeOperator(expression: IrTypeOperatorCall) {
expression.typeOperandClassifier.checkBinding("type operand", expression)
}
IrVerifier(JUnit4Assertions).verifyWithAssert(irFile)
}
internal class Expectations(val regexps: List<RegexpInText>, val irTreeFileLabels: List<IrTreeFileLabel>)

View File

@@ -0,0 +1,201 @@
/*
* 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.ir
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.util.dump
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.test.Assertions
class IrVerifier(private val assertions: Assertions) : IrElementVisitorVoid {
private val errors = ArrayList<String>()
private val symbolForDeclaration = HashMap<IrElement, IrSymbol>()
val hasErrors get() = errors.isNotEmpty()
val errorsAsMessage get() = errors.joinToString(prefix = "IR verifier errors:\n", separator = "\n")
private fun error(message: String) {
errors.add(message)
}
private inline fun require(condition: Boolean, message: () -> String) {
if (!condition) {
errors.add(message())
}
}
private val elementsAreUniqueChecker = object : IrElementVisitorVoid {
private val elements = HashSet<IrElement>()
override fun visitElement(element: IrElement) {
require(elements.add(element)) { "Non-unique element: ${element.render()}" }
element.acceptChildrenVoid(this)
}
}
fun verifyWithAssert(irFile: IrFile) {
irFile.acceptChildrenVoid(this)
irFile.acceptChildrenVoid(elementsAreUniqueChecker)
assertions.assertFalse(hasErrors) { errorsAsMessage + "\n\n\n" + irFile.dump() }
}
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
}
@OptIn(ObsoleteDescriptorBasedAPI::class)
override fun visitDeclaration(declaration: IrDeclarationBase) {
declaration.symbol.checkBinding("decl", declaration)
require(declaration.symbol.owner == declaration) {
"Symbol is not bound to declaration: ${declaration.render()}"
}
val containingDeclarationDescriptor = declaration.descriptor.containingDeclaration
if (containingDeclarationDescriptor != null) {
val parent = declaration.parent
if (parent is IrDeclaration) {
require(parent.descriptor == containingDeclarationDescriptor) {
"In declaration ${declaration.descriptor}: " +
"Mismatching parent descriptor (${parent.descriptor}) " +
"and containing declaration descriptor ($containingDeclarationDescriptor)"
}
}
}
}
override fun visitProperty(declaration: IrProperty) {
visitDeclaration(declaration)
require((declaration.origin == IrDeclarationOrigin.FAKE_OVERRIDE) == declaration.isFakeOverride) {
"${declaration.render()}: origin: ${declaration.origin}; isFakeOverride: ${declaration.isFakeOverride}"
}
}
@OptIn(ObsoleteDescriptorBasedAPI::class)
override fun visitFunction(declaration: IrFunction) {
visitDeclaration(declaration)
val functionDescriptor = declaration.descriptor
checkTypeParameters(functionDescriptor, declaration, functionDescriptor.typeParameters)
val expectedDispatchReceiver = functionDescriptor.dispatchReceiverParameter
val actualDispatchReceiver = declaration.dispatchReceiverParameter?.descriptor
require(expectedDispatchReceiver == actualDispatchReceiver) {
"$functionDescriptor: Dispatch receiver parameter mismatch: " +
"expected $expectedDispatchReceiver, actual $actualDispatchReceiver"
}
val expectedExtensionReceiver = functionDescriptor.extensionReceiverParameter
val actualExtensionReceiver = declaration.extensionReceiverParameter?.descriptor
require(expectedExtensionReceiver == actualExtensionReceiver) {
"$functionDescriptor: Extension receiver parameter mismatch: " +
"expected $expectedExtensionReceiver, actual $actualExtensionReceiver"
}
val declaredValueParameters = declaration.valueParameters.map { it.descriptor }
val actualValueParameters = functionDescriptor.valueParameters
if (declaredValueParameters.size != actualValueParameters.size) {
error("$functionDescriptor: Value parameters mismatch: $declaredValueParameters != $actualValueParameters")
} else {
declaredValueParameters.zip(actualValueParameters).forEach { (declaredValueParameter, actualValueParameter) ->
require(declaredValueParameter == actualValueParameter) {
"$functionDescriptor: Value parameters mismatch: $declaredValueParameter != $actualValueParameter"
}
}
}
}
override fun visitSimpleFunction(declaration: IrSimpleFunction) {
visitFunction(declaration)
require((declaration.origin == IrDeclarationOrigin.FAKE_OVERRIDE) == declaration.isFakeOverride) {
"${declaration.render()}: origin: ${declaration.origin}; isFakeOverride: ${declaration.isFakeOverride}"
}
}
override fun visitDeclarationReference(expression: IrDeclarationReference) {
expression.symbol.checkBinding("ref", expression)
}
override fun visitFunctionReference(expression: IrFunctionReference) {
expression.symbol.checkBinding("ref", expression)
}
override fun visitPropertyReference(expression: IrPropertyReference) {
expression.field?.checkBinding("field", expression)
expression.getter?.checkBinding("getter", expression)
expression.setter?.checkBinding("setter", expression)
}
override fun visitLocalDelegatedPropertyReference(expression: IrLocalDelegatedPropertyReference) {
expression.delegate.checkBinding("delegate", expression)
expression.getter.checkBinding("getter", expression)
expression.setter?.checkBinding("setter", expression)
}
private fun IrSymbol.checkBinding(kind: String, irElement: IrElement) {
if (!isBound) {
error("${javaClass.simpleName} descriptor is unbound @$kind ${irElement.render()}")
} else {
val irDeclaration = owner as? IrDeclaration
if (irDeclaration != null) {
try {
irDeclaration.parent
} catch (e: Throwable) {
error("Referenced declaration has no parent: ${irDeclaration.render()}")
}
}
}
val otherSymbol = symbolForDeclaration.getOrPut(owner) { this }
if (this != otherSymbol) {
error("Multiple symbols for descriptor of @$kind ${irElement.render()}")
}
}
@OptIn(ObsoleteDescriptorBasedAPI::class)
override fun visitClass(declaration: IrClass) {
visitDeclaration(declaration)
checkTypeParameters(declaration.descriptor, declaration, declaration.descriptor.declaredTypeParameters)
}
@ObsoleteDescriptorBasedAPI
private fun checkTypeParameters(
descriptor: DeclarationDescriptor,
declaration: IrTypeParametersContainer,
expectedTypeParameters: List<TypeParameterDescriptor>
) {
val declaredTypeParameters = declaration.typeParameters.map { it.descriptor }
if (declaredTypeParameters.size != expectedTypeParameters.size) {
error("$descriptor: Type parameters mismatch: $declaredTypeParameters != $expectedTypeParameters")
} else {
declaredTypeParameters.zip(expectedTypeParameters).forEach { (declaredTypeParameter, expectedTypeParameter) ->
require(declaredTypeParameter == expectedTypeParameter) {
"$descriptor: Type parameters mismatch: $declaredTypeParameter != $expectedTypeParameter"
}
}
}
}
override fun visitTypeOperator(expression: IrTypeOperatorCall) {
expression.typeOperandClassifier.checkBinding("type operand", expression)
}
}

View File

@@ -21,7 +21,13 @@ import javax.tools.ToolProvider
@JvmOverloads
@Throws(IOException::class)
fun compileJavaFiles(files: Collection<File>, options: List<String?>?, javaErrorFile: File? = null, assertions: Assertions): Boolean {
fun compileJavaFiles(
files: Collection<File>,
options: List<String?>?,
javaErrorFile: File? = null,
assertions: Assertions,
ignoreJavaErrors: Boolean = false
): Boolean {
val javaCompiler = ToolProvider.getSystemJavaCompiler()
val diagnosticCollector = DiagnosticCollector<JavaFileObject>()
javaCompiler.getStandardFileManager(diagnosticCollector, Locale.ENGLISH, Charset.forName("utf-8")).use { fileManager ->
@@ -36,7 +42,7 @@ fun compileJavaFiles(files: Collection<File>, options: List<String?>?, javaError
)
val success = task.call() // do NOT inline this variable, call() should complete before errorsToString()
if (javaErrorFile == null || !javaErrorFile.exists()) {
assertions.assertTrue(success) { errorsToString(diagnosticCollector, true) }
assertions.assertTrue(success || ignoreJavaErrors) { errorsToString(diagnosticCollector, true) }
} else {
assertions.assertEqualsToFile(javaErrorFile, errorsToString(diagnosticCollector, false))
}

View File

@@ -144,10 +144,6 @@ fun generateJUnit3CompilerTests(args: Array<String>) {
model("codegen/bytecodeText", targetBackend = TargetBackend.JVM)
}
testClass<AbstractIrTextTestCase> {
model("ir/irText")
}
testClass<AbstractIrJsTextTestCase> {
model("ir/irJsText", pattern = "^(.+)\\.kt(s)?\$")
}

View File

@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.test.generators
import org.jetbrains.kotlin.generators.model.AnnotationModel
import org.jetbrains.kotlin.generators.model.annotation
import org.jetbrains.kotlin.generators.util.TestGeneratorUtil
import org.jetbrains.kotlin.test.TargetBackend
@@ -13,6 +12,7 @@ import org.jetbrains.kotlin.test.runners.*
import org.jetbrains.kotlin.test.runners.codegen.AbstractBlackBoxCodegenTest
import org.jetbrains.kotlin.test.runners.codegen.AbstractFirBlackBoxCodegenTest
import org.jetbrains.kotlin.test.runners.codegen.AbstractIrBlackBoxCodegenTest
import org.jetbrains.kotlin.test.runners.ir.AbstractIrTextTest
import org.junit.jupiter.api.parallel.Execution
import org.junit.jupiter.api.parallel.ExecutionMode
@@ -68,6 +68,10 @@ fun generateJUnit5CompilerTests(args: Array<String>) {
testClass<AbstractIrBlackBoxCodegenTest> {
model("codegen/box", excludeDirs = listOf("oldLanguageVersions"))
}
testClass<AbstractIrTextTest> {
model("ir/irText")
}
}
// ---------------------------------------------- FIR tests ----------------------------------------------