提交 f05c8236 编写于 作者: M mcimadamore

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
上级 bfaaa53e
...@@ -2649,6 +2649,13 @@ public class Resolve { ...@@ -2649,6 +2649,13 @@ public class Resolve {
InferenceContext inferenceContext) { InferenceContext inferenceContext) {
MethodResolutionPhase maxPhase = boxingAllowed ? VARARITY : BASIC; 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; ReferenceLookupHelper boundLookupHelper;
if (!name.equals(names.init)) { if (!name.equals(names.init)) {
//method reference //method reference
...@@ -2675,24 +2682,52 @@ public class Resolve { ...@@ -2675,24 +2682,52 @@ public class Resolve {
//merge results //merge results
Pair<Symbol, ReferenceLookupHelper> res; Pair<Symbol, ReferenceLookupHelper> res;
if (!lookupSuccess(unboundSym)) { Symbol bestSym = choose(boundSym, unboundSym);
res = new Pair<Symbol, ReferenceLookupHelper>(boundSym, boundLookupHelper); res = new Pair<Symbol, ReferenceLookupHelper>(bestSym,
env.info.pendingResolutionPhase = boundEnv.info.pendingResolutionPhase; bestSym == unboundSym ? unboundLookupHelper : boundLookupHelper);
} else if (lookupSuccess(boundSym)) { env.info.pendingResolutionPhase = bestSym == unboundSym ?
res = new Pair<Symbol, ReferenceLookupHelper>(ambiguityError(boundSym, unboundSym), boundLookupHelper); unboundEnv.info.pendingResolutionPhase :
env.info.pendingResolutionPhase = boundEnv.info.pendingResolutionPhase; boundEnv.info.pendingResolutionPhase;
} else {
res = new Pair<Symbol, ReferenceLookupHelper>(unboundSym, unboundLookupHelper);
env.info.pendingResolutionPhase = unboundEnv.info.pendingResolutionPhase;
}
return res; return res;
} }
//private //where
boolean lookupSuccess(Symbol s) { 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; 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 * Helper for defining custom method-like lookup logic; a lookup helper
* provides hooks for (i) the actual lookup logic and (ii) accessing the * provides hooks for (i) the actual lookup logic and (ii) accessing the
...@@ -3504,7 +3539,9 @@ public class Resolve { ...@@ -3504,7 +3539,9 @@ public class Resolve {
List<Type> argtypes, List<Type> argtypes,
List<Type> typeargtypes) { List<Type> typeargtypes) {
Map<Symbol, JCDiagnostic> candidatesMap = mapCandidates(); Map<Symbol, JCDiagnostic> candidatesMap = mapCandidates();
Map<Symbol, JCDiagnostic> filteredCandidates = filterCandidates(candidatesMap); Map<Symbol, JCDiagnostic> filteredCandidates = compactMethodDiags ?
filterCandidates(candidatesMap) :
mapCandidates();
if (filteredCandidates.isEmpty()) { if (filteredCandidates.isEmpty()) {
filteredCandidates = candidatesMap; filteredCandidates = candidatesMap;
} }
...@@ -3556,8 +3593,7 @@ public class Resolve { ...@@ -3556,8 +3593,7 @@ public class Resolve {
Map<Symbol, JCDiagnostic> candidates = new LinkedHashMap<Symbol, JCDiagnostic>(); Map<Symbol, JCDiagnostic> candidates = new LinkedHashMap<Symbol, JCDiagnostic>();
for (Map.Entry<Symbol, JCDiagnostic> _entry : candidatesMap.entrySet()) { for (Map.Entry<Symbol, JCDiagnostic> _entry : candidatesMap.entrySet()) {
JCDiagnostic d = _entry.getValue(); JCDiagnostic d = _entry.getValue();
if (!compactMethodDiags || if (!new Template(MethodCheckDiag.ARITY_MISMATCH.regex()).matches(d)) {
!new Template(MethodCheckDiag.ARITY_MISMATCH.regex()).matches(d)) {
candidates.put(_entry.getKey(), d); candidates.put(_entry.getKey(), d);
} }
} }
......
...@@ -905,6 +905,10 @@ compiler.err.repeated.modifier=\ ...@@ -905,6 +905,10 @@ compiler.err.repeated.modifier=\
compiler.err.report.access=\ compiler.err.report.access=\
{0} has {1} access in {2} {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=\ compiler.err.ret.outside.meth=\
return outside method return outside method
......
/*
* 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;
}
}
/*
* @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;
}
}
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
/*
* @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
}
}
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
...@@ -9,6 +9,6 @@ MethodReference28.java:45:19: compiler.err.prob.found.req: (compiler.misc.invali ...@@ -9,6 +9,6 @@ MethodReference28.java:45:19: compiler.err.prob.found.req: (compiler.misc.invali
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: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: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: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)))) 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 13 errors
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册