Analysis API: fix PsiType creation for type parameter type with flexible upper bound

This commit is contained in:
Ilya Kirillov
2021-09-21 16:34:21 +02:00
parent 03c352c11e
commit 0c7728f64a
6 changed files with 95 additions and 0 deletions
@@ -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.analysis.api.fir.components
import org.jetbrains.kotlin.analysis.api.analyse
import org.jetbrains.kotlin.analysis.api.fir.FirFrontendApiTestConfiguratorService
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.expressionMarkerProvider
import org.jetbrains.kotlin.analysis.api.impl.base.test.test.framework.AbstractHLApiSingleFileTest
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.assertions
abstract class AbstractExpressionPsiTypeProviderTest : AbstractHLApiSingleFileTest(FirFrontendApiTestConfiguratorService) {
override fun doTestByFileStructure(ktFile: KtFile, module: TestModule, testServices: TestServices) {
val declarationAtCaret = testServices.expressionMarkerProvider.getSelectedElement(ktFile) as KtExpression
val actual = analyse(ktFile) {
val returnType = declarationAtCaret.getKtType()
?: error("Not a typable expression ${declarationAtCaret::class} ${declarationAtCaret.text}")
val psiType = returnType.asPsiType(declarationAtCaret)
buildString {
appendLine("KtType: ${returnType.render()}")
appendLine("PsiType: $psiType")
}
}
testServices.assertions.assertEqualsToFile(testDataFileSibling(".txt"), actual)
}
}
@@ -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.analysis.api.fir.components;
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("analysis/analysis-api/testData/components/expressionPsiType")
@TestDataPath("$PROJECT_ROOT")
public class ExpressionPsiTypeProviderTestGenerated extends AbstractExpressionPsiTypeProviderTest {
@Test
public void testAllFilesPresentInExpressionPsiType() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/expressionPsiType"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@Test
@TestMetadata("typeParamFlexibleUpperBound.kt")
public void testTypeParamFlexibleUpperBound() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionPsiType/typeParamFlexibleUpperBound.kt");
}
}
@@ -0,0 +1,21 @@
// FILE: kotlin.kt
fun bar() {
val xx = JavaClass::<expr>foo</expr>
}
// FILE: JavaClass.java
import org.jetbrains.annotations.NotNull;
public class JavaClass {
@NotNull
public static <T extends B> T foo(@NotNull T x) {
return x;
}
}
// FILE: B.java
public class B {
}
@@ -0,0 +1,2 @@
KtType: T!!
PsiType: PsiType:T
@@ -128,6 +128,10 @@ object AbstractTypeMapper {
}
}
type.isFlexible() -> {
return mapType(context, type.upperBoundIfFlexible(), mode, sw)
}
else -> throw UnsupportedOperationException("Unknown type $type")
}
}
@@ -109,6 +109,10 @@ fun main(args: Array<String>) {
model("components/psiTypeProvider")
}
testClass<AbstractExpressionPsiTypeProviderTest> {
model("components/expressionPsiType")
}
testClass<AbstractFirHLSmartCastInfoTest> {
model("components/smartCastInfo")
}