Skip to content

Commit

Permalink
[FIR] Prohibit Nothing as catch parameter type
Browse files Browse the repository at this point in the history
#KT-8322 Fixed
  • Loading branch information
cypressious authored and qodana-bot committed Sep 24, 2024
1 parent 283de17 commit 9748df7
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.jetbrains.kotlin.fir.analysis.checkers.expression

import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.diagnostics.reportOn
import org.jetbrains.kotlin.fir.analysis.checkers.MppCheckerKind
Expand All @@ -14,8 +15,10 @@ import org.jetbrains.kotlin.fir.analysis.checkers.isSubtypeOfThrowable
import org.jetbrains.kotlin.fir.analysis.checkers.valOrVarKeyword
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.fir.expressions.FirTryExpression
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.ConeTypeParameterType
import org.jetbrains.kotlin.fir.types.coneType
import org.jetbrains.kotlin.fir.types.isNothing
import org.jetbrains.kotlin.fir.types.isTypeMismatchDueToNullability
import org.jetbrains.kotlin.fir.types.typeContext

Expand Down Expand Up @@ -45,7 +48,7 @@ object FirCatchParameterChecker : FirTryExpressionChecker(MppCheckerKind.Common)
}

val session = context.session
if (!coneType.isSubtypeOfThrowable(session)) {
if (!coneType.isSubtypeOfThrowable(session) || isProhibitedNothing(context, coneType)) {
reporter.reportOn(
source,
FirErrors.THROWABLE_TYPE_MISMATCH,
Expand All @@ -59,4 +62,8 @@ object FirCatchParameterChecker : FirTryExpressionChecker(MppCheckerKind.Common)
}
}
}

private fun isProhibitedNothing(context: CheckerContext, coneType: ConeKotlinType): Boolean {
return context.languageVersionSettings.supportsFeature(LanguageFeature.ProhibitNothingAsCatchParameter) && coneType.isNothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fun foo() {
try {
throw Exception()
} catch (<!THROWABLE_TYPE_MISMATCH!>x: Nothing<!>) {
}

try {
throw Exception()
} catch (<!THROWABLE_TYPE_MISMATCH!>x: Nothing?<!>) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fun foo() {
try {
throw Exception()
} catch (x: Nothing) {
}

try {
throw Exception()
} catch (<!TYPE_MISMATCH!>x: Nothing?<!>) {
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ enum class LanguageFeature(
AllowSuperCallToJavaInterface(KOTLIN_2_1, kind = OTHER), // KT-69729
ProhibitJavaClassInheritingPrivateKotlinClass(KOTLIN_2_1, kind = BUG_FIX), // KT-66328
ProhibitReturningIncorrectNullabilityValuesFromSamConstructorLambdaOfJdkInterfaces(KOTLIN_2_1, kind = BUG_FIX), // KT-57014
ProhibitNothingAsCatchParameter(KOTLIN_2_1, kind = BUG_FIX), // KT-8322

// It's not a fully blown LF, but mostly a way to manage potential unexpected semantic changes
// See the single usage at org.jetbrains.kotlin.fir.types.ConeTypeApproximator.fastPathSkipApproximation
Expand Down

0 comments on commit 9748df7

Please sign in to comment.