src/share/classes/com/sun/tools/javac/comp/Infer.java

Tue, 13 Sep 2011 14:15:39 +0100

author
mcimadamore
date
Tue, 13 Sep 2011 14:15:39 +0100
changeset 1087
3a2200681d69
parent 1006
a2d422d480cb
child 1114
05814303a056
permissions
-rw-r--r--

7086601: Error message bug: cause for method mismatch is 'null'
Summary: Inference error during lub() does not set 'cause' for method resolution diagnostic
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.javac.comp;
    28 import com.sun.tools.javac.tree.JCTree;
    29 import com.sun.tools.javac.tree.JCTree.JCTypeCast;
    30 import com.sun.tools.javac.tree.TreeInfo;
    31 import com.sun.tools.javac.util.*;
    32 import com.sun.tools.javac.util.List;
    33 import com.sun.tools.javac.code.*;
    34 import com.sun.tools.javac.code.Type.*;
    35 import com.sun.tools.javac.code.Type.ForAll.ConstraintKind;
    36 import com.sun.tools.javac.code.Symbol.*;
    37 import com.sun.tools.javac.util.JCDiagnostic;
    39 import static com.sun.tools.javac.code.TypeTags.*;
    41 /** Helper class for type parameter inference, used by the attribution phase.
    42  *
    43  *  <p><b>This is NOT part of any supported API.
    44  *  If you write code that depends on this, you do so at your own risk.
    45  *  This code and its internal interfaces are subject to change or
    46  *  deletion without notice.</b>
    47  */
    48 public class Infer {
    49     protected static final Context.Key<Infer> inferKey =
    50         new Context.Key<Infer>();
    52     /** A value for prototypes that admit any type, including polymorphic ones. */
    53     public static final Type anyPoly = new Type(NONE, null);
    55     Symtab syms;
    56     Types types;
    57     Check chk;
    58     Resolve rs;
    59     JCDiagnostic.Factory diags;
    61     public static Infer instance(Context context) {
    62         Infer instance = context.get(inferKey);
    63         if (instance == null)
    64             instance = new Infer(context);
    65         return instance;
    66     }
    68     protected Infer(Context context) {
    69         context.put(inferKey, this);
    70         syms = Symtab.instance(context);
    71         types = Types.instance(context);
    72         rs = Resolve.instance(context);
    73         chk = Check.instance(context);
    74         diags = JCDiagnostic.Factory.instance(context);
    75         ambiguousNoInstanceException =
    76             new NoInstanceException(true, diags);
    77         unambiguousNoInstanceException =
    78             new NoInstanceException(false, diags);
    79         invalidInstanceException =
    80             new InvalidInstanceException(diags);
    82     }
    84     public static class InferenceException extends Resolve.InapplicableMethodException {
    85         private static final long serialVersionUID = 0;
    87         InferenceException(JCDiagnostic.Factory diags) {
    88             super(diags);
    89         }
    90     }
    92     public static class NoInstanceException extends InferenceException {
    93         private static final long serialVersionUID = 1;
    95         boolean isAmbiguous; // exist several incomparable best instances?
    97         NoInstanceException(boolean isAmbiguous, JCDiagnostic.Factory diags) {
    98             super(diags);
    99             this.isAmbiguous = isAmbiguous;
   100         }
   101     }
   103     public static class InvalidInstanceException extends InferenceException {
   104         private static final long serialVersionUID = 2;
   106         InvalidInstanceException(JCDiagnostic.Factory diags) {
   107             super(diags);
   108         }
   109     }
   111     private final NoInstanceException ambiguousNoInstanceException;
   112     private final NoInstanceException unambiguousNoInstanceException;
   113     private final InvalidInstanceException invalidInstanceException;
   115 /***************************************************************************
   116  * Auxiliary type values and classes
   117  ***************************************************************************/
   119     /** A mapping that turns type variables into undetermined type variables.
   120      */
   121     Mapping fromTypeVarFun = new Mapping("fromTypeVarFun") {
   122             public Type apply(Type t) {
   123                 if (t.tag == TYPEVAR) return new UndetVar(t);
   124                 else return t.map(this);
   125             }
   126         };
   128     /** A mapping that returns its type argument with every UndetVar replaced
   129      *  by its `inst' field. Throws a NoInstanceException
   130      *  if this not possible because an `inst' field is null.
   131      *  Note: mutually referring undertvars will be left uninstantiated
   132      *  (that is, they will be replaced by the underlying type-variable).
   133      */
   135     Mapping getInstFun = new Mapping("getInstFun") {
   136             public Type apply(Type t) {
   137                 switch (t.tag) {
   138                     case UNKNOWN:
   139                         throw ambiguousNoInstanceException
   140                             .setMessage("undetermined.type");
   141                     case UNDETVAR:
   142                         UndetVar that = (UndetVar) t;
   143                         if (that.inst == null)
   144                             throw ambiguousNoInstanceException
   145                                 .setMessage("type.variable.has.undetermined.type",
   146                                             that.qtype);
   147                         return isConstraintCyclic(that) ?
   148                             that.qtype :
   149                             apply(that.inst);
   150                         default:
   151                             return t.map(this);
   152                 }
   153             }
   155             private boolean isConstraintCyclic(UndetVar uv) {
   156                 Types.UnaryVisitor<Boolean> constraintScanner =
   157                         new Types.UnaryVisitor<Boolean>() {
   159                     List<Type> seen = List.nil();
   161                     Boolean visit(List<Type> ts) {
   162                         for (Type t : ts) {
   163                             if (visit(t)) return true;
   164                         }
   165                         return false;
   166                     }
   168                     public Boolean visitType(Type t, Void ignored) {
   169                         return false;
   170                     }
   172                     @Override
   173                     public Boolean visitClassType(ClassType t, Void ignored) {
   174                         if (t.isCompound()) {
   175                             return visit(types.supertype(t)) ||
   176                                     visit(types.interfaces(t));
   177                         } else {
   178                             return visit(t.getTypeArguments());
   179                         }
   180                     }
   181                     @Override
   182                     public Boolean visitWildcardType(WildcardType t, Void ignored) {
   183                         return visit(t.type);
   184                     }
   186                     @Override
   187                     public Boolean visitUndetVar(UndetVar t, Void ignored) {
   188                         if (seen.contains(t)) {
   189                             return true;
   190                         } else {
   191                             seen = seen.prepend(t);
   192                             return visit(t.inst);
   193                         }
   194                     }
   195                 };
   196                 return constraintScanner.visit(uv);
   197             }
   198         };
   200 /***************************************************************************
   201  * Mini/Maximization of UndetVars
   202  ***************************************************************************/
   204     /** Instantiate undetermined type variable to its minimal upper bound.
   205      *  Throw a NoInstanceException if this not possible.
   206      */
   207     void maximizeInst(UndetVar that, Warner warn) throws NoInstanceException {
   208         List<Type> hibounds = Type.filter(that.hibounds, errorFilter);
   209         if (that.inst == null) {
   210             if (hibounds.isEmpty())
   211                 that.inst = syms.objectType;
   212             else if (hibounds.tail.isEmpty())
   213                 that.inst = hibounds.head;
   214             else
   215                 that.inst = types.glb(hibounds);
   216         }
   217         if (that.inst == null ||
   218             that.inst.isErroneous())
   219             throw ambiguousNoInstanceException
   220                 .setMessage("no.unique.maximal.instance.exists",
   221                             that.qtype, hibounds);
   222     }
   223     //where
   224         private boolean isSubClass(Type t, final List<Type> ts) {
   225             t = t.baseType();
   226             if (t.tag == TYPEVAR) {
   227                 List<Type> bounds = types.getBounds((TypeVar)t);
   228                 for (Type s : ts) {
   229                     if (!types.isSameType(t, s.baseType())) {
   230                         for (Type bound : bounds) {
   231                             if (!isSubClass(bound, List.of(s.baseType())))
   232                                 return false;
   233                         }
   234                     }
   235                 }
   236             } else {
   237                 for (Type s : ts) {
   238                     if (!t.tsym.isSubClass(s.baseType().tsym, types))
   239                         return false;
   240                 }
   241             }
   242             return true;
   243         }
   245     private Filter<Type> errorFilter = new Filter<Type>() {
   246         @Override
   247         public boolean accepts(Type t) {
   248             return !t.isErroneous();
   249         }
   250     };
   252     /** Instantiate undetermined type variable to the lub of all its lower bounds.
   253      *  Throw a NoInstanceException if this not possible.
   254      */
   255     void minimizeInst(UndetVar that, Warner warn) throws NoInstanceException {
   256         List<Type> lobounds = Type.filter(that.lobounds, errorFilter);
   257         if (that.inst == null) {
   258             if (lobounds.isEmpty())
   259                 that.inst = syms.botType;
   260             else if (lobounds.tail.isEmpty())
   261                 that.inst = lobounds.head.isPrimitive() ? syms.errType : lobounds.head;
   262             else {
   263                 that.inst = types.lub(lobounds);
   264             }
   265             if (that.inst == null || that.inst.tag == ERROR)
   266                     throw ambiguousNoInstanceException
   267                         .setMessage("no.unique.minimal.instance.exists",
   268                                     that.qtype, lobounds);
   269             // VGJ: sort of inlined maximizeInst() below.  Adding
   270             // bounds can cause lobounds that are above hibounds.
   271             List<Type> hibounds = Type.filter(that.hibounds, errorFilter);
   272             Type hb = null;
   273             if (hibounds.isEmpty())
   274                 hb = syms.objectType;
   275             else if (hibounds.tail.isEmpty())
   276                 hb = hibounds.head;
   277             else
   278                 hb = types.glb(hibounds);
   279             if (hb == null ||
   280                 hb.isErroneous())
   281                 throw ambiguousNoInstanceException
   282                         .setMessage("incompatible.upper.bounds",
   283                                     that.qtype, hibounds);
   284         }
   285     }
   287 /***************************************************************************
   288  * Exported Methods
   289  ***************************************************************************/
   291     /** Try to instantiate expression type `that' to given type `to'.
   292      *  If a maximal instantiation exists which makes this type
   293      *  a subtype of type `to', return the instantiated type.
   294      *  If no instantiation exists, or if several incomparable
   295      *  best instantiations exist throw a NoInstanceException.
   296      */
   297     public Type instantiateExpr(ForAll that,
   298                                 Type to,
   299                                 Warner warn) throws InferenceException {
   300         List<Type> undetvars = Type.map(that.tvars, fromTypeVarFun);
   301         for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) {
   302             UndetVar uv = (UndetVar) l.head;
   303             TypeVar tv = (TypeVar)uv.qtype;
   304             ListBuffer<Type> hibounds = new ListBuffer<Type>();
   305             for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS)) {
   306                 hibounds.append(types.subst(t, that.tvars, undetvars));
   307             }
   309             List<Type> inst = that.getConstraints(tv, ConstraintKind.EQUAL);
   310             if (inst.nonEmpty() && inst.head.tag != BOT) {
   311                 uv.inst = inst.head;
   312             }
   313             uv.hibounds = hibounds.toList();
   314         }
   315         Type qtype1 = types.subst(that.qtype, that.tvars, undetvars);
   316         if (!types.isSubtype(qtype1,
   317                 qtype1.tag == UNDETVAR ? types.boxedTypeOrType(to) : to)) {
   318             throw unambiguousNoInstanceException
   319                 .setMessage("infer.no.conforming.instance.exists",
   320                             that.tvars, that.qtype, to);
   321         }
   322         for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail)
   323             maximizeInst((UndetVar) l.head, warn);
   324         // System.out.println(" = " + qtype1.map(getInstFun));//DEBUG
   326         // check bounds
   327         List<Type> targs = Type.map(undetvars, getInstFun);
   328         if (Type.containsAny(targs, that.tvars)) {
   329             //replace uninferred type-vars
   330             targs = types.subst(targs,
   331                     that.tvars,
   332                     instaniateAsUninferredVars(undetvars, that.tvars));
   333         }
   334         return chk.checkType(warn.pos(), that.inst(targs, types), to);
   335     }
   336     //where
   337     private List<Type> instaniateAsUninferredVars(List<Type> undetvars, List<Type> tvars) {
   338         ListBuffer<Type> new_targs = ListBuffer.lb();
   339         //step 1 - create syntethic captured vars
   340         for (Type t : undetvars) {
   341             UndetVar uv = (UndetVar)t;
   342             Type newArg = new CapturedType(t.tsym.name, t.tsym, uv.inst, syms.botType, null);
   343             new_targs = new_targs.append(newArg);
   344         }
   345         //step 2 - replace synthetic vars in their bounds
   346         for (Type t : new_targs.toList()) {
   347             CapturedType ct = (CapturedType)t;
   348             ct.bound = types.subst(ct.bound, tvars, new_targs.toList());
   349             WildcardType wt = new WildcardType(ct.bound, BoundKind.EXTENDS, syms.boundClass);
   350             ct.wildcard = wt;
   351         }
   352         return new_targs.toList();
   353     }
   355     /** Instantiate method type `mt' by finding instantiations of
   356      *  `tvars' so that method can be applied to `argtypes'.
   357      */
   358     public Type instantiateMethod(final Env<AttrContext> env,
   359                                   List<Type> tvars,
   360                                   MethodType mt,
   361                                   final Symbol msym,
   362                                   final List<Type> argtypes,
   363                                   final boolean allowBoxing,
   364                                   final boolean useVarargs,
   365                                   final Warner warn) throws InferenceException {
   366         //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
   367         List<Type> undetvars = Type.map(tvars, fromTypeVarFun);
   368         List<Type> formals = mt.argtypes;
   369         //need to capture exactly once - otherwise subsequent
   370         //applicability checks might fail
   371         final List<Type> capturedArgs = types.capture(argtypes);
   372         List<Type> actuals = capturedArgs;
   373         List<Type> actualsNoCapture = argtypes;
   374         // instantiate all polymorphic argument types and
   375         // set up lower bounds constraints for undetvars
   376         Type varargsFormal = useVarargs ? formals.last() : null;
   377         if (varargsFormal == null &&
   378                 actuals.size() != formals.size()) {
   379             throw unambiguousNoInstanceException
   380                 .setMessage("infer.arg.length.mismatch");
   381         }
   382         while (actuals.nonEmpty() && formals.head != varargsFormal) {
   383             Type formal = formals.head;
   384             Type actual = actuals.head.baseType();
   385             Type actualNoCapture = actualsNoCapture.head.baseType();
   386             if (actual.tag == FORALL)
   387                 actual = instantiateArg((ForAll)actual, formal, tvars, warn);
   388             Type undetFormal = types.subst(formal, tvars, undetvars);
   389             boolean works = allowBoxing
   390                 ? types.isConvertible(actual, undetFormal, warn)
   391                 : types.isSubtypeUnchecked(actual, undetFormal, warn);
   392             if (!works) {
   393                 throw unambiguousNoInstanceException
   394                     .setMessage("infer.no.conforming.assignment.exists",
   395                                 tvars, actualNoCapture, formal);
   396             }
   397             formals = formals.tail;
   398             actuals = actuals.tail;
   399             actualsNoCapture = actualsNoCapture.tail;
   400         }
   402         if (formals.head != varargsFormal) // not enough args
   403             throw unambiguousNoInstanceException.setMessage("infer.arg.length.mismatch");
   405         // for varargs arguments as well
   406         if (useVarargs) {
   407             Type elemType = types.elemtype(varargsFormal);
   408             Type elemUndet = types.subst(elemType, tvars, undetvars);
   409             while (actuals.nonEmpty()) {
   410                 Type actual = actuals.head.baseType();
   411                 Type actualNoCapture = actualsNoCapture.head.baseType();
   412                 if (actual.tag == FORALL)
   413                     actual = instantiateArg((ForAll)actual, elemType, tvars, warn);
   414                 boolean works = types.isConvertible(actual, elemUndet, warn);
   415                 if (!works) {
   416                     throw unambiguousNoInstanceException
   417                         .setMessage("infer.no.conforming.assignment.exists",
   418                                     tvars, actualNoCapture, elemType);
   419                 }
   420                 actuals = actuals.tail;
   421                 actualsNoCapture = actualsNoCapture.tail;
   422             }
   423         }
   425         // minimize as yet undetermined type variables
   426         for (Type t : undetvars)
   427             minimizeInst((UndetVar) t, warn);
   429         /** Type variables instantiated to bottom */
   430         ListBuffer<Type> restvars = new ListBuffer<Type>();
   432         /** Undet vars instantiated to bottom */
   433         final ListBuffer<Type> restundet = new ListBuffer<Type>();
   435         /** Instantiated types or TypeVars if under-constrained */
   436         ListBuffer<Type> insttypes = new ListBuffer<Type>();
   438         /** Instantiated types or UndetVars if under-constrained */
   439         ListBuffer<Type> undettypes = new ListBuffer<Type>();
   441         for (Type t : undetvars) {
   442             UndetVar uv = (UndetVar)t;
   443             if (uv.inst.tag == BOT) {
   444                 restvars.append(uv.qtype);
   445                 restundet.append(uv);
   446                 insttypes.append(uv.qtype);
   447                 undettypes.append(uv);
   448                 uv.inst = null;
   449             } else {
   450                 insttypes.append(uv.inst);
   451                 undettypes.append(uv.inst);
   452             }
   453         }
   454         checkWithinBounds(tvars, undettypes.toList(), warn);
   456         mt = (MethodType)types.subst(mt, tvars, insttypes.toList());
   458         if (!restvars.isEmpty()) {
   459             // if there are uninstantiated variables,
   460             // quantify result type with them
   461             final List<Type> inferredTypes = insttypes.toList();
   462             final List<Type> all_tvars = tvars; //this is the wrong tvars
   463             return new UninferredMethodType(mt, restvars.toList()) {
   464                 @Override
   465                 List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
   466                     for (Type t : restundet.toList()) {
   467                         UndetVar uv = (UndetVar)t;
   468                         if (uv.qtype == tv) {
   469                             switch (ck) {
   470                                 case EXTENDS: return uv.hibounds.appendList(types.subst(types.getBounds(tv), all_tvars, inferredTypes));
   471                                 case SUPER: return uv.lobounds;
   472                                 case EQUAL: return uv.inst != null ? List.of(uv.inst) : List.<Type>nil();
   473                             }
   474                         }
   475                     }
   476                     return List.nil();
   477                 }
   478                 @Override
   479                 void check(List<Type> inferred, Types types) throws NoInstanceException {
   480                     // check that actuals conform to inferred formals
   481                     checkArgumentsAcceptable(env, capturedArgs, getParameterTypes(), allowBoxing, useVarargs, warn);
   482                     // check that inferred bounds conform to their bounds
   483                     checkWithinBounds(all_tvars,
   484                            types.subst(inferredTypes, tvars, inferred), warn);
   485                     if (useVarargs) {
   486                         chk.checkVararg(env.tree.pos(), getParameterTypes(), msym);
   487                     }
   488             }};
   489         }
   490         else {
   491             // check that actuals conform to inferred formals
   492             checkArgumentsAcceptable(env, capturedArgs, mt.getParameterTypes(), allowBoxing, useVarargs, warn);
   493             // return instantiated version of method type
   494             return mt;
   495         }
   496     }
   497     //where
   499         /**
   500          * A delegated type representing a partially uninferred method type.
   501          * The return type of a partially uninferred method type is a ForAll
   502          * type - when the return type is instantiated (see Infer.instantiateExpr)
   503          * the underlying method type is also updated.
   504          */
   505         static abstract class UninferredMethodType extends DelegatedType {
   507             final List<Type> tvars;
   509             public UninferredMethodType(MethodType mtype, List<Type> tvars) {
   510                 super(METHOD, new MethodType(mtype.argtypes, null, mtype.thrown, mtype.tsym));
   511                 this.tvars = tvars;
   512                 asMethodType().restype = new UninferredReturnType(tvars, mtype.restype);
   513             }
   515             @Override
   516             public MethodType asMethodType() {
   517                 return qtype.asMethodType();
   518             }
   520             @Override
   521             public Type map(Mapping f) {
   522                 return qtype.map(f);
   523             }
   525             void instantiateReturnType(Type restype, List<Type> inferred, Types types) throws NoInstanceException {
   526                 //update method type with newly inferred type-arguments
   527                 qtype = new MethodType(types.subst(getParameterTypes(), tvars, inferred),
   528                                        restype,
   529                                        types.subst(UninferredMethodType.this.getThrownTypes(), tvars, inferred),
   530                                        UninferredMethodType.this.qtype.tsym);
   531                 check(inferred, types);
   532             }
   534             abstract void check(List<Type> inferred, Types types) throws NoInstanceException;
   536             abstract List<Type> getConstraints(TypeVar tv, ConstraintKind ck);
   538             class UninferredReturnType extends ForAll {
   539                 public UninferredReturnType(List<Type> tvars, Type restype) {
   540                     super(tvars, restype);
   541                 }
   542                 @Override
   543                 public Type inst(List<Type> actuals, Types types) {
   544                     Type newRestype = super.inst(actuals, types);
   545                     instantiateReturnType(newRestype, actuals, types);
   546                     return newRestype;
   547                 }
   548                 @Override
   549                 public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
   550                     return UninferredMethodType.this.getConstraints(tv, ck);
   551                 }
   552             }
   553         }
   555         private void checkArgumentsAcceptable(Env<AttrContext> env, List<Type> actuals, List<Type> formals,
   556                 boolean allowBoxing, boolean useVarargs, Warner warn) {
   557             try {
   558                 rs.checkRawArgumentsAcceptable(env, actuals, formals,
   559                        allowBoxing, useVarargs, warn);
   560             }
   561             catch (Resolve.InapplicableMethodException ex) {
   562                 // inferred method is not applicable
   563                 throw invalidInstanceException.setMessage(ex.getDiagnostic());
   564             }
   565         }
   567     /** Try to instantiate argument type `that' to given type `to'.
   568      *  If this fails, try to insantiate `that' to `to' where
   569      *  every occurrence of a type variable in `tvars' is replaced
   570      *  by an unknown type.
   571      */
   572     private Type instantiateArg(ForAll that,
   573                                 Type to,
   574                                 List<Type> tvars,
   575                                 Warner warn) throws InferenceException {
   576         List<Type> targs;
   577         try {
   578             return instantiateExpr(that, to, warn);
   579         } catch (NoInstanceException ex) {
   580             Type to1 = to;
   581             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail)
   582                 to1 = types.subst(to1, List.of(l.head), List.of(syms.unknownType));
   583             return instantiateExpr(that, to1, warn);
   584         }
   585     }
   587     /** check that type parameters are within their bounds.
   588      */
   589     void checkWithinBounds(List<Type> tvars,
   590                                    List<Type> arguments,
   591                                    Warner warn)
   592         throws InvalidInstanceException {
   593         for (List<Type> tvs = tvars, args = arguments;
   594              tvs.nonEmpty();
   595              tvs = tvs.tail, args = args.tail) {
   596             if (args.head instanceof UndetVar ||
   597                     tvars.head.getUpperBound().isErroneous()) continue;
   598             List<Type> bounds = types.subst(types.getBounds((TypeVar)tvs.head), tvars, arguments);
   599             if (!types.isSubtypeUnchecked(args.head, bounds, warn))
   600                 throw invalidInstanceException
   601                     .setMessage("inferred.do.not.conform.to.bounds",
   602                                 args.head, bounds);
   603         }
   604     }
   606     /**
   607      * Compute a synthetic method type corresponding to the requested polymorphic
   608      * method signature. The target return type is computed from the immediately
   609      * enclosing scope surrounding the polymorphic-signature call.
   610      */
   611     Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env, Type site,
   612                                             Name name,
   613                                             MethodSymbol spMethod,  // sig. poly. method or null if none
   614                                             List<Type> argtypes) {
   615         final Type restype;
   617         //The return type for a polymorphic signature call is computed from
   618         //the enclosing tree E, as follows: if E is a cast, then use the
   619         //target type of the cast expression as a return type; if E is an
   620         //expression statement, the return type is 'void' - otherwise the
   621         //return type is simply 'Object'. A correctness check ensures that
   622         //env.next refers to the lexically enclosing environment in which
   623         //the polymorphic signature call environment is nested.
   625         switch (env.next.tree.getTag()) {
   626             case JCTree.TYPECAST:
   627                 JCTypeCast castTree = (JCTypeCast)env.next.tree;
   628                 restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ?
   629                     castTree.clazz.type :
   630                     syms.objectType;
   631                 break;
   632             case JCTree.EXEC:
   633                 JCTree.JCExpressionStatement execTree =
   634                         (JCTree.JCExpressionStatement)env.next.tree;
   635                 restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ?
   636                     syms.voidType :
   637                     syms.objectType;
   638                 break;
   639             default:
   640                 restype = syms.objectType;
   641         }
   643         List<Type> paramtypes = Type.map(argtypes, implicitArgType);
   644         List<Type> exType = spMethod != null ?
   645             spMethod.getThrownTypes() :
   646             List.of(syms.throwableType); // make it throw all exceptions
   648         MethodType mtype = new MethodType(paramtypes,
   649                                           restype,
   650                                           exType,
   651                                           syms.methodClass);
   652         return mtype;
   653     }
   654     //where
   655         Mapping implicitArgType = new Mapping ("implicitArgType") {
   656                 public Type apply(Type t) {
   657                     t = types.erasure(t);
   658                     if (t.tag == BOT)
   659                         // nulls type as the marker type Null (which has no instances)
   660                         // infer as java.lang.Void for now
   661                         t = types.boxedClass(syms.voidType).type;
   662                     return t;
   663                 }
   664         };
   665     }

mercurial