changeset 1920:dae52d74c1fc

8020843: javac crashes on accessibility check with method reference with typevar receiver Summary: method reference overload check doesn't walk through type-variable receivers Reviewed-by: jjg
author mcimadamore
date Thu, 25 Jul 2013 14:51:40 +0100
parents b02f28bf7f1c
children 37048aa3ac19
files src/share/classes/com/sun/tools/javac/comp/Resolve.java src/share/classes/com/sun/tools/javac/resources/compiler.properties test/tools/javac/diags/examples/ReportAccessFragment.java test/tools/javac/lambda/8020843/T8020843a.java test/tools/javac/lambda/8020843/T8020843a.out test/tools/javac/lambda/8020843/T8020843b.java test/tools/javac/lambda/8020843/T8020843b.out test/tools/javac/lambda/MethodReference28.out
diffstat 8 files changed, 136 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Thu Jul 25 14:49:16 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Thu Jul 25 14:51:40 2013 +0100
@@ -2649,6 +2649,13 @@
                                   InferenceContext inferenceContext) {
         MethodResolutionPhase maxPhase = boxingAllowed ? VARARITY : BASIC;
 
+        if (site.hasTag(TYPEVAR)) {
+            return resolveMemberReference(pos, env, referenceTree, site.getUpperBound(),
+                    name, argtypes, typeargtypes, boxingAllowed, methodCheck, inferenceContext);
+        }
+
+        site = types.capture(site);
+
         ReferenceLookupHelper boundLookupHelper;
         if (!name.equals(names.init)) {
             //method reference
@@ -2675,24 +2682,52 @@
 
         //merge results
         Pair<Symbol, ReferenceLookupHelper> res;
-        if (!lookupSuccess(unboundSym)) {
-            res = new Pair<Symbol, ReferenceLookupHelper>(boundSym, boundLookupHelper);
-            env.info.pendingResolutionPhase = boundEnv.info.pendingResolutionPhase;
-        } else if (lookupSuccess(boundSym)) {
-            res = new Pair<Symbol, ReferenceLookupHelper>(ambiguityError(boundSym, unboundSym), boundLookupHelper);
-            env.info.pendingResolutionPhase = boundEnv.info.pendingResolutionPhase;
-        } else {
-            res = new Pair<Symbol, ReferenceLookupHelper>(unboundSym, unboundLookupHelper);
-            env.info.pendingResolutionPhase = unboundEnv.info.pendingResolutionPhase;
-        }
+        Symbol bestSym = choose(boundSym, unboundSym);
+        res = new Pair<Symbol, ReferenceLookupHelper>(bestSym,
+                bestSym == unboundSym ? unboundLookupHelper : boundLookupHelper);
+        env.info.pendingResolutionPhase = bestSym == unboundSym ?
+                unboundEnv.info.pendingResolutionPhase :
+                boundEnv.info.pendingResolutionPhase;
 
         return res;
     }
-    //private
-        boolean lookupSuccess(Symbol s) {
+    //where
+        private Symbol choose(Symbol s1, Symbol s2) {
+            if (lookupSuccess(s1) && lookupSuccess(s2)) {
+                return ambiguityError(s1, s2);
+            } else if (lookupSuccess(s1) ||
+                    (canIgnore(s2) && !canIgnore(s1))) {
+                return s1;
+            } else if (lookupSuccess(s2) ||
+                    (canIgnore(s1) && !canIgnore(s2))) {
+                return s2;
+            } else {
+                return s1;
+            }
+        }
+
+        private boolean lookupSuccess(Symbol s) {
             return s.kind == MTH || s.kind == AMBIGUOUS;
         }
 
+        private boolean canIgnore(Symbol s) {
+            switch (s.kind) {
+                case ABSENT_MTH:
+                    return true;
+                case WRONG_MTH:
+                    InapplicableSymbolError errSym =
+                            (InapplicableSymbolError)s;
+                    return new Template(MethodCheckDiag.ARITY_MISMATCH.regex())
+                            .matches(errSym.errCandidate().snd);
+                case WRONG_MTHS:
+                    InapplicableSymbolsError errSyms =
+                            (InapplicableSymbolsError)s;
+                    return errSyms.filterCandidates(errSyms.mapCandidates()).isEmpty();
+                default:
+                    return false;
+            }
+        }
+
     /**
      * Helper for defining custom method-like lookup logic; a lookup helper
      * provides hooks for (i) the actual lookup logic and (ii) accessing the
@@ -3504,7 +3539,9 @@
                 List<Type> argtypes,
                 List<Type> typeargtypes) {
             Map<Symbol, JCDiagnostic> candidatesMap = mapCandidates();
-            Map<Symbol, JCDiagnostic> filteredCandidates = filterCandidates(candidatesMap);
+            Map<Symbol, JCDiagnostic> filteredCandidates = compactMethodDiags ?
+                    filterCandidates(candidatesMap) :
+                    mapCandidates();
             if (filteredCandidates.isEmpty()) {
                 filteredCandidates = candidatesMap;
             }
@@ -3556,8 +3593,7 @@
                 Map<Symbol, JCDiagnostic> candidates = new LinkedHashMap<Symbol, JCDiagnostic>();
                 for (Map.Entry<Symbol, JCDiagnostic> _entry : candidatesMap.entrySet()) {
                     JCDiagnostic d = _entry.getValue();
-                    if (!compactMethodDiags ||
-                            !new Template(MethodCheckDiag.ARITY_MISMATCH.regex()).matches(d)) {
+                    if (!new Template(MethodCheckDiag.ARITY_MISMATCH.regex()).matches(d)) {
                         candidates.put(_entry.getKey(), d);
                     }
                 }
--- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Jul 25 14:49:16 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Jul 25 14:51:40 2013 +0100
@@ -905,6 +905,10 @@
 compiler.err.report.access=\
     {0} has {1} access in {2}
 
+# 0: symbol, 1: set of modifier, 2: symbol
+compiler.misc.report.access=\
+    {0} has {1} access in {2}
+
 compiler.err.ret.outside.meth=\
     return outside method
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/ReportAccessFragment.java	Thu Jul 25 14:51:40 2013 +0100
@@ -0,0 +1,32 @@
+/*
+ * 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.prob.found.req
+// key: compiler.misc.invalid.mref
+// key: compiler.misc.report.access
+
+class ReportAccessFragment {
+    void test(Object o) {
+        Runnable r = o::clone;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/8020843/T8020843a.java	Thu Jul 25 14:51:40 2013 +0100
@@ -0,0 +1,16 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8020843
+ * @summary javac crashes on accessibility check with method reference with typevar receiver
+ * @compile/fail/ref=T8020843a.out -XDrawDiagnostics T8020843a.java
+ */
+
+class T8020843a {
+    interface Function<X, Y> {
+        Y m(X x);
+    }
+
+    <T> void test(T t) {
+        Function<T, Object> ss = T::clone;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/8020843/T8020843a.out	Thu Jul 25 14:51:40 2013 +0100
@@ -0,0 +1,2 @@
+T8020843a.java:14:34: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.report.access: clone(), protected, java.lang.Object))
+1 error
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/8020843/T8020843b.java	Thu Jul 25 14:51:40 2013 +0100
@@ -0,0 +1,27 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8020843
+ * @summary javac crashes on accessibility check with method reference with typevar receiver
+ * @compile/fail/ref=T8020843b.out -XDrawDiagnostics T8020843b.java
+ */
+
+class T8020843b {
+    interface Function<X, Y> {
+        Y m(X x);
+    }
+
+    interface BiFunction<X, Y, Z> {
+        Z m(X x, Y y);
+    }
+
+    Object m(int i) { return null; }
+    static Object m(String t) { return null; }
+
+    Object m2(int i) { return null; }
+    static Object m2(long t) { return null; }
+
+    static void test() {
+        Function<T8020843b, Object> f1 = T8020843b::m; //show bound case diag
+        BiFunction<T8020843b, String, Object> f2 = T8020843b::m2; //show unbound case diag
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/8020843/T8020843b.out	Thu Jul 25 14:51:40 2013 +0100
@@ -0,0 +1,3 @@
+T8020843b.java:24:42: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.cant.apply.symbols: kindname.method, m, T8020843b,{(compiler.misc.inapplicable.method: kindname.method, T8020843b, m(int), (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: T8020843b, int))),(compiler.misc.inapplicable.method: kindname.method, T8020843b, m(java.lang.String), (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: T8020843b, java.lang.String)))}))
+T8020843b.java:25:52: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.cant.apply.symbols: kindname.method, m2, T8020843b,java.lang.String,{(compiler.misc.inapplicable.method: kindname.method, T8020843b, m2(int), (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: java.lang.String, int))),(compiler.misc.inapplicable.method: kindname.method, T8020843b, m2(long), (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: java.lang.String, long)))}))
+2 errors
--- a/test/tools/javac/lambda/MethodReference28.out	Thu Jul 25 14:49:16 2013 +0100
+++ b/test/tools/javac/lambda/MethodReference28.out	Thu Jul 25 14:51:40 2013 +0100
@@ -9,6 +9,6 @@
 MethodReference28.java:46:19: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.cant.apply.symbol: kindname.method, m3, java.lang.String, int, kindname.class, MethodReference28, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: int, java.lang.String))))
 MethodReference28.java:47:19: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.cant.apply.symbol: kindname.method, m4, java.lang.String[], int, kindname.class, MethodReference28, (compiler.misc.varargs.argument.mismatch: (compiler.misc.inconvertible.types: int, java.lang.String))))
 MethodReference28.java:52:19: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.cant.apply.symbol: kindname.method, m2, java.lang.Integer,java.lang.Integer, MethodReference28,int, kindname.class, MethodReference28, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: MethodReference28, java.lang.Integer))))
-MethodReference28.java:53:19: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.cant.apply.symbol: kindname.method, m3, java.lang.String, MethodReference28,int, kindname.class, MethodReference28, (compiler.misc.arg.length.mismatch)))
+MethodReference28.java:53:19: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.cant.apply.symbol: kindname.method, m3, java.lang.String, MethodReference28,int, kindname.class, MethodReference28, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: int, java.lang.String))))
 MethodReference28.java:54:19: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.cant.apply.symbol: kindname.method, m4, java.lang.String[], MethodReference28,int, kindname.class, MethodReference28, (compiler.misc.varargs.argument.mismatch: (compiler.misc.inconvertible.types: MethodReference28, java.lang.String))))
 13 errors