[FIR] Store abbreviated type in deserialized declarations as attribute

#KT-58542 Fixed
This commit is contained in:
Kirill Rakhman
2023-08-22 18:38:39 +02:00
committed by Space Team
parent 8e72f60996
commit 5b4409e34c
34 changed files with 251 additions and 89 deletions
@@ -5,7 +5,7 @@ FILE: noneWithForEach.kt
}
public final fun foo(conflicting: R|kotlin/collections/List<Diagnostic>|): R|kotlin/Unit| {
lval filtered: R|java/util/ArrayList<Diagnostic>| = R|kotlin/collections/arrayListOf|<R|Diagnostic|>()
lval filtered: R|kotlin/collections/ArrayList<Diagnostic>| = R|kotlin/collections/arrayListOf|<R|Diagnostic|>()
R|<local>/conflicting|.R|kotlin/collections/groupBy|<R|Diagnostic|, R|kotlin/String|>(<L> = groupBy@fun <anonymous>(it: R|Diagnostic|): R|kotlin/String| <inline=Inline, kind=UNKNOWN> {
^ R|<local>/it|.R|/Diagnostic.name|
}
@@ -1,7 +1,3 @@
// IGNORE_DIAGNOSTIC_API
// IGNORE_REVERSED_RESOLVE
// Ignore reason: KT-58786
interface Diagnostic {
val name: String
}
@@ -62,6 +62,9 @@ open class ConeTypeRenderer {
}
fun render(type: ConeKotlinType) {
type.abbreviatedType?.let {
return render(it)
}
if (type !is ConeFlexibleType && type !is ConeDefinitelyNotNullType) {
// We don't render attributes for flexible/definitely not null types here,
// because bounds duplicate these attributes often
@@ -0,0 +1,26 @@
/*
* 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.fir.types
import kotlin.reflect.KClass
class AbbreviatedTypeAttribute(
val coneType: ConeKotlinType
): ConeAttribute<AbbreviatedTypeAttribute>() {
override fun union(other: AbbreviatedTypeAttribute?): AbbreviatedTypeAttribute? = null
override fun intersect(other: AbbreviatedTypeAttribute?): AbbreviatedTypeAttribute? = null
override fun add(other: AbbreviatedTypeAttribute?): AbbreviatedTypeAttribute? = null
override fun isSubtypeOf(other: AbbreviatedTypeAttribute?): Boolean = true
override fun toString(): String = "{${coneType.renderForDebugging()}=}"
override val key: KClass<out AbbreviatedTypeAttribute>
get() = AbbreviatedTypeAttribute::class
}
val ConeAttributes.abbreviatedType: AbbreviatedTypeAttribute? by ConeAttributes.attributeAccessor<AbbreviatedTypeAttribute>()
val ConeKotlinType.abbreviatedType: ConeKotlinType?
get() = attributes.abbreviatedType?.coneType
@@ -110,6 +110,15 @@ class ConeAttributes private constructor(attributes: List<ConeAttribute<*>>) : A
return create(attributes)
}
fun replace(oldAttribute: ConeAttribute<*>, newAttribute: ConeAttribute<*>): ConeAttributes {
return create(buildList {
arrayMap.mapNotNullTo(this) { attr ->
attr.takeUnless { it == oldAttribute }
}
add(newAttribute)
})
}
private inline fun perform(other: ConeAttributes, op: ConeAttribute<*>.(ConeAttribute<*>?) -> ConeAttribute<*>?): ConeAttributes {
if (this.isEmpty() && other.isEmpty()) return this
val attributes = mutableListOf<ConeAttribute<*>>()
@@ -201,8 +201,10 @@ class FirTypeDeserializer(
else -> ConeClassLikeTypeImpl(constructor, arguments, isNullable = proto.nullable, attributes)
}
// TODO: Return abbreviated types for type aliases, see KT-58542
return simpleType
val abbreviatedType = proto.abbreviatedType(typeTable)?.let { simpleType(it, attributes) }
?: return simpleType
return simpleType.withAttributes(simpleType.attributes.plus(AbbreviatedTypeAttribute(abbreviatedType)))
}
private fun createSuspendFunctionTypeForBasicCase(
@@ -52587,6 +52587,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("deserializedAbbreviationWithRedundantArgument.kt")
public void testDeserializedAbbreviationWithRedundantArgument() throws Exception {
runTest("compiler/testData/codegen/box/typealias/deserializedAbbreviationWithRedundantArgument.kt");
}
@Test
@TestMetadata("enumEntryQualifier.kt")
public void testEnumEntryQualifier() throws Exception {
@@ -52587,6 +52587,12 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("deserializedAbbreviationWithRedundantArgument.kt")
public void testDeserializedAbbreviationWithRedundantArgument() throws Exception {
runTest("compiler/testData/codegen/box/typealias/deserializedAbbreviationWithRedundantArgument.kt");
}
@Test
@TestMetadata("enumEntryQualifier.kt")
public void testEnumEntryQualifier() throws Exception {
@@ -52587,6 +52587,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("deserializedAbbreviationWithRedundantArgument.kt")
public void testDeserializedAbbreviationWithRedundantArgument() throws Exception {
runTest("compiler/testData/codegen/box/typealias/deserializedAbbreviationWithRedundantArgument.kt");
}
@Test
@TestMetadata("enumEntryQualifier.kt")
public void testEnumEntryQualifier() throws Exception {
@@ -53,10 +53,33 @@ abstract class AbstractConeSubstitutor(protected val typeContext: ConeTypeContex
override fun substituteOrNull(type: ConeKotlinType): ConeKotlinType? {
val newType = substituteType(type)
if (newType != null && type is ConeDefinitelyNotNullType) {
return newType.makeConeTypeDefinitelyNotNullOrNotNull(typeContext, avoidComprehensiveCheck = false)
}
return (newType ?: type.substituteRecursive())
val substitutedType = newType ?: type.substituteRecursive()
val substitutedAttributes = (substitutedType ?: type).attributes.substituteAbbreviationOrNull()
return if (substitutedType != null || substitutedAttributes != null) {
var result = substitutedType ?: type
if (substitutedAttributes != null) {
result = result.withAttributes(substitutedAttributes)
}
result
} else {
null
}
}
private fun ConeAttributes.substituteAbbreviationOrNull(): ConeAttributes? {
val abbreviatedTypeAttribute = abbreviatedType ?: return null
substituteOrNull(abbreviatedTypeAttribute.coneType)?.let {
return replace(abbreviatedTypeAttribute, AbbreviatedTypeAttribute(it))
}
return null
}
private fun ConeKotlinType.substituteRecursive(): ConeKotlinType? {