From 3e2c662b6bfac8c4dcd3c0f2bf3d1bec5c9d614a Mon Sep 17 00:00:00 2001 From: Vladimir Sukharev Date: Fri, 1 Sep 2023 18:38:11 +0200 Subject: [PATCH] [FIR] Add testcases to throw class with unresolved parent. ^KT-60048 Fixed --- .../native/checkers/FirNativeThrowsChecker.kt | 11 ++++++++--- .../analysis/checkers/ConeTypeCompatibilityChecker.kt | 2 +- .../testData/diagnostics/nativeTests/throws.fir.kt | 7 +++++++ compiler/testData/diagnostics/nativeTests/throws.kt | 7 +++++++ compiler/testData/diagnostics/nativeTests/throws.txt | 9 +++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeThrowsChecker.kt b/compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeThrowsChecker.kt index 8ca8c636ba9..2aa2c3f8e51 100644 --- a/compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeThrowsChecker.kt +++ b/compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeThrowsChecker.kt @@ -9,7 +9,6 @@ import org.jetbrains.kotlin.builtins.StandardNames import org.jetbrains.kotlin.diagnostics.DiagnosticReporter import org.jetbrains.kotlin.diagnostics.reportOn import org.jetbrains.kotlin.fir.* -import org.jetbrains.kotlin.fir.analysis.checkers.ConeTypeCompatibilityChecker.getSuperTypes import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirBasicDeclarationChecker import org.jetbrains.kotlin.fir.analysis.checkers.hasModifier @@ -22,6 +21,7 @@ import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.resolve.toFirRegularClassSymbol import org.jetbrains.kotlin.fir.scopes.getDirectOverriddenFunctions import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirTypeAliasSymbol import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.name.ClassId @@ -160,8 +160,13 @@ object FirNativeThrowsChecker : FirBasicDeclarationChecker() { } } - if (this is FirResolvedQualifier && symbol?.getSuperTypes()?.any { it.hasError() } == true) { - return true + if (this is FirResolvedQualifier) { + symbol?.let { symbol -> + if (symbol is FirTypeAliasSymbol && symbol.resolvedExpandedTypeRef.coneTypeSafe()?.hasError() == true) { + return true + } + // TODO: accept also FirClassSymbol<*>, like `FirClassLikeSymbol<*>.getSuperTypes()` does. Write test for this use-case. + } } return false } diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/ConeTypeCompatibilityChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/ConeTypeCompatibilityChecker.kt index 932fad76f41..1a5b99090c9 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/ConeTypeCompatibilityChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/ConeTypeCompatibilityChecker.kt @@ -355,7 +355,7 @@ object ConeTypeCompatibilityChecker { } } - fun FirClassLikeSymbol<*>.getSuperTypes(): List { + private fun FirClassLikeSymbol<*>.getSuperTypes(): List { return when (this) { is FirTypeAliasSymbol -> listOfNotNull(resolvedExpandedTypeRef.coneTypeSafe()) is FirClassSymbol<*> -> resolvedSuperTypeRefs.mapNotNull { it.coneTypeSafe() } diff --git a/compiler/testData/diagnostics/nativeTests/throws.fir.kt b/compiler/testData/diagnostics/nativeTests/throws.fir.kt index 383247389c3..224e4e875f3 100644 --- a/compiler/testData/diagnostics/nativeTests/throws.fir.kt +++ b/compiler/testData/diagnostics/nativeTests/throws.fir.kt @@ -40,6 +40,10 @@ fun throwsEmptyParens() {} @Throws(UnresolvedException::class) fun throwsUnresolved() {} +class Orphan : MyUnresolvedParent +@Throws(Orphan::class) +fun throwsClassWithUnresolvedParent() {} + @Throws(exceptionClasses = UnresolvedException::class) fun throwsNamedUnresolved() {} @@ -312,6 +316,9 @@ suspend fun suspendThrowsSpreadArrayOfUnresolved() {} @Throws(UEAlias::class) suspend fun suspendThrowsTypealiasToUnresolved() {} +@Throws(Orphan::class) +suspend fun suspendThrowsClassWithUnresolvedParent() {} + @Throws(Exception1::class, CancellationException::class) suspend fun suspendThrowsCancellationException1() {} diff --git a/compiler/testData/diagnostics/nativeTests/throws.kt b/compiler/testData/diagnostics/nativeTests/throws.kt index 0d7aa1bda12..c0ca419b599 100644 --- a/compiler/testData/diagnostics/nativeTests/throws.kt +++ b/compiler/testData/diagnostics/nativeTests/throws.kt @@ -40,6 +40,10 @@ fun throwsEmptyParens() {} @Throws(UnresolvedException::class) fun throwsUnresolved() {} +class Orphan : MyUnresolvedParent +@Throws(Orphan::class) +fun throwsClassWithUnresolvedParent() {} + @Throws(exceptionClasses = UnresolvedException::class) fun throwsNamedUnresolved() {} @@ -312,6 +316,9 @@ suspend fun suspendThrowsSpreadArrayOfUnresolved() {} @Throws(UEAlias::class) suspend fun suspendThrowsTypealiasToUnresolved() {} +@Throws(Orphan::class) +suspend fun suspendThrowsClassWithUnresolvedParent() {} + @Throws(Exception1::class, CancellationException::class) suspend fun suspendThrowsCancellationException1() {} diff --git a/compiler/testData/diagnostics/nativeTests/throws.txt b/compiler/testData/diagnostics/nativeTests/throws.txt index 41b0bf13e26..7657709847e 100644 --- a/compiler/testData/diagnostics/nativeTests/throws.txt +++ b/compiler/testData/diagnostics/nativeTests/throws.txt @@ -6,6 +6,7 @@ package @kotlin.Throws(exceptionClasses = {Exception1::class, kotlin.coroutines.cancellation.CancellationException::class}) public suspend fun suspendThrowsCancellationException1(): kotlin.Unit @kotlin.Throws(exceptionClasses = {kotlin.coroutines.cancellation.CancellationException::class, Exception1::class}) public suspend fun suspendThrowsCancellationException2(): kotlin.Unit @kotlin.Throws(exceptionClasses = {kotlin.coroutines.cancellation.CancellationException::class}) public suspend fun suspendThrowsCancellationExceptionTypealias(): kotlin.Unit +@kotlin.Throws(exceptionClasses = {Orphan::class}) public suspend fun suspendThrowsClassWithUnresolvedParent(): kotlin.Unit @kotlin.Throws(exceptionClasses = {kotlin.Exception::class}) public suspend fun suspendThrowsException1(): kotlin.Unit @kotlin.Throws(exceptionClasses = {Exception1::class, kotlin.Exception::class}) public suspend fun suspendThrowsException2(): kotlin.Unit @kotlin.Throws(exceptionClasses = {kotlin.Exception::class}) public suspend fun suspendThrowsExceptionTypealias(): kotlin.Unit @@ -31,6 +32,7 @@ package @kotlin.Throws(exceptionClasses = {kotlin.Throwable::class}) public suspend fun suspendThrowsThrowableTypealias(): kotlin.Unit @kotlin.Throws(exceptionClasses = {}) public suspend fun suspendThrowsTypealiasToUnresolved(): kotlin.Unit @kotlin.Throws(exceptionClasses = {}) public suspend fun suspendThrowsUnresolved(): kotlin.Unit +@kotlin.Throws(exceptionClasses = {Orphan::class}) public fun throwsClassWithUnresolvedParent(): kotlin.Unit @kotlin.Throws(exceptionClasses = {}) public fun throwsEmptyParens(): kotlin.Unit @kotlin.Throws(exceptionClasses = {}) public fun throwsNamedArrayOfUnresolved(): kotlin.Unit @kotlin.Throws(exceptionClasses = {}) public fun throwsNamedEmptyArrayOf(): kotlin.Unit @@ -245,6 +247,13 @@ public final class InheritsThrowsAndNoThrows : Base0, Base1 { public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String } +public final class Orphan { + public constructor Orphan() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + public final class OverrideDifferentThrows4 : OverridesDifferentThrows3 { public constructor OverrideDifferentThrows4() public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean