From 5d6d287faa9de833b80b6540628f1b3375b3b1df Mon Sep 17 00:00:00 2001 From: Pavel Kirpichenkov Date: Thu, 13 Feb 2020 13:13:55 +0300 Subject: [PATCH] [NI] Refactor UnstableSmartCast diagnostic Split diagnostic classes for unstable smartcast with different resolution applicability. ^KT-36264 Fixed --- .../DiagnosticReporterByTrackingStrategy.kt | 3 ++- .../calls/model/KotlinCallDiagnostics.kt | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt index 74c425eab43..be386182c64 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt @@ -121,7 +121,8 @@ class DiagnosticReporterByTrackingStrategy( override fun onCallArgument(callArgument: KotlinCallArgument, diagnostic: KotlinCallDiagnostic) { when (diagnostic.javaClass) { SmartCastDiagnostic::class.java -> reportSmartCast(diagnostic as SmartCastDiagnostic) - UnstableSmartCast::class.java -> reportUnstableSmartCast(diagnostic as UnstableSmartCast) + UnstableSmartCastDiagnosticError::class.java, + UnstableSmartCastResolutionError::class.java -> reportUnstableSmartCast(diagnostic as UnstableSmartCast) TooManyArguments::class.java -> { trace.reportTrailingLambdaErrorOr(callArgument.psiExpression) { expr -> TOO_MANY_ARGUMENTS.on(expr, (diagnostic as TooManyArguments).descriptor) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallDiagnostics.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallDiagnostics.kt index b23c9daec64..d458fa8dbcf 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallDiagnostics.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallDiagnostics.kt @@ -139,7 +139,7 @@ class SmartCastDiagnostic( override fun report(reporter: DiagnosticReporter) = reporter.onCallArgument(argument, this) } -class UnstableSmartCast private constructor( +sealed class UnstableSmartCast( val argument: ExpressionKotlinCallArgument, val targetType: UnwrappedType, applicability: ResolutionCandidateApplicability, @@ -150,14 +150,25 @@ class UnstableSmartCast private constructor( operator fun invoke( argument: ExpressionKotlinCallArgument, targetType: UnwrappedType, - isReceiver: Boolean = false, + isReceiver: Boolean = false, // for reproducing OI behaviour ): UnstableSmartCast { - val applicability = if (isReceiver) MAY_THROW_RUNTIME_ERROR else RESOLVED_WITH_ERROR - return UnstableSmartCast(argument, targetType, applicability) + return if (isReceiver) + UnstableSmartCastResolutionError(argument, targetType) + else UnstableSmartCastDiagnosticError(argument, targetType) } } } +class UnstableSmartCastResolutionError( + argument: ExpressionKotlinCallArgument, + targetType: UnwrappedType, +) : UnstableSmartCast(argument, targetType, MAY_THROW_RUNTIME_ERROR) + +class UnstableSmartCastDiagnosticError( + argument: ExpressionKotlinCallArgument, + targetType: UnwrappedType, +) : UnstableSmartCast(argument, targetType, RESOLVED_WITH_ERROR) + class UnsafeCallError(val receiver: SimpleKotlinCallArgument) : KotlinCallDiagnostic(MAY_THROW_RUNTIME_ERROR) { override fun report(reporter: DiagnosticReporter) = reporter.onCallReceiver(receiver, this) }