Bug 110657

Summary: [DOM] wrong position for single variable declaration inside enhanced for statement
Product: [Eclipse Project] JDT Reporter: Olivier Thomann <Olivier_Thomann>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 3.2   
Target Milestone: 3.2 M3   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Olivier Thomann CLA 2005-09-26 14:00:08 EDT
Following bug 104492, the position for byte[] bs in the enhanced for statement
are bogus.

public class X {
    public static void main(String[] args) {
        byte[] b1 = new byte[0];
        byte[] b2 = new byte[0];
        for (byte[] bs : new byte[][] { b1, b2 }) {

        }
    }
}
Comment 1 Olivier Thomann CLA 2005-09-26 14:36:04 EDT
The bug comes from the wrong position for the compiler's array type reference.
In the case of the enhanced for statement, if the expression after the colon is
an array initializer, the endPosition in the parser is not the end of the array
type reference anymore.
I might be able to fix this in the AST converter, but the positions would remain
boggus for the compiler's node. So if we report errors against the type
reference, the highlighting might be boggus.
Comment 2 Olivier Thomann CLA 2005-09-29 13:18:42 EDT
This can be addressed with a grammar change.
Changing the rules to consume EnhancedForStatementHeader  to:

EnhancedForStatementHeaderInit ::= 'for' '(' Type PushModifiers Identifier Dimsopt
/.$putCase consumeEnhancedForStatementHeaderInit(false); $break ./
/:$readableName EnhancedForStatementHeaderInit:/

EnhancedForStatementHeaderInit ::= 'for' '(' Modifiers Type PushRealModifiers
Identifier Dimsopt
/.$putCase consumeEnhancedForStatementHeaderInit(true); $break ./
/:$readableName EnhancedForStatementHeaderInit:/

EnhancedForStatementHeader ::= EnhancedForStatementHeaderInit ':' Expression ')'
/.$putCase consumeEnhancedForStatementHeader(); $break ./
/:$readableName EnhancedForStatementHeader:/
/:$compliance 1.5:/

Doing this when EnhancedForStatementHeaderInit is reduced, the this.endPosition
has the right value.
Comment 3 Olivier Thomann CLA 2005-09-29 14:16:14 EDT
Fixed and released in HEAD.
Regression tests in org.eclipse.jdt.core.tests.dom.ASTConverter15Test.test0199/0201.
Comment 4 Olivier Thomann CLA 2005-09-29 14:23:28 EDT
Added new regression test in Regression tests in
org.eclipse.jdt.core.tests.dom.ASTConverter15Test.test0202
Comment 5 Frederic Fusier CLA 2005-10-30 07:30:48 EST
Verified for 3.2 M3 using ASTView and build I20051025-0800+v_619