6885255: Improve usability of raw warnings
authormcimadamore
Thu, 19 Aug 2010 11:52:58 +0100
changeset 6351 84c44db80d06
parent 6350 7070b0722ebb
child 6352 217d5a69681a
6885255: Improve usability of raw warnings Summary: raw warnings should be disabled in (i) instanceof expressions and (ii) when java.lang.Class is not parameterized Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/src/share/classes/com/sun/tools/javac/comp/Check.java
langtools/test/tools/javac/warnings/6747671/T6747671.java
langtools/test/tools/javac/warnings/6747671/T6747671.out
langtools/test/tools/javac/warnings/6885255/T6885255.java
langtools/test/tools/javac/warnings/6885255/T6885255.out
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Aug 19 11:50:50 2010 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Aug 19 11:52:58 2010 +0100
@@ -2008,7 +2008,7 @@
 
     public void visitTypeCast(JCTypeCast tree) {
         Type clazztype = attribType(tree.clazz, env);
-        chk.validate(tree.clazz, env);
+        chk.validate(tree.clazz, env, false);
         Type exprtype = attribExpr(tree.expr, env, Infer.anyPoly);
         Type owntype = chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
         if (exprtype.constValue() != null)
@@ -2021,7 +2021,7 @@
             tree.expr.pos(), attribExpr(tree.expr, env));
         Type clazztype = chk.checkReifiableReferenceType(
             tree.clazz.pos(), attribType(tree.clazz, env));
-        chk.validate(tree.clazz, env);
+        chk.validate(tree.clazz, env, false);
         chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
         result = check(tree, syms.booleanType, VAL, pkind, pt);
     }
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Thu Aug 19 11:50:50 2010 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Thu Aug 19 11:52:58 2010 +0100
@@ -906,33 +906,15 @@
      *
      *  and we can't make sure that the bound is already attributed because
      *  of possible cycles.
-     */
-    private Validator validator = new Validator();
-
-    /** Visitor method: Validate a type expression, if it is not null, catching
+     *
+     * Visitor method: Validate a type expression, if it is not null, catching
      *  and reporting any completion failures.
      */
     void validate(JCTree tree, Env<AttrContext> env) {
-        try {
-            if (tree != null) {
-                validator.env = env;
-                tree.accept(validator);
-                checkRaw(tree, env);
-            }
-        } catch (CompletionFailure ex) {
-            completionError(tree.pos(), ex);
-        }
+        validate(tree, env, true);
     }
