[NI] Refactor UnstableSmartCast diagnostic

Split diagnostic classes for unstable smartcast with different resolution applicability.

^KT-36264 Fixed
This commit is contained in:
Pavel Kirpichenkov
2020-02-13 13:13:55 +03:00
parent 15d744c3da
commit 5d6d287faa
2 changed files with 17 additions and 5 deletions
@@ -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)
@@ -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)
}