changeset 2132:33d1937af1a3

8012685: Spurious raw types warning when using unbound method references Summary: Spurious raw type warning when unbound method reference qualifier parameter types are inferred from target Reviewed-by: jjg, vromero
author mcimadamore
date Wed, 15 May 2013 14:02:37 +0100
parents 05ec778794d0
children 78717f2d00e8
files src/share/classes/com/sun/tools/javac/comp/Attr.java src/share/classes/com/sun/tools/javac/comp/Check.java test/tools/javac/lambda/MethodReference67.java test/tools/javac/lambda/MethodReference67.out
diffstat 4 files changed, 50 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Wed May 15 14:00:31 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Wed May 15 14:02:37 2013 +0100
@@ -2642,10 +2642,11 @@
                 return;
             }
 
-            if (TreeInfo.isStaticSelector(that.expr, names) &&
-                    (that.getMode() != ReferenceMode.NEW || !that.expr.type.isRaw())) {
-                //if the qualifier is a type, validate it
-                chk.validate(that.expr, env);
+            if (TreeInfo.isStaticSelector(that.expr, names)) {
+                //if the qualifier is a type, validate it; raw warning check is
+                //omitted as we don't know at this stage as to whether this is a
+                //raw selector (because of inference)
+                chk.validate(that.expr, env, false);
             }
 
             //attrib type-arguments
@@ -2731,6 +2732,13 @@
 
             if (resultInfo.checkContext.deferredAttrContext().mode == AttrMode.CHECK) {
 
+                if (that.getMode() == ReferenceMode.INVOKE &&
+                        TreeInfo.isStaticSelector(that.expr, names) &&
+                        that.kind.isUnbound() &&
+                        !desc.getParameterTypes().head.isParameterized()) {
+                    chk.checkRaw(that.expr, localEnv);
+                }
+
                 if (!that.kind.isUnbound() &&
                         that.getMode() == ReferenceMode.INVOKE &&
                         TreeInfo.isStaticSelector(that.expr, names) &&
--- a/src/share/classes/com/sun/tools/javac/comp/Check.java	Wed May 15 14:00:31 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/Check.java	Wed May 15 14:02:37 2013 +0100
@@ -1361,23 +1361,23 @@
             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(LintCategory.RAW) &&
-                tree.type.hasTag(CLASS) &&
-                !TreeInfo.isDiamond(tree) &&
-                !withinAnonConstr(env) &&
-                tree.type.isRaw()) {
-                log.warning(LintCategory.RAW,
-                        tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type);
-            }
+    }
+
+    void checkRaw(JCTree tree, Env<AttrContext> env) {
+        if (lint.isEnabled(LintCategory.RAW) &&
+            tree.type.hasTag(CLASS) &&
+            !TreeInfo.isDiamond(tree) &&
+            !withinAnonConstr(env) &&
+            tree.type.isRaw()) {
+            log.warning(LintCategory.RAW,
+                    tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type);
         }
-
-        boolean withinAnonConstr(Env<AttrContext> env) {
+    }
+    //where
+        private boolean withinAnonConstr(Env<AttrContext> env) {
             return env.enclClass.name.isEmpty() &&
                     env.enclMethod != null && env.enclMethod.name == names.init;
         }
-    }
 
 /* *************************************************************************
  * Exception checking
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/MethodReference67.java	Wed May 15 14:02:37 2013 +0100
@@ -0,0 +1,18 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8012685
+ * @summary Spurious raw types warning when using unbound method references
+ * @compile/fail/ref=MethodReference67.out -Werror -Xlint:rawtypes -XDrawDiagnostics MethodReference67.java
+ */
+import java.util.*;
+
+class MethodReference67 {
+    interface Foo<X> {
+        void m(List<X> lx, X x);
+    }
+
+    void test() {
+        Foo<String> fs1 = List::add; //no raw warnings here!
+        Foo fs2 = List::add;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/MethodReference67.out	Wed May 15 14:02:37 2013 +0100
@@ -0,0 +1,7 @@
+MethodReference67.java:16:9: compiler.warn.raw.class.use: MethodReference67.Foo, MethodReference67.Foo<X>
+MethodReference67.java:16:19: compiler.warn.raw.class.use: java.util.List, java.util.List<E>
+- compiler.err.warnings.and.werror
+- compiler.note.unchecked.filename: MethodReference67.java
+- compiler.note.unchecked.recompile
+1 error
+2 warnings