diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/optimization/boxing/RedundantBoxingInterpreter.java b/compiler/backend/src/org/jetbrains/jet/codegen/optimization/boxing/RedundantBoxingInterpreter.java index 8541e071487..aa2e51573c0 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/optimization/boxing/RedundantBoxingInterpreter.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/optimization/boxing/RedundantBoxingInterpreter.java @@ -100,11 +100,30 @@ class RedundantBoxingInterpreter extends BoxingInterpreter { @NotNull AbstractInsnNode insn, @NotNull BasicValue value ) throws AnalyzerException { + if (insn.getOpcode() == Opcodes.CHECKCAST && value instanceof BoxedBasicValue) { + TypeInsnNode typeInsn = (TypeInsnNode) insn; + + if (!isSafeCast((BoxedBasicValue) value, typeInsn.desc)) { + markValueAsDirty((BoxedBasicValue) value); + } + } + processOperationWithBoxedValue(value, insn); return super.unaryOperation(insn, value); } + private static boolean isSafeCast(@NotNull BoxedBasicValue value, @NotNull String descTo) { + try { + return Class.forName(Type.getObjectType(descTo).getClassName()).isAssignableFrom( + Class.forName(value.getType().getClassName()) + ); + } + catch (ClassNotFoundException e) { + return false; + } + } + @Override @NotNull public BasicValue copyOperation(@NotNull AbstractInsnNode insn, @NotNull BasicValue value) throws AnalyzerException {