Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 350209

Summary: [compiler] Compiler produces unnecessary goto statement blocks for ternary operator
Product: [Eclipse Project] JDT Reporter: shalaku_dx
Component: CoreAssignee: Manoj N Palat <manoj.palat>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: srikanth_sankaran
Version: 3.7   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard: stalebug

Description shalaku_dx CLA 2011-06-23 23:26:51 EDT
Build Identifier: 20110609-2236

return (a < b) ? 1 : (a > b) -1 : 0;

  public final test2(int,int) : int
    ILOAD 1
    ILOAD 2
    IF_ICMPGE L0
    ICONST_1
    GOTO L1
   L0
   FRAME SAME
    ILOAD 1
    ILOAD 2
    IF_ICMPLE L2
    ICONST_M1
    GOTO L1
   L2
   FRAME SAME
    ICONST_0
   L1
   FRAME SAME1 int
    IRETURN
    MAXSTACK = 2
    MAXLOCALS = 3

should at least return the same bytecode as below (if not better, ternary operator is known to be faster in some languages/platforms)

if (a < b) return 1;
if (a > b) return -1;
return 0;

  public final test(int,int) : int
    ILOAD 1
    ILOAD 2
    IF_ICMPGE L0
    ICONST_1
    IRETURN
   L0
   FRAME SAME
    ILOAD 1
    ILOAD 2
    IF_ICMPLE L1
    ICONST_M1
    IRETURN
   L1
   FRAME SAME
    ICONST_0
    IRETURN
    MAXSTACK = 2
    MAXLOCALS = 3

Note how there is an extra L2 label and the goto's.

Reproducible: Always

Steps to Reproduce:
1. Write the Java code in an IF statement.
2. Write the Java code using the ternary operator.
3. Compare the bytecode.
Comment 1 Srikanth Sankaran CLA 2012-10-16 04:12:56 EDT
Manoj, Please take a look.
Comment 2 Eclipse Genie CLA 2020-01-31 12:12:58 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.