From 6ca7b39f6a4f34e3eda3d23259734c7d318acbe6 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 20 May 2021 22:24:36 +0200 Subject: [PATCH] Psi2ir: do not load abbreviated types with not found classifier This is not an ideal fix of the problem since the fact that AbbreviatedType's classifier should be a TypeAliasDescriptor is a reasonable assumption that might fail somewhere else, later in the pipeline. Previous attempts to fix this issue that were unsuccessful: 1) Do not load abbreviations for such types in deserialization at all. Unfortunately, it broke quite a few things like reflection and decompiler, where types frequently refer to symbols not reachable from the point where they're requested, yet we have the FQ name of the typealias, which is enough to render the abbreviation properly in both these use cases. 2) Load classifiers for unresolved abbreviations as MockTypeAliasDescriptor instead of MockClassDescriptor in NotFoundClasses. Technically this was a revert of e19c1b5364e1d049673b7288b1818b0ad5a0fb21. But this failed because we don't have enough information about such typealias to correctly set its `expandedType`/`underlyingType` (just using nullable Any as before that commit is not good enough). We only know its underlying class (from one usage of such typealias), and even supporting that would involve a major refactoring of TypeDeserializer which is painful. #KT-45308 Fixed --- .../src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt | 8 +++----- compiler/testData/codegen/box/typealias/kt45308.kt | 6 ++++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt index 3c790483f43..7e5ef1c46f6 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt @@ -164,11 +164,9 @@ abstract class TypeTranslator( return translateType(approximate(commonSupertype.replaceArgumentsWithStarProjections()), variance) } - private fun SimpleType.toIrTypeAbbreviation(): IrTypeAbbreviation { - val typeAliasDescriptor = constructor.declarationDescriptor.let { - it as? TypeAliasDescriptor - ?: throw AssertionError("TypeAliasDescriptor expected: $it") - } + private fun SimpleType.toIrTypeAbbreviation(): IrTypeAbbreviation? { + // Abbreviated type's classifier might not be TypeAliasDescriptor in case it's MockClassDescriptor (not found in dependencies). + val typeAliasDescriptor = constructor.declarationDescriptor as? TypeAliasDescriptor ?: return null return IrTypeAbbreviationImpl( symbolTable.referenceTypeAlias(typeAliasDescriptor), isMarkedNullable, diff --git a/compiler/testData/codegen/box/typealias/kt45308.kt b/compiler/testData/codegen/box/typealias/kt45308.kt index c4ba5d2921c..de507f30575 100644 --- a/compiler/testData/codegen/box/typealias/kt45308.kt +++ b/compiler/testData/codegen/box/typealias/kt45308.kt @@ -1,5 +1,7 @@ -// KT-45308 Psi2ir: "AssertionError: TypeAliasDescriptor expected" caused by using typealias from one module as a type in another module without a transitive dependency -// IGNORE_BACKEND: JVM_IR +// This test checks that unresolved typealias in an abbreviated type does not crash the compiler or result in a compilation error. +// Apparently, there's some demand for this behavior, see KT-45308. + +// FIR reports RETURN_TYPE_MISMATCH: Return type mismatch: expected kotlin/String, actual a/A // IGNORE_BACKEND_FIR: JVM_IR // MODULE: a