mirror of
https://github.com/openjdk/jdk.git
synced 2026-05-11 14:11:36 +00:00
7129801: Merge the two method applicability routines
Resolve.java and Infer.java should reuse the same method applicability check routine Reviewed-by: dlsmith, jjg
This commit is contained in:
parent
323665eec5
commit
d353146b32
@ -34,6 +34,7 @@ import com.sun.tools.javac.code.*;
|
||||
import com.sun.tools.javac.code.Type.*;
|
||||
import com.sun.tools.javac.code.Type.ForAll.ConstraintKind;
|
||||
import com.sun.tools.javac.code.Symbol.*;
|
||||
import com.sun.tools.javac.comp.Resolve.InapplicableMethodException;
|
||||
import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode;
|
||||
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
|
||||
|
||||
@ -84,7 +85,7 @@ public class Infer {
|
||||
|
||||
}
|
||||
|
||||
public static class InferenceException extends Resolve.InapplicableMethodException {
|
||||
public static class InferenceException extends InapplicableMethodException {
|
||||
private static final long serialVersionUID = 0;
|
||||
|
||||
InferenceException(JCDiagnostic.Factory diags) {
|
||||
@ -287,6 +288,18 @@ public class Infer {
|
||||
}
|
||||
}
|
||||
|
||||
Type asUndetType(Type t, List<Type> undetvars) {
|
||||
return types.subst(t, inferenceVars(undetvars), undetvars);
|
||||
}
|
||||
|
||||
List<Type> inferenceVars(List<Type> undetvars) {
|
||||
ListBuffer<Type> tvars = ListBuffer.lb();
|
||||
for (Type uv : undetvars) {
|
||||
tvars.append(((UndetVar)uv).qtype);
|
||||
}
|
||||
return tvars.toList();
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
* Exported Methods
|
||||
***************************************************************************/
|
||||
@ -372,62 +385,11 @@ public class Infer {
|
||||
final Warner warn) throws InferenceException {
|
||||
//-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
|
||||
List<Type> undetvars = Type.map(tvars, fromTypeVarFun);
|
||||
List<Type> formals = mt.argtypes;
|
||||
//need to capture exactly once - otherwise subsequent
|
||||
//applicability checks might fail
|
||||
final List<Type> capturedArgs = types.capture(argtypes);
|
||||
List<Type> actuals = capturedArgs;
|
||||
List<Type> actualsNoCapture = argtypes;
|
||||
// instantiate all polymorphic argument types and
|
||||
// set up lower bounds constraints for undetvars
|
||||
Type varargsFormal = useVarargs ? formals.last() : null;
|
||||
if (varargsFormal == null &&
|
||||
actuals.size() != formals.size()) {
|
||||
throw unambiguousNoInstanceException
|
||||
.setMessage("infer.arg.length.mismatch");
|
||||
}
|
||||
while (actuals.nonEmpty() && formals.head != varargsFormal) {
|
||||
Type formal = formals.head;
|
||||
Type actual = actuals.head.baseType();
|
||||
Type actualNoCapture = actualsNoCapture.head.baseType();
|
||||
if (actual.tag == FORALL)
|
||||
actual = instantiateArg((ForAll)actual, formal, tvars, warn);
|
||||
Type undetFormal = types.subst(formal, tvars, undetvars);
|
||||
boolean works = allowBoxing
|
||||
? types.isConvertible(actual, undetFormal, warn)
|
||||
: types.isSubtypeUnchecked(actual, undetFormal, warn);
|
||||
if (!works) {
|
||||
throw unambiguousNoInstanceException
|
||||
.setMessage("infer.no.conforming.assignment.exists",
|
||||
tvars, actualNoCapture, formal);
|
||||
}
|
||||
formals = formals.tail;
|
||||
actuals = actuals.tail;
|
||||
actualsNoCapture = actualsNoCapture.tail;
|
||||
}
|
||||
//final List<Type> capturedArgs = types.capture(argtypes);
|
||||
|
||||
if (formals.head != varargsFormal) // not enough args
|
||||
throw unambiguousNoInstanceException.setMessage("infer.arg.length.mismatch");
|
||||
|
||||
// for varargs arguments as well
|
||||
if (useVarargs) {
|
||||
Type elemType = types.elemtype(varargsFormal);
|
||||
Type elemUndet = types.subst(elemType, tvars, undetvars);
|
||||
while (actuals.nonEmpty()) {
|
||||
Type actual = actuals.head.baseType();
|
||||
Type actualNoCapture = actualsNoCapture.head.baseType();
|
||||
if (actual.tag == FORALL)
|
||||
actual = instantiateArg((ForAll)actual, elemType, tvars, warn);
|
||||
boolean works = types.isConvertible(actual, elemUndet, warn);
|
||||
if (!works) {
|
||||
throw unambiguousNoInstanceException
|
||||
.setMessage("infer.no.conforming.assignment.exists",
|
||||
tvars, actualNoCapture, elemType);
|
||||
}
|
||||
actuals = actuals.tail;
|
||||
actualsNoCapture = actualsNoCapture.tail;
|
||||
}
|
||||
}
|
||||
final List<Type> capturedArgs =
|
||||
rs.checkRawArgumentsAcceptable(env, undetvars, argtypes, mt.getParameterTypes(),
|
||||
allowBoxing, useVarargs, warn, new InferenceCheckHandler(undetvars));
|
||||
|
||||
// minimize as yet undetermined type variables
|
||||
for (Type t : undetvars)
|
||||
@ -503,6 +465,31 @@ public class Infer {
|
||||
}
|
||||
//where
|
||||
|
||||
/** inference check handler **/
|
||||
class InferenceCheckHandler implements Resolve.MethodCheckHandler {
|
||||
|
||||
List<Type> undetvars;
|
||||
|
||||
public InferenceCheckHandler(List<Type> undetvars) {
|
||||
this.undetvars = undetvars;
|
||||
}
|
||||
|
||||
public InapplicableMethodException arityMismatch() {
|
||||
return unambiguousNoInstanceException.setMessage("infer.arg.length.mismatch");
|
||||
}
|
||||
public InapplicableMethodException argumentMismatch(boolean varargs, Type found, Type expected) {
|
||||
String key = varargs ?
|
||||
"infer.varargs.argument.mismatch" :
|
||||
"infer.no.conforming.assignment.exists";
|
||||
return unambiguousNoInstanceException.setMessage(key,
|
||||
inferenceVars(undetvars), found, expected);
|
||||
}
|
||||
public InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected) {
|
||||
return unambiguousNoInstanceException.setMessage("inaccessible.varargs.type",
|
||||
expected, Kinds.kindName(location), location);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A delegated type representing a partially uninferred method type.
|
||||
* The return type of a partially uninferred method type is a ForAll
|
||||
@ -572,7 +559,7 @@ public class Infer {
|
||||
rs.checkRawArgumentsAcceptable(env, actuals, formals,
|
||||
allowBoxing, useVarargs, warn);
|
||||
}
|
||||
catch (Resolve.InapplicableMethodException ex) {
|
||||
catch (InapplicableMethodException ex) {
|
||||
// inferred method is not applicable
|
||||
throw invalidInstanceException.setMessage(ex.getDiagnostic());
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1999, 2012, 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
|
||||
@ -474,52 +474,126 @@ public class Resolve {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* A check handler is used by the main method applicability routine in order
|
||||
* to handle specific method applicability failures. It is assumed that a class
|
||||
* implementing this interface should throw exceptions that are a subtype of
|
||||
* InapplicableMethodException (see below). Such exception will terminate the
|
||||
* method applicability check and propagate important info outwards (for the
|
||||
* purpose of generating better diagnostics).
|
||||
*/
|
||||
interface MethodCheckHandler {
|
||||
/* The number of actuals and formals differ */
|
||||
InapplicableMethodException arityMismatch();
|
||||
/* An actual argument type does not conform to the corresponding formal type */
|
||||
InapplicableMethodException argumentMismatch(boolean varargs, Type found, Type expected);
|
||||
/* The element type of a varargs is not accessible in the current context */
|
||||
InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic method check handler used within Resolve - all methods end up
|
||||
* throwing InapplicableMethodException; a diagnostic fragment that describes
|
||||
* the cause as to why the method is not applicable is set on the exception
|
||||
* before it is thrown.
|
||||
*/
|
||||
MethodCheckHandler resolveHandler = new MethodCheckHandler() {
|
||||
public InapplicableMethodException arityMismatch() {
|
||||
return inapplicableMethodException.setMessage("arg.length.mismatch");
|
||||
}
|
||||
public InapplicableMethodException argumentMismatch(boolean varargs, Type found, Type expected) {
|
||||
String key = varargs ?
|
||||
"varargs.argument.mismatch" :
|
||||
"no.conforming.assignment.exists";
|
||||
return inapplicableMethodException.setMessage(key,
|
||||
found, expected);
|
||||
}
|
||||
public InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected) {
|
||||
return inapplicableMethodException.setMessage("inaccessible.varargs.type",
|
||||
expected, Kinds.kindName(location), location);
|
||||
}
|
||||
};
|
||||
|
||||
void checkRawArgumentsAcceptable(Env<AttrContext> env,
|
||||
List<Type> argtypes,
|
||||
List<Type> formals,
|
||||
boolean allowBoxing,
|
||||
boolean useVarargs,
|
||||
Warner warn) {
|
||||
checkRawArgumentsAcceptable(env, List.<Type>nil(), argtypes, formals,
|
||||
allowBoxing, useVarargs, warn, resolveHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method applicability routine. Given a list of actual types A,
|
||||
* a list of formal types F, determines whether the types in A are
|
||||
* compatible (by method invocation conversion) with the types in F.
|
||||
*
|
||||
* Since this routine is shared between overload resolution and method
|
||||
* type-inference, it is crucial that actual types are converted to the
|
||||
* corresponding 'undet' form (i.e. where inference variables are replaced
|
||||
* with undetvars) so that constraints can be propagated and collected.
|
||||
*
|
||||
* Moreover, if one or more types in A is a poly type, this routine calls
|
||||
* Infer.instantiateArg in order to complete the poly type (this might involve
|
||||
* deferred attribution).
|
||||
*
|
||||
* A method check handler (see above) is used in order to report errors.
|
||||
*/
|
||||
List<Type> checkRawArgumentsAcceptable(Env<AttrContext> env,
|
||||
List<Type> undetvars,
|
||||
List<Type> argtypes,
|
||||
List<Type> formals,
|
||||
boolean allowBoxing,
|
||||
boolean useVarargs,
|
||||
Warner warn,
|
||||
MethodCheckHandler handler) {
|
||||
Type varargsFormal = useVarargs ? formals.last() : null;
|
||||
ListBuffer<Type> checkedArgs = ListBuffer.lb();
|
||||
|
||||
if (varargsFormal == null &&
|
||||
argtypes.size() != formals.size()) {
|
||||
throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
|
||||
throw handler.arityMismatch(); // not enough args
|
||||
}
|
||||
|
||||
while (argtypes.nonEmpty() && formals.head != varargsFormal) {
|
||||
boolean works = allowBoxing
|
||||
? types.isConvertible(argtypes.head, formals.head, warn)
|
||||
: types.isSubtypeUnchecked(argtypes.head, formals.head, warn);
|
||||
if (!works)
|
||||
throw inapplicableMethodException.setMessage("no.conforming.assignment.exists",
|
||||
argtypes.head,
|
||||
formals.head);
|
||||
Type undetFormal = infer.asUndetType(formals.head, undetvars);
|
||||
Type capturedActual = types.capture(argtypes.head);
|
||||
boolean works = allowBoxing ?
|
||||
types.isConvertible(capturedActual, undetFormal, warn) :
|
||||
types.isSubtypeUnchecked(capturedActual, undetFormal, warn);
|
||||
if (!works) {
|
||||
throw handler.argumentMismatch(false, argtypes.head, formals.head);
|
||||
}
|
||||
checkedArgs.append(capturedActual);
|
||||
argtypes = argtypes.tail;
|
||||
formals = formals.tail;
|
||||
}
|
||||
|
||||
if (formals.head != varargsFormal)
|
||||
throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
|
||||
if (formals.head != varargsFormal) {
|
||||
throw handler.arityMismatch(); // not enough args
|
||||
}
|
||||
|
||||
if (useVarargs) {
|
||||
//note: if applicability check is triggered by most specific test,
|
||||
//the last argument of a varargs is _not_ an array type (see JLS 15.12.2.5)
|
||||
Type elt = types.elemtype(varargsFormal);
|
||||
Type eltUndet = infer.asUndetType(elt, undetvars);
|
||||
while (argtypes.nonEmpty()) {
|
||||
if (!types.isConvertible(argtypes.head, elt, warn))
|
||||
throw inapplicableMethodException.setMessage("varargs.argument.mismatch",
|
||||
argtypes.head,
|
||||
elt);
|
||||
Type capturedActual = types.capture(argtypes.head);
|
||||
if (!types.isConvertible(capturedActual, eltUndet, warn)) {
|
||||
throw handler.argumentMismatch(true, argtypes.head, elt);
|
||||
}
|
||||
checkedArgs.append(capturedActual);
|
||||
argtypes = argtypes.tail;
|
||||
}
|
||||
//check varargs element type accessibility
|
||||
if (!isAccessible(env, elt)) {
|
||||
if (undetvars.isEmpty() && !isAccessible(env, elt)) {
|
||||
Symbol location = env.enclClass.sym;
|
||||
throw inapplicableMethodException.setMessage("inaccessible.varargs.type",
|
||||
elt,
|
||||
Kinds.kindName(location),
|
||||
location);
|
||||
throw handler.inaccessibleVarargs(location, elt);
|
||||
}
|
||||
}
|
||||
return;
|
||||
return checkedArgs.toList();
|
||||
}
|
||||
// where
|
||||
public static class InapplicableMethodException extends RuntimeException {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
# Copyright (c) 1999, 2012, 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
|
||||
@ -1620,6 +1620,10 @@ compiler.misc.infer.no.conforming.assignment.exists=\
|
||||
compiler.misc.infer.arg.length.mismatch=\
|
||||
cannot instantiate from arguments because actual and formal argument lists differ in length
|
||||
|
||||
# 0: list of type, 1: type, 2: type
|
||||
compiler.misc.infer.varargs.argument.mismatch=\
|
||||
no instance(s) of type variable(s) {0} exist so that argument type {1} conforms to vararg element type {2}
|
||||
|
||||
# 0: type, 1: list of type
|
||||
compiler.misc.inferred.do.not.conform.to.bounds=\
|
||||
inferred type does not conform to declared bound(s)\n\
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (c) 2012, 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.cant.apply.symbol.1
|
||||
// key: compiler.misc.infer.varargs.argument.mismatch
|
||||
|
||||
class InferVarargsArgumentMismatch {
|
||||
<X> void m(X x1, String... xs) {}
|
||||
{ this.m("", 1); }
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user