From e1d00c3d6e7f75a68d5fd3900e501e4338e0925a Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Fri, 10 Jun 2022 10:45:59 +0200 Subject: [PATCH] KAPT+IR: support nullability for error types #KT-49682 --- .../org/jetbrains/kotlin/ir/types/IrType.kt | 6 +- .../jetbrains/kotlin/ir/types/IrTypeUtils.kt | 1 + .../kotlin/ir/types/impl/IrErrorClassImpl.kt | 55 ++++++++++++++++++ .../kotlin/ir/types/impl/IrTypeBase.kt | 57 ++----------------- .../kotlin/ir/util/TypeTranslator.kt | 1 + 5 files changed, 66 insertions(+), 54 deletions(-) create mode 100644 compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrErrorClassImpl.kt diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrType.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrType.kt index 58496bdd2e5..8088a0f10dc 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrType.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrType.kt @@ -29,7 +29,11 @@ abstract class IrType : KotlinTypeMarker, IrAnnotationContainer { abstract override fun hashCode(): Int } -abstract class IrErrorType(kotlinType: KotlinType?, private val errorClassStubSymbol: IrClassSymbol) : IrTypeBase(kotlinType), SimpleTypeMarker { +abstract class IrErrorType( + kotlinType: KotlinType?, + private val errorClassStubSymbol: IrClassSymbol, + val isMarkedNullable: Boolean = false +) : IrTypeBase(kotlinType), SimpleTypeMarker { val symbol: IrClassSymbol get() = errorClassStubSymbol } diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeUtils.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeUtils.kt index e3b5c09835d..a41278a880e 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeUtils.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrTypeUtils.kt @@ -47,6 +47,7 @@ fun IrType.isNullable(): Boolean = else -> error("Unsupported classifier: $classifier") } is IrDynamicType -> true + is IrErrorType -> this.isMarkedNullable else -> false } diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrErrorClassImpl.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrErrorClassImpl.kt new file mode 100644 index 00000000000..7cc7d94911c --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrErrorClassImpl.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2022 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.ir.types.impl + +import org.jetbrains.kotlin.descriptors.ClassKind +import org.jetbrains.kotlin.descriptors.DescriptorVisibilities +import org.jetbrains.kotlin.descriptors.Modality +import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor +import org.jetbrains.kotlin.ir.IrFileEntry +import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI +import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET +import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl +import org.jetbrains.kotlin.ir.expressions.IrConstructorCall +import org.jetbrains.kotlin.ir.symbols.IrFileSymbol +import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name + +object IrErrorClassImpl : IrClassImpl( + UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.ERROR_CLASS, IrClassSymbolImpl(), + Name.special(""), ClassKind.CLASS, DescriptorVisibilities.DEFAULT_VISIBILITY, Modality.FINAL +) { + override var parent: IrDeclarationParent + get() = object : IrFile() { + override val startOffset: Int + get() = TODO("Not yet implemented") + override val endOffset: Int + get() = TODO("Not yet implemented") + override var annotations: List + get() = TODO("Not yet implemented") + set(_) {} + override val declarations: MutableList + get() = TODO("Not yet implemented") + override val symbol: IrFileSymbol + get() = TODO("Not yet implemented") + override val module: IrModuleFragment + get() = TODO("Not yet implemented") + override val fileEntry: IrFileEntry + get() = TODO("Not yet implemented") + override var metadata: MetadataSource? + get() = TODO("Not yet implemented") + set(_) {} + + @ObsoleteDescriptorBasedAPI + override val packageFragmentDescriptor: PackageFragmentDescriptor + get() = TODO("Not yet implemented") + override val fqName: FqName + get() = FqName.ROOT + } + set(_) = TODO() +} \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrTypeBase.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrTypeBase.kt index 5d17ba41fdf..725519b9073 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrTypeBase.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrTypeBase.kt @@ -5,26 +5,11 @@ package org.jetbrains.kotlin.ir.types.impl -import org.jetbrains.kotlin.descriptors.ClassKind -import org.jetbrains.kotlin.descriptors.DescriptorVisibilities -import org.jetbrains.kotlin.descriptors.Modality -import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor -import org.jetbrains.kotlin.ir.IrFileEntry -import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI -import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET -import org.jetbrains.kotlin.ir.declarations.* -import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl -import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl +import org.jetbrains.kotlin.ir.declarations.IrFunction +import org.jetbrains.kotlin.ir.declarations.IrTypeParameter import org.jetbrains.kotlin.ir.expressions.IrConstructorCall import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol -import org.jetbrains.kotlin.ir.symbols.IrFileSymbol -import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol -import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl -import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl import org.jetbrains.kotlin.ir.types.* -import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.model.CaptureStatus @@ -40,46 +25,13 @@ class IrErrorTypeImpl( kotlinType: KotlinType?, override val annotations: List, override val variance: Variance, -) : IrErrorType(kotlinType, IrErrorClassImpl.symbol) { + isMarkedNullable: Boolean = false +) : IrErrorType(kotlinType, IrErrorClassImpl.symbol, isMarkedNullable) { override fun equals(other: Any?): Boolean = other is IrErrorTypeImpl override fun hashCode(): Int = IrErrorTypeImpl::class.java.hashCode() } -object IrErrorClassImpl : IrClassImpl( - UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.ERROR_CLASS, IrClassSymbolImpl(), - Name.special(""), ClassKind.CLASS, DescriptorVisibilities.DEFAULT_VISIBILITY, Modality.FINAL -) { - override var parent: IrDeclarationParent - get() = object: IrFile() { - override val startOffset: Int - get() = TODO("Not yet implemented") - override val endOffset: Int - get() = TODO("Not yet implemented") - override var annotations: List - get() = TODO("Not yet implemented") - set(_) {} - override val declarations: MutableList - get() = TODO("Not yet implemented") - override val symbol: IrFileSymbol - get() = TODO("Not yet implemented") - override val module: IrModuleFragment - get() = TODO("Not yet implemented") - override val fileEntry: IrFileEntry - get() = TODO("Not yet implemented") - override var metadata: MetadataSource? - get() = TODO("Not yet implemented") - set(_) {} - - @ObsoleteDescriptorBasedAPI - override val packageFragmentDescriptor: PackageFragmentDescriptor - get() = TODO("Not yet implemented") - override val fqName: FqName - get() = FqName.ROOT - } - set(_) = TODO() -} - class IrDynamicTypeImpl( kotlinType: KotlinType?, override val annotations: List, @@ -90,7 +42,6 @@ class IrDynamicTypeImpl( override fun hashCode(): Int = IrDynamicTypeImpl::class.java.hashCode() } - val IrType.originalKotlinType: KotlinType? get() = safeAs()?.kotlinType 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 d011e80fc65..325c4c14f8e 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 @@ -99,6 +99,7 @@ abstract class TypeTranslator( approximatedType, translateTypeAnnotations(approximatedType), variance, + isMarkedNullable = approximatedType.isMarkedNullable, ) approximatedType.isDynamic() -> return IrDynamicTypeImpl(approximatedType, translateTypeAnnotations(approximatedType), variance)