Fix NPE into isArraySizeAccess

This commit is contained in:
Mikaël Peltier
2018-02-13 14:06:01 +01:00
committed by Dmitry Petrov
parent 3c2f137fd5
commit ef6b6cd261
2 changed files with 14 additions and 1 deletions
@@ -113,7 +113,8 @@ private fun ExpressionCodegen.isArraySizeAccess(expression: KtExpression): Boole
return when { return when {
expression is KtDotQualifiedExpression -> { expression is KtDotQualifiedExpression -> {
val selector = expression.selectorExpression val selector = expression.selectorExpression
asmType(bindingContext.getType(expression.receiverExpression)!!).sort == Type.ARRAY && val type = bindingContext.getType(expression.receiverExpression) ?: return false
asmType(type).sort == Type.ARRAY &&
selector is KtNameReferenceExpression && selector is KtNameReferenceExpression &&
selector.text == "size" selector.text == "size"
} }
@@ -124,6 +124,17 @@ fun checkReversedIntArray(): Boolean {
return true return true
} }
fun checkIntArrayMethodCallBound(): Boolean {
val intArray = intArrayOf(1, 2, 3)
var start = 0
var sum = 0
for (i in 0..Math.min(intArray.size, 10) - 1) {
sum += intArray[i]
}
if (sum != 6) return false
return true
}
fun box(): String { fun box(): String {
// Check that the specialization of 'for (i in 0..array.size-1)' to 'for (i in 0 until array.size)' does not fail on // Check that the specialization of 'for (i in 0..array.size-1)' to 'for (i in 0 until array.size)' does not fail on
// any kind of arrays. // any kind of arrays.
@@ -139,6 +150,7 @@ fun box(): String {
if (!checkWithArrayUpdate()) return "Failure" if (!checkWithArrayUpdate()) return "Failure"
if (!checkIntArrayMinusArbitraryConstant()) return "Failure" if (!checkIntArrayMinusArbitraryConstant()) return "Failure"
if (!checkReversedIntArray()) return "Failure" if (!checkReversedIntArray()) return "Failure"
if (!checkIntArrayMethodCallBound()) return "Failure"
return "OK" return "OK"
} }