From 270e58cf4e9020088631695f69590ed87999ce40 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Wed, 9 Jul 2014 11:52:54 +0400 Subject: [PATCH] Do not remove boxing when possibly unsuccessful CHECKCAST is performed --- .../boxing/RedundantBoxingInterpreter.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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 {