New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] Java8 parsing corner case with annotated array types #997

Open
oowekyala opened this Issue Mar 23, 2018 · 0 comments

Comments

Projects
None yet
3 participants
@oowekyala
Copy link
Member

oowekyala commented Mar 23, 2018

Problems

With Java 8 type annotations, the dimensions of an array may each have different annotations, which our parser actually doesn't support... There are several instances of this problem:

Type node

      // Doesn't parse
      int @Foo[]@Bar[] i;

JLS

Our grammar:

https://github.com/pmd/pmd/blob/master/pmd-java/etc/grammar/Java.jjt#L1885-L1887

Array creation expression

   // Doesn't parse
    new int @Foo[2] @Bar[];

JLS

Our grammar:

https://github.com/pmd/pmd/blob/master/pmd-java/etc/grammar/Java.jjt#L2261-L2269

VariableDeclaratorId

class Foo {
  int i @Bar[]; // field declaration

  void bar(int i@Bar[]) { // formal parameter 
    int i @Bar[]; // local variable declaration
  }
}

JLS

Our grammar:

https://github.com/pmd/pmd/blob/master/pmd-java/etc/grammar/Java.jjt#L1771-1787

Method declarations

The JLS allows array dimensions (including annotations) to be added after the formal parameter list, eg:

  int method()@Foo[]@Bar[] {
        return new int[2][];
  }

By the way, even without annotations, the parser currently discards the additionnal array dimensions, though it should be used in type resolution:

https://github.com/pmd/pmd/blob/master/pmd-java/etc/grammar/Java.jjt#L1822

This is related to the broader #910 changeset

Solution

  • Add a node for the array dimensions, which resembles the JLS for the Dims production. This wouldn't be API breaking, provided we make the dimensions of eg ReferenceType delegate to that Dims node.
  • Dimensionable should probably be deprecated entirely, and we could use a new interface to replace it that exposes the annotations and the dimensions for type resolution, based on the Dims node
  • To make the method return type resolution work correctly, we could make ReturnType a Dimensionable (and a TypeNode), and add the dimensions of the Dims node found in the method declarator to the dimensions found in the type node.

I imagine that could be 3 PRs

@oowekyala oowekyala added the a:bug label Mar 23, 2018

@oowekyala oowekyala self-assigned this Mar 23, 2018

@oowekyala oowekyala added this to the 6.3.0 milestone Mar 23, 2018

@adangel adangel modified the milestones: 6.3.0, 6.4.0 Apr 28, 2018

@adangel adangel modified the milestones: 6.4.0, 6.5.0 May 19, 2018

@oowekyala oowekyala added the in:ast label May 27, 2018

@jsotuyod jsotuyod modified the milestones: 6.5.0, 6.6.0 Jun 26, 2018

@adangel adangel modified the milestones: 6.6.0, 6.7.0 Jul 29, 2018

@adangel adangel modified the milestones: 6.7.0, 6.8.0 Sep 1, 2018

@oowekyala oowekyala removed this from the 6.8.0 milestone Sep 29, 2018

@oowekyala oowekyala added this to the 7.0.0 milestone Nov 6, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment