diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java index cba71288aec..334066ede03 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java @@ -5075,6 +5075,12 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/FieldVsSyntheticAccessor.kt"); } + @Test + @TestMetadata("InnerClassOfThrowableOnObject.kt") + public void testInnerClassOfThrowableOnObject() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/InnerClassOfThrowableOnObject.kt"); + } + @Test @TestMetadata("IntersectionWithJavaString.kt") public void testIntersectionWithJavaString() throws Exception { diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/InnerClassOfThrowableOnObject.fir.txt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/InnerClassOfThrowableOnObject.fir.txt new file mode 100644 index 00000000000..396f1e2b2ad --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/InnerClassOfThrowableOnObject.fir.txt @@ -0,0 +1,58 @@ +FILE: InnerClassOfThrowableOnObject.kt + public final class Generic : R|kotlin/Any| { + public constructor(): R|Generic| { + super() + } + + public final companion object Companion : R|kotlin/Any| { + private constructor(): R|Generic.Companion| { + super() + } + + public final fun foo(): R|kotlin/Unit| { + lval x: R|| = object : R|kotlin/Exception| { + private constructor(): R|| { + super() + } + + } + + } + + } + + public final class Nested : R|kotlin/Any| { + public constructor(): R|Generic.Nested| { + super() + } + + public final fun foo(): R|kotlin/Unit| { + lval x: R|| = object : R|kotlin/Exception| { + private constructor(): R|| { + super() + } + + } + + } + + } + + public final inner class Inner : R|kotlin/Any| { + public Generic.constructor(): R|Generic.Inner| { + super() + } + + public final fun foo(): R|kotlin/Unit| { + lval x: R|| = object : R|kotlin/Exception| { + private constructor(): R|| { + super() + } + + } + + } + + } + + } diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/InnerClassOfThrowableOnObject.kt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/InnerClassOfThrowableOnObject.kt new file mode 100644 index 00000000000..26f4bb816c2 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/InnerClassOfThrowableOnObject.kt @@ -0,0 +1,19 @@ +// See KT-49129 + +class Generic { + companion object { + fun foo() { + val x = object : Exception() {} + } + } + class Nested { + fun foo() { + val x = object : Exception() {} + } + } + inner class Inner { + fun foo() { + val x = object : Exception() {} + } + } +} diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index 97f8806b884..111bd7ac5ad 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -5075,6 +5075,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/FieldVsSyntheticAccessor.kt"); } + @Test + @TestMetadata("InnerClassOfThrowableOnObject.kt") + public void testInnerClassOfThrowableOnObject() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/InnerClassOfThrowableOnObject.kt"); + } + @Test @TestMetadata("IntersectionWithJavaString.kt") public void testIntersectionWithJavaString() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index 71fffaf664b..4ce80734897 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -5075,6 +5075,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/FieldVsSyntheticAccessor.kt"); } + @Test + @TestMetadata("InnerClassOfThrowableOnObject.kt") + public void testInnerClassOfThrowableOnObject() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/InnerClassOfThrowableOnObject.kt"); + } + @Test @TestMetadata("IntersectionWithJavaString.kt") public void testIntersectionWithJavaString() throws Exception { diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirThrowableSubclassChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirThrowableSubclassChecker.kt index 9812af39a95..5f6c33a4344 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirThrowableSubclassChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirThrowableSubclassChecker.kt @@ -13,6 +13,7 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.reportOn import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.utils.classId import org.jetbrains.kotlin.fir.declarations.utils.isInner +import org.jetbrains.kotlin.fir.declarations.utils.isLocal import org.jetbrains.kotlin.fir.declarations.utils.superConeTypes import org.jetbrains.kotlin.fir.types.ConeClassErrorType @@ -38,9 +39,16 @@ object FirThrowableSubclassChecker : FirClassChecker() { private fun FirClass.hasThrowableSupertype(context: CheckerContext) = superConeTypes.any { it !is ConeClassErrorType && it.isSubtypeOfThrowable(context.session) } - private fun FirClass.hasGenericOuterDeclaration(context: CheckerContext) = - classId.isLocal && context.containingDeclarations.anyIsGeneric() - - private fun Collection.anyIsGeneric() = - any { it is FirTypeParameterRefsOwner && it.typeParameters.isNotEmpty() } + private fun FirClass.hasGenericOuterDeclaration(context: CheckerContext): Boolean { + if (!classId.isLocal) return false + for (containingDeclaration in context.containingDeclarations.asReversed()) { + if (containingDeclaration is FirTypeParameterRefsOwner && containingDeclaration.typeParameters.isNotEmpty()) { + return true + } + if (containingDeclaration is FirRegularClass && !containingDeclaration.isLocal && !containingDeclaration.isInner) { + return false + } + } + return false + } }