AA: avoid NPE while evaluating operations with non-numbers

This commit is contained in:
Jinseong Jeon
2022-02-24 13:53:27 -08:00
committed by Ilya Kirillov
parent 6f64aedaf0
commit e36bf55807
5 changed files with 40 additions and 14 deletions
@@ -34,6 +34,12 @@ public class Fe10CompileTimeConstantEvaluatorTestGenerated extends AbstractCompi
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate"), Pattern.compile("^(.+)\\.kt$"), null, true); KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate"), Pattern.compile("^(.+)\\.kt$"), null, true);
} }
@Test
@TestMetadata("binaryExpressionWithString.kt")
public void testBinaryExpressionWithString() throws Exception {
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/binaryExpressionWithString.kt");
}
@Test @Test
@TestMetadata("propertyInit_Byte.kt") @TestMetadata("propertyInit_Byte.kt")
public void testPropertyInit_Byte() throws Exception { public void testPropertyInit_Byte() throws Exception {
@@ -115,12 +115,15 @@ internal object FirCompileTimeConstantEvaluator {
// Unary operators // Unary operators
private fun FirConstExpression<*>.evaluate(function: FirSimpleFunction): FirConstExpression<*>? { private fun FirConstExpression<*>.evaluate(function: FirSimpleFunction): FirConstExpression<*>? {
if (value == null) return null if (value == null) return null
return evalUnaryOp( // TODO: there are a couple operations on String, such as .length and .toString
function.name.asString(), return kind.convertToNumber(value as? Number)?.let { opr ->
kind.toCompileTimeType(), evalUnaryOp(
kind.convertToNumber(value as? Number)!! function.name.asString(),
)?.let { kind.toCompileTimeType(),
it.toConstantValueKind().toConstExpression(source, it) opr
)?.let {
it.toConstantValueKind().toConstExpression(source, it)
}
} }
} }
@@ -130,14 +133,19 @@ internal object FirCompileTimeConstantEvaluator {
other: FirConstExpression<*> other: FirConstExpression<*>
): FirConstExpression<*>? { ): FirConstExpression<*>? {
if (value == null || other.value == null) return null if (value == null || other.value == null) return null
return evalBinaryOp( // TODO: there are a couple operations on Strings, such as .compareTo, .equals, or .plus
function.name.asString(), return kind.convertToNumber(value as? Number)?.let { opr1 ->
kind.toCompileTimeType(), other.kind.convertToNumber(other.value as? Number)?.let { opr2 ->
kind.convertToNumber(value as? Number)!!, evalBinaryOp(
other.kind.toCompileTimeType(), function.name.asString(),
other.kind.convertToNumber(other.value as? Number)!! kind.toCompileTimeType(),
)?.let { opr1,
it.toConstantValueKind().toConstExpression(source, it) other.kind.toCompileTimeType(),
opr2
)?.let {
it.toConstantValueKind().toConstExpression(source, it)
}
}
} }
} }
@@ -34,6 +34,12 @@ public class FirCompileTimeConstantEvaluatorTestGenerated extends AbstractCompil
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate"), Pattern.compile("^(.+)\\.kt$"), null, true); KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate"), Pattern.compile("^(.+)\\.kt$"), null, true);
} }
@Test
@TestMetadata("binaryExpressionWithString.kt")
public void testBinaryExpressionWithString() throws Exception {
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/binaryExpressionWithString.kt");
}
@Test @Test
@TestMetadata("propertyInit_Byte.kt") @TestMetadata("propertyInit_Byte.kt")
public void testPropertyInit_Byte() throws Exception { public void testPropertyInit_Byte() throws Exception {
@@ -0,0 +1,3 @@
fun box(msg: String): String {
return <expr>"message" + msg</expr>
}
@@ -0,0 +1,3 @@
expression: "message" + msg
constant: NOT_EVALUATED
constantValueKind: NOT_EVALUATED