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

Bug 279715

Summary: [inline] Inline Constant and Inline Local Variable are missing parentheses for extended '-' chains
Product: [Eclipse Project] JDT Reporter: Markus Keller <markus.kell.r>
Component: UIAssignee: Markus Keller <markus.kell.r>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: daniel_megert, raksha.vasisht
Version: 3.5Flags: daniel_megert: review+
Target Milestone: 3.5.1   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Test
none
Fix none

Description Markus Keller CLA 2009-06-09 18:36:18 EDT
I20090605-1444

Inline Constant and Inline Local Variable are missing parentheses for extended '-' chains:

public class Try {
    public static final int B= 12;
    public static final int C= B - 1; //inline C
    public static final int K= 99;
    
    public static void main(String[] args) {
//        int B= 12;
//        int C= B - 1; //inline C
//        int K= 99;
        
        int f1= K - 1 - C;
        int f2= K - C - C - C;
        
        int x1= K + C;
        int x2= K - C;
        int x3= K + 1 - C;
        int x4= K - 1 + C;
        int x5= K + 1 + C - C - C;
    }
}
Comment 1 Markus Keller CLA 2009-06-09 18:49:52 EDT
The bug is in ASTNodes.substituteMustBeParenthesized(Expression, Expression), where this shortcut is invalid:

if (locationInParent instanceof ChildListPropertyDescriptor) {
    // e.g. argument lists of MethodInvocation, ClassInstanceCreation, ...
    return false;
} else

Fix is to replace the test by:

if (locationInParent instanceof ChildListPropertyDescriptor
        && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) {
Comment 2 Markus Keller CLA 2009-06-16 13:09:26 EDT
Created attachment 139321 [details]
Test
Comment 3 Markus Keller CLA 2009-06-16 13:13:17 EDT
Released to HEAD.

That's a nasty one, since the subtle error can be hard to spot (especially the off-by-1 case where I ran into this bug).

Dani, do you agree for 3.5.1?
Comment 4 Markus Keller CLA 2009-06-17 06:35:24 EDT
Created attachment 139411 [details]
Fix
Comment 5 Dani Megert CLA 2009-06-17 06:56:31 EDT
Patch is good and makes sense to fix in 3.5.1.
Comment 6 Dani Megert CLA 2009-06-25 08:55:22 EDT
Fixed in R3_5_maintenance branch.
Comment 7 Raksha Vasisht CLA 2009-08-04 08:37:13 EDT
Verified for 3.6 M1 using I20090803-1800

Filed bug 285565 to track a special case with tab size = 0. 
Comment 8 Markus Keller CLA 2009-08-04 14:08:29 EDT
Verified in M20090729-0903.