RFR: 8173165: javac should reject references to enum constants from nested classes inside instance initializers

Guoxiong Li github.com+13688759+lgxbslgx at openjdk.java.net
Tue Dec 29 11:24:07 UTC 2020


Hi all,

The [JLS 8.9.2](https://docs.oracle.com/javase/specs/jls/se15/html/jls-8.html#jls-8.9.2) states it as bellow.

> It is a compile-time error to refer to a static field of an enum type from a constructor, instance initializer, or instance variable initializer of the enum type, unless the field is a constant variable (§4.12.4).

Currently, compiler doesn't generate the compile-time error when compiling the following code.

import java.util.HashSet;
public enum Test {
    FOO;
    public HashSet<Test> vals = new HashSet<Test>(){
        Test test = FOO;
        {
            add(FOO);
        }
    };
}

This patch fixes it and adds two corresponding tests. Thank you for taking the time to review.

Best Regards.

-------------

Commit messages:
 - 8173165: javac should reject references to enum constants from nested classes inside instance initializers

Changes: https://git.openjdk.java.net/jdk/pull/1905/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=1905&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8173165
  Stats: 135 lines in 2 files changed: 131 ins; 0 del; 4 mod
  Patch: https://git.openjdk.java.net/jdk/pull/1905.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/1905/head:pull/1905

PR: https://git.openjdk.java.net/jdk/pull/1905


More information about the compiler-dev mailing list