[FIR] Fix invalid expression throwing exception when determining type

^KTIJ-25421 fixed

Merge-request: KT-MR-10317
Merged-by: Egor Kulikov <Egor.Kulikov@jetbrains.com>
This commit is contained in:
Egor Kulikov
2023-05-25 12:31:42 +00:00
committed by Space Team
parent 5e38c2d570
commit 9a77632f1c
5 changed files with 33 additions and 8 deletions
@@ -74,19 +74,24 @@ internal class KtFirExpressionTypeProvider(
is FirNamedReference -> fir.getReferencedElementType().asKtType()
is FirStatement -> with(analysisSession) { builtinTypes.UNIT }
is FirTypeRef, is FirImport, is FirPackageDirective, is FirLabel, is FirTypeParameterRef -> null
// For invalid code like the following,
// `listOf<_>(1)` where `expression` is `_`
is FirPlaceholderProjection -> null
// There are various cases where we have no corresponding fir due to invalid code
// Some examples:
// ```
// when {
// true, false -> {}
// }
// ```
// `false` does not have a corresponding elements on the FIR side and hence the containing `FirWhenBranch` is returned. In this
// case, we simply report null since FIR does not know about it.
is FirWhenBranch -> null
// `listOf<_>(1)` where `expression` is `_`
is FirPlaceholderProjection -> null
else -> error("Unexpected ${fir?.let { it::class }} for ${expression::class} with text `${expression.text}`")
// `false` does not have a corresponding elements on the FIR side and hence the containing `FirWhenBranch` is returned.
// ```
// @Volatile
// private var
// ```
// Volatile does not have corresponding element, so `FirFileImpl` is returned
else -> null
}
}
@@ -0,0 +1,2 @@
@<expr>Volatile</expr>
private var
@@ -0,0 +1,6 @@
KT element: KtConstructorCalleeExpression
FIR element: FirAnnotationCallImpl
FIR source kind: KtRealSourceElementKind
FIR element rendered:
@<ERROR TYPE REF: Symbol not found for Volatile>[Types]()
@@ -805,6 +805,12 @@ public class OutOfContentRootGetOrBuildFirTestGenerated extends AbstractOutOfCon
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/invalidCode/delegatedProperty.kt");
}
@Test
@TestMetadata("incompletePropertyWithAnnotation.kt")
public void testIncompletePropertyWithAnnotation() throws Exception {
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/invalidCode/incompletePropertyWithAnnotation.kt");
}
@Test
@TestMetadata("javaClassLiteral.kt")
public void testJavaClassLiteral() throws Exception {
@@ -805,6 +805,12 @@ public class SourceGetOrBuildFirTestGenerated extends AbstractSourceGetOrBuildFi
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/invalidCode/delegatedProperty.kt");
}
@Test
@TestMetadata("incompletePropertyWithAnnotation.kt")
public void testIncompletePropertyWithAnnotation() throws Exception {
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/invalidCode/incompletePropertyWithAnnotation.kt");
}
@Test
@TestMetadata("javaClassLiteral.kt")
public void testJavaClassLiteral() throws Exception {