From 5455942859daf4f79093103f5f7d7b24967c725a Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Tue, 21 Feb 2023 17:40:32 -0800 Subject: [PATCH] AA: handle underscore as type arguments ^KTIJ-24742 Fixed --- .../components/KtFe10TypeProvider.kt | 21 ++++- ...ourceModuleTypeReferenceTestGenerated.java | 90 +++++++++++++++++++ .../api/fir/components/KtFirTypeProvider.kt | 6 ++ ...ourceModuleTypeReferenceTestGenerated.java | 90 +++++++++++++++++++ ...ourceModuleTypeReferenceTestGenerated.java | 90 +++++++++++++++++++ .../typeProvider/AbstractTypeReferenceTest.kt | 31 +++++++ ...ourceModuleTypeReferenceTestGenerated.java | 90 +++++++++++++++++++ .../analysis/api/components/KtTypeProvider.kt | 8 +- .../typeReference/starProjection.kt | 12 +++ .../typeReference/starProjection.txt | 2 + .../typeReference/superTypeEntry.kt | 3 + .../typeReference/superTypeEntry.txt | 2 + .../superTypeEntry_withTypeArgument.kt | 7 ++ .../superTypeEntry_withTypeArgument.txt | 2 + .../typeArgument_functionCall.kt | 7 ++ .../typeArgument_functionCall.txt | 2 + .../typeArgument_superTypeEntry.kt | 7 ++ .../typeArgument_superTypeEntry.txt | 2 + .../underscoreTypeArgument_inferred.kt | 32 +++++++ .../underscoreTypeArgument_inferred.txt | 2 + .../underscoreTypeArgument_reified.kt | 32 +++++++ .../underscoreTypeArgument_reified.txt | 2 + .../tests/analysis/api/analysisApi.kt | 4 + 23 files changed, 537 insertions(+), 7 deletions(-) create mode 100644 analysis/analysis-api-fe10/tests-gen/org/jetbrains/kotlin/analysis/api/fe10/test/cases/generated/cases/components/typeProvider/Fe10IdeNormalAnalysisSourceModuleTypeReferenceTestGenerated.java create mode 100644 analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/typeProvider/FirIdeDependentAnalysisSourceModuleTypeReferenceTestGenerated.java create mode 100644 analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/typeProvider/FirIdeNormalAnalysisSourceModuleTypeReferenceTestGenerated.java create mode 100644 analysis/analysis-api-impl-base/tests/org/jetbrains/kotlin/analysis/api/impl/base/test/cases/components/typeProvider/AbstractTypeReferenceTest.kt create mode 100644 analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/typeProvider/FirStandaloneNormalAnalysisSourceModuleTypeReferenceTestGenerated.java create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/starProjection.kt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/starProjection.txt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry.kt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry.txt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry_withTypeArgument.kt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry_withTypeArgument.txt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_functionCall.kt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_functionCall.txt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_superTypeEntry.kt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_superTypeEntry.txt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_inferred.kt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_inferred.txt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_reified.kt create mode 100644 analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_reified.txt diff --git a/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/components/KtFe10TypeProvider.kt b/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/components/KtFe10TypeProvider.kt index fb7f33b75af..5cfaf814ea1 100644 --- a/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/components/KtFe10TypeProvider.kt +++ b/analysis/analysis-api-fe10/src/org/jetbrains/kotlin/analysis/api/descriptors/components/KtFe10TypeProvider.kt @@ -30,12 +30,15 @@ import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.name.StandardClassIds +import org.jetbrains.kotlin.psi.KtCallElement import org.jetbrains.kotlin.psi.KtDoubleColonExpression import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.KtTypeReference +import org.jetbrains.kotlin.psi.psiUtil.getParentOfType import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.calls.inference.CapturedType +import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall import org.jetbrains.kotlin.resolve.scopes.utils.getImplicitReceiversHierarchy import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.NewCapturedType @@ -65,18 +68,19 @@ internal class KtFe10TypeProvider( override fun approximateToSuperPublicDenotableType(type: KtType, approximateLocalTypes: Boolean): KtType? { require(type is KtFe10Type) - return typeApproximator.approximateToSuperType(type.fe10Type, PublicApproximatorConfiguration(approximateLocalTypes))?.toKtType(analysisContext) + return typeApproximator.approximateToSuperType(type.fe10Type, PublicApproximatorConfiguration(approximateLocalTypes)) + ?.toKtType(analysisContext) } override fun approximateToSubPublicDenotableType(type: KtType, approximateLocalTypes: Boolean): KtType? { require(type is KtFe10Type) - return typeApproximator.approximateToSubType(type.fe10Type, PublicApproximatorConfiguration(approximateLocalTypes))?.toKtType(analysisContext) + return typeApproximator.approximateToSubType(type.fe10Type, PublicApproximatorConfiguration(approximateLocalTypes)) + ?.toKtType(analysisContext) } override fun buildSelfClassType(symbol: KtNamedClassOrObjectSymbol): KtType { val kotlinType = (getSymbolDescriptor(symbol) as? ClassDescriptor)?.defaultType ?: ErrorUtils.createErrorType(ErrorTypeKind.UNRESOLVED_CLASS_TYPE, symbol.nameOrAnonymous.toString()) - return kotlinType.toKtType(analysisContext) } @@ -88,10 +92,21 @@ internal class KtFe10TypeProvider( override fun getKtType(ktTypeReference: KtTypeReference): KtType { val bindingContext = analysisContext.analyze(ktTypeReference, AnalysisMode.PARTIAL) val kotlinType = bindingContext[BindingContext.TYPE, ktTypeReference] + ?: getKtTypeAsTypeArgument(ktTypeReference) ?: ErrorUtils.createErrorType(ErrorTypeKind.UNRESOLVED_TYPE, ktTypeReference.text) return kotlinType.toKtType(analysisContext) } + private fun getKtTypeAsTypeArgument(ktTypeReference: KtTypeReference): KotlinType? { + val call = ktTypeReference.getParentOfType(strict = true) ?: return null + val bindingContext = analysisContext.analyze(ktTypeReference, AnalysisMode.PARTIAL) + val resolvedCall = call.getResolvedCall(bindingContext) ?: return null + val typeProjection = call.typeArguments.find { it.typeReference == ktTypeReference } ?: return null + val index = call.typeArguments.indexOf(typeProjection) + val paramDescriptor = resolvedCall.candidateDescriptor.typeParameters.find { it.index == index } ?: return null + return resolvedCall.typeArguments[paramDescriptor] + } + override fun getReceiverTypeForDoubleColonExpression(expression: KtDoubleColonExpression): KtType? { val bindingContext = analysisContext.analyze(expression, AnalysisMode.PARTIAL) val lhs = bindingContext[BindingContext.DOUBLE_COLON_LHS, expression] ?: return null diff --git a/analysis/analysis-api-fe10/tests-gen/org/jetbrains/kotlin/analysis/api/fe10/test/cases/generated/cases/components/typeProvider/Fe10IdeNormalAnalysisSourceModuleTypeReferenceTestGenerated.java b/analysis/analysis-api-fe10/tests-gen/org/jetbrains/kotlin/analysis/api/fe10/test/cases/generated/cases/components/typeProvider/Fe10IdeNormalAnalysisSourceModuleTypeReferenceTestGenerated.java new file mode 100644 index 00000000000..a73f777b997 --- /dev/null +++ b/analysis/analysis-api-fe10/tests-gen/org/jetbrains/kotlin/analysis/api/fe10/test/cases/generated/cases/components/typeProvider/Fe10IdeNormalAnalysisSourceModuleTypeReferenceTestGenerated.java @@ -0,0 +1,90 @@ +/* + * Copyright 2010-2023 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.fe10.test.cases.generated.cases.components.typeProvider; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.util.KtTestUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.kotlin.analysis.api.fe10.test.configurator.AnalysisApiFe10TestConfiguratorFactory; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfiguratorFactoryData; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.FrontendKind; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisSessionMode; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiMode; +import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.typeProvider.AbstractTypeReferenceTest; +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 org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("analysis/analysis-api/testData/components/typeProvider/typeReference") +@TestDataPath("$PROJECT_ROOT") +public class Fe10IdeNormalAnalysisSourceModuleTypeReferenceTestGenerated extends AbstractTypeReferenceTest { + @NotNull + @Override + public AnalysisApiTestConfigurator getConfigurator() { + return AnalysisApiFe10TestConfiguratorFactory.INSTANCE.createConfigurator( + new AnalysisApiTestConfiguratorFactoryData( + FrontendKind.Fe10, + TestModuleKind.Source, + AnalysisSessionMode.Normal, + AnalysisApiMode.Ide + ) + ); + } + + @Test + public void testAllFilesPresentInTypeReference() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/typeProvider/typeReference"), Pattern.compile("^(.+)\\.kt$"), null, true); + } + + @Test + @TestMetadata("starProjection.kt") + public void testStarProjection() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/starProjection.kt"); + } + + @Test + @TestMetadata("superTypeEntry.kt") + public void testSuperTypeEntry() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry.kt"); + } + + @Test + @TestMetadata("superTypeEntry_withTypeArgument.kt") + public void testSuperTypeEntry_withTypeArgument() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry_withTypeArgument.kt"); + } + + @Test + @TestMetadata("typeArgument_functionCall.kt") + public void testTypeArgument_functionCall() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_functionCall.kt"); + } + + @Test + @TestMetadata("typeArgument_superTypeEntry.kt") + public void testTypeArgument_superTypeEntry() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_superTypeEntry.kt"); + } + + @Test + @TestMetadata("underscoreTypeArgument_inferred.kt") + public void testUnderscoreTypeArgument_inferred() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_inferred.kt"); + } + + @Test + @TestMetadata("underscoreTypeArgument_reified.kt") + public void testUnderscoreTypeArgument_reified() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_reified.kt"); + } +} diff --git a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirTypeProvider.kt b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirTypeProvider.kt index 44854e87791..3a2e3e5e4a0 100644 --- a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirTypeProvider.kt +++ b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/components/KtFirTypeProvider.kt @@ -97,6 +97,12 @@ internal class KtFirTypeProvider( return when (val fir = ktTypeReference.getOrBuildFir(firResolveSession)) { is FirResolvedTypeRef -> fir.coneType.asKtType() is FirDelegatedConstructorCall -> fir.constructedTypeRef.coneType.asKtType() + is FirTypeProjectionWithVariance -> { + when (val typeRef = fir.typeRef) { + is FirResolvedTypeRef -> typeRef.coneType.asKtType() + else -> throwUnexpectedFirElementError(fir, ktTypeReference) + } + } else -> throwUnexpectedFirElementError(fir, ktTypeReference) } } diff --git a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/typeProvider/FirIdeDependentAnalysisSourceModuleTypeReferenceTestGenerated.java b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/typeProvider/FirIdeDependentAnalysisSourceModuleTypeReferenceTestGenerated.java new file mode 100644 index 00000000000..7cd931157f6 --- /dev/null +++ b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/typeProvider/FirIdeDependentAnalysisSourceModuleTypeReferenceTestGenerated.java @@ -0,0 +1,90 @@ +/* + * Copyright 2010-2023 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.test.cases.generated.cases.components.typeProvider; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.util.KtTestUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.kotlin.analysis.api.fir.test.configurators.AnalysisApiFirTestConfiguratorFactory; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfiguratorFactoryData; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.FrontendKind; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisSessionMode; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiMode; +import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.typeProvider.AbstractTypeReferenceTest; +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 org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("analysis/analysis-api/testData/components/typeProvider/typeReference") +@TestDataPath("$PROJECT_ROOT") +public class FirIdeDependentAnalysisSourceModuleTypeReferenceTestGenerated extends AbstractTypeReferenceTest { + @NotNull + @Override + public AnalysisApiTestConfigurator getConfigurator() { + return AnalysisApiFirTestConfiguratorFactory.INSTANCE.createConfigurator( + new AnalysisApiTestConfiguratorFactoryData( + FrontendKind.Fir, + TestModuleKind.Source, + AnalysisSessionMode.Dependent, + AnalysisApiMode.Ide + ) + ); + } + + @Test + public void testAllFilesPresentInTypeReference() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/typeProvider/typeReference"), Pattern.compile("^(.+)\\.kt$"), null, true); + } + + @Test + @TestMetadata("starProjection.kt") + public void testStarProjection() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/starProjection.kt"); + } + + @Test + @TestMetadata("superTypeEntry.kt") + public void testSuperTypeEntry() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry.kt"); + } + + @Test + @TestMetadata("superTypeEntry_withTypeArgument.kt") + public void testSuperTypeEntry_withTypeArgument() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry_withTypeArgument.kt"); + } + + @Test + @TestMetadata("typeArgument_functionCall.kt") + public void testTypeArgument_functionCall() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_functionCall.kt"); + } + + @Test + @TestMetadata("typeArgument_superTypeEntry.kt") + public void testTypeArgument_superTypeEntry() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_superTypeEntry.kt"); + } + + @Test + @TestMetadata("underscoreTypeArgument_inferred.kt") + public void testUnderscoreTypeArgument_inferred() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_inferred.kt"); + } + + @Test + @TestMetadata("underscoreTypeArgument_reified.kt") + public void testUnderscoreTypeArgument_reified() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_reified.kt"); + } +} diff --git a/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/typeProvider/FirIdeNormalAnalysisSourceModuleTypeReferenceTestGenerated.java b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/typeProvider/FirIdeNormalAnalysisSourceModuleTypeReferenceTestGenerated.java new file mode 100644 index 00000000000..7d61942b273 --- /dev/null +++ b/analysis/analysis-api-fir/tests-gen/org/jetbrains/kotlin/analysis/api/fir/test/cases/generated/cases/components/typeProvider/FirIdeNormalAnalysisSourceModuleTypeReferenceTestGenerated.java @@ -0,0 +1,90 @@ +/* + * Copyright 2010-2023 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.test.cases.generated.cases.components.typeProvider; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.util.KtTestUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.kotlin.analysis.api.fir.test.configurators.AnalysisApiFirTestConfiguratorFactory; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfiguratorFactoryData; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.FrontendKind; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisSessionMode; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiMode; +import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.typeProvider.AbstractTypeReferenceTest; +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 org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("analysis/analysis-api/testData/components/typeProvider/typeReference") +@TestDataPath("$PROJECT_ROOT") +public class FirIdeNormalAnalysisSourceModuleTypeReferenceTestGenerated extends AbstractTypeReferenceTest { + @NotNull + @Override + public AnalysisApiTestConfigurator getConfigurator() { + return AnalysisApiFirTestConfiguratorFactory.INSTANCE.createConfigurator( + new AnalysisApiTestConfiguratorFactoryData( + FrontendKind.Fir, + TestModuleKind.Source, + AnalysisSessionMode.Normal, + AnalysisApiMode.Ide + ) + ); + } + + @Test + public void testAllFilesPresentInTypeReference() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/typeProvider/typeReference"), Pattern.compile("^(.+)\\.kt$"), null, true); + } + + @Test + @TestMetadata("starProjection.kt") + public void testStarProjection() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/starProjection.kt"); + } + + @Test + @TestMetadata("superTypeEntry.kt") + public void testSuperTypeEntry() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry.kt"); + } + + @Test + @TestMetadata("superTypeEntry_withTypeArgument.kt") + public void testSuperTypeEntry_withTypeArgument() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry_withTypeArgument.kt"); + } + + @Test + @TestMetadata("typeArgument_functionCall.kt") + public void testTypeArgument_functionCall() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_functionCall.kt"); + } + + @Test + @TestMetadata("typeArgument_superTypeEntry.kt") + public void testTypeArgument_superTypeEntry() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_superTypeEntry.kt"); + } + + @Test + @TestMetadata("underscoreTypeArgument_inferred.kt") + public void testUnderscoreTypeArgument_inferred() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_inferred.kt"); + } + + @Test + @TestMetadata("underscoreTypeArgument_reified.kt") + public void testUnderscoreTypeArgument_reified() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_reified.kt"); + } +} diff --git a/analysis/analysis-api-impl-base/tests/org/jetbrains/kotlin/analysis/api/impl/base/test/cases/components/typeProvider/AbstractTypeReferenceTest.kt b/analysis/analysis-api-impl-base/tests/org/jetbrains/kotlin/analysis/api/impl/base/test/cases/components/typeProvider/AbstractTypeReferenceTest.kt new file mode 100644 index 00000000000..e829f74968f --- /dev/null +++ b/analysis/analysis-api-impl-base/tests/org/jetbrains/kotlin/analysis/api/impl/base/test/cases/components/typeProvider/AbstractTypeReferenceTest.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2010-2023 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.impl.base.test.cases.components.typeProvider + +import org.jetbrains.kotlin.analysis.test.framework.base.AbstractAnalysisApiSingleFileTest +import org.jetbrains.kotlin.analysis.test.framework.services.expressionMarkerProvider +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtTypeReference +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.types.Variance + +abstract class AbstractTypeReferenceTest : AbstractAnalysisApiSingleFileTest() { + override fun doTestByFileStructure(ktFile: KtFile, module: TestModule, testServices: TestServices) { + val expressionAtCaret = testServices.expressionMarkerProvider.getElementOfTypeAtCaret(ktFile) as KtTypeReference + + val actual = analyseForTest(expressionAtCaret) { + val ktType = expressionAtCaret.getKtType() + buildString { + appendLine("expression: ${expressionAtCaret.text}") + appendLine("ktType: ${ktType.render(position = Variance.INVARIANT)}") + } + } + + testServices.assertions.assertEqualsToTestDataFileSibling(actual) + } +} diff --git a/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/typeProvider/FirStandaloneNormalAnalysisSourceModuleTypeReferenceTestGenerated.java b/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/typeProvider/FirStandaloneNormalAnalysisSourceModuleTypeReferenceTestGenerated.java new file mode 100644 index 00000000000..577a647a4a3 --- /dev/null +++ b/analysis/analysis-api-standalone/tests-gen/org/jetbrains/kotlin/analysis/api/standalone/fir/test/cases/generated/cases/components/typeProvider/FirStandaloneNormalAnalysisSourceModuleTypeReferenceTestGenerated.java @@ -0,0 +1,90 @@ +/* + * Copyright 2010-2023 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.standalone.fir.test.cases.generated.cases.components.typeProvider; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.util.KtTestUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.kotlin.analysis.api.standalone.fir.test.AnalysisApiFirStandaloneModeTestConfiguratorFactory; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfiguratorFactoryData; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.FrontendKind; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisSessionMode; +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiMode; +import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.typeProvider.AbstractTypeReferenceTest; +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 org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("analysis/analysis-api/testData/components/typeProvider/typeReference") +@TestDataPath("$PROJECT_ROOT") +public class FirStandaloneNormalAnalysisSourceModuleTypeReferenceTestGenerated extends AbstractTypeReferenceTest { + @NotNull + @Override + public AnalysisApiTestConfigurator getConfigurator() { + return AnalysisApiFirStandaloneModeTestConfiguratorFactory.INSTANCE.createConfigurator( + new AnalysisApiTestConfiguratorFactoryData( + FrontendKind.Fir, + TestModuleKind.Source, + AnalysisSessionMode.Normal, + AnalysisApiMode.Standalone + ) + ); + } + + @Test + public void testAllFilesPresentInTypeReference() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/typeProvider/typeReference"), Pattern.compile("^(.+)\\.kt$"), null, true); + } + + @Test + @TestMetadata("starProjection.kt") + public void testStarProjection() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/starProjection.kt"); + } + + @Test + @TestMetadata("superTypeEntry.kt") + public void testSuperTypeEntry() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry.kt"); + } + + @Test + @TestMetadata("superTypeEntry_withTypeArgument.kt") + public void testSuperTypeEntry_withTypeArgument() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry_withTypeArgument.kt"); + } + + @Test + @TestMetadata("typeArgument_functionCall.kt") + public void testTypeArgument_functionCall() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_functionCall.kt"); + } + + @Test + @TestMetadata("typeArgument_superTypeEntry.kt") + public void testTypeArgument_superTypeEntry() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_superTypeEntry.kt"); + } + + @Test + @TestMetadata("underscoreTypeArgument_inferred.kt") + public void testUnderscoreTypeArgument_inferred() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_inferred.kt"); + } + + @Test + @TestMetadata("underscoreTypeArgument_reified.kt") + public void testUnderscoreTypeArgument_reified() throws Exception { + runTest("analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_reified.kt"); + } +} diff --git a/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtTypeProvider.kt b/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtTypeProvider.kt index 5e401f171da..b951372b74c 100644 --- a/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtTypeProvider.kt +++ b/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtTypeProvider.kt @@ -49,19 +49,19 @@ public interface KtTypeProviderMixIn : KtAnalysisSessionMixIn { get() = withValidityAssertion { analysisSession.typeProvider.builtinTypes } /** - * Approximates [KtType] with the a supertype which can be rendered in a source code + * Approximates [KtType] with a supertype which can be rendered in a source code * * Return `null` if the type do not need approximation and can be rendered as is - * Otherwise, for type `T` return type `S` such `T <: S` and `T` and every it type argument is denotable + * Otherwise, for type `T` return type `S` such `T <: S` and `T` and every type argument is denotable */ public fun KtType.approximateToSuperPublicDenotable(approximateLocalTypes: Boolean): KtType? = withValidityAssertion { analysisSession.typeProvider.approximateToSuperPublicDenotableType(this, approximateLocalTypes) } /** - * Approximates [KtType] with the a subtype which can be rendered in a source code + * Approximates [KtType] with a subtype which can be rendered in a source code * * Return `null` if the type do not need approximation and can be rendered as is - * Otherwise, for type `T` return type `S` such `S <: T` and `T` and every it type argument is denotable + * Otherwise, for type `T` return type `S` such `S <: T` and `T` and every type argument is denotable */ public fun KtType.approximateToSubPublicDenotable(approximateLocalTypes: Boolean): KtType? = withValidityAssertion { analysisSession.typeProvider.approximateToSubPublicDenotableType(this, approximateLocalTypes) } diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/starProjection.kt b/analysis/analysis-api/testData/components/typeProvider/typeReference/starProjection.kt new file mode 100644 index 00000000000..43a8e364d51 --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/starProjection.kt @@ -0,0 +1,12 @@ +abstract class Base { + abstract foo(arg: T): T +} + +class StringSub : Base { + override fun foo(arg: String) = arg + " = 42" +} + +fun test() { + val x : Base<*> = StringSub() + x.foo("42") +} \ No newline at end of file diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/starProjection.txt b/analysis/analysis-api/testData/components/typeProvider/typeReference/starProjection.txt new file mode 100644 index 00000000000..79d5c1be797 --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/starProjection.txt @@ -0,0 +1,2 @@ +expression: Base<*> +ktType: Base<*> diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry.kt b/analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry.kt new file mode 100644 index 00000000000..45d2e4123de --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry.kt @@ -0,0 +1,3 @@ +open class Base + +class Sub : Base() \ No newline at end of file diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry.txt b/analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry.txt new file mode 100644 index 00000000000..72f50a4c993 --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry.txt @@ -0,0 +1,2 @@ +expression: Base +ktType: Base diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry_withTypeArgument.kt b/analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry_withTypeArgument.kt new file mode 100644 index 00000000000..e2b593f71fe --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry_withTypeArgument.kt @@ -0,0 +1,7 @@ +abstract class Base { + abstract foo(arg: T): T +} + +class StringSub : Base { + override fun foo(arg: String) = arg + " = 42" +} diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry_withTypeArgument.txt b/analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry_withTypeArgument.txt new file mode 100644 index 00000000000..cc23a96d5bc --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/superTypeEntry_withTypeArgument.txt @@ -0,0 +1,2 @@ +expression: Base +ktType: Base diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_functionCall.kt b/analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_functionCall.kt new file mode 100644 index 00000000000..c041cb29de6 --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_functionCall.kt @@ -0,0 +1,7 @@ +inline fun foo(arg: T) { + println(arg.toString()) +} + +fun test() { + foog>("42") +} \ No newline at end of file diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_functionCall.txt b/analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_functionCall.txt new file mode 100644 index 00000000000..013f7979354 --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_functionCall.txt @@ -0,0 +1,2 @@ +expression: String +ktType: kotlin.String diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_superTypeEntry.kt b/analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_superTypeEntry.kt new file mode 100644 index 00000000000..57c1cc0575d --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_superTypeEntry.kt @@ -0,0 +1,7 @@ +abstract class Base { + abstract foo(arg: T): T +} + +class StringSub : Baseg> { + override fun foo(arg: String) = arg + " = 42" +} diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_superTypeEntry.txt b/analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_superTypeEntry.txt new file mode 100644 index 00000000000..013f7979354 --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/typeArgument_superTypeEntry.txt @@ -0,0 +1,2 @@ +expression: String +ktType: kotlin.String diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_inferred.kt b/analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_inferred.kt new file mode 100644 index 00000000000..26733c60a27 --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_inferred.kt @@ -0,0 +1,32 @@ +// example from https://kotlinlang.org/docs/generics.html#underscore-operator-for-type-arguments +// modified to avoid using reflection (::class.java) + +abstract class SomeClass { + abstract fun execute() : T +} + +class SomeImplementation : SomeClass() { + override fun execute(): String = "Test" +} + +class OtherImplementation : SomeClass() { + override fun execute(): Int = 42 +} + +object Runner { + inline fun , T> run(instance: S) : T { + return instance.execute() + } +} + +fun test() { + val i = SomeImplementation() + // T is inferred as String because SomeImplementation derives from SomeClass + val s = Runner.run<_, _>(i) + assert(s == "Test") + + val j = OtherImplementation() + // T is inferred as Int because OtherImplementation derives from SomeClass + val n = Runner.run<_, _>(j) + assert(n == 42) +} diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_inferred.txt b/analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_inferred.txt new file mode 100644 index 00000000000..212f2f33177 --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_inferred.txt @@ -0,0 +1,2 @@ +expression: _ +ktType: kotlin.Int diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_reified.kt b/analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_reified.kt new file mode 100644 index 00000000000..9cb0f097277 --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_reified.kt @@ -0,0 +1,32 @@ +// example from https://kotlinlang.org/docs/generics.html#underscore-operator-for-type-arguments +// modified to avoid using reflection (::class.java) + +abstract class SomeClass { + abstract fun execute() : T +} + +class SomeImplementation : SomeClass() { + override fun execute(): String = "Test" +} + +class OtherImplementation : SomeClass() { + override fun execute(): Int = 42 +} + +object Runner { + inline fun , T> run(instance: S) : T { + return instance.execute() + } +} + +fun test() { + val i = SomeImplementation() + // T is inferred as String because SomeImplementation derives from SomeClass + val s = Runner.run<_, _>(i) + assert(s == "Test") + + val j = OtherImplementation() + // T is inferred as Int because OtherImplementation derives from SomeClass + val n = Runner.run<_, _>(j) + assert(n == 42) +} diff --git a/analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_reified.txt b/analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_reified.txt new file mode 100644 index 00000000000..208941fbb14 --- /dev/null +++ b/analysis/analysis-api/testData/components/typeProvider/typeReference/underscoreTypeArgument_reified.txt @@ -0,0 +1,2 @@ +expression: _ +ktType: OtherImplementation diff --git a/generators/analysis-api-generator/tests/org/jetbrains/kotlin/generators/tests/analysis/api/analysisApi.kt b/generators/analysis-api-generator/tests/org/jetbrains/kotlin/generators/tests/analysis/api/analysisApi.kt index 7218768027e..8c36a3e1651 100644 --- a/generators/analysis-api-generator/tests/org/jetbrains/kotlin/generators/tests/analysis/api/analysisApi.kt +++ b/generators/analysis-api-generator/tests/org/jetbrains/kotlin/generators/tests/analysis/api/analysisApi.kt @@ -43,6 +43,7 @@ import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.typeInf import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.typeInfoProvider.AbstractIsDenotableTest import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.typeProvider.AbstractAnalysisApiGetSuperTypesTest import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.typeProvider.AbstractHasCommonSubtypeTest +import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.components.typeProvider.AbstractTypeReferenceTest import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.references.AbstractReferenceResolveTest import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.references.AbstractReferenceShortenerTest import org.jetbrains.kotlin.analysis.api.impl.base.test.cases.scopes.* @@ -369,6 +370,9 @@ private fun AnalysisApiTestGroup.generateAnalysisApiComponentsTests() { model("haveCommonSubtype") } } + test(AbstractTypeReferenceTest::class) { + model("typeReference") + } } component("signatureSubstitution") {