[Analysis API] render enhanced types
^KTIJ-28555 Fixed
This commit is contained in:
committed by
teamcity
parent
69fe60f494
commit
d935db9e3a
+9
-6
@@ -8,8 +8,8 @@ package org.jetbrains.kotlin.analysis.api.descriptors.components
|
||||
import org.jetbrains.kotlin.analysis.api.components.KtBuiltinTypes
|
||||
import org.jetbrains.kotlin.analysis.api.components.KtTypeProvider
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisContext
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.KtFe10AnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisFacade.AnalysisMode
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.KtFe10AnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.components.base.Fe10KtAnalysisSessionComponent
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.base.KtFe10Symbol
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.*
|
||||
@@ -20,13 +20,13 @@ import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.bas
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.psiBased.base.getResolutionScope
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.types.base.KtFe10Type
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.utils.PublicApproximatorConfiguration
|
||||
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
|
||||
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtNamedClassOrObjectSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.nameOrAnonymous
|
||||
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtType
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtTypeNullability
|
||||
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
@@ -49,11 +49,8 @@ import org.jetbrains.kotlin.types.error.ErrorType
|
||||
import org.jetbrains.kotlin.types.error.ErrorTypeKind
|
||||
import org.jetbrains.kotlin.types.error.ErrorUtils
|
||||
import org.jetbrains.kotlin.types.typeUtil.builtIns
|
||||
import org.jetbrains.kotlin.types.typeUtil.isGenericArrayOfTypeParameter
|
||||
import org.jetbrains.kotlin.types.typeUtil.isNothing
|
||||
import org.jetbrains.kotlin.types.typeUtil.makeNullable
|
||||
import org.jetbrains.kotlin.util.containingNonLocalDeclaration
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.applyIf
|
||||
|
||||
internal class KtFe10TypeProvider(
|
||||
override val analysisSession: KtFe10AnalysisSession
|
||||
@@ -83,6 +80,12 @@ internal class KtFe10TypeProvider(
|
||||
?.toKtType(analysisContext)
|
||||
}
|
||||
|
||||
override fun getEnhancedType(type: KtType): KtType? {
|
||||
require(type is KtFe10Type)
|
||||
val enhancement = (type.fe10Type as? TypeWithEnhancement)?.enhancement
|
||||
return enhancement?.toKtType(analysisContext)
|
||||
}
|
||||
|
||||
override fun buildSelfClassType(symbol: KtNamedClassOrObjectSymbol): KtType {
|
||||
val kotlinType = (getSymbolDescriptor(symbol) as? ClassDescriptor)?.defaultType
|
||||
?: ErrorUtils.createErrorType(ErrorTypeKind.UNRESOLVED_CLASS_TYPE, symbol.nameOrAnonymous.toString())
|
||||
|
||||
+12
@@ -124,6 +124,18 @@ public class Fe10IdeNormalAnalysisSourceModuleHLExpressionTypeTestGenerated exte
|
||||
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/intLiteral.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedType.kt")
|
||||
public void testJavaEnhancedType() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/javaEnhancedType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedTypeExternalAnnotation.kt")
|
||||
public void testJavaEnhancedTypeExternalAnnotation() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/javaEnhancedTypeExternalAnnotation.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("listElement_listOf.kt")
|
||||
public void testListElement_listOf() throws Exception {
|
||||
|
||||
+12
@@ -46,6 +46,18 @@ public class Fe10IdeNormalAnalysisSourceModuleSymbolRenderingByReferenceTestGene
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/symbolDeclarationRenderer/symbolRenderingByReference"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedType.kt")
|
||||
public void testJavaEnhancedType() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/symbolRenderingByReference/javaEnhancedType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedTypeExternalAnnotation.kt")
|
||||
public void testJavaEnhancedTypeExternalAnnotation() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/symbolRenderingByReference/javaEnhancedTypeExternalAnnotation.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaMethodWithMultipleTypeParametersBound.kt")
|
||||
public void testJavaMethodWithMultipleTypeParametersBound() throws Exception {
|
||||
|
||||
+10
@@ -38,6 +38,7 @@ import org.jetbrains.kotlin.fir.expressions.FirCallableReferenceAccess
|
||||
import org.jetbrains.kotlin.fir.expressions.FirDelegatedConstructorCall
|
||||
import org.jetbrains.kotlin.fir.expressions.FirGetClassCall
|
||||
import org.jetbrains.kotlin.fir.expressions.FirPropertyAccessExpression
|
||||
import org.jetbrains.kotlin.fir.java.enhancement.EnhancedForWarningConeSubstitutor
|
||||
import org.jetbrains.kotlin.fir.psi
|
||||
import org.jetbrains.kotlin.fir.renderer.FirRenderer
|
||||
import org.jetbrains.kotlin.fir.resolve.SessionHolderImpl
|
||||
@@ -85,6 +86,15 @@ internal class KtFirTypeProvider(
|
||||
return approximatedConeType?.asKtType()
|
||||
}
|
||||
|
||||
override fun getEnhancedType(type: KtType): KtType? {
|
||||
require(type is KtFirType)
|
||||
val coneType = type.coneType
|
||||
val substitutor = EnhancedForWarningConeSubstitutor(typeContext)
|
||||
val enhancedConeType = substitutor.substituteType(coneType)
|
||||
|
||||
return enhancedConeType?.asKtType()
|
||||
}
|
||||
|
||||
override fun buildSelfClassType(symbol: KtNamedClassOrObjectSymbol): KtType {
|
||||
require(symbol is KtFirNamedClassOrObjectSymbol)
|
||||
symbol.firSymbol.lazyResolveToPhase(FirResolvePhase.SUPER_TYPES)
|
||||
|
||||
+12
@@ -124,6 +124,18 @@ public class FirIdeDependentAnalysisSourceModuleHLExpressionTypeTestGenerated ex
|
||||
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/intLiteral.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedType.kt")
|
||||
public void testJavaEnhancedType() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/javaEnhancedType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedTypeExternalAnnotation.kt")
|
||||
public void testJavaEnhancedTypeExternalAnnotation() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/javaEnhancedTypeExternalAnnotation.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("listElement_listOf.kt")
|
||||
public void testListElement_listOf() throws Exception {
|
||||
|
||||
+12
@@ -124,6 +124,18 @@ public class FirIdeNormalAnalysisSourceModuleHLExpressionTypeTestGenerated exten
|
||||
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/intLiteral.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedType.kt")
|
||||
public void testJavaEnhancedType() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/javaEnhancedType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedTypeExternalAnnotation.kt")
|
||||
public void testJavaEnhancedTypeExternalAnnotation() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/javaEnhancedTypeExternalAnnotation.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("listElement_listOf.kt")
|
||||
public void testListElement_listOf() throws Exception {
|
||||
|
||||
+12
@@ -46,6 +46,18 @@ public class FirIdeDependentAnalysisSourceModuleSymbolRenderingByReferenceTestGe
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/symbolDeclarationRenderer/symbolRenderingByReference"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedType.kt")
|
||||
public void testJavaEnhancedType() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/symbolRenderingByReference/javaEnhancedType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedTypeExternalAnnotation.kt")
|
||||
public void testJavaEnhancedTypeExternalAnnotation() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/symbolRenderingByReference/javaEnhancedTypeExternalAnnotation.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaMethodWithMultipleTypeParametersBound.kt")
|
||||
public void testJavaMethodWithMultipleTypeParametersBound() throws Exception {
|
||||
|
||||
+12
@@ -46,6 +46,18 @@ public class FirIdeNormalAnalysisSourceModuleSymbolRenderingByReferenceTestGener
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/symbolDeclarationRenderer/symbolRenderingByReference"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedType.kt")
|
||||
public void testJavaEnhancedType() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/symbolRenderingByReference/javaEnhancedType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedTypeExternalAnnotation.kt")
|
||||
public void testJavaEnhancedTypeExternalAnnotation() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/symbolRenderingByReference/javaEnhancedTypeExternalAnnotation.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaMethodWithMultipleTypeParametersBound.kt")
|
||||
public void testJavaMethodWithMultipleTypeParametersBound() throws Exception {
|
||||
|
||||
+12
@@ -124,6 +124,18 @@ public class FirStandaloneNormalAnalysisSourceModuleHLExpressionTypeTestGenerate
|
||||
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/intLiteral.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedType.kt")
|
||||
public void testJavaEnhancedType() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/javaEnhancedType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedTypeExternalAnnotation.kt")
|
||||
public void testJavaEnhancedTypeExternalAnnotation() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/javaEnhancedTypeExternalAnnotation.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("listElement_listOf.kt")
|
||||
public void testListElement_listOf() throws Exception {
|
||||
|
||||
+12
@@ -46,6 +46,18 @@ public class FirStandaloneNormalAnalysisSourceModuleSymbolRenderingByReferenceTe
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/symbolDeclarationRenderer/symbolRenderingByReference"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedType.kt")
|
||||
public void testJavaEnhancedType() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/symbolRenderingByReference/javaEnhancedType.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaEnhancedTypeExternalAnnotation.kt")
|
||||
public void testJavaEnhancedTypeExternalAnnotation() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/symbolRenderingByReference/javaEnhancedTypeExternalAnnotation.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaMethodWithMultipleTypeParametersBound.kt")
|
||||
public void testJavaMethodWithMultipleTypeParametersBound() throws Exception {
|
||||
|
||||
+9
@@ -23,6 +23,8 @@ public abstract class KtTypeProvider : KtAnalysisSessionComponent() {
|
||||
|
||||
public abstract fun approximateToSubPublicDenotableType(type: KtType, approximateLocalTypes: Boolean): KtType?
|
||||
|
||||
public abstract fun getEnhancedType(type: KtType): KtType?
|
||||
|
||||
public abstract fun buildSelfClassType(symbol: KtNamedClassOrObjectSymbol): KtType
|
||||
|
||||
public abstract fun commonSuperType(types: Collection<KtType>): KtType?
|
||||
@@ -74,6 +76,13 @@ public interface KtTypeProviderMixIn : KtAnalysisSessionMixIn {
|
||||
public fun KtType.approximateToSuperPublicDenotableOrSelf(approximateLocalTypes: Boolean): KtType =
|
||||
withValidityAssertion { approximateToSuperPublicDenotable(approximateLocalTypes) ?: this }
|
||||
|
||||
/**
|
||||
* Returns a warning-level enhanced type for [KtType] if it is present. Otherwise, returns `null`.
|
||||
*/
|
||||
public fun KtType.getEnhancedType(): KtType? = withValidityAssertion { analysisSession.typeProvider.getEnhancedType(this) }
|
||||
|
||||
public fun KtType.getEnhancedTypeOrSelf(): KtType? = withValidityAssertion { getEnhancedType() ?: this }
|
||||
|
||||
public fun KtNamedClassOrObjectSymbol.buildSelfClassType(): KtType =
|
||||
withValidityAssertion { analysisSession.typeProvider.buildSelfClassType(this) }
|
||||
|
||||
|
||||
+2
@@ -16,6 +16,8 @@ public interface KtRendererTypeApproximator {
|
||||
public object TO_DENOTABLE : KtRendererTypeApproximator {
|
||||
context(KtAnalysisSession)
|
||||
override fun approximateType(type: KtType, position: Variance): KtType {
|
||||
type.getEnhancedType()?.let { return it }
|
||||
|
||||
return when (position) {
|
||||
Variance.INVARIANT -> type
|
||||
Variance.IN_VARIANCE -> type.approximateToSubPublicDenotableOrSelf(approximateLocalTypes = false)
|
||||
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
// FILE: androidx/annotation/RecentlyNonNull.java
|
||||
package androidx.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.PARAMETER, ElementType.TYPE})
|
||||
public @interface RecentlyNonNull {
|
||||
}
|
||||
|
||||
// FILE: androidx/annotation/A.java
|
||||
package androidx.annotation;
|
||||
|
||||
public class A {
|
||||
@RecentlyNonNull
|
||||
public String bar(@RecentlyNonNull String string) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: main.kt
|
||||
import androidx.annotation.A
|
||||
|
||||
fun test(a: A) = <expr>a.bar()</expr>
|
||||
analysis/analysis-api/testData/components/expressionTypeProvider/expressionType/javaEnhancedType.txt
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
expression: a.bar()
|
||||
type: kotlin.String
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
expression: a.externalNotNullMethod()
|
||||
type: kotlin.String!
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// FILE: ClassWithExternalAnnotatedMembers.java
|
||||
public class ClassWithExternalAnnotatedMembers {
|
||||
public String externalNotNullMethod() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: annotations.xml
|
||||
<root>
|
||||
<item name='ClassWithExternalAnnotatedMembers java.lang.String externalNotNullMethod()'>
|
||||
<annotation name='org.jetbrains.annotations.NotNull'/>
|
||||
</item>
|
||||
</root>
|
||||
|
||||
|
||||
|
||||
// FILE: main.kt
|
||||
import ClassWithExternalAnnotatedMembers
|
||||
|
||||
fun test(a: ClassWithExternalAnnotatedMembers) = <expr>a.externalNotNullMethod()</expr>
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
expression: a.externalNotNullMethod()
|
||||
type: kotlin.String
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// FILE: androidx/annotation/RecentlyNonNull.java
|
||||
package androidx.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.PARAMETER, ElementType.TYPE})
|
||||
public @interface RecentlyNonNull {
|
||||
}
|
||||
|
||||
// FILE: androidx/annotation/A.java
|
||||
package androidx.annotation;
|
||||
|
||||
public class A {
|
||||
@RecentlyNonNull
|
||||
public String bar(@RecentlyNonNull String string) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: main.kt
|
||||
import androidx.annotation.A
|
||||
|
||||
fun test(a: A) = a.b<caret><caret_onAirContext>ar()
|
||||
+1
@@ -0,0 +1 @@
|
||||
open fun bar(string: kotlin.String): kotlin.String
|
||||
+1
@@ -0,0 +1 @@
|
||||
open fun externalNotNullMethod(): kotlin.String?
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// FILE: ClassWithExternalAnnotatedMembers.java
|
||||
public class ClassWithExternalAnnotatedMembers {
|
||||
public String externalNotNullMethod() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: annotations.xml
|
||||
<root>
|
||||
<item name='ClassWithExternalAnnotatedMembers java.lang.String externalNotNullMethod()'>
|
||||
<annotation name='org.jetbrains.annotations.NotNull'/>
|
||||
</item>
|
||||
</root>
|
||||
|
||||
|
||||
|
||||
// FILE: main.kt
|
||||
import ClassWithExternalAnnotatedMembers
|
||||
|
||||
fun test(a: ClassWithExternalAnnotatedMembers) = a.e<caret><caret_onAirContext>xternalNotNullMethod()
|
||||
+1
@@ -0,0 +1 @@
|
||||
open fun externalNotNullMethod(): kotlin.String
|
||||
Reference in New Issue
Block a user