FIR IDE: tolerate comma in when conditions

For invalid code like the following,
```
when {
  true, false -> {}
}
```
`false` does not have a corresponding elements on the FIR side and hence
the containing `FirWhenBranch` is returned by `getOrBuildFir`. This
change makes the analysis API bail out for such cases.
This commit is contained in:
Tianyu Geng
2021-10-13 18:10:58 -07:00
committed by Ilya Kirillov
parent 78ee6b2a09
commit 3877dc8cc3
2 changed files with 19 additions and 0 deletions
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.analysis.api.fir.evaluate.KtFirConstantValueConverte
import org.jetbrains.kotlin.analysis.api.symbols.markers.*
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.fir.expressions.FirWhenBranch
import org.jetbrains.kotlin.psi.KtExpression
internal class KtFirCompileTimeConstantProvider(
@@ -28,6 +29,15 @@ internal class KtFirCompileTimeConstantProvider(
FirCompileTimeConstantEvaluator.evaluate(fir)?.let { KtFirConstantValueConverter.toConstantValue(it) }
?: KtFirConstantValueConverter.toConstantValue(fir, firResolveState.rootModuleSession)
}
// For invalid code like the following,
// ```
// 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
else -> throwUnexpectedFirElementError(fir, expression)
}
}
@@ -43,6 +43,15 @@ internal class KtFirExpressionTypeProvider(
is FirNamedReference -> fir.getReferencedElementType(firResolveState).asKtType()
is FirStatement -> with(analysisSession) { builtinTypes.UNIT }
is FirTypeRef, is FirImport, is FirPackageDirective, is FirLabel -> null
// For invalid code like the following,
// ```
// 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
else -> error("Unexpected ${fir?.let { it::class }} for ${expression::class} with text `${expression.text}`")
}
}