From 699ad87be2f0ee4c95d7f2faa6e9563d73ab2c97 Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Fri, 14 Apr 2023 08:31:24 +0200 Subject: [PATCH] [KAPT+IR] Use ErrorType for unresolved delegate class. ^KT-57946 --- .../psi2ir/generators/ClassGenerator.kt | 15 +++++++-- .../unresolvedDelegateExpression.ir.txt | 31 +++++++++++++++++++ .../converter/unresolvedDelegateExpression.kt | 8 +++++ .../unresolvedDelegateExpression.txt | 31 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ClassGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ClassGenerator.kt index 67f0e2360c0..5741a012f21 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ClassGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ClassGenerator.kt @@ -53,6 +53,8 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.TypeProjectionImpl import org.jetbrains.kotlin.types.TypeSubstitutor +import org.jetbrains.kotlin.types.error.ErrorTypeKind +import org.jetbrains.kotlin.types.error.ErrorUtils.createErrorType import org.jetbrains.kotlin.utils.newHashMapWithExpectedSize @ObsoleteDescriptorBasedAPI @@ -199,7 +201,11 @@ internal class ClassGenerator( delegateNumber: Int ) { val ktDelegateExpression = ktEntry.delegateExpression!! - val delegateType = getTypeInferredByFrontendOrFail(ktDelegateExpression) + val delegateType = if (context.configuration.generateBodies) { + getTypeInferredByFrontendOrFail(ktDelegateExpression) + } else { + getTypeInferredByFrontend(ktDelegateExpression) ?: createErrorType(ErrorTypeKind.UNRESOLVED_TYPE, ktDelegateExpression.text) + } val superType = getOrFail(BindingContext.TYPE, ktEntry.typeReference!!) val superTypeConstructorDescriptor = superType.constructor.declarationDescriptor @@ -212,11 +218,16 @@ internal class ClassGenerator( irClass.properties.first { it.descriptor == propertyDescriptor }.backingField!! } else { val delegateDescriptor = IrImplementingDelegateDescriptorImpl(irClass.descriptor, delegateType, superType, delegateNumber) + val initializer = if (context.configuration.generateBodies) { + createBodyGenerator(irClass.symbol).generateExpressionBody(ktDelegateExpression) + } else { + null + } context.symbolTable.declareField( ktDelegateExpression.startOffsetSkippingComments, ktDelegateExpression.endOffset, IrDeclarationOrigin.DELEGATE, delegateDescriptor, delegateDescriptor.type.toIrType(), - createBodyGenerator(irClass.symbol).generateExpressionBody(ktDelegateExpression) + initializer ).apply { irClass.addMember(this) } diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/unresolvedDelegateExpression.ir.txt b/plugins/kapt3/kapt3-compiler/testData/converter/unresolvedDelegateExpression.ir.txt index c989bd4b86f..040f330c39d 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/unresolvedDelegateExpression.ir.txt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/unresolvedDelegateExpression.ir.txt @@ -33,6 +33,37 @@ public class C { //////////////////// +@kotlin.Metadata() +@kotlin.Suppress(names = {"UNRESOLVED_REFERENCE"}) +public final class D implements I { + + public D() { + super(); + } +} + +//////////////////// + + +@kotlin.Metadata() +@kotlin.Suppress(names = {"UNRESOLVED_REFERENCE", "DELEGATION_NOT_TO_INTERFACE"}) +public final class E implements NonExisting { + + public E() { + super(); + } +} + +//////////////////// + + +@kotlin.Metadata() +public abstract interface I { +} + +//////////////////// + + @kotlin.Metadata() public final class UnresolvedDelegateExpressionKt { diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/unresolvedDelegateExpression.kt b/plugins/kapt3/kapt3-compiler/testData/converter/unresolvedDelegateExpression.kt index 7c5c71bc822..8abae3854dd 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/unresolvedDelegateExpression.kt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/unresolvedDelegateExpression.kt @@ -9,4 +9,12 @@ class B : NonExisting { val a: String by flaf() } +interface I + +@Suppress("UNRESOLVED_REFERENCE") +class D : I by NonExisting + fun C.flaf() = "OK" + +@Suppress("UNRESOLVED_REFERENCE", "DELEGATION_NOT_TO_INTERFACE") +class E : NonExisting by NonExisting diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/unresolvedDelegateExpression.txt b/plugins/kapt3/kapt3-compiler/testData/converter/unresolvedDelegateExpression.txt index 8c92724f941..66b806c1e98 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/unresolvedDelegateExpression.txt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/unresolvedDelegateExpression.txt @@ -33,6 +33,37 @@ public class C { //////////////////// +@kotlin.Metadata() +@kotlin.Suppress(names = {"UNRESOLVED_REFERENCE"}) +public final class D implements I { + + public D() { + super(); + } +} + +//////////////////// + + +@kotlin.Metadata() +@kotlin.Suppress(names = {"UNRESOLVED_REFERENCE", "DELEGATION_NOT_TO_INTERFACE"}) +public final class E implements NonExisting { + + public E() { + super(); + } +} + +//////////////////// + + +@kotlin.Metadata() +public abstract interface I { +} + +//////////////////// + + @kotlin.Metadata() public final class UnresolvedDelegateExpressionKt {