Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8274938

15.10.3: ArrayAccess is unnecessarily restrictive

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Fixed
    • P4
    • 18
    • 17
    • specification

    Description

      See https://stackoverflow.com/questions/69469913/what-is-the-java-grammar-that-allows-new-int-00-1-to-compile

      Summarizing, assignments like the following:

        new int[] {1,2} [0] = 99;
        new int[][] { {1,2},{3,4} } [0][0] = 99;
        new int[][][] { { {1,2},{3,4} }, { {5,6},{7,8} } } [0][0][0] = 99;

      are allowed by javac, but unfortunately disallowed by JLS 15.10.3 because the ArrayAccess on the LHS of the Assignment does not permit an ArrayCreationExpression to serve as the array to access. Instead, ArrayAccess permits PrimaryNoNewArray, which is every Primary _except_ ArrayCreationExpression.

      If ArrayCreationExpression was permitted within ArrayAccess, then `new int[3] [3]` would be ambiguous: it could mean either the creation of a multidimensional array (desired) or an array access which does an inline array creation `(new int[3]) [3]` (not desired). However, there is no ambiguity when an inline array creation uses an initializer, since {..} looks different from an index [...]. That is, `new int[] {...} [3]` should unambiguously mean `(new int[] {...}) [3]`.

      Accordingly, it would be legitimate to factor ArrayCreationExpression in JLS 15.10.1 into:

        ArrayCreationExpressionWithoutInitializer
        ArrayCreationExpressionWithInitializer

      so that ArrayAccess in JLS 15.10.3 could accept a third alternative (beyond ExpressionName and PrimaryNoNewArray):

        ArrayCreationExpressionWithInitializer [ Expression ]

      Attachments

        Activity

          People

            abuckley Alex Buckley
            abuckley Alex Buckley
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: