[enhanced-enums]: Enhanced enums failure with raw types?

B. Blaser bsrbnd at gmail.com
Sun Sep 10 17:20:15 UTC 2017


Vicente, Maurizio, All,

Consider the following example involving a raw type in an enhanced enum:

public class A {
    enum E<X> {
        INTEGER(Integer.class), FLOAT<Float>(Float.class);
        E(Class<X> clazz) {}
    }

    enum F {
        INTEGER(Integer.class);
        F(Class<?> clazz) {}
    }

    static class C<X> {
        C INTEGER = new C(Integer.class), FLOAT = new C<Float>(Float.class);
        C(Class<X> clazz) {}
    }
}

Javac fails to compile the latter but I'd be more expecting an
unchecked warning instead, as for classes:

$ javac -Xlint:unchecked A.java
A.java:3: warning: [unchecked] unchecked call to E(Class<X>) as a
member of the raw type E
        INTEGER(Integer.class), FLOAT<Float>(Float.class);
               ^
  where X is a type-variable:
    X extends Object declared in enum E
A.java:13: warning: [unchecked] unchecked call to C(Class<X>) as a
member of the raw type C
        C INTEGER = new C(Integer.class), FLOAT = new C<Float>(Float.class);
                    ^
  where X is a type-variable:
    X extends Object declared in class C
2 warnings

I've provided a simple fix here under (repo.: amber, branch:
enhanced-enums, rev.: 7a22956a0562).

Any comment is welcome,
Bernard

diff -r 7a22956a0562
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
   Fri Sep 08 00:00:41 2017 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
   Sun Sep 10 16:23:02 2017 +0200
@@ -903,15 +903,19 @@
     }

     Type attribExprAsEnumType(Env<AttrContext> env, JCExpression type) {
+        return attribExprAsEnumType(env, type, true);
+    }
+    Type attribExprAsEnumType(Env<AttrContext> env, JCExpression
type, boolean erase) {
         if (type.hasTag(IDENT)) {
             JCIdent id = (JCIdent)type;
             Assert.check((env.enclClass.sym.flags() & ENUM) != 0);
-            id.type = env.info.scope.owner.enclClass().type;
+            Type t = env.info.scope.owner.enclClass().type;
+            id.type = t.isParameterized() && erase ? types.erasure(t): t;
             id.sym = env.info.scope.owner.enclClass();
             return id.type;
         } else {
             JCTypeApply ta = (JCTypeApply)type;
-            Type enumBaseType = attribExprAsEnumType(env, ta.clazz);
+            Type enumBaseType = attribExprAsEnumType(env, ta.clazz, false);
             ResultInfo prevInfo = resultInfo;
             resultInfo = unknownTypeInfo;
             try {


More information about the amber-dev mailing list