-    //where
-    void checkRaw(JCTree tree, Env<AttrContext> env) {
-        if (lint.isEnabled(Lint.LintCategory.RAW) &&
-            tree.type.tag == CLASS &&
-            !TreeInfo.isDiamond(tree) &&
-            !env.enclClass.name.isEmpty() &&  //anonymous or intersection
-            tree.type.isRaw()) {
-            log.warning(Lint.LintCategory.RAW,
-                    tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type);
-        }
+    void validate(JCTree tree, Env<AttrContext> env, boolean checkRaw) {
+        new Validator(env).validateTree(tree, checkRaw, true);
     }
 
     /** Visitor method: Validate a list of type expressions.
@@ -946,9 +928,16 @@
      */
     class Validator extends JCTree.Visitor {
 
+        boolean isOuter;
+        Env<AttrContext> env;
+
+        Validator(Env<AttrContext> env) {
+            this.env = env;
+        }
+
         @Override
         public void visitTypeArray(JCArrayTypeTree tree) {
-            validate(tree.elemtype, env);
+            tree.elemtype.accept(this);
         }
 
         @Override
@@ -960,10 +949,14 @@
                 List<Type> forms = tree.type.tsym.type.getTypeArguments();
                 ListBuffer<Type> tvars_buf = new ListBuffer<Type>();
 
+                boolean is_java_lang_Class = tree.type.tsym.flatName() == names.java_lang_Class;
+
                 // For matching pairs of actual argument types `a' and
                 // formal type parameters with declared bound `b' ...
                 while (args.nonEmpty() && forms.nonEmpty()) {
-                    validate(args.head, env);
+                    validateTree(args.head,
+                            !(isOuter && is_java_lang_Class),
+                            false);
 
                     // exact type arguments needs to know their
                     // bounds (for upper and lower bound
@@ -1015,14 +1008,14 @@
 
         @Override
         public void visitTypeParameter(JCTypeParameter tree) {
-            validate(tree.bounds, env);
+            validateTrees(tree.bounds, true, isOuter);
             checkClassBounds(tree.pos(), tree.type);
         }
 
         @Override
         public void visitWildcard(JCWildcard tree) {
             if (tree.inner != null)
-                validate(tree.inner, env);
+                validateTree(tree.inner, true, isOuter);
         }
 
         @Override
@@ -1060,7 +1053,34 @@
         public void visitTree(JCTree tree) {
         }
 
-        Env<AttrContext> env;
+        public void validateTree(JCTree tree, boolean checkRaw, boolean isOuter) {
+            try {
+                if (tree != null) {
+                    this.isOuter = isOuter;
+                    tree.accept(this);
+                    if (checkRaw)
+                        checkRaw(tree, env);
+                }
+            } catch (CompletionFailure ex) {
+                completionError(tree.pos(), ex);
+            }
+        }
+
+        public void validateTrees(List<? extends JCTree> trees, boolean checkRaw, boolean isOuter) {
+            for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
+                validateTree(l.head, checkRaw, isOuter);
+        }
+
+        void checkRaw(JCTree tree, Env<AttrContext> env) {
+            if (lint.isEnabled(Lint.LintCategory.RAW) &&
+                tree.type.tag == CLASS &&
+                !TreeInfo.isDiamond(tree) &&
+                !env.enclClass.name.isEmpty() &&  //anonymous or intersection
+                tree.type.isRaw()) {
+                log.warning(Lint.LintCategory.RAW,
+                        tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type);
+            }
+        }
     }
 
 /* *************************************************************************
--- a/langtools/test/tools/javac/warnings/6747671/T6747671.java	Thu Aug 19 11:50:50 2010 +0100
+++ b/langtools/test/tools/javac/warnings/6747671/T6747671.java	Thu Aug 19 11:52:58 2010 +0100
@@ -27,8 +27,8 @@
     A<B>.Z<A<B>> z3;//raw warning (2)
 
     void test(Object arg1, B arg2) {//raw warning
-        boolean b = arg1 instanceof A;//raw warning
-        Object a = (A)arg1;//raw warning
+        boolean b = arg1 instanceof A;//ok
+        Object a = (A)arg1;//ok
         A a2 = new A() {};//raw warning (2)
         a2.new Z() {};//raw warning
     }
--- a/langtools/test/tools/javac/warnings/6747671/T6747671.out	Thu Aug 19 11:50:50 2010 +0100
+++ b/langtools/test/tools/javac/warnings/6747671/T6747671.out	Thu Aug 19 11:52:58 2010 +0100
@@ -4,9 +4,7 @@
 T6747671.java:27:14: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
 T6747671.java:27:7: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
 T6747671.java:29:28: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
-T6747671.java:30:37: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
-T6747671.java:31:21: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
 T6747671.java:32:9: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
 T6747671.java:32:20: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
 T6747671.java:33:16: compiler.warn.raw.class.use: T6747671.A.Z, T6747671<E>.A<X>.Z<Y>
-11 warnings
+9 warnings
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/warnings/6885255/T6885255.java	Thu Aug 19 11:52:58 2010 +0100
@@ -0,0 +1,31 @@
+/**
+ * @test /nodynamiccopyright/
+ * @bug 6885255
+ * @summary -Xlint:rawtypes
+ * @compile/ref=T6885255.out -XDrawDiagnostics -Xlint:rawtypes T6885255.java
+ */
+
+class T6885255 {
+
+    static class Test<X, Y> {}
+
+    Class<Test> ct; //no warn - outer Class w/ raw param
+    Class<Test<Test, Test>> ctt; //warn - outer Class w/o raw param (2)
+
+    Class<Class<Test>> cct; //warn - outer Class w/o raw param
+    Class<Class<Test<Test, Test>>> cctt; //warn - outer Class w/o raw param (2)
+
+    Object o1 = (Test)null; //no warn - outer raw and cast
+    Object o2 = (Test<Test, Test>)null; //warn - inner raw (2)
+
+    Object o3 = (Class)null; //no warn - outer raw and cast
+    Object o4 = (Class<Test>)null; //no warn - outer Class w/ raw param
+
+    Object o5 = (Class<Test<Test, Test>>)null; //warn - outer Class w/ non raw param (2)
+    Object o6 = (Class<Class<Test<Test, Test>>>)null; //warn - outer Class w/ non raw param (2)
+
+    Object o7 = (Test<Class, Class>)null; //warn - inner raw (2)
+    Object o8 = (Test<Class<Test>, Class<Test>>)null; //warn - inner Class (2)
+
+    boolean b = null instanceof Test; //no warn - raw and instanceof
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/warnings/6885255/T6885255.out	Thu Aug 19 11:52:58 2010 +0100
@@ -0,0 +1,16 @@
+T6885255.java:13:16: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+T6885255.java:13:22: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+T6885255.java:15:17: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+T6885255.java:16:22: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+T6885255.java:16:28: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+T6885255.java:19:23: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+T6885255.java:19:29: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+T6885255.java:24:29: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+T6885255.java:24:35: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+T6885255.java:25:35: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+T6885255.java:25:41: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+T6885255.java:27:23: compiler.warn.raw.class.use: java.lang.Class, java.lang.Class<T>
+T6885255.java:27:30: compiler.warn.raw.class.use: java.lang.Class, java.lang.Class<T>
+T6885255.java:28:29: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+T6885255.java:28:42: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test<X,Y>
+15 warnings