Bug 14974

Summary: Bad generated code for '+=' and '-=' operators
Product: [Eclipse Project] JDT Reporter: Luc Bourlier <eclipse>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 2.0   
Target Milestone: 2.0 M6   
Hardware: PC   
OS: All   
Whiteboard:

Description Luc Bourlier CLA 2002-04-30 18:26:56 EDT
Build 20020425++

The following code compiled with javac and run with java return '0'.
Compiled with the JDT compiler and run will java (in eclipse) return '-1'.

public class Foo2 {
  public static void main(String[] args) {
    int x = -8;
    x += 7.8f;
    System.out.println(x);
  }
}

The problem is when you generate the bytecode, you try to optimise the code by
used the iinc opcode if possible. But you do that even if the right operand is a
floating-point value, that can generate some probleme if we work on negative
value (as in the example).

I fix the probleme in my target by replacing the line 393 of
SingleNameReference.generateCompoundAssignment(BlockScope, CodeStream,
MethodBinding, Expression, int, int, boolean) with :

if (((assignConstant = expression.constant) != NotAConstant) &&
    /* new code */
    ((incrementTypeId = assignConstant.typeID()) != T_float) &&
    (incrementTypeId != T_double) &&
    /* */
   ((increment = assignConstant.intValue()) == (short) increment)) {
Comment 1 Philipe Mulet CLA 2002-05-01 07:55:16 EDT
Indeed, we should not optimize this case.
Good find.

Fixed