8023549: Compiler emitting spurious errors when constructor reference type is inferred and explicit type arguments are supplied
authorvromero
Wed, 04 Sep 2013 00:01:05 +0100
changeset 19918 3bdf0c6b869c
parent 19917 ebb957ba7b07
child 19919 ffb2cc047744
8023549: Compiler emitting spurious errors when constructor reference type is inferred and explicit type arguments are supplied Reviewed-by: jjg, vromero Contributed-by: maurizio.cimadamore@oracle.com
langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/test/tools/javac/diags/examples/MrefInferAndExplicitParams.java
langtools/test/tools/javac/lambda/8023549/T8023549.java
langtools/test/tools/javac/lambda/8023549/T8023549.out
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Sep 03 23:41:37 2013 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Sep 04 00:01:05 2013 +0100
@@ -2647,6 +2647,13 @@
 
             if (that.getMode() == JCMemberReference.ReferenceMode.NEW) {
                 exprType = chk.checkConstructorRefType(that.expr, exprType);
+                if (!exprType.isErroneous() &&
+                    exprType.isRaw() &&
+                    that.typeargs != null) {
+                    log.error(that.expr.pos(), "invalid.mref", Kinds.kindName(that.getMode()),
+                        diags.fragment("mref.infer.and.explicit.params"));
+                    exprType = types.createErrorType(exprType);
+                }
             }
 
             if (exprType.isErroneous()) {
--- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Sep 03 23:41:37 2013 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Sep 04 00:01:05 2013 +0100
@@ -1927,6 +1927,10 @@
 compiler.misc.diamond.and.explicit.params=\
     cannot use ''<>'' with explicit type parameters for constructor
 
+# 0: unused
+compiler.misc.mref.infer.and.explicit.params=\
+    cannot use raw constructor reference with explicit type parameters for constructor
+
 # 0: type, 1: list of type
 compiler.misc.explicit.param.do.not.conform.to.bounds=\
     explicit type argument {0} does not conform to declared bound(s) {1}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/MrefInferAndExplicitParams.java	Wed Sep 04 00:01:05 2013 +0100
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.err.invalid.mref
+// key: compiler.misc.mref.infer.and.explicit.params
+
+public class MrefInferAndExplicitParams {
+    static class Foo<X> {}
+
+    interface Supplier<X> {
+        X make();
+    }
+
+    Supplier<Foo<String>> sfs = Foo::<Number>new;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/8023549/T8023549.java	Wed Sep 04 00:01:05 2013 +0100
@@ -0,0 +1,27 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8023549
+ * @summary Compiler emitting spurious errors when constructor reference type is inferred and explicit type arguments are supplied
+ * @compile/fail/ref=T8023549.out -XDrawDiagnostics T8023549.java
+ */
+
+public class T8023549 {
+    static class Foo<X> { }
+
+    interface Supplier<X> {
+        X make();
+    }
+
+    interface ExtSupplier<X> extends Supplier<X> { }
+
+    void m1(Supplier<Foo<String>> sfs) { }
+
+    void m2(Supplier<Foo<String>> sfs) { }
+    void m2(ExtSupplier<Foo<Integer>> sfs) { }
+
+    void test() {
+        Supplier<Foo<String>> sfs = Foo::<Number>new;
+        m1(Foo::<Number>new);
+        m2(Foo::<Number>new);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/8023549/T8023549.out	Wed Sep 04 00:01:05 2013 +0100
@@ -0,0 +1,5 @@
+T8023549.java:23:37: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.mref.infer.and.explicit.params)
+T8023549.java:24:12: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.mref.infer.and.explicit.params)
+T8023549.java:25:9: compiler.err.ref.ambiguous: m2, kindname.method, m2(T8023549.Supplier<T8023549.Foo<java.lang.String>>), T8023549, kindname.method, m2(T8023549.ExtSupplier<T8023549.Foo<java.lang.Integer>>), T8023549
+T8023549.java:25:12: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.mref.infer.and.explicit.params)
+4 errors