FIR IDE: make constant evaluator robust to arithmetic exception
This commit is contained in:
committed by
Ilya Kirillov
parent
e5bff514b6
commit
c3c79b874d
+6
-2
@@ -26,8 +26,12 @@ internal class KtFirCompileTimeConstantProvider(
|
||||
override fun evaluate(expression: KtExpression): KtConstantValue? = withValidityAssertion {
|
||||
when (val fir = expression.getOrBuildFir(firResolveState)) {
|
||||
is FirExpression -> {
|
||||
FirCompileTimeConstantEvaluator.evaluate(fir)?.let { KtFirConstantValueConverter.toConstantValue(it) }
|
||||
?: KtFirConstantValueConverter.toConstantValue(fir, firResolveState.rootModuleSession)
|
||||
try {
|
||||
FirCompileTimeConstantEvaluator.evaluate(fir)?.let { KtFirConstantValueConverter.toConstantValue(it) }
|
||||
?: KtFirConstantValueConverter.toConstantValue(fir, firResolveState.rootModuleSession)
|
||||
} catch (e: ArithmeticException) {
|
||||
KtErrorValue(e.localizedMessage)
|
||||
}
|
||||
}
|
||||
// For invalid code like the following,
|
||||
// ```
|
||||
|
||||
+3
-3
@@ -98,7 +98,7 @@ internal object FirCompileTimeConstantEvaluator {
|
||||
return evalUnaryOp(
|
||||
function.name.asString(),
|
||||
kind.toCompileTimeType(),
|
||||
value!!
|
||||
kind.convertToNumber(value as? Number)!!
|
||||
)?.let {
|
||||
it.toConstantValueKind()?.toConstExpression(source, it)
|
||||
}
|
||||
@@ -113,9 +113,9 @@ internal object FirCompileTimeConstantEvaluator {
|
||||
return evalBinaryOp(
|
||||
function.name.asString(),
|
||||
kind.toCompileTimeType(),
|
||||
value!!,
|
||||
kind.convertToNumber(value as? Number)!!,
|
||||
other.kind.toCompileTimeType(),
|
||||
other.value!!
|
||||
other.kind.convertToNumber(other.value as? Number)!!
|
||||
)?.let {
|
||||
it.toConstantValueKind()?.toConstExpression(source, it)
|
||||
}
|
||||
|
||||
+6
@@ -78,6 +78,12 @@ public class FirCompileTimeConstantEvaluatorTestGenerated extends AbstractFirCom
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantEvaluator/propertyInit_Byte.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertyInit_DivByZero.kt")
|
||||
public void testPropertyInit_DivByZero() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantEvaluator/propertyInit_DivByZero.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertyInit_Double.kt")
|
||||
public void testPropertyInit_Double() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user