JDK-4321177 : Incorrect parsing of anonymous array followed by array access
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2000-03-13
  • Updated: 2000-12-06
  • Resolved: 2000-12-06
Related Reports
Relates :  
Description

Name: stC104175			Date: 03/13/2000


java version "1.3.0rc1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc1-T)
Java HotSpot(TM) Client VM (build 1.3.0rc1-S, mixed mode)

It should not be possible to follow the declaration of an anonymous
array by an array access, due to the following production rules:

Primary:
    PrimaryNoNewArray
    ArrayCreationExpression

PrimaryNoNewArray:
    Literal
    this
    ( Expression )
    ClassInstanceCreationExpression
    FieldAccess
    MethodInvocation
    ArrayAccess

ArrayAccess:
    Name [ Expression ]
    PrimaryNoNewArray [ Expression ]

ArrayCreationExpression:
    new PrimitiveType DimExprs Dimsopt
    new ClassOrInterfaceType DimExprs Dimsopt
    new Type Dims ArrayInitializer

Because an anonymous array is created in an ArrayCreationExpression,
but an array access is performed on a PrimaryNewArray, the two
constructs may not be mixed. But the code below compiles under javac:

public class ArrayBug {
    public static void main(String args[]) {
        for (int i = 0; i < 3; i++) {
            System.out.print(new char[] { 'B', 'u', 'g' }[i]);
        }
        System.out.println(new char[] { 'O', 'K' }.length);
    }
}

The Jikes compiler correctly detects the error in the code.
Both compilers correctly compile the code when parenthesis
are placed around the anonymous array creation.
Note that it is okay to access the length field of an
anonymous array without using the parenthesis, as in the
last line of the program (see Bug Id 4091602).
(Review ID: 102221) 
======================================================================

Comments
WORK AROUND Name: stC104175 Date: 03/13/2000 Don't follow an anonymous array by an array access, or use Jikes. ======================================================================
11-06-2004

EVALUATION This appears to be a parser issue only -- should be easy to fix. william.maddox@Eng 2000-03-13 According to the latest draft JPS, this is allowed. For the expression new X[] { a, b, c }[i] Expression then using Expression: Expression1 Expression1 then using Expression1: Expression2 Expression2 then using Expression2: Expression3 Expression3 then using Expression3: Primary Selector Primary Selector then using Primary: new Creator new Creator Selector then using Selector: [ Expression ] new Creator [ Expression ] then using Creator: QualifiedIdentifier ArrayCreatorRest new QualifiedIdentifier ArrayCreatorRest [ Expression ] then using QualifiedIdentifier: IDENTIFIER new IDENTIFIER ArrayCreatorRest [ Expression ] then using ArrayCreatorRest: [] ArrayInitializer new IDENTIFIER [] ArrayInitializer [ Expression ] then using ArrayInitializer: { VariableInitializer, VariableInitializer, VariableInitializer } new IDENTIFIER [] { VariableInitializer, VariableInitializer, VariableInitializer } [ Expression ] QED neal.gafter@Eng 2000-12-05
05-12-2000