[FE 1.0] Remove constraint system tests

These tests used the old type inference constraint system, so it didn't cover the current compiler logic almost at all

It'd be fine to implement similar test for the new type inference constraint system

^KT-52699
This commit is contained in:
Victor Petukhov
2022-06-09 11:12:39 +02:00
committed by teamcity
parent 8500ee08a8
commit 9be181f1ad
255 changed files with 0 additions and 4662 deletions
@@ -1,154 +0,0 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.resolve.constraintSystem
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.diagnostics.rendering.Renderers
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.TypeResolver
import org.jetbrains.kotlin.resolve.calls.inference.CallHandle
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintContext
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilderImpl
import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.SPECIAL
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil
import org.jetbrains.kotlin.test.ConfigurationKind
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.KotlinTestWithEnvironment
import org.jetbrains.kotlin.test.util.KtTestUtil
import org.jetbrains.kotlin.tests.di.createContainerForTests
import org.jetbrains.kotlin.types.error.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.Variance
import java.io.File
abstract class AbstractConstraintSystemTest : KotlinTestWithEnvironment() {
private var _typeResolver: TypeResolver? = null
private val typeResolver: TypeResolver
get() = _typeResolver!!
private var _testDeclarations: ConstraintSystemTestData? = null
private val testDeclarations: ConstraintSystemTestData
get() = _testDeclarations!!
override fun createEnvironment(): KotlinCoreEnvironment {
return createEnvironmentWithMockJdk(ConfigurationKind.ALL)
}
override fun setUp() {
super.setUp()
_typeResolver = createContainerForTests(project, KotlinTestUtils.createEmptyModule()).typeResolver
_testDeclarations = analyzeDeclarations()
}
override fun tearDown() {
_typeResolver = null
_testDeclarations = null
super.tearDown()
}
private val testDataPath: String
get() = KtTestUtil.getTestDataPathBase() + "/constraintSystem/"
private fun analyzeDeclarations(): ConstraintSystemTestData {
val fileName = "declarations.kt"
val psiFile = KtTestUtil.createFile(
fileName,
KtTestUtil.doLoadFile(testDataPath, fileName),
project
)
val bindingContext = JvmResolveUtil.analyzeAndCheckForErrors(psiFile, environment).bindingContext
return ConstraintSystemTestData(bindingContext, project, typeResolver)
}
fun doTest(filePath: String) {
val constraintsFile = File(filePath)
val constraintsFileText = constraintsFile.readLines()
val builder = ConstraintSystemBuilderImpl()
val variables = parseVariables(constraintsFileText)
val fixVariables = constraintsFileText.contains("FIX_VARIABLES")
val typeParameterDescriptors = variables.map { testDeclarations.getParameterDescriptor(it) }
val substitutor = builder.registerTypeVariables(CallHandle.NONE, typeParameterDescriptors)
val constraints = parseConstraints(constraintsFileText)
fun getType(typeString: String): KotlinType {
val type = testDeclarations.getType(typeString).apply {
assert(!ErrorUtils.containsErrorType(this)) { "Type $this is resolved to or contains error type" }
}
return substitutor.substitute(type, Variance.INVARIANT) ?: error("Failed to substitute $type")
}
for (constraint in constraints) {
val firstType = getType(constraint.firstType)
val secondType = getType(constraint.secondType)
val context = ConstraintContext(SPECIAL.position(), initial = true, initialReduction = true)
when (constraint.kind) {
MyConstraintKind.SUBTYPE -> builder.addSubtypeConstraint(firstType, secondType, context.position)
MyConstraintKind.SUPERTYPE -> builder.addSubtypeConstraint(secondType, firstType, context.position)
MyConstraintKind.EQUAL -> builder.addConstraint(
ConstraintSystemBuilderImpl.ConstraintKind.EQUAL, firstType, secondType, context
)
}
}
if (fixVariables) builder.fixVariables()
val system = builder.build()
val resultingStatus = Renderers.renderConstraintSystem(system, Renderers.ConstraintSystemRenderingVerbosity.COMPACT)
val resultingSubstitutor = system.resultingSubstitutor
val result = typeParameterDescriptors.map {
val parameterType = testDeclarations.getType(it.name.asString())
val resultType = resultingSubstitutor.substitute(parameterType, Variance.INVARIANT)
"${it.name}=${resultType?.let { DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(it) }}"
}.joinToString("\n", prefix = "result:\n")
val boundsFile = File(filePath.replace("constraints", "bounds"))
KotlinTestUtils.assertEqualsToFile(boundsFile, "${constraintsFileText.joinToString("\n")}\n\n$resultingStatus\n\n$result")
}
class MyConstraint(val kind: MyConstraintKind, val firstType: String, val secondType: String)
enum class MyConstraintKind(val token: String) {
SUBTYPE("<:"), SUPERTYPE(">:"), EQUAL(":=")
}
private fun parseVariables(lines: List<String>): List<String> {
val first = lines.first()
val variablesString = "VARIABLES "
assert (first.startsWith(variablesString)) { "The first line should contain variables: $first"}
val variables = first.substringAfter(variablesString).split(' ')
return variables.toList()
}
private fun parseConstraints(lines: List<String>): List<MyConstraint> {
val kindsMap = MyConstraintKind.values().map { it.token to it }.toMap()
val kinds = kindsMap.keys
val linesWithConstraints = lines.filter { line -> kinds.any { kind -> line.contains(kind) } }
return linesWithConstraints.map {
line ->
val kind = kinds.first { line.contains(it) }
val firstType = line.substringBefore(kind).trim()
val secondType = line.substringAfter(kind).trim()
MyConstraint(kindsMap[kind]!!, firstType, secondType)
}
}
}
@@ -1,84 +0,0 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.resolve.constraintSystem
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.TypeResolver
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant
import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstructor
import org.jetbrains.kotlin.resolve.descriptorUtil.module
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.test.DummyTraces
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.KotlinTypeFactory
import org.jetbrains.kotlin.types.TypeAttributes
import java.util.regex.Pattern
class ConstraintSystemTestData(
context: BindingContext,
private val project: Project,
private val typeResolver: TypeResolver
) {
private val functionFoo: FunctionDescriptor
private val scopeToResolveTypeParameters: LexicalScope
init {
val functions = context.getSliceContents(BindingContext.FUNCTION)
functionFoo = findFunctionByName(functions.values, "foo")
val function = DescriptorToSourceUtils.descriptorToDeclaration(functionFoo) as KtFunction
val fooBody = function.bodyExpression
scopeToResolveTypeParameters = context.get(BindingContext.LEXICAL_SCOPE, fooBody)!!
}
private fun findFunctionByName(functions: Collection<FunctionDescriptor>, name: String): FunctionDescriptor {
return functions.firstOrNull { it.name.asString() == name } ?:
throw AssertionError("Function ${name} is not declared")
}
fun getParameterDescriptor(name: String): TypeParameterDescriptor {
return functionFoo.typeParameters.firstOrNull { it.name.asString() == name } ?:
throw AssertionError("Unsupported type parameter name: $name. You may add it to constraintSystem/declarations.kt")
}
fun getType(name: String): KotlinType {
val matcher = INTEGER_VALUE_TYPE_PATTERN.matcher(name)
if (matcher.find()) {
val number = matcher.group(1)!!
val parameters = CompileTimeConstant.Parameters(false, false, false, false, false, false, false, false)
return KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(
TypeAttributes.Empty,
IntegerValueTypeConstructor(number.toLong(), functionFoo.module, parameters),
listOf(),
false,
MemberScope.Empty
)
}
return typeResolver.resolveType(
scopeToResolveTypeParameters, KtPsiFactory(project).createType(name),
DummyTraces.DUMMY_TRACE, true)
}
}
private val INTEGER_VALUE_TYPE_PATTERN = Pattern.compile("""IntegerValueType\((\d*)\)""")