Don't eliminate redundant casts to multi-dimension arrays

Dex doesn't recognize ANEWARRAY [Ljava/lang/Object; types as Object [][], but Object [].
It's not clear is it bug in dex or not

 #KT-17200 InProgress
This commit is contained in:
Mikhael Bogdanov
2017-04-04 18:01:25 +02:00
parent 86c4ad1165
commit f3e2abfd5f
@@ -19,10 +19,7 @@ package org.jetbrains.kotlin.codegen.optimization
import org.jetbrains.kotlin.codegen.inline.ReifiedTypeInliner
import org.jetbrains.kotlin.codegen.optimization.common.OptimizationBasicInterpreter
import org.jetbrains.kotlin.codegen.optimization.fixStack.top
import org.jetbrains.kotlin.codegen.optimization.nullCheck.popReferenceValueBefore
import org.jetbrains.kotlin.codegen.optimization.transformer.MethodTransformer
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.tree.*
@@ -44,6 +41,10 @@ class RedundantCheckCastEliminationMethodTransformer : MethodTransformer() {
val insnType = Type.getObjectType(insn.desc)
if (!isTrivialSubtype(insnType, valueType)) continue
//Keep casts to multiarray types cause dex doesn't recognize ANEWARRAY [Ljava/lang/Object; as Object [][], but Object [] type
//It's not clear is it bug in dex or not and maybe best to distinguish such types from MULTINEWARRRAY ones in method analyzer
if (isMultiArrayType(insnType)) continue
if (insn.opcode == Opcodes.CHECKCAST) {
redundantCheckCasts.add(insn)
}
@@ -57,4 +58,6 @@ class RedundantCheckCastEliminationMethodTransformer : MethodTransformer() {
private fun isTrivialSubtype(superType: Type, subType: Type) =
superType == subType
private fun isMultiArrayType(type: Type) = type.sort == Type.ARRAY && type.dimensions != 1
}