[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? {
@@ -0,0 +1,19 @@
Module: a
FILE: a.kt
public final typealias Foo<T> = R|kotlin/collections/List<kotlin/String>|
public final class C<T> : R|kotlin/Any| {
public constructor<T>(): R|C<T>| {
super<R|kotlin/Any|>()
}
public final val foo: R|Foo<T>?| = Null(null)
public get(): R|Foo<T>?|
}
Module: b
FILE: b.kt
public final val bar: R|Foo<kotlin/Int>?| = R|/C.C|<R|kotlin/Int|>().R|SubstitutionOverride</C.foo: R|Foo<kotlin/Int>?|>|
public get(): R|Foo<kotlin/Int>?|
public final fun box(): R|kotlin/String| {
^box String(OK)
}
@@ -0,0 +1,15 @@
// FIR_DUMP
// MODULE: a
// FILE: a.kt
typealias Foo<T> = List<String>
class C<T> {
val foo: Foo<T>? = null
}
// MODULE: b(a)
// FILE: b.kt
val bar = C<Int>().foo
fun box() = "OK"
@@ -6,4 +6,3 @@ public final class Context : R|kotlin/Any| {
}
public final typealias SuspendWithContext = R|suspend test/Context.() -> kotlin/Unit|
@@ -0,0 +1,9 @@
public final fun foo(f: R|@ExtensionFunctionType {@ExtensionFunctionType kotlin/coroutines/SuspendFunction1<test/Context, kotlin/Unit>=} suspend test/Context.() -> kotlin/Unit|): R|kotlin/Unit|
public final class Context : R|kotlin/Any| {
public constructor(): R|test/Context|
}
public final typealias SuspendWithContext = R|suspend test/Context.() -> kotlin/Unit|
@@ -1,23 +0,0 @@
public final val x1: R|kotlin/String|
public get(): R|kotlin/String|
public final val x2: R|kotlin/String|
public get(): R|kotlin/String|
public final val x3: R|kotlin/String|
public get(): R|kotlin/String|
public final val x4: R|kotlin/String?|
public get(): R|kotlin/String?|
public final val x5: R|kotlin/String?|
public get(): R|kotlin/String?|
public final val x6: R|kotlin/String?|
public get(): R|kotlin/String?|
public final typealias S = R|kotlin/String|
public final typealias SS = R|test/S|
public final typealias SSS = R|test/SS|
@@ -1,23 +0,0 @@
public final val x1: R|kotlin/String|
public get(): R|kotlin/String|
public final val x2: R|kotlin/String|
public get(): R|kotlin/String|
public final val x3: R|kotlin/String|
public get(): R|kotlin/String|
public final val x4: R|kotlin/String?|
public get(): R|kotlin/String?|
public final val x5: R|kotlin/String?|
public get(): R|kotlin/String?|
public final val x6: R|kotlin/String?|
public get(): R|kotlin/String?|
public final typealias S = R|kotlin/String|
public final typealias SS = R|kotlin/String|
public final typealias SSS = R|kotlin/String|
@@ -0,0 +1,23 @@
public final val x1: R|test/S|
public get(): R|test/S|
public final val x2: R|test/SS|
public get(): R|test/SS|
public final val x3: R|test/SSS|
public get(): R|test/SSS|
public final val x4: R|test/S?|
public get(): R|test/S?|
public final val x5: R|test/SS?|
public get(): R|test/SS?|
public final val x6: R|test/SSS?|
public get(): R|test/SSS?|
public final typealias S = R|kotlin/String|
public final typealias SS = R|test/S|
public final typealias SSS = R|test/SS|
@@ -1,17 +0,0 @@
public final fun test1(x: R|kotlin/collections/List<kotlin/String>|): R|kotlin/Unit|
public final fun test2(x: R|kotlin/collections/List<kotlin/String>|): R|kotlin/Unit|
public final fun test3(x: R|kotlin/collections/List<kotlin/String>|): R|kotlin/Unit|
public final fun test4(x: R|kotlin/collections/List<kotlin/collections/List<kotlin/String>>|): R|kotlin/Unit|
public final fun test5(x: R|kotlin/collections/List<kotlin/collections/List<kotlin/String>>|): R|kotlin/Unit|
public final fun test6(x: R|kotlin/collections/List<kotlin/collections/List<kotlin/String>>|): R|kotlin/Unit|
public final typealias L<T> = R|kotlin/collections/List<T>|
public final typealias LL<T> = R|test/L<T>|
public final typealias LLL<T> = R|test/LL<T>|
@@ -1,17 +0,0 @@
public final fun test1(x: R|kotlin/collections/List<kotlin/String>|): R|kotlin/Unit|
public final fun test2(x: R|kotlin/collections/List<kotlin/String>|): R|kotlin/Unit|
public final fun test3(x: R|kotlin/collections/List<kotlin/String>|): R|kotlin/Unit|
public final fun test4(x: R|kotlin/collections/List<kotlin/collections/List<kotlin/String>>|): R|kotlin/Unit|
public final fun test5(x: R|kotlin/collections/List<kotlin/collections/List<kotlin/String>>|): R|kotlin/Unit|
public final fun test6(x: R|kotlin/collections/List<kotlin/collections/List<kotlin/String>>|): R|kotlin/Unit|
public final typealias L<T> = R|kotlin/collections/List<T>|
public final typealias LL<T> = R|kotlin/collections/List<T>|
public final typealias LLL<T> = R|kotlin/collections/List<T>|
@@ -0,0 +1,17 @@
public final fun test1(x: R|test/L<kotlin/String>|): R|kotlin/Unit|
public final fun test2(x: R|test/LL<kotlin/String>|): R|kotlin/Unit|
public final fun test3(x: R|test/LLL<kotlin/String>|): R|kotlin/Unit|
public final fun test4(x: R|test/L<test/L<kotlin/String>>|): R|kotlin/Unit|
public final fun test5(x: R|test/LL<test/LL<kotlin/String>>|): R|kotlin/Unit|
public final fun test6(x: R|test/LLL<test/LLL<kotlin/String>>|): R|kotlin/Unit|
public final typealias L<T> = R|kotlin/collections/List<T>|
public final typealias LL<T> = R|test/L<T>|
public final typealias LLL<T> = R|test/LL<T>|
@@ -49617,6 +49617,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, 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 IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
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 IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
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 {
@@ -42445,6 +42445,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@TestMetadata("deserializedAbbreviationWithRedundantArgument.kt")
public void testDeserializedAbbreviationWithRedundantArgument() throws Exception {
runTest("compiler/testData/codegen/box/typealias/deserializedAbbreviationWithRedundantArgument.kt");
}
@TestMetadata("enumEntryQualifier.kt")
public void testEnumEntryQualifier() throws Exception {
runTest("compiler/testData/codegen/box/typealias/enumEntryQualifier.kt");
@@ -36465,6 +36465,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_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 {
@@ -36465,6 +36465,12 @@ public class FirJsES6CodegenBoxTestGenerated extends AbstractFirJsES6CodegenBoxT
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, 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 {
@@ -36465,6 +36465,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_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 {
@@ -36465,6 +36465,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, 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 {
@@ -39923,6 +39923,12 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, 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 {
@@ -40957,6 +40957,12 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, 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 {
@@ -39407,6 +39407,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, 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 {
@@ -39924,6 +39924,12 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, 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 {
@@ -36135,6 +36135,12 @@ public class FirWasmCodegenBoxTestGenerated extends AbstractFirWasmCodegenBoxTes
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, 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 {
@@ -36135,6 +36135,12 @@ public class K1WasmCodegenBoxTestGenerated extends AbstractK1WasmCodegenBoxTest
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, 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 {