# HG changeset patch # User mcimadamore # Date 1374760300 -3600 # Node ID dae52d74c1fc59a8b492b11d5e040027bb8762e8 # Parent b02f28bf7f1cefb3174398166cf933e433b24efa 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 diff -r b02f28bf7f1c -r dae52d74c1fc src/share/classes/com/sun/tools/javac/comp/Resolve.java --- 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 res; - if (!lookupSuccess(unboundSym)) { - res = new Pair(boundSym, boundLookupHelper); - env.info.pendingResolutionPhase = boundEnv.info.pendingResolutionPhase; - } else if (lookupSuccess(boundSym)) { - res = new Pair(ambiguityError(boundSym, unboundSym), boundLookupHelper); - env.info.pendingResolutionPhase = boundEnv.info.pendingResolutionPhase; - } else { - res = new Pair(unboundSym, unboundLookupHelper); - env.info.pendingResolutionPhase = unboundEnv.info.pendingResolutionPhase; - } + Symbol bestSym = choose(boundSym, unboundSym); + res = new Pair(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 argtypes, List typeargtypes) { Map candidatesMap = mapCandidates(); - Map filteredCandidates = filterCandidates(candidatesMap); + Map filteredCandidates = compactMethodDiags ? + filterCandidates(candidatesMap) : + mapCandidates(); if (filteredCandidates.isEmpty()) { filteredCandidates = candidatesMap; } @@ -3556,8 +3593,7 @@ Map candidates = new LinkedHashMap(); for (Map.Entry _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); } } diff -r b02f28bf7f1c -r dae52d74c1fc src/share/classes/com/sun/tools/javac/resources/compiler.properties --- 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 diff -r b02f28bf7f1c -r dae52d74c1fc test/tools/javac/diags/examples/ReportAccessFragment.java --- /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; + } +} diff -r b02f28bf7f1c -r dae52d74c1fc test/tools/javac/lambda/8020843/T8020843a.java --- /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 { + Y m(X x); + } + + void test(T t) { + Function ss = T::clone; + } +} diff -r b02f28bf7f1c -r dae52d74c1fc test/tools/javac/lambda/8020843/T8020843a.out --- /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 diff -r b02f28bf7f1c -r dae52d74c1fc test/tools/javac/lambda/8020843/T8020843b.java --- /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 { + Y m(X x); + } + + interface BiFunction { + 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 f1 = T8020843b::m; //show bound case diag + BiFunction f2 = T8020843b::m2; //show unbound case diag + } +} diff -r b02f28bf7f1c -r dae52d74c1fc test/tools/javac/lambda/8020843/T8020843b.out --- /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 diff -r b02f28bf7f1c -r dae52d74c1fc test/tools/javac/lambda/MethodReference28.out --- 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