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

Fri, 18 Apr 2014 23:50:41 +0100

author
vromero
date
Fri, 18 Apr 2014 23:50:41 +0100
changeset 2368
0524f786d7e8
parent 2302
f35effa10297
child 2369
77352397867a
permissions
-rw-r--r--

8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 1999, 2014, 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.JCDiagnostic.DiagnosticPosition;
    33 import com.sun.tools.javac.util.List;
    34 import com.sun.tools.javac.code.*;
    35 import com.sun.tools.javac.code.Type.*;
    36 import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
    37 import com.sun.tools.javac.code.Symbol.*;
    38 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
    39 import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph;
    40 import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph.Node;
    41 import com.sun.tools.javac.comp.Resolve.InapplicableMethodException;
    42 import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode;
    43 import com.sun.tools.javac.util.GraphUtils.TarjanNode;
    45 import java.util.ArrayList;
    46 import java.util.Collections;
    47 import java.util.EnumMap;
    48 import java.util.EnumSet;
    49 import java.util.HashMap;
    50 import java.util.HashSet;
    51 import java.util.LinkedHashSet;
    52 import java.util.Map;
    53 import java.util.Set;
    55 import static com.sun.tools.javac.code.TypeTag.*;
    57 /** Helper class for type parameter inference, used by the attribution phase.
    58  *
    59  *  <p><b>This is NOT part of any supported API.
    60  *  If you write code that depends on this, you do so at your own risk.
    61  *  This code and its internal interfaces are subject to change or
    62  *  deletion without notice.</b>
    63  */
    64 public class Infer {
    65     protected static final Context.Key<Infer> inferKey =
    66         new Context.Key<Infer>();
    68     Resolve rs;
    69     Check chk;
    70     Symtab syms;
    71     Types types;
    72     JCDiagnostic.Factory diags;
    73     Log log;
    75     /** should the graph solver be used? */
    76     boolean allowGraphInference;
    78     public static Infer instance(Context context) {
    79         Infer instance = context.get(inferKey);
    80         if (instance == null)
    81             instance = new Infer(context);
    82         return instance;
    83     }
    85     protected Infer(Context context) {
    86         context.put(inferKey, this);
    88         rs = Resolve.instance(context);
    89         chk = Check.instance(context);
    90         syms = Symtab.instance(context);
    91         types = Types.instance(context);
    92         diags = JCDiagnostic.Factory.instance(context);
    93         log = Log.instance(context);
    94         inferenceException = new InferenceException(diags);
    95         Options options = Options.instance(context);
    96         allowGraphInference = Source.instance(context).allowGraphInference()
    97                 && options.isUnset("useLegacyInference");
    98     }
   100     /** A value for prototypes that admit any type, including polymorphic ones. */
   101     public static final Type anyPoly = new JCNoType();
   103    /**
   104     * This exception class is design to store a list of diagnostics corresponding
   105     * to inference errors that can arise during a method applicability check.
   106     */
   107     public static class InferenceException extends InapplicableMethodException {
   108         private static final long serialVersionUID = 0;
   110         List<JCDiagnostic> messages = List.nil();
   112         InferenceException(JCDiagnostic.Factory diags) {
   113             super(diags);
   114         }
   116         @Override
   117         InapplicableMethodException setMessage() {
   118             //no message to set
   119             return this;
   120         }
   122         @Override
   123         InapplicableMethodException setMessage(JCDiagnostic diag) {
   124             messages = messages.append(diag);
   125             return this;
   126         }
   128         @Override
   129         public JCDiagnostic getDiagnostic() {
   130             return messages.head;
   131         }
   133         void clear() {
   134             messages = List.nil();
   135         }
   136     }
   138     protected final InferenceException inferenceException;
   140     // <editor-fold defaultstate="collapsed" desc="Inference routines">
   141     /**
   142      * Main inference entry point - instantiate a generic method type
   143      * using given argument types and (possibly) an expected target-type.
   144      */
   145     public Type instantiateMethod(Env<AttrContext> env,
   146                                   List<Type> tvars,
   147                                   MethodType mt,
   148                                   Attr.ResultInfo resultInfo,
   149                                   Symbol msym,
   150                                   List<Type> argtypes,
   151                                   boolean allowBoxing,
   152                                   boolean useVarargs,
   153                                   Resolve.MethodResolutionContext resolveContext,
   154                                   Warner warn) throws InferenceException {
   155         //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
   156         final InferenceContext inferenceContext = new InferenceContext(tvars);
   157         inferenceException.clear();
   158         try {
   159             DeferredAttr.DeferredAttrContext deferredAttrContext =
   160                         resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn);
   162             resolveContext.methodCheck.argumentsAcceptable(env, deferredAttrContext,
   163                     argtypes, mt.getParameterTypes(), warn);
   165             if (allowGraphInference &&
   166                     resultInfo != null &&
   167                     !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
   168                 //inject return constraints earlier
   169                 checkWithinBounds(inferenceContext, warn); //propagation
   170                 Type newRestype = generateReturnConstraints(resultInfo, mt, inferenceContext);
   171                 mt = (MethodType)types.createMethodTypeWithReturn(mt, newRestype);
   172                 //propagate outwards if needed
   173                 if (resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) {
   174                     //propagate inference context outwards and exit
   175                     inferenceContext.dupTo(resultInfo.checkContext.inferenceContext());
   176                     deferredAttrContext.complete();
   177                     return mt;
   178                 }
   179             }
   181             deferredAttrContext.complete();
   183             // minimize as yet undetermined type variables
   184             if (allowGraphInference) {
   185                 inferenceContext.solve(warn);
   186             } else {
   187                 inferenceContext.solveLegacy(true, warn, LegacyInferenceSteps.EQ_LOWER.steps); //minimizeInst
   188             }
   190             mt = (MethodType)inferenceContext.asInstType(mt);
   192             if (!allowGraphInference &&
   193                     inferenceContext.restvars().nonEmpty() &&
   194                     resultInfo != null &&
   195                     !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
   196                 generateReturnConstraints(resultInfo, mt, inferenceContext);
   197                 inferenceContext.solveLegacy(false, warn, LegacyInferenceSteps.EQ_UPPER.steps); //maximizeInst
   198                 mt = (MethodType)inferenceContext.asInstType(mt);
   199             }
   201             if (resultInfo != null && rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) {
   202                 log.note(env.tree.pos, "deferred.method.inst", msym, mt, resultInfo.pt);
   203             }
   205             // return instantiated version of method type
   206             return mt;
   207         } finally {
   208             if (resultInfo != null || !allowGraphInference) {
   209                 inferenceContext.notifyChange();
   210             } else {
   211                 inferenceContext.notifyChange(inferenceContext.boundedVars());
   212             }
   213         }
   214     }
   216     /**
   217      * Generate constraints from the generic method's return type. If the method
   218      * call occurs in a context where a type T is expected, use the expected
   219      * type to derive more constraints on the generic method inference variables.
   220      */
   221     Type generateReturnConstraints(Attr.ResultInfo resultInfo,
   222             MethodType mt, InferenceContext inferenceContext) {
   223         InferenceContext rsInfoInfContext = resultInfo.checkContext.inferenceContext();
   224         Type from = mt.getReturnType();
   225         if (mt.getReturnType().containsAny(inferenceContext.inferencevars) &&
   226                 rsInfoInfContext != emptyContext) {
   227             from = types.capture(from);
   228             //add synthetic captured ivars
   229             for (Type t : from.getTypeArguments()) {
   230                 if (t.hasTag(TYPEVAR) && ((TypeVar)t).isCaptured()) {
   231                     inferenceContext.addVar((TypeVar)t);
   232                 }
   233             }
   234         }
   235         Type qtype1 = inferenceContext.asUndetVar(from);
   236         Type to = returnConstraintTarget(qtype1, resultInfo.pt);
   237         Assert.check(allowGraphInference || !rsInfoInfContext.free(to),
   238                 "legacy inference engine cannot handle constraints on both sides of a subtyping assertion");
   239         //we need to skip capture?
   240         Warner retWarn = new Warner();
   241         if (!resultInfo.checkContext.compatible(qtype1, rsInfoInfContext.asUndetVar(to), retWarn) ||
   242                 //unchecked conversion is not allowed in source 7 mode
   243                 (!allowGraphInference && retWarn.hasLint(Lint.LintCategory.UNCHECKED))) {
   244             throw inferenceException
   245                     .setMessage("infer.no.conforming.instance.exists",
   246                     inferenceContext.restvars(), mt.getReturnType(), to);
   247         }
   248         return from;
   249     }
   251     Type returnConstraintTarget(Type from, Type to) {
   252         if (from.hasTag(VOID)) {
   253             return syms.voidType;
   254         } else if (to.hasTag(NONE)) {
   255             return from.isPrimitive() ? from : syms.objectType;
   256         } else if (from.hasTag(UNDETVAR) && to.isPrimitive()) {
   257             if (!allowGraphInference) {
   258                 //if legacy, just return boxed type
   259                 return types.boxedClass(to).type;
   260             }
   261             //if graph inference we need to skip conflicting boxed bounds...
   262             UndetVar uv = (UndetVar)from;
   263             for (Type t : uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) {
   264                 Type boundAsPrimitive = types.unboxedType(t);
   265                 if (boundAsPrimitive == null) continue;
   266                 if (types.isConvertible(boundAsPrimitive, to)) {
   267                     //effectively skip return-type constraint generation (compatibility)
   268                     return syms.objectType;
   269                 }
   270             }
   271             return types.boxedClass(to).type;
   272         } else {
   273             return to;
   274         }
   275     }
   277     /**
   278       * Infer cyclic inference variables as described in 15.12.2.8.
   279       */
   280     private void instantiateAsUninferredVars(List<Type> vars, InferenceContext inferenceContext) {
   281         ListBuffer<Type> todo = new ListBuffer<>();
   282         //step 1 - create fresh tvars
   283         for (Type t : vars) {
   284             UndetVar uv = (UndetVar)inferenceContext.asUndetVar(t);
   285             List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER);
   286             if (Type.containsAny(upperBounds, vars)) {
   287                 TypeSymbol fresh_tvar = new TypeVariableSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner);
   288                 fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null);
   289                 todo.append(uv);
   290                 uv.inst = fresh_tvar.type;
   291             } else if (upperBounds.nonEmpty()) {
   292                 uv.inst = types.glb(upperBounds);
   293             } else {
   294                 uv.inst = syms.objectType;
   295             }
   296         }
   297         //step 2 - replace fresh tvars in their bounds
   298         List<Type> formals = vars;
   299         for (Type t : todo) {
   300             UndetVar uv = (UndetVar)t;
   301             TypeVar ct = (TypeVar)uv.inst;
   302             ct.bound = types.glb(inferenceContext.asInstTypes(types.getBounds(ct)));
   303             if (ct.bound.isErroneous()) {
   304                 //report inference error if glb fails
   305                 reportBoundError(uv, BoundErrorKind.BAD_UPPER);
   306             }
   307             formals = formals.tail;
   308         }
   309     }
   311     /**
   312      * Compute a synthetic method type corresponding to the requested polymorphic
   313      * method signature. The target return type is computed from the immediately
   314      * enclosing scope surrounding the polymorphic-signature call.
   315      */
   316     Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env,
   317                                             MethodSymbol spMethod,  // sig. poly. method or null if none
   318                                             Resolve.MethodResolutionContext resolveContext,
   319                                             List<Type> argtypes) {
   320         final Type restype;
   322         //The return type for a polymorphic signature call is computed from
   323         //the enclosing tree E, as follows: if E is a cast, then use the
   324         //target type of the cast expression as a return type; if E is an
   325         //expression statement, the return type is 'void' - otherwise the
   326         //return type is simply 'Object'. A correctness check ensures that
   327         //env.next refers to the lexically enclosing environment in which
   328         //the polymorphic signature call environment is nested.
   330         switch (env.next.tree.getTag()) {
   331             case TYPECAST:
   332                 JCTypeCast castTree = (JCTypeCast)env.next.tree;
   333                 restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ?
   334                     castTree.clazz.type :
   335                     syms.objectType;
   336                 break;
   337             case EXEC:
   338                 JCTree.JCExpressionStatement execTree =
   339                         (JCTree.JCExpressionStatement)env.next.tree;
   340                 restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ?
   341                     syms.voidType :
   342                     syms.objectType;
   343                 break;
   344             default:
   345                 restype = syms.objectType;
   346         }
   348         List<Type> paramtypes = Type.map(argtypes, new ImplicitArgType(spMethod, resolveContext.step));
   349         List<Type> exType = spMethod != null ?
   350             spMethod.getThrownTypes() :
   351             List.of(syms.throwableType); // make it throw all exceptions
   353         MethodType mtype = new MethodType(paramtypes,
   354                                           restype,
   355                                           exType,
   356                                           syms.methodClass);
   357         return mtype;
   358     }
   359     //where
   360         class ImplicitArgType extends DeferredAttr.DeferredTypeMap {
   362             public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) {
   363                 rs.deferredAttr.super(AttrMode.SPECULATIVE, msym, phase);
   364             }
   366             public Type apply(Type t) {
   367                 t = types.erasure(super.apply(t));
   368                 if (t.hasTag(BOT))
   369                     // nulls type as the marker type Null (which has no instances)
   370                     // infer as java.lang.Void for now
   371                     t = types.boxedClass(syms.voidType).type;
   372                 return t;
   373             }
   374         }
   376     /**
   377       * This method is used to infer a suitable target SAM in case the original
   378       * SAM type contains one or more wildcards. An inference process is applied
   379       * so that wildcard bounds, as well as explicit lambda/method ref parameters
   380       * (where applicable) are used to constraint the solution.
   381       */
   382     public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface,
   383             List<Type> paramTypes, Check.CheckContext checkContext) {
   384         if (types.capture(funcInterface) == funcInterface) {
   385             //if capture doesn't change the type then return the target unchanged
   386             //(this means the target contains no wildcards!)
   387             return funcInterface;
   388         } else {
   389             Type formalInterface = funcInterface.tsym.type;
   390             InferenceContext funcInterfaceContext =
   391                     new InferenceContext(funcInterface.tsym.type.getTypeArguments());
   393             Assert.check(paramTypes != null);
   394             //get constraints from explicit params (this is done by
   395             //checking that explicit param types are equal to the ones
   396             //in the functional interface descriptors)
   397             List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes();
   398             if (descParameterTypes.size() != paramTypes.size()) {
   399                 checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda"));
   400                 return types.createErrorType(funcInterface);
   401             }
   402             for (Type p : descParameterTypes) {
   403                 if (!types.isSameType(funcInterfaceContext.asUndetVar(p), paramTypes.head)) {
   404                     checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
   405                     return types.createErrorType(funcInterface);
   406                 }
   407                 paramTypes = paramTypes.tail;
   408             }
   410             try {
   411                 funcInterfaceContext.solve(funcInterfaceContext.boundedVars(), types.noWarnings);
   412             } catch (InferenceException ex) {
   413                 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
   414             }
   416             List<Type> actualTypeargs = funcInterface.getTypeArguments();
   417             for (Type t : funcInterfaceContext.undetvars) {
   418                 UndetVar uv = (UndetVar)t;
   419                 if (uv.inst == null) {
   420                     uv.inst = actualTypeargs.head;
   421                 }
   422                 actualTypeargs = actualTypeargs.tail;
   423             }
   425             Type owntype = funcInterfaceContext.asInstType(formalInterface);
   426             if (!chk.checkValidGenericType(owntype)) {
   427                 //if the inferred functional interface type is not well-formed,
   428                 //or if it's not a subtype of the original target, issue an error
   429                 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
   430             }
   431             return owntype;
   432         }
   433     }
   434     // </editor-fold>
   436     // <editor-fold defaultstate="collapsed" desc="Bound checking">
   437     /**
   438      * Check bounds and perform incorporation
   439      */
   440     void checkWithinBounds(InferenceContext inferenceContext,
   441                              Warner warn) throws InferenceException {
   442         MultiUndetVarListener mlistener = new MultiUndetVarListener(inferenceContext.undetvars);
   443         List<Type> saved_undet = inferenceContext.save();
   444         try {
   445             while (true) {
   446                 mlistener.reset();
   447                 if (!allowGraphInference) {
   448                     //in legacy mode we lack of transitivity, so bound check
   449                     //cannot be run in parallel with other incoprporation rounds
   450                     for (Type t : inferenceContext.undetvars) {
   451                         UndetVar uv = (UndetVar)t;
   452                         IncorporationStep.CHECK_BOUNDS.apply(uv, inferenceContext, warn);
   453                     }
   454                 }
   455                 for (Type t : inferenceContext.undetvars) {
   456                     UndetVar uv = (UndetVar)t;
   457                     //bound incorporation
   458                     EnumSet<IncorporationStep> incorporationSteps = allowGraphInference ?
   459                             incorporationStepsGraph : incorporationStepsLegacy;
   460                     for (IncorporationStep is : incorporationSteps) {
   461                         if (is.accepts(uv, inferenceContext)) {
   462                             is.apply(uv, inferenceContext, warn);
   463                         }
   464                     }
   465                 }
   466                 if (!mlistener.changed || !allowGraphInference) break;
   467             }
   468         }
   469         finally {
   470             mlistener.detach();
   471             if (incorporationCache.size() == MAX_INCORPORATION_STEPS) {
   472                 inferenceContext.rollback(saved_undet);
   473             }
   474             incorporationCache.clear();
   475         }
   476     }
   477     //where
   478         /**
   479          * This listener keeps track of changes on a group of inference variable
   480          * bounds. Note: the listener must be detached (calling corresponding
   481          * method) to make sure that the underlying inference variable is
   482          * left in a clean state.
   483          */
   484         class MultiUndetVarListener implements UndetVar.UndetVarListener {
   486             boolean changed;
   487             List<Type> undetvars;
   489             public MultiUndetVarListener(List<Type> undetvars) {
   490                 this.undetvars = undetvars;
   491                 for (Type t : undetvars) {
   492                     UndetVar uv = (UndetVar)t;
   493                     uv.listener = this;
   494                 }
   495             }
   497             public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
   498                 //avoid non-termination
   499                 if (incorporationCache.size() < MAX_INCORPORATION_STEPS) {
   500                     changed = true;
   501                 }
   502             }
   504             void reset() {
   505                 changed = false;
   506             }
   508             void detach() {
   509                 for (Type t : undetvars) {
   510                     UndetVar uv = (UndetVar)t;
   511                     uv.listener = null;
   512                 }
   513             }
   514         };
   516         /** max number of incorporation rounds */
   517         static final int MAX_INCORPORATION_STEPS = 100;
   519     /**
   520      * This enumeration defines an entry point for doing inference variable
   521      * bound incorporation - it can be used to inject custom incorporation
   522      * logic into the basic bound checking routine
   523      */
   524     enum IncorporationStep {
   525         /**
   526          * Performs basic bound checking - i.e. is the instantiated type for a given
   527          * inference variable compatible with its bounds?
   528          */
   529         CHECK_BOUNDS() {
   530             public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
   531                 Infer infer = inferenceContext.infer();
   532                 uv.substBounds(inferenceContext.inferenceVars(), inferenceContext.instTypes(), infer.types);
   533                 infer.checkCompatibleUpperBounds(uv, inferenceContext);
   534                 if (uv.inst != null) {
   535                     Type inst = uv.inst;
   536                     for (Type u : uv.getBounds(InferenceBound.UPPER)) {
   537                         if (!isSubtype(inst, inferenceContext.asUndetVar(u), warn, infer)) {
   538                             infer.reportBoundError(uv, BoundErrorKind.UPPER);
   539                         }
   540                     }
   541                     for (Type l : uv.getBounds(InferenceBound.LOWER)) {
   542                         if (!isSubtype(inferenceContext.asUndetVar(l), inst, warn, infer)) {
   543                             infer.reportBoundError(uv, BoundErrorKind.LOWER);
   544                         }
   545                     }
   546                     for (Type e : uv.getBounds(InferenceBound.EQ)) {
   547                         if (!isSameType(inst, inferenceContext.asUndetVar(e), infer)) {
   548                             infer.reportBoundError(uv, BoundErrorKind.EQ);
   549                         }
   550                     }
   551                 }
   552             }
   554             @Override
   555             boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
   556                 //applies to all undetvars
   557                 return true;
   558             }
   559         },
   560         /**
   561          * Check consistency of equality constraints. This is a slightly more aggressive
   562          * inference routine that is designed as to maximize compatibility with JDK 7.
   563          * Note: this is not used in graph mode.
   564          */
   565         EQ_CHECK_LEGACY() {
   566             public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
   567                 Infer infer = inferenceContext.infer();
   568                 Type eq = null;
   569                 for (Type e : uv.getBounds(InferenceBound.EQ)) {
   570                     Assert.check(!inferenceContext.free(e));
   571                     if (eq != null && !isSameType(e, eq, infer)) {
   572                         infer.reportBoundError(uv, BoundErrorKind.EQ);
   573                     }
   574                     eq = e;
   575                     for (Type l : uv.getBounds(InferenceBound.LOWER)) {
   576                         Assert.check(!inferenceContext.free(l));
   577                         if (!isSubtype(l, e, warn, infer)) {
   578                             infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
   579                         }
   580                     }
   581                     for (Type u : uv.getBounds(InferenceBound.UPPER)) {
   582                         if (inferenceContext.free(u)) continue;
   583                         if (!isSubtype(e, u, warn, infer)) {
   584                             infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
   585                         }
   586                     }
   587                 }
   588             }
   589         },
   590         /**
   591          * Check consistency of equality constraints.
   592          */
   593         EQ_CHECK() {
   594             public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
   595                 Infer infer = inferenceContext.infer();
   596                 for (Type e : uv.getBounds(InferenceBound.EQ)) {
   597                     if (e.containsAny(inferenceContext.inferenceVars())) continue;
   598                     for (Type u : uv.getBounds(InferenceBound.UPPER)) {
   599                         if (!isSubtype(e, inferenceContext.asUndetVar(u), warn, infer)) {
   600                             infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
   601                         }
   602                     }
   603                     for (Type l : uv.getBounds(InferenceBound.LOWER)) {
   604                         if (!isSubtype(inferenceContext.asUndetVar(l), e, warn, infer)) {
   605                             infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
   606                         }
   607                     }
   608                 }
   609             }
   610         },
   611         /**
   612          * Given a bound set containing {@code alpha <: T} and {@code alpha :> S}
   613          * perform {@code S <: T} (which could lead to new bounds).
   614          */
   615         CROSS_UPPER_LOWER() {
   616             public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
   617                 Infer infer = inferenceContext.infer();
   618                 for (Type b1 : uv.getBounds(InferenceBound.UPPER)) {
   619                     for (Type b2 : uv.getBounds(InferenceBound.LOWER)) {
   620                         isSubtype(inferenceContext.asUndetVar(b2), inferenceContext.asUndetVar(b1), warn , infer);
   621                     }
   622                 }
   623             }
   624         },
   625         /**
   626          * Given a bound set containing {@code alpha <: T} and {@code alpha == S}
   627          * perform {@code S <: T} (which could lead to new bounds).
   628          */
   629         CROSS_UPPER_EQ() {
   630             public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
   631                 Infer infer = inferenceContext.infer();
   632                 for (Type b1 : uv.getBounds(InferenceBound.UPPER)) {
   633                     for (Type b2 : uv.getBounds(InferenceBound.EQ)) {
   634                         isSubtype(inferenceContext.asUndetVar(b2), inferenceContext.asUndetVar(b1), warn, infer);
   635                     }
   636                 }
   637             }
   638         },
   639         /**
   640          * Given a bound set containing {@code alpha :> S} and {@code alpha == T}
   641          * perform {@code S <: T} (which could lead to new bounds).
   642          */
   643         CROSS_EQ_LOWER() {
   644             public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
   645                 Infer infer = inferenceContext.infer();
   646                 for (Type b1 : uv.getBounds(InferenceBound.EQ)) {
   647                     for (Type b2 : uv.getBounds(InferenceBound.LOWER)) {
   648                         isSubtype(inferenceContext.asUndetVar(b2), inferenceContext.asUndetVar(b1), warn, infer);
   649                     }
   650                 }
   651             }
   652         },
   653         /**
   654          * Given a bound set containing {@code alpha == S} and {@code alpha == T}
   655          * perform {@code S == T} (which could lead to new bounds).
   656          */
   657         CROSS_EQ_EQ() {
   658             public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
   659                 Infer infer = inferenceContext.infer();
   660                 for (Type b1 : uv.getBounds(InferenceBound.EQ)) {
   661                     for (Type b2 : uv.getBounds(InferenceBound.EQ)) {
   662                         if (b1 != b2) {
   663                             isSameType(inferenceContext.asUndetVar(b2), inferenceContext.asUndetVar(b1), infer);
   664                         }
   665                     }
   666                 }
   667             }
   668         },
   669         /**
   670          * Given a bound set containing {@code alpha <: beta} propagate lower bounds
   671          * from alpha to beta; also propagate upper bounds from beta to alpha.
   672          */
   673         PROP_UPPER() {
   674             public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
   675                 Infer infer = inferenceContext.infer();
   676                 for (Type b : uv.getBounds(InferenceBound.UPPER)) {
   677                     if (inferenceContext.inferenceVars().contains(b)) {
   678                         UndetVar uv2 = (UndetVar)inferenceContext.asUndetVar(b);
   679                         if (uv2.isCaptured()) continue;
   680                         //alpha <: beta
   681                         //0. set beta :> alpha
   682                         addBound(InferenceBound.LOWER, uv2, inferenceContext.asInstType(uv.qtype), infer);
   683                         //1. copy alpha's lower to beta's
   684                         for (Type l : uv.getBounds(InferenceBound.LOWER)) {
   685                             addBound(InferenceBound.LOWER, uv2, inferenceContext.asInstType(l), infer);
   686                         }
   687                         //2. copy beta's upper to alpha's
   688                         for (Type u : uv2.getBounds(InferenceBound.UPPER)) {
   689                             addBound(InferenceBound.UPPER, uv, inferenceContext.asInstType(u), infer);
   690                         }
   691                     }
   692                 }
   693             }
   694         },
   695         /**
   696          * Given a bound set containing {@code alpha :> beta} propagate lower bounds
   697          * from beta to alpha; also propagate upper bounds from alpha to beta.
   698          */
   699         PROP_LOWER() {
   700             public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
   701                 Infer infer = inferenceContext.infer();
   702                 for (Type b : uv.getBounds(InferenceBound.LOWER)) {
   703                     if (inferenceContext.inferenceVars().contains(b)) {
   704                         UndetVar uv2 = (UndetVar)inferenceContext.asUndetVar(b);
   705                         if (uv2.isCaptured()) continue;
   706                         //alpha :> beta
   707                         //0. set beta <: alpha
   708                         addBound(InferenceBound.UPPER, uv2, inferenceContext.asInstType(uv.qtype), infer);
   709                         //1. copy alpha's upper to beta's
   710                         for (Type u : uv.getBounds(InferenceBound.UPPER)) {
   711                             addBound(InferenceBound.UPPER, uv2, inferenceContext.asInstType(u), infer);
   712                         }
   713                         //2. copy beta's lower to alpha's
   714                         for (Type l : uv2.getBounds(InferenceBound.LOWER)) {
   715                             addBound(InferenceBound.LOWER, uv, inferenceContext.asInstType(l), infer);
   716                         }
   717                     }
   718                 }
   719             }
   720         },
   721         /**
   722          * Given a bound set containing {@code alpha == beta} propagate lower/upper
   723          * bounds from alpha to beta and back.
   724          */
   725         PROP_EQ() {
   726             public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
   727                 Infer infer = inferenceContext.infer();
   728                 for (Type b : uv.getBounds(InferenceBound.EQ)) {
   729                     if (inferenceContext.inferenceVars().contains(b)) {
   730                         UndetVar uv2 = (UndetVar)inferenceContext.asUndetVar(b);
   731                         if (uv2.isCaptured()) continue;
   732                         //alpha == beta
   733                         //0. set beta == alpha
   734                         addBound(InferenceBound.EQ, uv2, inferenceContext.asInstType(uv.qtype), infer);
   735                         //1. copy all alpha's bounds to beta's
   736                         for (InferenceBound ib : InferenceBound.values()) {
   737                             for (Type b2 : uv.getBounds(ib)) {
   738                                 if (b2 != uv2) {
   739                                     addBound(ib, uv2, inferenceContext.asInstType(b2), infer);
   740                                 }
   741                             }
   742                         }
   743                         //2. copy all beta's bounds to alpha's
   744                         for (InferenceBound ib : InferenceBound.values()) {
   745                             for (Type b2 : uv2.getBounds(ib)) {
   746                                 if (b2 != uv) {
   747                                     addBound(ib, uv, inferenceContext.asInstType(b2), infer);
   748                                 }
   749                             }
   750                         }
   751                     }
   752                 }
   753             }
   754         };
   756         abstract void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn);
   758         boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
   759             return !uv.isCaptured();
   760         }
   762         boolean isSubtype(Type s, Type t, Warner warn, Infer infer) {
   763             return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn, infer);
   764         }
   766         boolean isSameType(Type s, Type t, Infer infer) {
   767             return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null, infer);
   768         }
   770         void addBound(InferenceBound ib, UndetVar uv, Type b, Infer infer) {
   771             doIncorporationOp(opFor(ib), uv, b, null, infer);
   772         }
   774         IncorporationBinaryOpKind opFor(InferenceBound boundKind) {
   775             switch (boundKind) {
   776                 case EQ:
   777                     return IncorporationBinaryOpKind.ADD_EQ_BOUND;
   778                 case LOWER:
   779                     return IncorporationBinaryOpKind.ADD_LOWER_BOUND;
   780                 case UPPER:
   781                     return IncorporationBinaryOpKind.ADD_UPPER_BOUND;
   782                 default:
   783                     Assert.error("Can't get here!");
   784                     return null;
   785             }
   786         }
   788         boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn, Infer infer) {
   789             IncorporationBinaryOp newOp = infer.new IncorporationBinaryOp(opKind, op1, op2);
   790             Boolean res = infer.incorporationCache.get(newOp);
   791             if (res == null) {
   792                 infer.incorporationCache.put(newOp, res = newOp.apply(warn));
   793             }
   794             return res;
   795         }
   796     }
   798     /** incorporation steps to be executed when running in legacy mode */
   799     EnumSet<IncorporationStep> incorporationStepsLegacy = EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY);
   801     /** incorporation steps to be executed when running in graph mode */
   802     EnumSet<IncorporationStep> incorporationStepsGraph =
   803             EnumSet.complementOf(EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY));
   805     /**
   806      * Three kinds of basic operation are supported as part of an incorporation step:
   807      * (i) subtype check, (ii) same type check and (iii) bound addition (either
   808      * upper/lower/eq bound).
   809      */
   810     enum IncorporationBinaryOpKind {
   811         IS_SUBTYPE() {
   812             @Override
   813             boolean apply(Type op1, Type op2, Warner warn, Types types) {
   814                 return types.isSubtypeUnchecked(op1, op2, warn);
   815             }
   816         },
   817         IS_SAME_TYPE() {
   818             @Override
   819             boolean apply(Type op1, Type op2, Warner warn, Types types) {
   820                 return types.isSameType(op1, op2);
   821             }
   822         },
   823         ADD_UPPER_BOUND() {
   824             @Override
   825             boolean apply(Type op1, Type op2, Warner warn, Types types) {
   826                 UndetVar uv = (UndetVar)op1;
   827                 uv.addBound(InferenceBound.UPPER, op2, types);
   828                 return true;
   829             }
   830         },
   831         ADD_LOWER_BOUND() {
   832             @Override
   833             boolean apply(Type op1, Type op2, Warner warn, Types types) {
   834                 UndetVar uv = (UndetVar)op1;
   835                 uv.addBound(InferenceBound.LOWER, op2, types);
   836                 return true;
   837             }
   838         },
   839         ADD_EQ_BOUND() {
   840             @Override
   841             boolean apply(Type op1, Type op2, Warner warn, Types types) {
   842                 UndetVar uv = (UndetVar)op1;
   843                 uv.addBound(InferenceBound.EQ, op2, types);
   844                 return true;
   845             }
   846         };
   848         abstract boolean apply(Type op1, Type op2, Warner warn, Types types);
   849     }
   851     /**
   852      * This class encapsulates a basic incorporation operation; incorporation
   853      * operations takes two type operands and a kind. Each operation performed
   854      * during an incorporation round is stored in a cache, so that operations
   855      * are not executed unnecessarily (which would potentially lead to adding
   856      * same bounds over and over).
   857      */
   858     class IncorporationBinaryOp {
   860         IncorporationBinaryOpKind opKind;
   861         Type op1;
   862         Type op2;
   864         IncorporationBinaryOp(IncorporationBinaryOpKind opKind, Type op1, Type op2) {
   865             this.opKind = opKind;
   866             this.op1 = op1;
   867             this.op2 = op2;
   868         }
   870         @Override
   871         public boolean equals(Object o) {
   872             if (!(o instanceof IncorporationBinaryOp)) {
   873                 return false;
   874             } else {
   875                 IncorporationBinaryOp that = (IncorporationBinaryOp)o;
   876                 return opKind == that.opKind &&
   877                         types.isSameType(op1, that.op1, true) &&
   878                         types.isSameType(op2, that.op2, true);
   879             }
   880         }
   882         @Override
   883         public int hashCode() {
   884             int result = opKind.hashCode();
   885             result *= 127;
   886             result += types.hashCode(op1);
   887             result *= 127;
   888             result += types.hashCode(op2);
   889             return result;
   890         }
   892         boolean apply(Warner warn) {
   893             return opKind.apply(op1, op2, warn, types);
   894         }
   895     }
   897     /** an incorporation cache keeps track of all executed incorporation-related operations */
   898     Map<IncorporationBinaryOp, Boolean> incorporationCache =
   899             new HashMap<IncorporationBinaryOp, Boolean>();
   901     /**
   902      * Make sure that the upper bounds we got so far lead to a solvable inference
   903      * variable by making sure that a glb exists.
   904      */
   905     void checkCompatibleUpperBounds(UndetVar uv, InferenceContext inferenceContext) {
   906         List<Type> hibounds =
   907                 Type.filter(uv.getBounds(InferenceBound.UPPER), new BoundFilter(inferenceContext));
   908         Type hb = null;
   909         if (hibounds.isEmpty())
   910             hb = syms.objectType;
   911         else if (hibounds.tail.isEmpty())
   912             hb = hibounds.head;
   913         else
   914             hb = types.glb(hibounds);
   915         if (hb == null || hb.isErroneous())
   916             reportBoundError(uv, BoundErrorKind.BAD_UPPER);
   917     }
   918     //where
   919         protected static class BoundFilter implements Filter<Type> {
   921             InferenceContext inferenceContext;
   923             public BoundFilter(InferenceContext inferenceContext) {
   924                 this.inferenceContext = inferenceContext;
   925             }
   927             @Override
   928             public boolean accepts(Type t) {
   929                 return !t.isErroneous() && !inferenceContext.free(t) &&
   930                         !t.hasTag(BOT);
   931             }
   932         };
   934     /**
   935      * This enumeration defines all possible bound-checking related errors.
   936      */
   937     enum BoundErrorKind {
   938         /**
   939          * The (uninstantiated) inference variable has incompatible upper bounds.
   940          */
   941         BAD_UPPER() {
   942             @Override
   943             InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
   944                 return ex.setMessage("incompatible.upper.bounds", uv.qtype,
   945                         uv.getBounds(InferenceBound.UPPER));
   946             }
   947         },
   948         /**
   949          * An equality constraint is not compatible with an upper bound.
   950          */
   951         BAD_EQ_UPPER() {
   952             @Override
   953             InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
   954                 return ex.setMessage("incompatible.eq.upper.bounds", uv.qtype,
   955                         uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.UPPER));
   956             }
   957         },
   958         /**
   959          * An equality constraint is not compatible with a lower bound.
   960          */
   961         BAD_EQ_LOWER() {
   962             @Override
   963             InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
   964                 return ex.setMessage("incompatible.eq.lower.bounds", uv.qtype,
   965                         uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.LOWER));
   966             }
   967         },
   968         /**
   969          * Instantiated inference variable is not compatible with an upper bound.
   970          */
   971         UPPER() {
   972             @Override
   973             InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
   974                 return ex.setMessage("inferred.do.not.conform.to.upper.bounds", uv.inst,
   975                         uv.getBounds(InferenceBound.UPPER));
   976             }
   977         },
   978         /**
   979          * Instantiated inference variable is not compatible with a lower bound.
   980          */
   981         LOWER() {
   982             @Override
   983             InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
   984                 return ex.setMessage("inferred.do.not.conform.to.lower.bounds", uv.inst,
   985                         uv.getBounds(InferenceBound.LOWER));
   986             }
   987         },
   988         /**
   989          * Instantiated inference variable is not compatible with an equality constraint.
   990          */
   991         EQ() {
   992             @Override
   993             InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
   994                 return ex.setMessage("inferred.do.not.conform.to.eq.bounds", uv.inst,
   995                         uv.getBounds(InferenceBound.EQ));
   996             }
   997         };
   999         abstract InapplicableMethodException setMessage(InferenceException ex, UndetVar uv);
  1002     /**
  1003      * Report a bound-checking error of given kind
  1004      */
  1005     void reportBoundError(UndetVar uv, BoundErrorKind bk) {
  1006         throw bk.setMessage(inferenceException, uv);
  1008     // </editor-fold>
  1010     // <editor-fold defaultstate="collapsed" desc="Inference engine">
  1011     /**
  1012      * Graph inference strategy - act as an input to the inference solver; a strategy is
  1013      * composed of two ingredients: (i) find a node to solve in the inference graph,
  1014      * and (ii) tell th engine when we are done fixing inference variables
  1015      */
  1016     interface GraphStrategy {
  1018         /**
  1019          * A NodeNotFoundException is thrown whenever an inference strategy fails
  1020          * to pick the next node to solve in the inference graph.
  1021          */
  1022         public static class NodeNotFoundException extends RuntimeException {
  1023             private static final long serialVersionUID = 0;
  1025             InferenceGraph graph;
  1027             public NodeNotFoundException(InferenceGraph graph) {
  1028                 this.graph = graph;
  1031         /**
  1032          * Pick the next node (leaf) to solve in the graph
  1033          */
  1034         Node pickNode(InferenceGraph g) throws NodeNotFoundException;
  1035         /**
  1036          * Is this the last step?
  1037          */
  1038         boolean done();
  1041     /**
  1042      * Simple solver strategy class that locates all leaves inside a graph
  1043      * and picks the first leaf as the next node to solve
  1044      */
  1045     abstract class LeafSolver implements GraphStrategy {
  1046         public Node pickNode(InferenceGraph g) {
  1047             if (g.nodes.isEmpty()) {
  1048                 //should not happen
  1049                 throw new NodeNotFoundException(g);
  1050             };
  1051             return g.nodes.get(0);
  1054         boolean isSubtype(Type s, Type t, Warner warn, Infer infer) {
  1055             return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn, infer);
  1058         boolean isSameType(Type s, Type t, Infer infer) {
  1059             return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null, infer);
  1062         void addBound(InferenceBound ib, UndetVar uv, Type b, Infer infer) {
  1063             doIncorporationOp(opFor(ib), uv, b, null, infer);
  1066         IncorporationBinaryOpKind opFor(InferenceBound boundKind) {
  1067             switch (boundKind) {
  1068                 case EQ:
  1069                     return IncorporationBinaryOpKind.ADD_EQ_BOUND;
  1070                 case LOWER:
  1071                     return IncorporationBinaryOpKind.ADD_LOWER_BOUND;
  1072                 case UPPER:
  1073                     return IncorporationBinaryOpKind.ADD_UPPER_BOUND;
  1074                 default:
  1075                     Assert.error("Can't get here!");
  1076                     return null;
  1080         boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn, Infer infer) {
  1081             IncorporationBinaryOp newOp = infer.new IncorporationBinaryOp(opKind, op1, op2);
  1082             Boolean res = infer.incorporationCache.get(newOp);
  1083             if (res == null) {
  1084                 infer.incorporationCache.put(newOp, res = newOp.apply(warn));
  1086             return res;
  1090     /**
  1091      * This solver uses an heuristic to pick the best leaf - the heuristic
  1092      * tries to select the node that has maximal probability to contain one
  1093      * or more inference variables in a given list
  1094      */
  1095     abstract class BestLeafSolver extends LeafSolver {
  1097         /** list of ivars of which at least one must be solved */
  1098         List<Type> varsToSolve;
  1100         BestLeafSolver(List<Type> varsToSolve) {
  1101             this.varsToSolve = varsToSolve;
  1104         /**
  1105          * Computes a path that goes from a given node to the leafs in the graph.
  1106          * Typically this will start from a node containing a variable in
  1107          * {@code varsToSolve}. For any given path, the cost is computed as the total
  1108          * number of type-variables that should be eagerly instantiated across that path.
  1109          */
  1110         Pair<List<Node>, Integer> computeTreeToLeafs(Node n) {
  1111             Pair<List<Node>, Integer> cachedPath = treeCache.get(n);
  1112             if (cachedPath == null) {
  1113                 //cache miss
  1114                 if (n.isLeaf()) {
  1115                     //if leaf, stop
  1116                     cachedPath = new Pair<List<Node>, Integer>(List.of(n), n.data.length());
  1117                 } else {
  1118                     //if non-leaf, proceed recursively
  1119                     Pair<List<Node>, Integer> path = new Pair<List<Node>, Integer>(List.of(n), n.data.length());
  1120                     for (Node n2 : n.getAllDependencies()) {
  1121                         if (n2 == n) continue;
  1122                         Pair<List<Node>, Integer> subpath = computeTreeToLeafs(n2);
  1123                         path = new Pair<List<Node>, Integer>(
  1124                                 path.fst.prependList(subpath.fst),
  1125                                 path.snd + subpath.snd);
  1127                     cachedPath = path;
  1129                 //save results in cache
  1130                 treeCache.put(n, cachedPath);
  1132             return cachedPath;
  1135         /** cache used to avoid redundant computation of tree costs */
  1136         final Map<Node, Pair<List<Node>, Integer>> treeCache =
  1137                 new HashMap<Node, Pair<List<Node>, Integer>>();
  1139         /** constant value used to mark non-existent paths */
  1140         final Pair<List<Node>, Integer> noPath =
  1141                 new Pair<List<Node>, Integer>(null, Integer.MAX_VALUE);
  1143         /**
  1144          * Pick the leaf that minimize cost
  1145          */
  1146         @Override
  1147         public Node pickNode(final InferenceGraph g) {
  1148             treeCache.clear(); //graph changes at every step - cache must be cleared
  1149             Pair<List<Node>, Integer> bestPath = noPath;
  1150             for (Node n : g.nodes) {
  1151                 if (!Collections.disjoint(n.data, varsToSolve)) {
  1152                     Pair<List<Node>, Integer> path = computeTreeToLeafs(n);
  1153                     //discard all paths containing at least a node in the
  1154                     //closure computed above
  1155                     if (path.snd < bestPath.snd) {
  1156                         bestPath = path;
  1160             if (bestPath == noPath) {
  1161                 //no path leads there
  1162                 throw new NodeNotFoundException(g);
  1164             return bestPath.fst.head;
  1168     /**
  1169      * The inference process can be thought of as a sequence of steps. Each step
  1170      * instantiates an inference variable using a subset of the inference variable
  1171      * bounds, if certain condition are met. Decisions such as the sequence in which
  1172      * steps are applied, or which steps are to be applied are left to the inference engine.
  1173      */
  1174     enum InferenceStep {
  1176         /**
  1177          * Instantiate an inference variables using one of its (ground) equality
  1178          * constraints
  1179          */
  1180         EQ(InferenceBound.EQ) {
  1181             @Override
  1182             Type solve(UndetVar uv, InferenceContext inferenceContext) {
  1183                 return filterBounds(uv, inferenceContext).head;
  1185         },
  1186         /**
  1187          * Instantiate an inference variables using its (ground) lower bounds. Such
  1188          * bounds are merged together using lub().
  1189          */
  1190         LOWER(InferenceBound.LOWER) {
  1191             @Override
  1192             Type solve(UndetVar uv, InferenceContext inferenceContext) {
  1193                 Infer infer = inferenceContext.infer();
  1194                 List<Type> lobounds = filterBounds(uv, inferenceContext);
  1195                 //note: lobounds should have at least one element
  1196                 Type owntype = lobounds.tail.tail == null  ? lobounds.head : infer.types.lub(lobounds);
  1197                 if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
  1198                     throw infer.inferenceException
  1199                         .setMessage("no.unique.minimal.instance.exists",
  1200                                     uv.qtype, lobounds);
  1201                 } else {
  1202                     return owntype;
  1205         },
  1206         /**
  1207          * Infer uninstantiated/unbound inference variables occurring in 'throws'
  1208          * clause as RuntimeException
  1209          */
  1210         THROWS(InferenceBound.UPPER) {
  1211             @Override
  1212             public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
  1213                 if ((t.qtype.tsym.flags() & Flags.THROWS) == 0) {
  1214                     //not a throws undet var
  1215                     return false;
  1217                 if (t.getBounds(InferenceBound.EQ, InferenceBound.LOWER, InferenceBound.UPPER)
  1218                             .diff(t.getDeclaredBounds()).nonEmpty()) {
  1219                     //not an unbounded undet var
  1220                     return false;
  1222                 Infer infer = inferenceContext.infer();
  1223                 for (Type db : t.getDeclaredBounds()) {
  1224                     if (t.isInterface()) continue;
  1225                     if (infer.types.asSuper(infer.syms.runtimeExceptionType, db.tsym) != null) {
  1226                         //declared bound is a supertype of RuntimeException
  1227                         return true;
  1230                 //declared bound is more specific then RuntimeException - give up
  1231                 return false;
  1234             @Override
  1235             Type solve(UndetVar uv, InferenceContext inferenceContext) {
  1236                 return inferenceContext.infer().syms.runtimeExceptionType;
  1238         },
  1239         /**
  1240          * Instantiate an inference variables using its (ground) upper bounds. Such
  1241          * bounds are merged together using glb().
  1242          */
  1243         UPPER(InferenceBound.UPPER) {
  1244             @Override
  1245             Type solve(UndetVar uv, InferenceContext inferenceContext) {
  1246                 Infer infer = inferenceContext.infer();
  1247                 List<Type> hibounds = filterBounds(uv, inferenceContext);
  1248                 //note: lobounds should have at least one element
  1249                 Type owntype = hibounds.tail.tail == null  ? hibounds.head : infer.types.glb(hibounds);
  1250                 if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
  1251                     throw infer.inferenceException
  1252                         .setMessage("no.unique.maximal.instance.exists",
  1253                                     uv.qtype, hibounds);
  1254                 } else {
  1255                     return owntype;
  1258         },
  1259         /**
  1260          * Like the former; the only difference is that this step can only be applied
  1261          * if all upper bounds are ground.
  1262          */
  1263         UPPER_LEGACY(InferenceBound.UPPER) {
  1264             @Override
  1265             public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
  1266                 return !inferenceContext.free(t.getBounds(ib)) && !t.isCaptured();
  1269             @Override
  1270             Type solve(UndetVar uv, InferenceContext inferenceContext) {
  1271                 return UPPER.solve(uv, inferenceContext);
  1273         },
  1274         /**
  1275          * Like the former; the only difference is that this step can only be applied
  1276          * if all upper/lower bounds are ground.
  1277          */
  1278         CAPTURED(InferenceBound.UPPER) {
  1279             @Override
  1280             public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
  1281                 return t.isCaptured() &&
  1282                         !inferenceContext.free(t.getBounds(InferenceBound.UPPER, InferenceBound.LOWER));
  1285             @Override
  1286             Type solve(UndetVar uv, InferenceContext inferenceContext) {
  1287                 Infer infer = inferenceContext.infer();
  1288                 Type upper = UPPER.filterBounds(uv, inferenceContext).nonEmpty() ?
  1289                         UPPER.solve(uv, inferenceContext) :
  1290                         infer.syms.objectType;
  1291                 Type lower = LOWER.filterBounds(uv, inferenceContext).nonEmpty() ?
  1292                         LOWER.solve(uv, inferenceContext) :
  1293                         infer.syms.botType;
  1294                 CapturedType prevCaptured = (CapturedType)uv.qtype;
  1295                 return new CapturedType(prevCaptured.tsym.name, prevCaptured.tsym.owner, upper, lower, prevCaptured.wildcard);
  1297         };
  1299         final InferenceBound ib;
  1301         InferenceStep(InferenceBound ib) {
  1302             this.ib = ib;
  1305         /**
  1306          * Find an instantiated type for a given inference variable within
  1307          * a given inference context
  1308          */
  1309         abstract Type solve(UndetVar uv, InferenceContext inferenceContext);
  1311         /**
  1312          * Can the inference variable be instantiated using this step?
  1313          */
  1314         public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
  1315             return filterBounds(t, inferenceContext).nonEmpty() && !t.isCaptured();
  1318         /**
  1319          * Return the subset of ground bounds in a given bound set (i.e. eq/lower/upper)
  1320          */
  1321         List<Type> filterBounds(UndetVar uv, InferenceContext inferenceContext) {
  1322             return Type.filter(uv.getBounds(ib), new BoundFilter(inferenceContext));
  1326     /**
  1327      * This enumeration defines the sequence of steps to be applied when the
  1328      * solver works in legacy mode. The steps in this enumeration reflect
  1329      * the behavior of old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8).
  1330      */
  1331     enum LegacyInferenceSteps {
  1333         EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
  1334         EQ_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.UPPER_LEGACY));
  1336         final EnumSet<InferenceStep> steps;
  1338         LegacyInferenceSteps(EnumSet<InferenceStep> steps) {
  1339             this.steps = steps;
  1343     /**
  1344      * This enumeration defines the sequence of steps to be applied when the
  1345      * graph solver is used. This order is defined so as to maximize compatibility
  1346      * w.r.t. old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8).
  1347      */
  1348     enum GraphInferenceSteps {
  1350         EQ(EnumSet.of(InferenceStep.EQ)),
  1351         EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
  1352         EQ_LOWER_THROWS_UPPER_CAPTURED(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER, InferenceStep.UPPER, InferenceStep.THROWS, InferenceStep.CAPTURED));
  1354         final EnumSet<InferenceStep> steps;
  1356         GraphInferenceSteps(EnumSet<InferenceStep> steps) {
  1357             this.steps = steps;
  1361     /**
  1362      * There are two kinds of dependencies between inference variables. The basic
  1363      * kind of dependency (or bound dependency) arises when a variable mention
  1364      * another variable in one of its bounds. There's also a more subtle kind
  1365      * of dependency that arises when a variable 'might' lead to better constraints
  1366      * on another variable (this is typically the case with variables holding up
  1367      * stuck expressions).
  1368      */
  1369     enum DependencyKind implements GraphUtils.DependencyKind {
  1371         /** bound dependency */
  1372         BOUND("dotted"),
  1373         /** stuck dependency */
  1374         STUCK("dashed");
  1376         final String dotSyle;
  1378         private DependencyKind(String dotSyle) {
  1379             this.dotSyle = dotSyle;
  1382         @Override
  1383         public String getDotStyle() {
  1384             return dotSyle;
  1388     /**
  1389      * This is the graph inference solver - the solver organizes all inference variables in
  1390      * a given inference context by bound dependencies - in the general case, such dependencies
  1391      * would lead to a cyclic directed graph (hence the name); the dependency info is used to build
  1392      * an acyclic graph, where all cyclic variables are bundled together. An inference
  1393      * step corresponds to solving a node in the acyclic graph - this is done by
  1394      * relying on a given strategy (see GraphStrategy).
  1395      */
  1396     class GraphSolver {
  1398         InferenceContext inferenceContext;
  1399         Map<Type, Set<Type>> stuckDeps;
  1400         Warner warn;
  1402         GraphSolver(InferenceContext inferenceContext, Map<Type, Set<Type>> stuckDeps, Warner warn) {
  1403             this.inferenceContext = inferenceContext;
  1404             this.stuckDeps = stuckDeps;
  1405             this.warn = warn;
  1408         /**
  1409          * Solve variables in a given inference context. The amount of variables
  1410          * to be solved, and the way in which the underlying acyclic graph is explored
  1411          * depends on the selected solver strategy.
  1412          */
  1413         void solve(GraphStrategy sstrategy) {
  1414             checkWithinBounds(inferenceContext, warn); //initial propagation of bounds
  1415             InferenceGraph inferenceGraph = new InferenceGraph(stuckDeps);
  1416             while (!sstrategy.done()) {
  1417                 InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph);
  1418                 List<Type> varsToSolve = List.from(nodeToSolve.data);
  1419                 List<Type> saved_undet = inferenceContext.save();
  1420                 try {
  1421                     //repeat until all variables are solved
  1422                     outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) {
  1423                         //for each inference phase
  1424                         for (GraphInferenceSteps step : GraphInferenceSteps.values()) {
  1425                             if (inferenceContext.solveBasic(varsToSolve, step.steps)) {
  1426                                 checkWithinBounds(inferenceContext, warn);
  1427                                 continue outer;
  1430                         //no progress
  1431                         throw inferenceException.setMessage();
  1434                 catch (InferenceException ex) {
  1435                     //did we fail because of interdependent ivars?
  1436                     inferenceContext.rollback(saved_undet);
  1437                     instantiateAsUninferredVars(varsToSolve, inferenceContext);
  1438                     checkWithinBounds(inferenceContext, warn);
  1440                 inferenceGraph.deleteNode(nodeToSolve);
  1444         /**
  1445          * The dependencies between the inference variables that need to be solved
  1446          * form a (possibly cyclic) graph. This class reduces the original dependency graph
  1447          * to an acyclic version, where cyclic nodes are folded into a single 'super node'.
  1448          */
  1449         class InferenceGraph {
  1451             /**
  1452              * This class represents a node in the graph. Each node corresponds
  1453              * to an inference variable and has edges (dependencies) on other
  1454              * nodes. The node defines an entry point that can be used to receive
  1455              * updates on the structure of the graph this node belongs to (used to
  1456              * keep dependencies in sync).
  1457              */
  1458             class Node extends GraphUtils.TarjanNode<ListBuffer<Type>> {
  1460                 /** map listing all dependencies (grouped by kind) */
  1461                 EnumMap<DependencyKind, Set<Node>> deps;
  1463                 Node(Type ivar) {
  1464                     super(ListBuffer.of(ivar));
  1465                     this.deps = new EnumMap<DependencyKind, Set<Node>>(DependencyKind.class);
  1468                 @Override
  1469                 public GraphUtils.DependencyKind[] getSupportedDependencyKinds() {
  1470                     return DependencyKind.values();
  1473                 @Override
  1474                 public String getDependencyName(GraphUtils.Node<ListBuffer<Type>> to, GraphUtils.DependencyKind dk) {
  1475                     if (dk == DependencyKind.STUCK) return "";
  1476                     else {
  1477                         StringBuilder buf = new StringBuilder();
  1478                         String sep = "";
  1479                         for (Type from : data) {
  1480                             UndetVar uv = (UndetVar)inferenceContext.asUndetVar(from);
  1481                             for (Type bound : uv.getBounds(InferenceBound.values())) {
  1482                                 if (bound.containsAny(List.from(to.data))) {
  1483                                     buf.append(sep);
  1484                                     buf.append(bound);
  1485                                     sep = ",";
  1489                         return buf.toString();
  1493                 @Override
  1494                 public Iterable<? extends Node> getAllDependencies() {
  1495                     return getDependencies(DependencyKind.values());
  1498                 @Override
  1499                 public Iterable<? extends TarjanNode<ListBuffer<Type>>> getDependenciesByKind(GraphUtils.DependencyKind dk) {
  1500                     return getDependencies((DependencyKind)dk);
  1503                 /**
  1504                  * Retrieves all dependencies with given kind(s).
  1505                  */
  1506                 protected Set<Node> getDependencies(DependencyKind... depKinds) {
  1507                     Set<Node> buf = new LinkedHashSet<Node>();
  1508                     for (DependencyKind dk : depKinds) {
  1509                         Set<Node> depsByKind = deps.get(dk);
  1510                         if (depsByKind != null) {
  1511                             buf.addAll(depsByKind);
  1514                     return buf;
  1517                 /**
  1518                  * Adds dependency with given kind.
  1519                  */
  1520                 protected void addDependency(DependencyKind dk, Node depToAdd) {
  1521                     Set<Node> depsByKind = deps.get(dk);
  1522                     if (depsByKind == null) {
  1523                         depsByKind = new LinkedHashSet<Node>();
  1524                         deps.put(dk, depsByKind);
  1526                     depsByKind.add(depToAdd);
  1529                 /**
  1530                  * Add multiple dependencies of same given kind.
  1531                  */
  1532                 protected void addDependencies(DependencyKind dk, Set<Node> depsToAdd) {
  1533                     for (Node n : depsToAdd) {
  1534                         addDependency(dk, n);
  1538                 /**
  1539                  * Remove a dependency, regardless of its kind.
  1540                  */
  1541                 protected Set<DependencyKind> removeDependency(Node n) {
  1542                     Set<DependencyKind> removedKinds = new HashSet<>();
  1543                     for (DependencyKind dk : DependencyKind.values()) {
  1544                         Set<Node> depsByKind = deps.get(dk);
  1545                         if (depsByKind == null) continue;
  1546                         if (depsByKind.remove(n)) {
  1547                             removedKinds.add(dk);
  1550                     return removedKinds;
  1553                 /**
  1554                  * Compute closure of a give node, by recursively walking
  1555                  * through all its dependencies (of given kinds)
  1556                  */
  1557                 protected Set<Node> closure(DependencyKind... depKinds) {
  1558                     boolean progress = true;
  1559                     Set<Node> closure = new HashSet<Node>();
  1560                     closure.add(this);
  1561                     while (progress) {
  1562                         progress = false;
  1563                         for (Node n1 : new HashSet<Node>(closure)) {
  1564                             progress = closure.addAll(n1.getDependencies(depKinds));
  1567                     return closure;
  1570                 /**
  1571                  * Is this node a leaf? This means either the node has no dependencies,
  1572                  * or it just has self-dependencies.
  1573                  */
  1574                 protected boolean isLeaf() {
  1575                     //no deps, or only one self dep
  1576                     Set<Node> allDeps = getDependencies(DependencyKind.BOUND, DependencyKind.STUCK);
  1577                     if (allDeps.isEmpty()) return true;
  1578                     for (Node n : allDeps) {
  1579                         if (n != this) {
  1580                             return false;
  1583                     return true;
  1586                 /**
  1587                  * Merge this node with another node, acquiring its dependencies.
  1588                  * This routine is used to merge all cyclic node together and
  1589                  * form an acyclic graph.
  1590                  */
  1591                 protected void mergeWith(List<? extends Node> nodes) {
  1592                     for (Node n : nodes) {
  1593                         Assert.check(n.data.length() == 1, "Attempt to merge a compound node!");
  1594                         data.appendList(n.data);
  1595                         for (DependencyKind dk : DependencyKind.values()) {
  1596                             addDependencies(dk, n.getDependencies(dk));
  1599                     //update deps
  1600                     EnumMap<DependencyKind, Set<Node>> deps2 = new EnumMap<DependencyKind, Set<Node>>(DependencyKind.class);
  1601                     for (DependencyKind dk : DependencyKind.values()) {
  1602                         for (Node d : getDependencies(dk)) {
  1603                             Set<Node> depsByKind = deps2.get(dk);
  1604                             if (depsByKind == null) {
  1605                                 depsByKind = new LinkedHashSet<Node>();
  1606                                 deps2.put(dk, depsByKind);
  1608                             if (data.contains(d.data.first())) {
  1609                                 depsByKind.add(this);
  1610                             } else {
  1611                                 depsByKind.add(d);
  1615                     deps = deps2;
  1618                 /**
  1619                  * Notify all nodes that something has changed in the graph
  1620                  * topology.
  1621                  */
  1622                 private void graphChanged(Node from, Node to) {
  1623                     for (DependencyKind dk : removeDependency(from)) {
  1624                         if (to != null) {
  1625                             addDependency(dk, to);
  1631             /** the nodes in the inference graph */
  1632             ArrayList<Node> nodes;
  1634             InferenceGraph(Map<Type, Set<Type>> optDeps) {
  1635                 initNodes(optDeps);
  1638             /**
  1639              * Basic lookup helper for retrieving a graph node given an inference
  1640              * variable type.
  1641              */
  1642             public Node findNode(Type t) {
  1643                 for (Node n : nodes) {
  1644                     if (n.data.contains(t)) {
  1645                         return n;
  1648                 return null;
  1651             /**
  1652              * Delete a node from the graph. This update the underlying structure
  1653              * of the graph (including dependencies) via listeners updates.
  1654              */
  1655             public void deleteNode(Node n) {
  1656                 Assert.check(nodes.contains(n));
  1657                 nodes.remove(n);
  1658                 notifyUpdate(n, null);
  1661             /**
  1662              * Notify all nodes of a change in the graph. If the target node is
  1663              * {@code null} the source node is assumed to be removed.
  1664              */
  1665             void notifyUpdate(Node from, Node to) {
  1666                 for (Node n : nodes) {
  1667                     n.graphChanged(from, to);
  1671             /**
  1672              * Create the graph nodes. First a simple node is created for every inference
  1673              * variables to be solved. Then Tarjan is used to found all connected components
  1674              * in the graph. For each component containing more than one node, a super node is
  1675              * created, effectively replacing the original cyclic nodes.
  1676              */
  1677             void initNodes(Map<Type, Set<Type>> stuckDeps) {
  1678                 //add nodes
  1679                 nodes = new ArrayList<Node>();
  1680                 for (Type t : inferenceContext.restvars()) {
  1681                     nodes.add(new Node(t));
  1683                 //add dependencies
  1684                 for (Node n_i : nodes) {
  1685                     Type i = n_i.data.first();
  1686                     Set<Type> optDepsByNode = stuckDeps.get(i);
  1687                     for (Node n_j : nodes) {
  1688                         Type j = n_j.data.first();
  1689                         UndetVar uv_i = (UndetVar)inferenceContext.asUndetVar(i);
  1690                         if (Type.containsAny(uv_i.getBounds(InferenceBound.values()), List.of(j))) {
  1691                             //update i's bound dependencies
  1692                             n_i.addDependency(DependencyKind.BOUND, n_j);
  1694                         if (optDepsByNode != null && optDepsByNode.contains(j)) {
  1695                             //update i's stuck dependencies
  1696                             n_i.addDependency(DependencyKind.STUCK, n_j);
  1700                 //merge cyclic nodes
  1701                 ArrayList<Node> acyclicNodes = new ArrayList<Node>();
  1702                 for (List<? extends Node> conSubGraph : GraphUtils.tarjan(nodes)) {
  1703                     if (conSubGraph.length() > 1) {
  1704                         Node root = conSubGraph.head;
  1705                         root.mergeWith(conSubGraph.tail);
  1706                         for (Node n : conSubGraph) {
  1707                             notifyUpdate(n, root);
  1710                     acyclicNodes.add(conSubGraph.head);
  1712                 nodes = acyclicNodes;
  1715             /**
  1716              * Debugging: dot representation of this graph
  1717              */
  1718             String toDot() {
  1719                 StringBuilder buf = new StringBuilder();
  1720                 for (Type t : inferenceContext.undetvars) {
  1721                     UndetVar uv = (UndetVar)t;
  1722                     buf.append(String.format("var %s - upper bounds = %s, lower bounds = %s, eq bounds = %s\\n",
  1723                             uv.qtype, uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER),
  1724                             uv.getBounds(InferenceBound.EQ)));
  1726                 return GraphUtils.toDot(nodes, "inferenceGraph" + hashCode(), buf.toString());
  1730     // </editor-fold>
  1732     // <editor-fold defaultstate="collapsed" desc="Inference context">
  1733     /**
  1734      * Functional interface for defining inference callbacks. Certain actions
  1735      * (i.e. subtyping checks) might need to be redone after all inference variables
  1736      * have been fixed.
  1737      */
  1738     interface FreeTypeListener {
  1739         void typesInferred(InferenceContext inferenceContext);
  1742     /**
  1743      * An inference context keeps track of the set of variables that are free
  1744      * in the current context. It provides utility methods for opening/closing
  1745      * types to their corresponding free/closed forms. It also provide hooks for
  1746      * attaching deferred post-inference action (see PendingCheck). Finally,
  1747      * it can be used as an entry point for performing upper/lower bound inference
  1748      * (see InferenceKind).
  1749      */
  1750      class InferenceContext {
  1752         /** list of inference vars as undet vars */
  1753         List<Type> undetvars;
  1755         /** list of inference vars in this context */
  1756         List<Type> inferencevars;
  1758         java.util.Map<FreeTypeListener, List<Type>> freeTypeListeners =
  1759                 new java.util.HashMap<FreeTypeListener, List<Type>>();
  1761         List<FreeTypeListener> freetypeListeners = List.nil();
  1763         public InferenceContext(List<Type> inferencevars) {
  1764             this.undetvars = Type.map(inferencevars, fromTypeVarFun);
  1765             this.inferencevars = inferencevars;
  1767         //where
  1768             Mapping fromTypeVarFun = new Mapping("fromTypeVarFunWithBounds") {
  1769                 // mapping that turns inference variables into undet vars
  1770                 public Type apply(Type t) {
  1771                     if (t.hasTag(TYPEVAR)) {
  1772                         TypeVar tv = (TypeVar)t;
  1773                         if (tv.isCaptured()) {
  1774                             return new CapturedUndetVar((CapturedType)tv, types);
  1775                         } else {
  1776                             return new UndetVar(tv, types);
  1778                     } else {
  1779                         return t.map(this);
  1782             };
  1784         /**
  1785          * add a new inference var to this inference context
  1786          */
  1787         void addVar(TypeVar t) {
  1788             this.undetvars = this.undetvars.prepend(fromTypeVarFun.apply(t));
  1789             this.inferencevars = this.inferencevars.prepend(t);
  1792         /**
  1793          * returns the list of free variables (as type-variables) in this
  1794          * inference context
  1795          */
  1796         List<Type> inferenceVars() {
  1797             return inferencevars;
  1800         /**
  1801          * returns the list of uninstantiated variables (as type-variables) in this
  1802          * inference context
  1803          */
  1804         List<Type> restvars() {
  1805             return filterVars(new Filter<UndetVar>() {
  1806                 public boolean accepts(UndetVar uv) {
  1807                     return uv.inst == null;
  1809             });
  1812         /**
  1813          * returns the list of instantiated variables (as type-variables) in this
  1814          * inference context
  1815          */
  1816         List<Type> instvars() {
  1817             return filterVars(new Filter<UndetVar>() {
  1818                 public boolean accepts(UndetVar uv) {
  1819                     return uv.inst != null;
  1821             });
  1824         /**
  1825          * Get list of bounded inference variables (where bound is other than
  1826          * declared bounds).
  1827          */
  1828         final List<Type> boundedVars() {
  1829             return filterVars(new Filter<UndetVar>() {
  1830                 public boolean accepts(UndetVar uv) {
  1831                     return uv.getBounds(InferenceBound.UPPER)
  1832                              .diff(uv.getDeclaredBounds())
  1833                              .appendList(uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)).nonEmpty();
  1835             });
  1838         /* Returns the corresponding inference variables.
  1839          */
  1840         private List<Type> filterVars(Filter<UndetVar> fu) {
  1841             ListBuffer<Type> res = new ListBuffer<>();
  1842             for (Type t : undetvars) {
  1843                 UndetVar uv = (UndetVar)t;
  1844                 if (fu.accepts(uv)) {
  1845                     res.append(uv.qtype);
  1848             return res.toList();
  1851         /**
  1852          * is this type free?
  1853          */
  1854         final boolean free(Type t) {
  1855             return t.containsAny(inferencevars);
  1858         final boolean free(List<Type> ts) {
  1859             for (Type t : ts) {
  1860                 if (free(t)) return true;
  1862             return false;
  1865         /**
  1866          * Returns a list of free variables in a given type
  1867          */
  1868         final List<Type> freeVarsIn(Type t) {
  1869             ListBuffer<Type> buf = new ListBuffer<>();
  1870             for (Type iv : inferenceVars()) {
  1871                 if (t.contains(iv)) {
  1872                     buf.add(iv);
  1875             return buf.toList();
  1878         final List<Type> freeVarsIn(List<Type> ts) {
  1879             ListBuffer<Type> buf = new ListBuffer<>();
  1880             for (Type t : ts) {
  1881                 buf.appendList(freeVarsIn(t));
  1883             ListBuffer<Type> buf2 = new ListBuffer<>();
  1884             for (Type t : buf) {
  1885                 if (!buf2.contains(t)) {
  1886                     buf2.add(t);
  1889             return buf2.toList();
  1892         /**
  1893          * Replace all free variables in a given type with corresponding
  1894          * undet vars (used ahead of subtyping/compatibility checks to allow propagation
  1895          * of inference constraints).
  1896          */
  1897         final Type asUndetVar(Type t) {
  1898             return types.subst(t, inferencevars, undetvars);
  1901         final List<Type> asUndetVars(List<Type> ts) {
  1902             ListBuffer<Type> buf = new ListBuffer<>();
  1903             for (Type t : ts) {
  1904                 buf.append(asUndetVar(t));
  1906             return buf.toList();
  1909         List<Type> instTypes() {
  1910             ListBuffer<Type> buf = new ListBuffer<>();
  1911             for (Type t : undetvars) {
  1912                 UndetVar uv = (UndetVar)t;
  1913                 buf.append(uv.inst != null ? uv.inst : uv.qtype);
  1915             return buf.toList();
  1918         /**
  1919          * Replace all free variables in a given type with corresponding
  1920          * instantiated types - if one or more free variable has not been
  1921          * fully instantiated, it will still be available in the resulting type.
  1922          */
  1923         Type asInstType(Type t) {
  1924             return types.subst(t, inferencevars, instTypes());
  1927         List<Type> asInstTypes(List<Type> ts) {
  1928             ListBuffer<Type> buf = new ListBuffer<>();
  1929             for (Type t : ts) {
  1930                 buf.append(asInstType(t));
  1932             return buf.toList();
  1935         /**
  1936          * Add custom hook for performing post-inference action
  1937          */
  1938         void addFreeTypeListener(List<Type> types, FreeTypeListener ftl) {
  1939             freeTypeListeners.put(ftl, freeVarsIn(types));
  1942         /**
  1943          * Mark the inference context as complete and trigger evaluation
  1944          * of all deferred checks.
  1945          */
  1946         void notifyChange() {
  1947             notifyChange(inferencevars.diff(restvars()));
  1950         void notifyChange(List<Type> inferredVars) {
  1951             InferenceException thrownEx = null;
  1952             for (Map.Entry<FreeTypeListener, List<Type>> entry :
  1953                     new HashMap<FreeTypeListener, List<Type>>(freeTypeListeners).entrySet()) {
  1954                 if (!Type.containsAny(entry.getValue(), inferencevars.diff(inferredVars))) {
  1955                     try {
  1956                         entry.getKey().typesInferred(this);
  1957                         freeTypeListeners.remove(entry.getKey());
  1958                     } catch (InferenceException ex) {
  1959                         if (thrownEx == null) {
  1960                             thrownEx = ex;
  1965             //inference exception multiplexing - present any inference exception
  1966             //thrown when processing listeners as a single one
  1967             if (thrownEx != null) {
  1968                 throw thrownEx;
  1972         /**
  1973          * Save the state of this inference context
  1974          */
  1975         List<Type> save() {
  1976             ListBuffer<Type> buf = new ListBuffer<>();
  1977             for (Type t : undetvars) {
  1978                 UndetVar uv = (UndetVar)t;
  1979                 UndetVar uv2 = new UndetVar((TypeVar)uv.qtype, types);
  1980                 for (InferenceBound ib : InferenceBound.values()) {
  1981                     for (Type b : uv.getBounds(ib)) {
  1982                         uv2.addBound(ib, b, types);
  1985                 uv2.inst = uv.inst;
  1986                 buf.add(uv2);
  1988             return buf.toList();
  1991         /**
  1992          * Restore the state of this inference context to the previous known checkpoint
  1993          */
  1994         void rollback(List<Type> saved_undet) {
  1995              Assert.check(saved_undet != null && saved_undet.length() == undetvars.length());
  1996             //restore bounds (note: we need to preserve the old instances)
  1997             for (Type t : undetvars) {
  1998                 UndetVar uv = (UndetVar)t;
  1999                 UndetVar uv_saved = (UndetVar)saved_undet.head;
  2000                 for (InferenceBound ib : InferenceBound.values()) {
  2001                     uv.setBounds(ib, uv_saved.getBounds(ib));
  2003                 uv.inst = uv_saved.inst;
  2004                 saved_undet = saved_undet.tail;
  2008         /**
  2009          * Copy variable in this inference context to the given context
  2010          */
  2011         void dupTo(final InferenceContext that) {
  2012             that.inferencevars = that.inferencevars.appendList(inferencevars);
  2013             that.undetvars = that.undetvars.appendList(undetvars);
  2014             //set up listeners to notify original inference contexts as
  2015             //propagated vars are inferred in new context
  2016             for (Type t : inferencevars) {
  2017                 that.freeTypeListeners.put(new FreeTypeListener() {
  2018                     public void typesInferred(InferenceContext inferenceContext) {
  2019                         InferenceContext.this.notifyChange();
  2021                 }, List.of(t));
  2025         private void solve(GraphStrategy ss, Warner warn) {
  2026             solve(ss, new HashMap<Type, Set<Type>>(), warn);
  2029         /**
  2030          * Solve with given graph strategy.
  2031          */
  2032         private void solve(GraphStrategy ss, Map<Type, Set<Type>> stuckDeps, Warner warn) {
  2033             GraphSolver s = new GraphSolver(this, stuckDeps, warn);
  2034             s.solve(ss);
  2037         /**
  2038          * Solve all variables in this context.
  2039          */
  2040         public void solve(Warner warn) {
  2041             solve(new LeafSolver() {
  2042                 public boolean done() {
  2043                     return restvars().isEmpty();
  2045             }, warn);
  2048         /**
  2049          * Solve all variables in the given list.
  2050          */
  2051         public void solve(final List<Type> vars, Warner warn) {
  2052             solve(new BestLeafSolver(vars) {
  2053                 public boolean done() {
  2054                     return !free(asInstTypes(vars));
  2056             }, warn);
  2059         /**
  2060          * Solve at least one variable in given list.
  2061          */
  2062         public void solveAny(List<Type> varsToSolve, Map<Type, Set<Type>> optDeps, Warner warn) {
  2063             solve(new BestLeafSolver(varsToSolve.intersect(restvars())) {
  2064                 public boolean done() {
  2065                     return instvars().intersect(varsToSolve).nonEmpty();
  2067             }, optDeps, warn);
  2070         /**
  2071          * Apply a set of inference steps
  2072          */
  2073         private boolean solveBasic(EnumSet<InferenceStep> steps) {
  2074             return solveBasic(inferencevars, steps);
  2077         private boolean solveBasic(List<Type> varsToSolve, EnumSet<InferenceStep> steps) {
  2078             boolean changed = false;
  2079             for (Type t : varsToSolve.intersect(restvars())) {
  2080                 UndetVar uv = (UndetVar)asUndetVar(t);
  2081                 for (InferenceStep step : steps) {
  2082                     if (step.accepts(uv, this)) {
  2083                         uv.inst = step.solve(uv, this);
  2084                         changed = true;
  2085                         break;
  2089             return changed;
  2092         /**
  2093          * Instantiate inference variables in legacy mode (JLS 15.12.2.7, 15.12.2.8).
  2094          * During overload resolution, instantiation is done by doing a partial
  2095          * inference process using eq/lower bound instantiation. During check,
  2096          * we also instantiate any remaining vars by repeatedly using eq/upper
  2097          * instantiation, until all variables are solved.
  2098          */
  2099         public void solveLegacy(boolean partial, Warner warn, EnumSet<InferenceStep> steps) {
  2100             while (true) {
  2101                 boolean stuck = !solveBasic(steps);
  2102                 if (restvars().isEmpty() || partial) {
  2103                     //all variables have been instantiated - exit
  2104                     break;
  2105                 } else if (stuck) {
  2106                     //some variables could not be instantiated because of cycles in
  2107                     //upper bounds - provide a (possibly recursive) default instantiation
  2108                     instantiateAsUninferredVars(restvars(), this);
  2109                     break;
  2110                 } else {
  2111                     //some variables have been instantiated - replace newly instantiated
  2112                     //variables in remaining upper bounds and continue
  2113                     for (Type t : undetvars) {
  2114                         UndetVar uv = (UndetVar)t;
  2115                         uv.substBounds(inferenceVars(), instTypes(), types);
  2119             checkWithinBounds(this, warn);
  2122         private Infer infer() {
  2123             //back-door to infer
  2124             return Infer.this;
  2127         @Override
  2128         public String toString() {
  2129             return "Inference vars: " + inferencevars + '\n' +
  2130                    "Undet vars: " + undetvars;
  2134     final InferenceContext emptyContext = new InferenceContext(List.<Type>nil());
  2135     // </editor-fold>

mercurial