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

Thu, 25 Jul 2013 14:47:43 +0100

author
mcimadamore
date
Thu, 25 Jul 2013 14:47:43 +0100
changeset 1919
3155e77d2676
parent 1905
f65a807714ba
child 2000
4a6acc42c3a1
permissions
-rw-r--r--

8020804: javac crashes when speculative attribution infers intersection type with array component
Summary: Assertion is causing javac to crash because of lack of support for arrays in intersection types
Reviewed-by: jjg

duke@1 1 /*
mcimadamore@1562 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.comp;
duke@1 27
mcimadamore@674 28 import com.sun.tools.javac.tree.JCTree;
mcimadamore@674 29 import com.sun.tools.javac.tree.JCTree.JCTypeCast;
mcimadamore@820 30 import com.sun.tools.javac.tree.TreeInfo;
duke@1 31 import com.sun.tools.javac.util.*;
mcimadamore@1562 32 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1 33 import com.sun.tools.javac.util.List;
mcimadamore@1562 34 import com.sun.tools.javac.code.*;
mcimadamore@1562 35 import com.sun.tools.javac.code.Type.*;
mcimadamore@1562 36 import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
mcimadamore@1562 37 import com.sun.tools.javac.code.Symbol.*;
mcimadamore@1562 38 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
mcimadamore@1562 39 import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph;
mcimadamore@1562 40 import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph.Node;
mcimadamore@1562 41 import com.sun.tools.javac.comp.Resolve.InapplicableMethodException;
mcimadamore@1562 42 import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode;
duke@1 43
mcimadamore@1903 44 import java.util.Comparator;
mcimadamore@1337 45 import java.util.HashMap;
mcimadamore@1337 46 import java.util.Map;
mcimadamore@1562 47 import java.util.Set;
mcimadamore@1903 48 import java.util.TreeSet;
mcimadamore@1562 49
mcimadamore@1562 50 import java.util.ArrayList;
mcimadamore@1562 51 import java.util.Collections;
mcimadamore@1562 52 import java.util.EnumSet;
mcimadamore@1562 53 import java.util.HashSet;
mcimadamore@1337 54
jjg@1374 55 import static com.sun.tools.javac.code.TypeTag.*;
duke@1 56
duke@1 57 /** Helper class for type parameter inference, used by the attribution phase.
duke@1 58 *
jjg@581 59 * <p><b>This is NOT part of any supported API.
jjg@581 60 * If you write code that depends on this, you do so at your own risk.
duke@1 61 * This code and its internal interfaces are subject to change or
duke@1 62 * deletion without notice.</b>
duke@1 63 */
duke@1 64 public class Infer {
duke@1 65 protected static final Context.Key<Infer> inferKey =
duke@1 66 new Context.Key<Infer>();
duke@1 67
mcimadamore@1562 68 Resolve rs;
mcimadamore@1562 69 Check chk;
duke@1 70 Symtab syms;
duke@1 71 Types types;
mcimadamore@1562 72 JCDiagnostic.Factory diags;
mcimadamore@1114 73 Log log;
duke@1 74
mcimadamore@1562 75 /** should the graph solver be used? */
mcimadamore@1562 76 boolean allowGraphInference;
mcimadamore@1510 77
duke@1 78 public static Infer instance(Context context) {
duke@1 79 Infer instance = context.get(inferKey);
duke@1 80 if (instance == null)
duke@1 81 instance = new Infer(context);
duke@1 82 return instance;
duke@1 83 }
duke@1 84
duke@1 85 protected Infer(Context context) {
duke@1 86 context.put(inferKey, this);
mcimadamore@1562 87
mcimadamore@1562 88 rs = Resolve.instance(context);
mcimadamore@1562 89 chk = Check.instance(context);
duke@1 90 syms = Symtab.instance(context);
duke@1 91 types = Types.instance(context);
mcimadamore@1562 92 diags = JCDiagnostic.Factory.instance(context);
mcimadamore@1114 93 log = Log.instance(context);
mcimadamore@1298 94 inferenceException = new InferenceException(diags);
mcimadamore@1562 95 Options options = Options.instance(context);
mcimadamore@1562 96 allowGraphInference = Source.instance(context).allowGraphInference()
mcimadamore@1562 97 && options.isUnset("useLegacyInference");
duke@1 98 }
duke@1 99
mcimadamore@1562 100 /** A value for prototypes that admit any type, including polymorphic ones. */
vromero@1853 101 public static final Type anyPoly = new JCNoType();
mcimadamore@1562 102
mcimadamore@1337 103 /**
mcimadamore@1337 104 * This exception class is design to store a list of diagnostics corresponding
mcimadamore@1337 105 * to inference errors that can arise during a method applicability check.
mcimadamore@1337 106 */
mcimadamore@1186 107 public static class InferenceException extends InapplicableMethodException {
duke@1 108 private static final long serialVersionUID = 0;
duke@1 109
mcimadamore@1337 110 List<JCDiagnostic> messages = List.nil();
mcimadamore@1337 111
mcimadamore@299 112 InferenceException(JCDiagnostic.Factory diags) {
mcimadamore@689 113 super(diags);
duke@1 114 }
mcimadamore@1337 115
mcimadamore@1337 116 @Override
mcimadamore@1337 117 InapplicableMethodException setMessage(JCDiagnostic diag) {
mcimadamore@1337 118 messages = messages.append(diag);
mcimadamore@1337 119 return this;
mcimadamore@1337 120 }
mcimadamore@1337 121
mcimadamore@1337 122 @Override
mcimadamore@1337 123 public JCDiagnostic getDiagnostic() {
mcimadamore@1337 124 return messages.head;
mcimadamore@1337 125 }
mcimadamore@1337 126
mcimadamore@1337 127 void clear() {
mcimadamore@1337 128 messages = List.nil();
mcimadamore@1337 129 }
mcimadamore@299 130 }
mcimadamore@299 131
mcimadamore@1562 132 protected final InferenceException inferenceException;
duke@1 133
mcimadamore@1562 134 // <editor-fold defaultstate="collapsed" desc="Inference routines">
mcimadamore@1337 135 /**
mcimadamore@1562 136 * Main inference entry point - instantiate a generic method type
mcimadamore@1562 137 * using given argument types and (possibly) an expected target-type.
duke@1 138 */
mcimadamore@1268 139 public Type instantiateMethod(Env<AttrContext> env,
mcimadamore@547 140 List<Type> tvars,
duke@1 141 MethodType mt,
mcimadamore@1268 142 Attr.ResultInfo resultInfo,
mcimadamore@1268 143 Symbol msym,
mcimadamore@1268 144 List<Type> argtypes,
mcimadamore@1268 145 boolean allowBoxing,
mcimadamore@1268 146 boolean useVarargs,
mcimadamore@1347 147 Resolve.MethodResolutionContext resolveContext,
mcimadamore@1268 148 Warner warn) throws InferenceException {
duke@1 149 //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
mcimadamore@1550 150 final InferenceContext inferenceContext = new InferenceContext(tvars);
mcimadamore@1337 151 inferenceException.clear();
mcimadamore@1562 152 try {
mcimadamore@1562 153 DeferredAttr.DeferredAttrContext deferredAttrContext =
mcimadamore@1897 154 resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn);
mcimadamore@689 155
mcimadamore@1674 156 resolveContext.methodCheck.argumentsAcceptable(env, deferredAttrContext,
mcimadamore@1562 157 argtypes, mt.getParameterTypes(), warn);
mcimadamore@1479 158
mcimadamore@1562 159 if (allowGraphInference &&
mcimadamore@1562 160 resultInfo != null &&
mcimadamore@1510 161 !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
mcimadamore@1562 162 //inject return constraints earlier
mcimadamore@1562 163 checkWithinBounds(inferenceContext, warn); //propagation
mcimadamore@1898 164 Type newRestype = generateReturnConstraints(resultInfo, mt, inferenceContext);
mcimadamore@1898 165 mt = (MethodType)types.createMethodTypeWithReturn(mt, newRestype);
mcimadamore@1562 166 //propagate outwards if needed
mcimadamore@1562 167 if (resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) {
mcimadamore@1562 168 //propagate inference context outwards and exit
mcimadamore@1562 169 inferenceContext.dupTo(resultInfo.checkContext.inferenceContext());
mcimadamore@1562 170 deferredAttrContext.complete();
mcimadamore@1562 171 return mt;
mcimadamore@1562 172 }
mcimadamore@1510 173 }
mcimadamore@1510 174
mcimadamore@1479 175 deferredAttrContext.complete();
duke@1 176
mcimadamore@1337 177 // minimize as yet undetermined type variables
mcimadamore@1562 178 if (allowGraphInference) {
mcimadamore@1562 179 inferenceContext.solve(warn);
mcimadamore@1562 180 } else {
mcimadamore@1562 181 inferenceContext.solveLegacy(true, warn, LegacyInferenceSteps.EQ_LOWER.steps); //minimizeInst
mcimadamore@1337 182 }
duke@1 183
mcimadamore@1550 184 mt = (MethodType)inferenceContext.asInstType(mt);
mcimadamore@396 185
mcimadamore@1562 186 if (!allowGraphInference &&
mcimadamore@1562 187 inferenceContext.restvars().nonEmpty() &&
mcimadamore@1562 188 resultInfo != null &&
mcimadamore@1562 189 !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
mcimadamore@1562 190 generateReturnConstraints(resultInfo, mt, inferenceContext);
mcimadamore@1562 191 inferenceContext.solveLegacy(false, warn, LegacyInferenceSteps.EQ_UPPER.steps); //maximizeInst
mcimadamore@1562 192 mt = (MethodType)inferenceContext.asInstType(mt);
mcimadamore@1562 193 }
duke@1 194
mcimadamore@1562 195 if (resultInfo != null && rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) {
mcimadamore@1562 196 log.note(env.tree.pos, "deferred.method.inst", msym, mt, resultInfo.pt);
mcimadamore@1337 197 }
duke@1 198
mcimadamore@1337 199 // return instantiated version of method type
mcimadamore@1337 200 return mt;
mcimadamore@1337 201 } finally {
mcimadamore@1562 202 if (resultInfo != null || !allowGraphInference) {
mcimadamore@1562 203 inferenceContext.notifyChange();
mcimadamore@1562 204 } else {
mcimadamore@1562 205 inferenceContext.notifyChange(inferenceContext.boundedVars());
mcimadamore@1338 206 }
duke@1 207 }
mcimadamore@895 208 }
duke@1 209
mcimadamore@1562 210 /**
mcimadamore@1562 211 * Generate constraints from the generic method's return type. If the method
mcimadamore@1562 212 * call occurs in a context where a type T is expected, use the expected
mcimadamore@1562 213 * type to derive more constraints on the generic method inference variables.
mcimadamore@1562 214 */
mcimadamore@1898 215 Type generateReturnConstraints(Attr.ResultInfo resultInfo,
mcimadamore@1562 216 MethodType mt, InferenceContext inferenceContext) {
mcimadamore@1898 217 Type from = mt.getReturnType();
mcimadamore@1898 218 if (mt.getReturnType().containsAny(inferenceContext.inferencevars) &&
mcimadamore@1898 219 resultInfo.checkContext.inferenceContext() != emptyContext) {
mcimadamore@1898 220 from = types.capture(from);
mcimadamore@1898 221 //add synthetic captured ivars
mcimadamore@1898 222 for (Type t : from.getTypeArguments()) {
mcimadamore@1898 223 if (t.hasTag(TYPEVAR) && ((TypeVar)t).isCaptured()) {
mcimadamore@1898 224 inferenceContext.addVar((TypeVar)t);
mcimadamore@1898 225 }
mcimadamore@1898 226 }
mcimadamore@1898 227 }
mcimadamore@1898 228 Type qtype1 = inferenceContext.asFree(from);
mcimadamore@1562 229 Type to = returnConstraintTarget(qtype1, resultInfo.pt);
mcimadamore@1562 230 Assert.check(allowGraphInference || !resultInfo.checkContext.inferenceContext().free(to),
mcimadamore@1562 231 "legacy inference engine cannot handle constraints on both sides of a subtyping assertion");
mcimadamore@1562 232 //we need to skip capture?
mcimadamore@1562 233 Warner retWarn = new Warner();
mcimadamore@1562 234 if (!resultInfo.checkContext.compatible(qtype1, resultInfo.checkContext.inferenceContext().asFree(to), retWarn) ||
mcimadamore@1800 235 //unchecked conversion is not allowed in source 7 mode
mcimadamore@1800 236 (!allowGraphInference && retWarn.hasLint(Lint.LintCategory.UNCHECKED))) {
mcimadamore@1562 237 throw inferenceException
mcimadamore@1562 238 .setMessage("infer.no.conforming.instance.exists",
mcimadamore@1562 239 inferenceContext.restvars(), mt.getReturnType(), to);
mcimadamore@1562 240 }
mcimadamore@1898 241 return from;
mcimadamore@1562 242 }
mcimadamore@1897 243
mcimadamore@1897 244 Type returnConstraintTarget(Type from, Type to) {
mcimadamore@1897 245 if (from.hasTag(VOID)) {
mcimadamore@1897 246 return syms.voidType;
mcimadamore@1897 247 } else if (to.hasTag(NONE)) {
mcimadamore@1897 248 return from.isPrimitive() ? from : syms.objectType;
mcimadamore@1897 249 } else if (from.hasTag(UNDETVAR) && to.isPrimitive()) {
mcimadamore@1897 250 if (!allowGraphInference) {
mcimadamore@1897 251 //if legacy, just return boxed type
mcimadamore@1897 252 return types.boxedClass(to).type;
mcimadamore@1897 253 }
mcimadamore@1897 254 //if graph inference we need to skip conflicting boxed bounds...
mcimadamore@1897 255 UndetVar uv = (UndetVar)from;
mcimadamore@1897 256 for (Type t : uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) {
mcimadamore@1897 257 Type boundAsPrimitive = types.unboxedType(t);
mcimadamore@1897 258 if (boundAsPrimitive == null) continue;
mcimadamore@1897 259 if (types.isConvertible(boundAsPrimitive, to)) {
mcimadamore@1897 260 //effectively skip return-type constraint generation (compatibility)
mcimadamore@1897 261 return syms.objectType;
mcimadamore@1562 262 }
mcimadamore@1251 263 }
mcimadamore@1897 264 return types.boxedClass(to).type;
mcimadamore@1897 265 } else {
mcimadamore@1897 266 return to;
duke@1 267 }
mcimadamore@1897 268 }
mcimadamore@1251 269
mcimadamore@1562 270 /**
mcimadamore@1562 271 * Infer cyclic inference variables as described in 15.12.2.8.
mcimadamore@1562 272 */
mcimadamore@1562 273 private void instantiateAsUninferredVars(List<Type> vars, InferenceContext inferenceContext) {
mcimadamore@1562 274 ListBuffer<Type> todo = ListBuffer.lb();
mcimadamore@1562 275 //step 1 - create fresh tvars
mcimadamore@1562 276 for (Type t : vars) {
mcimadamore@1562 277 UndetVar uv = (UndetVar)inferenceContext.asFree(t);
mcimadamore@1562 278 List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER);
mcimadamore@1562 279 if (Type.containsAny(upperBounds, vars)) {
jfranck@1689 280 TypeSymbol fresh_tvar = new TypeVariableSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner);
mcimadamore@1562 281 fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null);
mcimadamore@1562 282 todo.append(uv);
mcimadamore@1562 283 uv.inst = fresh_tvar.type;
mcimadamore@1562 284 } else if (upperBounds.nonEmpty()) {
mcimadamore@1562 285 uv.inst = types.glb(upperBounds);
mcimadamore@1562 286 } else {
mcimadamore@1562 287 uv.inst = syms.objectType;
mcimadamore@1338 288 }
mcimadamore@1562 289 }
mcimadamore@1562 290 //step 2 - replace fresh tvars in their bounds
mcimadamore@1562 291 List<Type> formals = vars;
mcimadamore@1562 292 for (Type t : todo) {
mcimadamore@1562 293 UndetVar uv = (UndetVar)t;
mcimadamore@1562 294 TypeVar ct = (TypeVar)uv.inst;
mcimadamore@1562 295 ct.bound = types.glb(inferenceContext.asInstTypes(types.getBounds(ct)));
mcimadamore@1562 296 if (ct.bound.isErroneous()) {
mcimadamore@1562 297 //report inference error if glb fails
mcimadamore@1562 298 reportBoundError(uv, BoundErrorKind.BAD_UPPER);
mcimadamore@1338 299 }
mcimadamore@1562 300 formals = formals.tail;
mcimadamore@1348 301 }
mcimadamore@1348 302 }
mcimadamore@1348 303
mcimadamore@674 304 /**
mcimadamore@674 305 * Compute a synthetic method type corresponding to the requested polymorphic
mcimadamore@820 306 * method signature. The target return type is computed from the immediately
mcimadamore@820 307 * enclosing scope surrounding the polymorphic-signature call.
mcimadamore@674 308 */
mcimadamore@1239 309 Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env,
mcimadamore@674 310 MethodSymbol spMethod, // sig. poly. method or null if none
mcimadamore@1347 311 Resolve.MethodResolutionContext resolveContext,
mcimadamore@820 312 List<Type> argtypes) {
mcimadamore@674 313 final Type restype;
mcimadamore@716 314
mcimadamore@820 315 //The return type for a polymorphic signature call is computed from
mcimadamore@820 316 //the enclosing tree E, as follows: if E is a cast, then use the
mcimadamore@820 317 //target type of the cast expression as a return type; if E is an
mcimadamore@820 318 //expression statement, the return type is 'void' - otherwise the
mcimadamore@820 319 //return type is simply 'Object'. A correctness check ensures that
mcimadamore@820 320 //env.next refers to the lexically enclosing environment in which
mcimadamore@820 321 //the polymorphic signature call environment is nested.
mcimadamore@820 322
mcimadamore@820 323 switch (env.next.tree.getTag()) {
jjg@1127 324 case TYPECAST:
mcimadamore@820 325 JCTypeCast castTree = (JCTypeCast)env.next.tree;
mcimadamore@820 326 restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ?
mcimadamore@820 327 castTree.clazz.type :
mcimadamore@820 328 syms.objectType;
mcimadamore@820 329 break;
jjg@1127 330 case EXEC:
mcimadamore@820 331 JCTree.JCExpressionStatement execTree =
mcimadamore@820 332 (JCTree.JCExpressionStatement)env.next.tree;
mcimadamore@820 333 restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ?
mcimadamore@820 334 syms.voidType :
mcimadamore@820 335 syms.objectType;
mcimadamore@820 336 break;
mcimadamore@820 337 default:
mcimadamore@820 338 restype = syms.objectType;
mcimadamore@674 339 }
mcimadamore@674 340
mcimadamore@1347 341 List<Type> paramtypes = Type.map(argtypes, new ImplicitArgType(spMethod, resolveContext.step));
mcimadamore@674 342 List<Type> exType = spMethod != null ?
mcimadamore@674 343 spMethod.getThrownTypes() :
mcimadamore@674 344 List.of(syms.throwableType); // make it throw all exceptions
mcimadamore@674 345
mcimadamore@674 346 MethodType mtype = new MethodType(paramtypes,
mcimadamore@674 347 restype,
mcimadamore@674 348 exType,
mcimadamore@674 349 syms.methodClass);
mcimadamore@674 350 return mtype;
mcimadamore@674 351 }
mcimadamore@674 352 //where
mcimadamore@1347 353 class ImplicitArgType extends DeferredAttr.DeferredTypeMap {
mcimadamore@1347 354
mcimadamore@1347 355 public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) {
mcimadamore@1562 356 rs.deferredAttr.super(AttrMode.SPECULATIVE, msym, phase);
mcimadamore@1347 357 }
mcimadamore@1347 358
mcimadamore@1347 359 public Type apply(Type t) {
mcimadamore@1347 360 t = types.erasure(super.apply(t));
jjg@1374 361 if (t.hasTag(BOT))
mcimadamore@1347 362 // nulls type as the marker type Null (which has no instances)
mcimadamore@1347 363 // infer as java.lang.Void for now
mcimadamore@1347 364 t = types.boxedClass(syms.voidType).type;
mcimadamore@1347 365 return t;
mcimadamore@1347 366 }
mcimadamore@1347 367 }
mcimadamore@1337 368
mcimadamore@1337 369 /**
mcimadamore@1562 370 * This method is used to infer a suitable target SAM in case the original
mcimadamore@1562 371 * SAM type contains one or more wildcards. An inference process is applied
mcimadamore@1562 372 * so that wildcard bounds, as well as explicit lambda/method ref parameters
mcimadamore@1562 373 * (where applicable) are used to constraint the solution.
mcimadamore@1562 374 */
mcimadamore@1562 375 public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface,
mcimadamore@1562 376 List<Type> paramTypes, Check.CheckContext checkContext) {
mcimadamore@1562 377 if (types.capture(funcInterface) == funcInterface) {
mcimadamore@1562 378 //if capture doesn't change the type then return the target unchanged
mcimadamore@1562 379 //(this means the target contains no wildcards!)
mcimadamore@1562 380 return funcInterface;
mcimadamore@1562 381 } else {
mcimadamore@1562 382 Type formalInterface = funcInterface.tsym.type;
mcimadamore@1562 383 InferenceContext funcInterfaceContext =
mcimadamore@1562 384 new InferenceContext(funcInterface.tsym.type.getTypeArguments());
mcimadamore@1562 385
mcimadamore@1562 386 Assert.check(paramTypes != null);
mcimadamore@1562 387 //get constraints from explicit params (this is done by
mcimadamore@1562 388 //checking that explicit param types are equal to the ones
mcimadamore@1562 389 //in the functional interface descriptors)
mcimadamore@1562 390 List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes();
mcimadamore@1562 391 if (descParameterTypes.size() != paramTypes.size()) {
mcimadamore@1562 392 checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda"));
mcimadamore@1562 393 return types.createErrorType(funcInterface);
mcimadamore@1562 394 }
mcimadamore@1562 395 for (Type p : descParameterTypes) {
mcimadamore@1562 396 if (!types.isSameType(funcInterfaceContext.asFree(p), paramTypes.head)) {
mcimadamore@1562 397 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
mcimadamore@1562 398 return types.createErrorType(funcInterface);
mcimadamore@1562 399 }
mcimadamore@1562 400 paramTypes = paramTypes.tail;
mcimadamore@1562 401 }
mcimadamore@1562 402
mcimadamore@1562 403 try {
mcimadamore@1562 404 funcInterfaceContext.solve(funcInterfaceContext.boundedVars(), types.noWarnings);
mcimadamore@1562 405 } catch (InferenceException ex) {
mcimadamore@1562 406 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
mcimadamore@1562 407 }
mcimadamore@1562 408
mcimadamore@1562 409 List<Type> actualTypeargs = funcInterface.getTypeArguments();
mcimadamore@1562 410 for (Type t : funcInterfaceContext.undetvars) {
mcimadamore@1562 411 UndetVar uv = (UndetVar)t;
mcimadamore@1562 412 if (uv.inst == null) {
mcimadamore@1562 413 uv.inst = actualTypeargs.head;
mcimadamore@1562 414 }
mcimadamore@1562 415 actualTypeargs = actualTypeargs.tail;
mcimadamore@1562 416 }
mcimadamore@1562 417
mcimadamore@1562 418 Type owntype = funcInterfaceContext.asInstType(formalInterface);
mcimadamore@1562 419 if (!chk.checkValidGenericType(owntype)) {
mcimadamore@1562 420 //if the inferred functional interface type is not well-formed,
mcimadamore@1562 421 //or if it's not a subtype of the original target, issue an error
mcimadamore@1562 422 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
mcimadamore@1562 423 }
mcimadamore@1562 424 return owntype;
mcimadamore@1562 425 }
mcimadamore@1562 426 }
mcimadamore@1562 427 // </editor-fold>
mcimadamore@1562 428
mcimadamore@1562 429 // <editor-fold defaultstate="collapsed" desc="Bound checking">
mcimadamore@1562 430 /**
mcimadamore@1562 431 * Check bounds and perform incorporation
mcimadamore@1562 432 */
mcimadamore@1562 433 void checkWithinBounds(InferenceContext inferenceContext,
mcimadamore@1562 434 Warner warn) throws InferenceException {
mcimadamore@1562 435 MultiUndetVarListener mlistener = new MultiUndetVarListener(inferenceContext.undetvars);
mcimadamore@1891 436 List<Type> saved_undet = inferenceContext.save();
mcimadamore@1562 437 try {
mcimadamore@1562 438 while (true) {
mcimadamore@1562 439 mlistener.reset();
mcimadamore@1562 440 if (!allowGraphInference) {
mcimadamore@1562 441 //in legacy mode we lack of transitivity, so bound check
mcimadamore@1562 442 //cannot be run in parallel with other incoprporation rounds
mcimadamore@1562 443 for (Type t : inferenceContext.undetvars) {
mcimadamore@1562 444 UndetVar uv = (UndetVar)t;
mcimadamore@1562 445 IncorporationStep.CHECK_BOUNDS.apply(uv, inferenceContext, warn);
mcimadamore@1562 446 }
mcimadamore@1562 447 }
mcimadamore@1562 448 for (Type t : inferenceContext.undetvars) {
mcimadamore@1562 449 UndetVar uv = (UndetVar)t;
mcimadamore@1562 450 //bound incorporation
mcimadamore@1562 451 EnumSet<IncorporationStep> incorporationSteps = allowGraphInference ?
mcimadamore@1562 452 incorporationStepsGraph : incorporationStepsLegacy;
mcimadamore@1562 453 for (IncorporationStep is : incorporationSteps) {
mcimadamore@1898 454 if (is.accepts(uv, inferenceContext)) {
mcimadamore@1898 455 is.apply(uv, inferenceContext, warn);
mcimadamore@1898 456 }
mcimadamore@1562 457 }
mcimadamore@1562 458 }
mcimadamore@1562 459 if (!mlistener.changed || !allowGraphInference) break;
mcimadamore@1562 460 }
mcimadamore@1562 461 }
mcimadamore@1562 462 finally {
mcimadamore@1562 463 mlistener.detach();
mcimadamore@1905 464 if (incorporationCache.size() == MAX_INCORPORATION_STEPS) {
mcimadamore@1891 465 inferenceContext.rollback(saved_undet);
mcimadamore@1891 466 }
mcimadamore@1905 467 incorporationCache.clear();
mcimadamore@1562 468 }
mcimadamore@1562 469 }
mcimadamore@1562 470 //where
mcimadamore@1562 471 /**
mcimadamore@1562 472 * This listener keeps track of changes on a group of inference variable
mcimadamore@1562 473 * bounds. Note: the listener must be detached (calling corresponding
mcimadamore@1562 474 * method) to make sure that the underlying inference variable is
mcimadamore@1562 475 * left in a clean state.
mcimadamore@1562 476 */
mcimadamore@1562 477 class MultiUndetVarListener implements UndetVar.UndetVarListener {
mcimadamore@1562 478
mcimadamore@1562 479 boolean changed;
mcimadamore@1562 480 List<Type> undetvars;
mcimadamore@1562 481
mcimadamore@1562 482 public MultiUndetVarListener(List<Type> undetvars) {
mcimadamore@1562 483 this.undetvars = undetvars;
mcimadamore@1562 484 for (Type t : undetvars) {
mcimadamore@1562 485 UndetVar uv = (UndetVar)t;
mcimadamore@1562 486 uv.listener = this;
mcimadamore@1562 487 }
mcimadamore@1562 488 }
mcimadamore@1562 489
mcimadamore@1562 490 public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
mcimadamore@1562 491 //avoid non-termination
mcimadamore@1905 492 if (incorporationCache.size() < MAX_INCORPORATION_STEPS) {
mcimadamore@1562 493 changed = true;
mcimadamore@1562 494 }
mcimadamore@1562 495 }
mcimadamore@1562 496
mcimadamore@1562 497 void reset() {
mcimadamore@1562 498 changed = false;
mcimadamore@1562 499 }
mcimadamore@1562 500
mcimadamore@1562 501 void detach() {
mcimadamore@1562 502 for (Type t : undetvars) {
mcimadamore@1562 503 UndetVar uv = (UndetVar)t;
mcimadamore@1562 504 uv.listener = null;
mcimadamore@1562 505 }
mcimadamore@1562 506 }
mcimadamore@1562 507 };
mcimadamore@1562 508
mcimadamore@1562 509 /** max number of incorporation rounds */
mcimadamore@1562 510 static final int MAX_INCORPORATION_STEPS = 100;
mcimadamore@1562 511
mcimadamore@1562 512 /**
mcimadamore@1562 513 * This enumeration defines an entry point for doing inference variable
mcimadamore@1562 514 * bound incorporation - it can be used to inject custom incorporation
mcimadamore@1562 515 * logic into the basic bound checking routine
mcimadamore@1562 516 */
mcimadamore@1562 517 enum IncorporationStep {
mcimadamore@1562 518 /**
mcimadamore@1562 519 * Performs basic bound checking - i.e. is the instantiated type for a given
mcimadamore@1562 520 * inference variable compatible with its bounds?
mcimadamore@1562 521 */
mcimadamore@1562 522 CHECK_BOUNDS() {
mcimadamore@1562 523 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 524 Infer infer = inferenceContext.infer();
mcimadamore@1562 525 uv.substBounds(inferenceContext.inferenceVars(), inferenceContext.instTypes(), infer.types);
mcimadamore@1562 526 infer.checkCompatibleUpperBounds(uv, inferenceContext);
mcimadamore@1562 527 if (uv.inst != null) {
mcimadamore@1562 528 Type inst = uv.inst;
mcimadamore@1562 529 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1905 530 if (!isSubtype(inst, inferenceContext.asFree(u), warn, infer)) {
mcimadamore@1562 531 infer.reportBoundError(uv, BoundErrorKind.UPPER);
mcimadamore@1562 532 }
mcimadamore@1562 533 }
mcimadamore@1562 534 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1905 535 if (!isSubtype(inferenceContext.asFree(l), inst, warn, infer)) {
mcimadamore@1562 536 infer.reportBoundError(uv, BoundErrorKind.LOWER);
mcimadamore@1562 537 }
mcimadamore@1562 538 }
mcimadamore@1562 539 for (Type e : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1905 540 if (!isSameType(inst, inferenceContext.asFree(e), infer)) {
mcimadamore@1562 541 infer.reportBoundError(uv, BoundErrorKind.EQ);
mcimadamore@1562 542 }
mcimadamore@1562 543 }
mcimadamore@1562 544 }
mcimadamore@1562 545 }
mcimadamore@1898 546 @Override
mcimadamore@1898 547 boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1898 548 //applies to all undetvars
mcimadamore@1898 549 return true;
mcimadamore@1898 550 }
mcimadamore@1562 551 },
mcimadamore@1562 552 /**
mcimadamore@1562 553 * Check consistency of equality constraints. This is a slightly more aggressive
mcimadamore@1562 554 * inference routine that is designed as to maximize compatibility with JDK 7.
mcimadamore@1562 555 * Note: this is not used in graph mode.
mcimadamore@1562 556 */
mcimadamore@1562 557 EQ_CHECK_LEGACY() {
mcimadamore@1562 558 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 559 Infer infer = inferenceContext.infer();
mcimadamore@1562 560 Type eq = null;
mcimadamore@1562 561 for (Type e : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1562 562 Assert.check(!inferenceContext.free(e));
mcimadamore@1905 563 if (eq != null && !isSameType(e, eq, infer)) {
mcimadamore@1562 564 infer.reportBoundError(uv, BoundErrorKind.EQ);
mcimadamore@1562 565 }
mcimadamore@1562 566 eq = e;
mcimadamore@1562 567 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1562 568 Assert.check(!inferenceContext.free(l));
mcimadamore@1905 569 if (!isSubtype(l, e, warn, infer)) {
mcimadamore@1562 570 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
mcimadamore@1562 571 }
mcimadamore@1562 572 }
mcimadamore@1562 573 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1562 574 if (inferenceContext.free(u)) continue;
mcimadamore@1905 575 if (!isSubtype(e, u, warn, infer)) {
mcimadamore@1562 576 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
mcimadamore@1562 577 }
mcimadamore@1562 578 }
mcimadamore@1562 579 }
mcimadamore@1562 580 }
mcimadamore@1562 581 },
mcimadamore@1562 582 /**
mcimadamore@1562 583 * Check consistency of equality constraints.
mcimadamore@1562 584 */
mcimadamore@1562 585 EQ_CHECK() {
mcimadamore@1562 586 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 587 Infer infer = inferenceContext.infer();
mcimadamore@1562 588 for (Type e : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1562 589 if (e.containsAny(inferenceContext.inferenceVars())) continue;
mcimadamore@1562 590 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1905 591 if (!isSubtype(e, inferenceContext.asFree(u), warn, infer)) {
mcimadamore@1562 592 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
mcimadamore@1562 593 }
mcimadamore@1562 594 }
mcimadamore@1562 595 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1905 596 if (!isSubtype(inferenceContext.asFree(l), e, warn, infer)) {
mcimadamore@1562 597 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
mcimadamore@1562 598 }
mcimadamore@1562 599 }
mcimadamore@1562 600 }
mcimadamore@1562 601 }
mcimadamore@1562 602 },
mcimadamore@1562 603 /**
mcimadamore@1562 604 * Given a bound set containing {@code alpha <: T} and {@code alpha :> S}
mcimadamore@1562 605 * perform {@code S <: T} (which could lead to new bounds).
mcimadamore@1562 606 */
mcimadamore@1562 607 CROSS_UPPER_LOWER() {
mcimadamore@1562 608 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 609 Infer infer = inferenceContext.infer();
mcimadamore@1562 610 for (Type b1 : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1562 611 for (Type b2 : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1905 612 isSubtype(inferenceContext.asFree(b2), inferenceContext.asFree(b1), warn , infer);
mcimadamore@1562 613 }
mcimadamore@1562 614 }
mcimadamore@1562 615 }
mcimadamore@1562 616 },
mcimadamore@1562 617 /**
mcimadamore@1562 618 * Given a bound set containing {@code alpha <: T} and {@code alpha == S}
mcimadamore@1562 619 * perform {@code S <: T} (which could lead to new bounds).
mcimadamore@1562 620 */
mcimadamore@1562 621 CROSS_UPPER_EQ() {
mcimadamore@1562 622 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 623 Infer infer = inferenceContext.infer();
mcimadamore@1562 624 for (Type b1 : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1562 625 for (Type b2 : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1905 626 isSubtype(inferenceContext.asFree(b2), inferenceContext.asFree(b1), warn, infer);
mcimadamore@1562 627 }
mcimadamore@1562 628 }
mcimadamore@1562 629 }
mcimadamore@1562 630 },
mcimadamore@1562 631 /**
mcimadamore@1562 632 * Given a bound set containing {@code alpha :> S} and {@code alpha == T}
mcimadamore@1562 633 * perform {@code S <: T} (which could lead to new bounds).
mcimadamore@1562 634 */
mcimadamore@1562 635 CROSS_EQ_LOWER() {
mcimadamore@1562 636 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 637 Infer infer = inferenceContext.infer();
mcimadamore@1562 638 for (Type b1 : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1562 639 for (Type b2 : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1905 640 isSubtype(inferenceContext.asFree(b2), inferenceContext.asFree(b1), warn, infer);
mcimadamore@1655 641 }
mcimadamore@1655 642 }
mcimadamore@1655 643 }
mcimadamore@1655 644 },
mcimadamore@1655 645 /**
mcimadamore@1655 646 * Given a bound set containing {@code alpha == S} and {@code alpha == T}
mcimadamore@1655 647 * perform {@code S == T} (which could lead to new bounds).
mcimadamore@1655 648 */
mcimadamore@1655 649 CROSS_EQ_EQ() {
mcimadamore@1655 650 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1655 651 Infer infer = inferenceContext.infer();
mcimadamore@1655 652 for (Type b1 : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1655 653 for (Type b2 : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1655 654 if (b1 != b2) {
mcimadamore@1905 655 isSameType(inferenceContext.asFree(b2), inferenceContext.asFree(b1), infer);
mcimadamore@1562 656 }
mcimadamore@1562 657 }
mcimadamore@1562 658 }
mcimadamore@1562 659 }
mcimadamore@1562 660 },
mcimadamore@1562 661 /**
mcimadamore@1562 662 * Given a bound set containing {@code alpha <: beta} propagate lower bounds
mcimadamore@1562 663 * from alpha to beta; also propagate upper bounds from beta to alpha.
mcimadamore@1562 664 */
mcimadamore@1562 665 PROP_UPPER() {
mcimadamore@1562 666 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 667 Infer infer = inferenceContext.infer();
mcimadamore@1562 668 for (Type b : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1562 669 if (inferenceContext.inferenceVars().contains(b)) {
mcimadamore@1562 670 UndetVar uv2 = (UndetVar)inferenceContext.asFree(b);
mcimadamore@1898 671 if (uv2.isCaptured()) continue;
mcimadamore@1562 672 //alpha <: beta
mcimadamore@1628 673 //0. set beta :> alpha
mcimadamore@1905 674 addBound(InferenceBound.LOWER, uv2, inferenceContext.asInstType(uv.qtype), infer);
mcimadamore@1562 675 //1. copy alpha's lower to beta's
mcimadamore@1562 676 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1905 677 addBound(InferenceBound.LOWER, uv2, inferenceContext.asInstType(l), infer);
mcimadamore@1562 678 }
mcimadamore@1562 679 //2. copy beta's upper to alpha's
mcimadamore@1562 680 for (Type u : uv2.getBounds(InferenceBound.UPPER)) {
mcimadamore@1905 681 addBound(InferenceBound.UPPER, uv, inferenceContext.asInstType(u), infer);
mcimadamore@1562 682 }
mcimadamore@1562 683 }
mcimadamore@1562 684 }
mcimadamore@1562 685 }
mcimadamore@1562 686 },
mcimadamore@1562 687 /**
mcimadamore@1562 688 * Given a bound set containing {@code alpha :> beta} propagate lower bounds
mcimadamore@1562 689 * from beta to alpha; also propagate upper bounds from alpha to beta.
mcimadamore@1562 690 */
mcimadamore@1562 691 PROP_LOWER() {
mcimadamore@1562 692 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 693 Infer infer = inferenceContext.infer();
mcimadamore@1562 694 for (Type b : uv.getBounds(InferenceBound.LOWER)) {
mcimadamore@1562 695 if (inferenceContext.inferenceVars().contains(b)) {
mcimadamore@1562 696 UndetVar uv2 = (UndetVar)inferenceContext.asFree(b);
mcimadamore@1898 697 if (uv2.isCaptured()) continue;
mcimadamore@1562 698 //alpha :> beta
mcimadamore@1628 699 //0. set beta <: alpha
mcimadamore@1905 700 addBound(InferenceBound.UPPER, uv2, inferenceContext.asInstType(uv.qtype), infer);
mcimadamore@1562 701 //1. copy alpha's upper to beta's
mcimadamore@1562 702 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
mcimadamore@1905 703 addBound(InferenceBound.UPPER, uv2, inferenceContext.asInstType(u), infer);
mcimadamore@1562 704 }
mcimadamore@1562 705 //2. copy beta's lower to alpha's
mcimadamore@1562 706 for (Type l : uv2.getBounds(InferenceBound.LOWER)) {
mcimadamore@1905 707 addBound(InferenceBound.LOWER, uv, inferenceContext.asInstType(l), infer);
mcimadamore@1562 708 }
mcimadamore@1562 709 }
mcimadamore@1562 710 }
mcimadamore@1562 711 }
mcimadamore@1562 712 },
mcimadamore@1562 713 /**
mcimadamore@1562 714 * Given a bound set containing {@code alpha == beta} propagate lower/upper
mcimadamore@1562 715 * bounds from alpha to beta and back.
mcimadamore@1562 716 */
mcimadamore@1562 717 PROP_EQ() {
mcimadamore@1562 718 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 719 Infer infer = inferenceContext.infer();
mcimadamore@1562 720 for (Type b : uv.getBounds(InferenceBound.EQ)) {
mcimadamore@1562 721 if (inferenceContext.inferenceVars().contains(b)) {
mcimadamore@1562 722 UndetVar uv2 = (UndetVar)inferenceContext.asFree(b);
mcimadamore@1898 723 if (uv2.isCaptured()) continue;
mcimadamore@1562 724 //alpha == beta
mcimadamore@1628 725 //0. set beta == alpha
mcimadamore@1905 726 addBound(InferenceBound.EQ, uv2, inferenceContext.asInstType(uv.qtype), infer);
mcimadamore@1562 727 //1. copy all alpha's bounds to beta's
mcimadamore@1562 728 for (InferenceBound ib : InferenceBound.values()) {
mcimadamore@1562 729 for (Type b2 : uv.getBounds(ib)) {
mcimadamore@1562 730 if (b2 != uv2) {
mcimadamore@1905 731 addBound(ib, uv2, inferenceContext.asInstType(b2), infer);
mcimadamore@1562 732 }
mcimadamore@1562 733 }
mcimadamore@1562 734 }
mcimadamore@1562 735 //2. copy all beta's bounds to alpha's
mcimadamore@1562 736 for (InferenceBound ib : InferenceBound.values()) {
mcimadamore@1562 737 for (Type b2 : uv2.getBounds(ib)) {
mcimadamore@1562 738 if (b2 != uv) {
mcimadamore@1905 739 addBound(ib, uv, inferenceContext.asInstType(b2), infer);
mcimadamore@1562 740 }
mcimadamore@1562 741 }
mcimadamore@1562 742 }
mcimadamore@1562 743 }
mcimadamore@1562 744 }
mcimadamore@1562 745 }
mcimadamore@1562 746 };
mcimadamore@1562 747
mcimadamore@1562 748 abstract void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn);
mcimadamore@1898 749
mcimadamore@1898 750 boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1898 751 return !uv.isCaptured();
mcimadamore@1898 752 }
mcimadamore@1905 753
mcimadamore@1905 754 boolean isSubtype(Type s, Type t, Warner warn, Infer infer) {
mcimadamore@1905 755 return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn, infer);
mcimadamore@1905 756 }
mcimadamore@1905 757
mcimadamore@1905 758 boolean isSameType(Type s, Type t, Infer infer) {
mcimadamore@1905 759 return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null, infer);
mcimadamore@1905 760 }
mcimadamore@1905 761
mcimadamore@1905 762 void addBound(InferenceBound ib, UndetVar uv, Type b, Infer infer) {
mcimadamore@1905 763 doIncorporationOp(opFor(ib), uv, b, null, infer);
mcimadamore@1905 764 }
mcimadamore@1905 765
mcimadamore@1905 766 IncorporationBinaryOpKind opFor(InferenceBound boundKind) {
mcimadamore@1905 767 switch (boundKind) {
mcimadamore@1905 768 case EQ:
mcimadamore@1905 769 return IncorporationBinaryOpKind.ADD_EQ_BOUND;
mcimadamore@1905 770 case LOWER:
mcimadamore@1905 771 return IncorporationBinaryOpKind.ADD_LOWER_BOUND;
mcimadamore@1905 772 case UPPER:
mcimadamore@1905 773 return IncorporationBinaryOpKind.ADD_UPPER_BOUND;
mcimadamore@1905 774 default:
mcimadamore@1905 775 Assert.error("Can't get here!");
mcimadamore@1905 776 return null;
mcimadamore@1905 777 }
mcimadamore@1905 778 }
mcimadamore@1905 779
mcimadamore@1905 780 boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn, Infer infer) {
mcimadamore@1905 781 IncorporationBinaryOp newOp = infer.new IncorporationBinaryOp(opKind, op1, op2);
mcimadamore@1905 782 Boolean res = infer.incorporationCache.get(newOp);
mcimadamore@1905 783 if (res == null) {
mcimadamore@1905 784 infer.incorporationCache.put(newOp, res = newOp.apply(warn));
mcimadamore@1905 785 }
mcimadamore@1905 786 return res;
mcimadamore@1905 787 }
mcimadamore@1562 788 }
mcimadamore@1562 789
mcimadamore@1562 790 /** incorporation steps to be executed when running in legacy mode */
mcimadamore@1562 791 EnumSet<IncorporationStep> incorporationStepsLegacy = EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY);
mcimadamore@1562 792
mcimadamore@1562 793 /** incorporation steps to be executed when running in graph mode */
mcimadamore@1562 794 EnumSet<IncorporationStep> incorporationStepsGraph =
mcimadamore@1562 795 EnumSet.complementOf(EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY));
mcimadamore@1562 796
mcimadamore@1562 797 /**
mcimadamore@1905 798 * Three kinds of basic operation are supported as part of an incorporation step:
mcimadamore@1905 799 * (i) subtype check, (ii) same type check and (iii) bound addition (either
mcimadamore@1905 800 * upper/lower/eq bound).
mcimadamore@1905 801 */
mcimadamore@1905 802 enum IncorporationBinaryOpKind {
mcimadamore@1905 803 IS_SUBTYPE() {
mcimadamore@1905 804 @Override
mcimadamore@1905 805 boolean apply(Type op1, Type op2, Warner warn, Types types) {
mcimadamore@1905 806 return types.isSubtypeUnchecked(op1, op2, warn);
mcimadamore@1905 807 }
mcimadamore@1905 808 },
mcimadamore@1905 809 IS_SAME_TYPE() {
mcimadamore@1905 810 @Override
mcimadamore@1905 811 boolean apply(Type op1, Type op2, Warner warn, Types types) {
mcimadamore@1905 812 return types.isSameType(op1, op2);
mcimadamore@1905 813 }
mcimadamore@1905 814 },
mcimadamore@1905 815 ADD_UPPER_BOUND() {
mcimadamore@1905 816 @Override
mcimadamore@1905 817 boolean apply(Type op1, Type op2, Warner warn, Types types) {
mcimadamore@1905 818 UndetVar uv = (UndetVar)op1;
mcimadamore@1905 819 uv.addBound(InferenceBound.UPPER, op2, types);
mcimadamore@1905 820 return true;
mcimadamore@1905 821 }
mcimadamore@1905 822 },
mcimadamore@1905 823 ADD_LOWER_BOUND() {
mcimadamore@1905 824 @Override
mcimadamore@1905 825 boolean apply(Type op1, Type op2, Warner warn, Types types) {
mcimadamore@1905 826 UndetVar uv = (UndetVar)op1;
mcimadamore@1905 827 uv.addBound(InferenceBound.LOWER, op2, types);
mcimadamore@1905 828 return true;
mcimadamore@1905 829 }
mcimadamore@1905 830 },
mcimadamore@1905 831 ADD_EQ_BOUND() {
mcimadamore@1905 832 @Override
mcimadamore@1905 833 boolean apply(Type op1, Type op2, Warner warn, Types types) {
mcimadamore@1905 834 UndetVar uv = (UndetVar)op1;
mcimadamore@1905 835 uv.addBound(InferenceBound.EQ, op2, types);
mcimadamore@1905 836 return true;
mcimadamore@1905 837 }
mcimadamore@1905 838 };
mcimadamore@1905 839
mcimadamore@1905 840 abstract boolean apply(Type op1, Type op2, Warner warn, Types types);
mcimadamore@1905 841 }
mcimadamore@1905 842
mcimadamore@1905 843 /**
mcimadamore@1905 844 * This class encapsulates a basic incorporation operation; incorporation
mcimadamore@1905 845 * operations takes two type operands and a kind. Each operation performed
mcimadamore@1905 846 * during an incorporation round is stored in a cache, so that operations
mcimadamore@1905 847 * are not executed unnecessarily (which would potentially lead to adding
mcimadamore@1905 848 * same bounds over and over).
mcimadamore@1905 849 */
mcimadamore@1905 850 class IncorporationBinaryOp {
mcimadamore@1905 851
mcimadamore@1905 852 IncorporationBinaryOpKind opKind;
mcimadamore@1905 853 Type op1;
mcimadamore@1905 854 Type op2;
mcimadamore@1905 855
mcimadamore@1905 856 IncorporationBinaryOp(IncorporationBinaryOpKind opKind, Type op1, Type op2) {
mcimadamore@1905 857 this.opKind = opKind;
mcimadamore@1905 858 this.op1 = op1;
mcimadamore@1905 859 this.op2 = op2;
mcimadamore@1905 860 }
mcimadamore@1905 861
mcimadamore@1905 862 @Override
mcimadamore@1905 863 public boolean equals(Object o) {
mcimadamore@1905 864 if (!(o instanceof IncorporationBinaryOp)) {
mcimadamore@1905 865 return false;
mcimadamore@1905 866 } else {
mcimadamore@1905 867 IncorporationBinaryOp that = (IncorporationBinaryOp)o;
mcimadamore@1905 868 return opKind == that.opKind &&
mcimadamore@1905 869 types.isSameType(op1, that.op1, true) &&
mcimadamore@1905 870 types.isSameType(op2, that.op2, true);
mcimadamore@1905 871 }
mcimadamore@1905 872 }
mcimadamore@1905 873
mcimadamore@1905 874 @Override
mcimadamore@1905 875 public int hashCode() {
mcimadamore@1905 876 int result = opKind.hashCode();
mcimadamore@1905 877 result *= 127;
mcimadamore@1905 878 result += types.hashCode(op1);
mcimadamore@1905 879 result *= 127;
mcimadamore@1905 880 result += types.hashCode(op2);
mcimadamore@1905 881 return result;
mcimadamore@1905 882 }
mcimadamore@1905 883
mcimadamore@1905 884 boolean apply(Warner warn) {
mcimadamore@1905 885 return opKind.apply(op1, op2, warn, types);
mcimadamore@1905 886 }
mcimadamore@1905 887 }
mcimadamore@1905 888
mcimadamore@1905 889 /** an incorporation cache keeps track of all executed incorporation-related operations */
mcimadamore@1905 890 Map<IncorporationBinaryOp, Boolean> incorporationCache =
mcimadamore@1905 891 new HashMap<IncorporationBinaryOp, Boolean>();
mcimadamore@1905 892
mcimadamore@1905 893 /**
mcimadamore@1562 894 * Make sure that the upper bounds we got so far lead to a solvable inference
mcimadamore@1562 895 * variable by making sure that a glb exists.
mcimadamore@1562 896 */
mcimadamore@1562 897 void checkCompatibleUpperBounds(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1562 898 List<Type> hibounds =
mcimadamore@1562 899 Type.filter(uv.getBounds(InferenceBound.UPPER), new BoundFilter(inferenceContext));
mcimadamore@1562 900 Type hb = null;
mcimadamore@1562 901 if (hibounds.isEmpty())
mcimadamore@1562 902 hb = syms.objectType;
mcimadamore@1562 903 else if (hibounds.tail.isEmpty())
mcimadamore@1562 904 hb = hibounds.head;
mcimadamore@1562 905 else
mcimadamore@1562 906 hb = types.glb(hibounds);
mcimadamore@1562 907 if (hb == null || hb.isErroneous())
mcimadamore@1562 908 reportBoundError(uv, BoundErrorKind.BAD_UPPER);
mcimadamore@1562 909 }
mcimadamore@1562 910 //where
mcimadamore@1562 911 protected static class BoundFilter implements Filter<Type> {
mcimadamore@1562 912
mcimadamore@1562 913 InferenceContext inferenceContext;
mcimadamore@1562 914
mcimadamore@1562 915 public BoundFilter(InferenceContext inferenceContext) {
mcimadamore@1562 916 this.inferenceContext = inferenceContext;
mcimadamore@1562 917 }
mcimadamore@1562 918
mcimadamore@1562 919 @Override
mcimadamore@1562 920 public boolean accepts(Type t) {
mcimadamore@1562 921 return !t.isErroneous() && !inferenceContext.free(t) &&
mcimadamore@1562 922 !t.hasTag(BOT);
mcimadamore@1562 923 }
mcimadamore@1562 924 };
mcimadamore@1562 925
mcimadamore@1562 926 /**
mcimadamore@1562 927 * This enumeration defines all possible bound-checking related errors.
mcimadamore@1562 928 */
mcimadamore@1562 929 enum BoundErrorKind {
mcimadamore@1562 930 /**
mcimadamore@1562 931 * The (uninstantiated) inference variable has incompatible upper bounds.
mcimadamore@1562 932 */
mcimadamore@1562 933 BAD_UPPER() {
mcimadamore@1562 934 @Override
mcimadamore@1562 935 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
mcimadamore@1562 936 return ex.setMessage("incompatible.upper.bounds", uv.qtype,
mcimadamore@1562 937 uv.getBounds(InferenceBound.UPPER));
mcimadamore@1562 938 }
mcimadamore@1562 939 },
mcimadamore@1562 940 /**
mcimadamore@1562 941 * An equality constraint is not compatible with an upper bound.
mcimadamore@1562 942 */
mcimadamore@1562 943 BAD_EQ_UPPER() {
mcimadamore@1562 944 @Override
mcimadamore@1562 945 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
mcimadamore@1562 946 return ex.setMessage("incompatible.eq.upper.bounds", uv.qtype,
mcimadamore@1562 947 uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.UPPER));
mcimadamore@1562 948 }
mcimadamore@1562 949 },
mcimadamore@1562 950 /**
mcimadamore@1562 951 * An equality constraint is not compatible with a lower bound.
mcimadamore@1562 952 */
mcimadamore@1562 953 BAD_EQ_LOWER() {
mcimadamore@1562 954 @Override
mcimadamore@1562 955 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
mcimadamore@1562 956 return ex.setMessage("incompatible.eq.lower.bounds", uv.qtype,
mcimadamore@1562 957 uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.LOWER));
mcimadamore@1562 958 }
mcimadamore@1562 959 },
mcimadamore@1562 960 /**
mcimadamore@1562 961 * Instantiated inference variable is not compatible with an upper bound.
mcimadamore@1562 962 */
mcimadamore@1562 963 UPPER() {
mcimadamore@1562 964 @Override
mcimadamore@1562 965 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
mcimadamore@1562 966 return ex.setMessage("inferred.do.not.conform.to.upper.bounds", uv.inst,
mcimadamore@1562 967 uv.getBounds(InferenceBound.UPPER));
mcimadamore@1562 968 }
mcimadamore@1562 969 },
mcimadamore@1562 970 /**
mcimadamore@1562 971 * Instantiated inference variable is not compatible with a lower bound.
mcimadamore@1562 972 */
mcimadamore@1562 973 LOWER() {
mcimadamore@1562 974 @Override
mcimadamore@1562 975 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
mcimadamore@1562 976 return ex.setMessage("inferred.do.not.conform.to.lower.bounds", uv.inst,
mcimadamore@1562 977 uv.getBounds(InferenceBound.LOWER));
mcimadamore@1562 978 }
mcimadamore@1562 979 },
mcimadamore@1562 980 /**
mcimadamore@1562 981 * Instantiated inference variable is not compatible with an equality constraint.
mcimadamore@1562 982 */
mcimadamore@1562 983 EQ() {
mcimadamore@1562 984 @Override
mcimadamore@1562 985 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
mcimadamore@1562 986 return ex.setMessage("inferred.do.not.conform.to.eq.bounds", uv.inst,
mcimadamore@1562 987 uv.getBounds(InferenceBound.EQ));
mcimadamore@1562 988 }
mcimadamore@1562 989 };
mcimadamore@1562 990
mcimadamore@1562 991 abstract InapplicableMethodException setMessage(InferenceException ex, UndetVar uv);
mcimadamore@1562 992 }
mcimadamore@1562 993
mcimadamore@1562 994 /**
mcimadamore@1562 995 * Report a bound-checking error of given kind
mcimadamore@1562 996 */
mcimadamore@1562 997 void reportBoundError(UndetVar uv, BoundErrorKind bk) {
mcimadamore@1562 998 throw bk.setMessage(inferenceException, uv);
mcimadamore@1562 999 }
mcimadamore@1562 1000 // </editor-fold>
mcimadamore@1562 1001
mcimadamore@1562 1002 // <editor-fold defaultstate="collapsed" desc="Inference engine">
mcimadamore@1562 1003 /**
mcimadamore@1562 1004 * Graph inference strategy - act as an input to the inference solver; a strategy is
mcimadamore@1562 1005 * composed of two ingredients: (i) find a node to solve in the inference graph,
mcimadamore@1562 1006 * and (ii) tell th engine when we are done fixing inference variables
mcimadamore@1562 1007 */
mcimadamore@1562 1008 interface GraphStrategy {
mcimadamore@1562 1009 /**
mcimadamore@1562 1010 * Pick the next node (leaf) to solve in the graph
mcimadamore@1562 1011 */
mcimadamore@1562 1012 Node pickNode(InferenceGraph g);
mcimadamore@1562 1013 /**
mcimadamore@1562 1014 * Is this the last step?
mcimadamore@1562 1015 */
mcimadamore@1562 1016 boolean done();
mcimadamore@1562 1017 }
mcimadamore@1562 1018
mcimadamore@1562 1019 /**
mcimadamore@1562 1020 * Simple solver strategy class that locates all leaves inside a graph
mcimadamore@1562 1021 * and picks the first leaf as the next node to solve
mcimadamore@1562 1022 */
mcimadamore@1562 1023 abstract class LeafSolver implements GraphStrategy {
mcimadamore@1562 1024 public Node pickNode(InferenceGraph g) {
mcimadamore@1562 1025 Assert.check(!g.nodes.isEmpty(), "No nodes to solve!");
mcimadamore@1562 1026 return g.nodes.get(0);
mcimadamore@1562 1027 }
mcimadamore@1905 1028
mcimadamore@1905 1029 boolean isSubtype(Type s, Type t, Warner warn, Infer infer) {
mcimadamore@1905 1030 return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn, infer);
mcimadamore@1905 1031 }
mcimadamore@1905 1032
mcimadamore@1905 1033 boolean isSameType(Type s, Type t, Infer infer) {
mcimadamore@1905 1034 return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null, infer);
mcimadamore@1905 1035 }
mcimadamore@1905 1036
mcimadamore@1905 1037 void addBound(InferenceBound ib, UndetVar uv, Type b, Infer infer) {
mcimadamore@1905 1038 doIncorporationOp(opFor(ib), uv, b, null, infer);
mcimadamore@1905 1039 }
mcimadamore@1905 1040
mcimadamore@1905 1041 IncorporationBinaryOpKind opFor(InferenceBound boundKind) {
mcimadamore@1905 1042 switch (boundKind) {
mcimadamore@1905 1043 case EQ:
mcimadamore@1905 1044 return IncorporationBinaryOpKind.ADD_EQ_BOUND;
mcimadamore@1905 1045 case LOWER:
mcimadamore@1905 1046 return IncorporationBinaryOpKind.ADD_LOWER_BOUND;
mcimadamore@1905 1047 case UPPER:
mcimadamore@1905 1048 return IncorporationBinaryOpKind.ADD_UPPER_BOUND;
mcimadamore@1905 1049 default:
mcimadamore@1905 1050 Assert.error("Can't get here!");
mcimadamore@1905 1051 return null;
mcimadamore@1905 1052 }
mcimadamore@1905 1053 }
mcimadamore@1905 1054
mcimadamore@1905 1055 boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn, Infer infer) {
mcimadamore@1905 1056 IncorporationBinaryOp newOp = infer.new IncorporationBinaryOp(opKind, op1, op2);
mcimadamore@1905 1057 Boolean res = infer.incorporationCache.get(newOp);
mcimadamore@1905 1058 if (res == null) {
mcimadamore@1905 1059 infer.incorporationCache.put(newOp, res = newOp.apply(warn));
mcimadamore@1905 1060 }
mcimadamore@1905 1061 return res;
mcimadamore@1905 1062 }
mcimadamore@1562 1063 }
mcimadamore@1562 1064
mcimadamore@1562 1065 /**
mcimadamore@1562 1066 * This solver uses an heuristic to pick the best leaf - the heuristic
mcimadamore@1562 1067 * tries to select the node that has maximal probability to contain one
mcimadamore@1562 1068 * or more inference variables in a given list
mcimadamore@1562 1069 */
mcimadamore@1562 1070 abstract class BestLeafSolver extends LeafSolver {
mcimadamore@1562 1071
mcimadamore@1562 1072 List<Type> varsToSolve;
mcimadamore@1562 1073
mcimadamore@1562 1074 BestLeafSolver(List<Type> varsToSolve) {
mcimadamore@1562 1075 this.varsToSolve = varsToSolve;
mcimadamore@1562 1076 }
mcimadamore@1562 1077
mcimadamore@1562 1078 /**
mcimadamore@1903 1079 * Computes the minimum path that goes from a given node to any of the nodes
mcimadamore@1903 1080 * containing a variable in {@code varsToSolve}. For any given path, the cost
mcimadamore@1903 1081 * is computed as the total number of type-variables that should be eagerly
mcimadamore@1903 1082 * instantiated across that path.
mcimadamore@1562 1083 */
mcimadamore@1903 1084 int computeMinPath(InferenceGraph g, Node n) {
mcimadamore@1903 1085 return computeMinPath(g, n, List.<Node>nil(), 0);
mcimadamore@1903 1086 }
mcimadamore@1903 1087
mcimadamore@1903 1088 int computeMinPath(InferenceGraph g, Node n, List<Node> path, int cost) {
mcimadamore@1903 1089 if (path.contains(n)) return Integer.MAX_VALUE;
mcimadamore@1903 1090 List<Node> path2 = path.prepend(n);
mcimadamore@1903 1091 int cost2 = cost + n.data.size();
mcimadamore@1903 1092 if (!Collections.disjoint(n.data, varsToSolve)) {
mcimadamore@1903 1093 return cost2;
mcimadamore@1562 1094 } else {
mcimadamore@1903 1095 int bestPath = Integer.MAX_VALUE;
mcimadamore@1903 1096 for (Node n2 : g.nodes) {
mcimadamore@1903 1097 if (n2.deps.contains(n)) {
mcimadamore@1903 1098 int res = computeMinPath(g, n2, path2, cost2);
mcimadamore@1903 1099 if (res < bestPath) {
mcimadamore@1903 1100 bestPath = res;
mcimadamore@1903 1101 }
mcimadamore@1903 1102 }
mcimadamore@1562 1103 }
mcimadamore@1903 1104 return bestPath;
mcimadamore@1562 1105 }
mcimadamore@1562 1106 }
mcimadamore@1562 1107
mcimadamore@1562 1108 /**
mcimadamore@1562 1109 * Pick the leaf that minimize cost
mcimadamore@1562 1110 */
mcimadamore@1562 1111 @Override
mcimadamore@1562 1112 public Node pickNode(final InferenceGraph g) {
mcimadamore@1903 1113 final Map<Node, Integer> leavesMap = new HashMap<Node, Integer>();
mcimadamore@1562 1114 for (Node n : g.nodes) {
mcimadamore@1562 1115 if (n.isLeaf(n)) {
mcimadamore@1903 1116 leavesMap.put(n, computeMinPath(g, n));
mcimadamore@1562 1117 }
mcimadamore@1562 1118 }
mcimadamore@1903 1119 Assert.check(!leavesMap.isEmpty(), "No nodes to solve!");
mcimadamore@1903 1120 TreeSet<Node> orderedLeaves = new TreeSet<Node>(new Comparator<Node>() {
mcimadamore@1562 1121 public int compare(Node n1, Node n2) {
mcimadamore@1903 1122 return leavesMap.get(n1) - leavesMap.get(n2);
mcimadamore@1562 1123 }
mcimadamore@1562 1124 });
mcimadamore@1903 1125 orderedLeaves.addAll(leavesMap.keySet());
mcimadamore@1903 1126 return orderedLeaves.first();
mcimadamore@1562 1127 }
mcimadamore@1562 1128 }
mcimadamore@1562 1129
mcimadamore@1562 1130 /**
mcimadamore@1562 1131 * The inference process can be thought of as a sequence of steps. Each step
mcimadamore@1562 1132 * instantiates an inference variable using a subset of the inference variable
mcimadamore@1562 1133 * bounds, if certain condition are met. Decisions such as the sequence in which
mcimadamore@1562 1134 * steps are applied, or which steps are to be applied are left to the inference engine.
mcimadamore@1562 1135 */
mcimadamore@1562 1136 enum InferenceStep {
mcimadamore@1562 1137
mcimadamore@1562 1138 /**
mcimadamore@1562 1139 * Instantiate an inference variables using one of its (ground) equality
mcimadamore@1562 1140 * constraints
mcimadamore@1562 1141 */
mcimadamore@1562 1142 EQ(InferenceBound.EQ) {
mcimadamore@1562 1143 @Override
mcimadamore@1562 1144 Type solve(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1562 1145 return filterBounds(uv, inferenceContext).head;
mcimadamore@1562 1146 }
mcimadamore@1562 1147 },
mcimadamore@1562 1148 /**
mcimadamore@1562 1149 * Instantiate an inference variables using its (ground) lower bounds. Such
mcimadamore@1562 1150 * bounds are merged together using lub().
mcimadamore@1562 1151 */
mcimadamore@1562 1152 LOWER(InferenceBound.LOWER) {
mcimadamore@1562 1153 @Override
mcimadamore@1562 1154 Type solve(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1562 1155 Infer infer = inferenceContext.infer();
mcimadamore@1562 1156 List<Type> lobounds = filterBounds(uv, inferenceContext);
vromero@1826 1157 //note: lobounds should have at least one element
vromero@1826 1158 Type owntype = lobounds.tail.tail == null ? lobounds.head : infer.types.lub(lobounds);
vromero@1826 1159 if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
mcimadamore@1562 1160 throw infer.inferenceException
mcimadamore@1562 1161 .setMessage("no.unique.minimal.instance.exists",
mcimadamore@1562 1162 uv.qtype, lobounds);
mcimadamore@1562 1163 } else {
mcimadamore@1562 1164 return owntype;
mcimadamore@1562 1165 }
mcimadamore@1562 1166 }
mcimadamore@1562 1167 },
mcimadamore@1562 1168 /**
mcimadamore@1896 1169 * Infer uninstantiated/unbound inference variables occurring in 'throws'
mcimadamore@1896 1170 * clause as RuntimeException
mcimadamore@1896 1171 */
mcimadamore@1896 1172 THROWS(InferenceBound.UPPER) {
mcimadamore@1896 1173 @Override
mcimadamore@1896 1174 public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
mcimadamore@1896 1175 if ((t.qtype.tsym.flags() & Flags.THROWS) == 0) {
mcimadamore@1896 1176 //not a throws undet var
mcimadamore@1896 1177 return false;
mcimadamore@1896 1178 }
mcimadamore@1896 1179 if (t.getBounds(InferenceBound.EQ, InferenceBound.LOWER, InferenceBound.UPPER)
mcimadamore@1896 1180 .diff(t.getDeclaredBounds()).nonEmpty()) {
mcimadamore@1896 1181 //not an unbounded undet var
mcimadamore@1896 1182 return false;
mcimadamore@1896 1183 }
mcimadamore@1896 1184 Infer infer = inferenceContext.infer();
mcimadamore@1896 1185 for (Type db : t.getDeclaredBounds()) {
mcimadamore@1896 1186 if (t.isInterface()) continue;
mcimadamore@1896 1187 if (infer.types.asSuper(infer.syms.runtimeExceptionType, db.tsym) != null) {
mcimadamore@1896 1188 //declared bound is a supertype of RuntimeException
mcimadamore@1896 1189 return true;
mcimadamore@1896 1190 }
mcimadamore@1896 1191 }
mcimadamore@1896 1192 //declared bound is more specific then RuntimeException - give up
mcimadamore@1896 1193 return false;
mcimadamore@1896 1194 }
mcimadamore@1896 1195
mcimadamore@1896 1196 @Override
mcimadamore@1896 1197 Type solve(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1896 1198 return inferenceContext.infer().syms.runtimeExceptionType;
mcimadamore@1896 1199 }
mcimadamore@1896 1200 },
mcimadamore@1896 1201 /**
mcimadamore@1562 1202 * Instantiate an inference variables using its (ground) upper bounds. Such
mcimadamore@1562 1203 * bounds are merged together using glb().
mcimadamore@1562 1204 */
mcimadamore@1562 1205 UPPER(InferenceBound.UPPER) {
mcimadamore@1562 1206 @Override
mcimadamore@1562 1207 Type solve(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1562 1208 Infer infer = inferenceContext.infer();
mcimadamore@1562 1209 List<Type> hibounds = filterBounds(uv, inferenceContext);
vromero@1826 1210 //note: lobounds should have at least one element
vromero@1826 1211 Type owntype = hibounds.tail.tail == null ? hibounds.head : infer.types.glb(hibounds);
vromero@1826 1212 if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
mcimadamore@1562 1213 throw infer.inferenceException
mcimadamore@1562 1214 .setMessage("no.unique.maximal.instance.exists",
mcimadamore@1562 1215 uv.qtype, hibounds);
mcimadamore@1562 1216 } else {
mcimadamore@1562 1217 return owntype;
mcimadamore@1562 1218 }
mcimadamore@1562 1219 }
mcimadamore@1562 1220 },
mcimadamore@1562 1221 /**
mcimadamore@1562 1222 * Like the former; the only difference is that this step can only be applied
mcimadamore@1562 1223 * if all upper bounds are ground.
mcimadamore@1562 1224 */
mcimadamore@1562 1225 UPPER_LEGACY(InferenceBound.UPPER) {
mcimadamore@1562 1226 @Override
mcimadamore@1562 1227 public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
mcimadamore@1898 1228 return !inferenceContext.free(t.getBounds(ib)) && !t.isCaptured();
mcimadamore@1562 1229 }
mcimadamore@1562 1230
mcimadamore@1562 1231 @Override
mcimadamore@1562 1232 Type solve(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1562 1233 return UPPER.solve(uv, inferenceContext);
mcimadamore@1562 1234 }
mcimadamore@1898 1235 },
mcimadamore@1898 1236 /**
mcimadamore@1898 1237 * Like the former; the only difference is that this step can only be applied
mcimadamore@1898 1238 * if all upper/lower bounds are ground.
mcimadamore@1898 1239 */
mcimadamore@1898 1240 CAPTURED(InferenceBound.UPPER) {
mcimadamore@1898 1241 @Override
mcimadamore@1898 1242 public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
mcimadamore@1919 1243 return t.isCaptured() &&
mcimadamore@1919 1244 !inferenceContext.free(t.getBounds(InferenceBound.UPPER, InferenceBound.LOWER));
mcimadamore@1898 1245 }
mcimadamore@1898 1246
mcimadamore@1898 1247 @Override
mcimadamore@1898 1248 Type solve(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1898 1249 Infer infer = inferenceContext.infer();
mcimadamore@1898 1250 Type upper = UPPER.filterBounds(uv, inferenceContext).nonEmpty() ?
mcimadamore@1898 1251 UPPER.solve(uv, inferenceContext) :
mcimadamore@1898 1252 infer.syms.objectType;
mcimadamore@1898 1253 Type lower = LOWER.filterBounds(uv, inferenceContext).nonEmpty() ?
mcimadamore@1898 1254 LOWER.solve(uv, inferenceContext) :
mcimadamore@1898 1255 infer.syms.botType;
mcimadamore@1898 1256 CapturedType prevCaptured = (CapturedType)uv.qtype;
mcimadamore@1898 1257 return new CapturedType(prevCaptured.tsym.name, prevCaptured.tsym.owner, upper, lower, prevCaptured.wildcard);
mcimadamore@1898 1258 }
mcimadamore@1562 1259 };
mcimadamore@1562 1260
mcimadamore@1562 1261 final InferenceBound ib;
mcimadamore@1562 1262
mcimadamore@1562 1263 InferenceStep(InferenceBound ib) {
mcimadamore@1562 1264 this.ib = ib;
mcimadamore@1562 1265 }
mcimadamore@1562 1266
mcimadamore@1562 1267 /**
mcimadamore@1562 1268 * Find an instantiated type for a given inference variable within
mcimadamore@1562 1269 * a given inference context
mcimadamore@1562 1270 */
mcimadamore@1562 1271 abstract Type solve(UndetVar uv, InferenceContext inferenceContext);
mcimadamore@1562 1272
mcimadamore@1562 1273 /**
mcimadamore@1562 1274 * Can the inference variable be instantiated using this step?
mcimadamore@1562 1275 */
mcimadamore@1562 1276 public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
mcimadamore@1898 1277 return filterBounds(t, inferenceContext).nonEmpty() && !t.isCaptured();
mcimadamore@1562 1278 }
mcimadamore@1562 1279
mcimadamore@1562 1280 /**
mcimadamore@1562 1281 * Return the subset of ground bounds in a given bound set (i.e. eq/lower/upper)
mcimadamore@1562 1282 */
mcimadamore@1562 1283 List<Type> filterBounds(UndetVar uv, InferenceContext inferenceContext) {
mcimadamore@1562 1284 return Type.filter(uv.getBounds(ib), new BoundFilter(inferenceContext));
mcimadamore@1562 1285 }
mcimadamore@1562 1286 }
mcimadamore@1562 1287
mcimadamore@1562 1288 /**
mcimadamore@1562 1289 * This enumeration defines the sequence of steps to be applied when the
mcimadamore@1562 1290 * solver works in legacy mode. The steps in this enumeration reflect
mcimadamore@1562 1291 * the behavior of old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8).
mcimadamore@1562 1292 */
mcimadamore@1562 1293 enum LegacyInferenceSteps {
mcimadamore@1562 1294
mcimadamore@1562 1295 EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
mcimadamore@1562 1296 EQ_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.UPPER_LEGACY));
mcimadamore@1562 1297
mcimadamore@1562 1298 final EnumSet<InferenceStep> steps;
mcimadamore@1562 1299
mcimadamore@1562 1300 LegacyInferenceSteps(EnumSet<InferenceStep> steps) {
mcimadamore@1562 1301 this.steps = steps;
mcimadamore@1562 1302 }
mcimadamore@1562 1303 }
mcimadamore@1562 1304
mcimadamore@1562 1305 /**
mcimadamore@1562 1306 * This enumeration defines the sequence of steps to be applied when the
mcimadamore@1562 1307 * graph solver is used. This order is defined so as to maximize compatibility
mcimadamore@1562 1308 * w.r.t. old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8).
mcimadamore@1562 1309 */
mcimadamore@1562 1310 enum GraphInferenceSteps {
mcimadamore@1562 1311
mcimadamore@1562 1312 EQ(EnumSet.of(InferenceStep.EQ)),
mcimadamore@1562 1313 EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
mcimadamore@1898 1314 EQ_LOWER_THROWS_UPPER_CAPTURED(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER, InferenceStep.UPPER, InferenceStep.THROWS, InferenceStep.CAPTURED));
mcimadamore@1562 1315
mcimadamore@1562 1316 final EnumSet<InferenceStep> steps;
mcimadamore@1562 1317
mcimadamore@1562 1318 GraphInferenceSteps(EnumSet<InferenceStep> steps) {
mcimadamore@1562 1319 this.steps = steps;
mcimadamore@1562 1320 }
mcimadamore@1562 1321 }
mcimadamore@1562 1322
mcimadamore@1562 1323 /**
mcimadamore@1562 1324 * This is the graph inference solver - the solver organizes all inference variables in
mcimadamore@1562 1325 * a given inference context by bound dependencies - in the general case, such dependencies
mcimadamore@1562 1326 * would lead to a cyclic directed graph (hence the name); the dependency info is used to build
mcimadamore@1562 1327 * an acyclic graph, where all cyclic variables are bundled together. An inference
mcimadamore@1562 1328 * step corresponds to solving a node in the acyclic graph - this is done by
mcimadamore@1562 1329 * relying on a given strategy (see GraphStrategy).
mcimadamore@1562 1330 */
mcimadamore@1562 1331 class GraphSolver {
mcimadamore@1562 1332
mcimadamore@1562 1333 InferenceContext inferenceContext;
mcimadamore@1562 1334 Warner warn;
mcimadamore@1562 1335
mcimadamore@1562 1336 GraphSolver(InferenceContext inferenceContext, Warner warn) {
mcimadamore@1562 1337 this.inferenceContext = inferenceContext;
mcimadamore@1562 1338 this.warn = warn;
mcimadamore@1562 1339 }
mcimadamore@1562 1340
mcimadamore@1562 1341 /**
mcimadamore@1562 1342 * Solve variables in a given inference context. The amount of variables
mcimadamore@1562 1343 * to be solved, and the way in which the underlying acyclic graph is explored
mcimadamore@1562 1344 * depends on the selected solver strategy.
mcimadamore@1562 1345 */
mcimadamore@1562 1346 void solve(GraphStrategy sstrategy) {
mcimadamore@1562 1347 checkWithinBounds(inferenceContext, warn); //initial propagation of bounds
mcimadamore@1562 1348 InferenceGraph inferenceGraph = new InferenceGraph();
mcimadamore@1562 1349 while (!sstrategy.done()) {
mcimadamore@1562 1350 InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph);
mcimadamore@1562 1351 List<Type> varsToSolve = List.from(nodeToSolve.data);
mcimadamore@1891 1352 List<Type> saved_undet = inferenceContext.save();
mcimadamore@1562 1353 try {
mcimadamore@1562 1354 //repeat until all variables are solved
mcimadamore@1562 1355 outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) {
mcimadamore@1562 1356 //for each inference phase
mcimadamore@1562 1357 for (GraphInferenceSteps step : GraphInferenceSteps.values()) {
mcimadamore@1562 1358 if (inferenceContext.solveBasic(varsToSolve, step.steps)) {
mcimadamore@1562 1359 checkWithinBounds(inferenceContext, warn);
mcimadamore@1562 1360 continue outer;
mcimadamore@1562 1361 }
mcimadamore@1562 1362 }
mcimadamore@1562 1363 //no progress
vromero@1826 1364 throw inferenceException.setMessage();
mcimadamore@1562 1365 }
mcimadamore@1562 1366 }
mcimadamore@1562 1367 catch (InferenceException ex) {
vromero@1826 1368 //did we fail because of interdependent ivars?
mcimadamore@1891 1369 inferenceContext.rollback(saved_undet);
mcimadamore@1562 1370 instantiateAsUninferredVars(varsToSolve, inferenceContext);
mcimadamore@1562 1371 checkWithinBounds(inferenceContext, warn);
mcimadamore@1562 1372 }
mcimadamore@1562 1373 inferenceGraph.deleteNode(nodeToSolve);
mcimadamore@1562 1374 }
mcimadamore@1562 1375 }
mcimadamore@1562 1376
mcimadamore@1562 1377 /**
mcimadamore@1562 1378 * The dependencies between the inference variables that need to be solved
mcimadamore@1562 1379 * form a (possibly cyclic) graph. This class reduces the original dependency graph
mcimadamore@1562 1380 * to an acyclic version, where cyclic nodes are folded into a single 'super node'.
mcimadamore@1562 1381 */
mcimadamore@1562 1382 class InferenceGraph {
mcimadamore@1562 1383
mcimadamore@1562 1384 /**
mcimadamore@1562 1385 * This class represents a node in the graph. Each node corresponds
mcimadamore@1562 1386 * to an inference variable and has edges (dependencies) on other
mcimadamore@1562 1387 * nodes. The node defines an entry point that can be used to receive
mcimadamore@1562 1388 * updates on the structure of the graph this node belongs to (used to
mcimadamore@1562 1389 * keep dependencies in sync).
mcimadamore@1562 1390 */
mcimadamore@1562 1391 class Node extends GraphUtils.TarjanNode<ListBuffer<Type>> {
mcimadamore@1562 1392
mcimadamore@1562 1393 Set<Node> deps;
mcimadamore@1562 1394
mcimadamore@1562 1395 Node(Type ivar) {
mcimadamore@1562 1396 super(ListBuffer.of(ivar));
mcimadamore@1562 1397 this.deps = new HashSet<Node>();
mcimadamore@1562 1398 }
mcimadamore@1562 1399
mcimadamore@1562 1400 @Override
mcimadamore@1562 1401 public Iterable<? extends Node> getDependencies() {
mcimadamore@1562 1402 return deps;
mcimadamore@1562 1403 }
mcimadamore@1562 1404
mcimadamore@1562 1405 @Override
mcimadamore@1562 1406 public String printDependency(GraphUtils.Node<ListBuffer<Type>> to) {
mcimadamore@1562 1407 StringBuilder buf = new StringBuilder();
mcimadamore@1562 1408 String sep = "";
mcimadamore@1562 1409 for (Type from : data) {
mcimadamore@1562 1410 UndetVar uv = (UndetVar)inferenceContext.asFree(from);
mcimadamore@1562 1411 for (Type bound : uv.getBounds(InferenceBound.values())) {
mcimadamore@1562 1412 if (bound.containsAny(List.from(to.data))) {
mcimadamore@1562 1413 buf.append(sep);
mcimadamore@1562 1414 buf.append(bound);
mcimadamore@1562 1415 sep = ",";
mcimadamore@1562 1416 }
mcimadamore@1562 1417 }
mcimadamore@1562 1418 }
mcimadamore@1562 1419 return buf.toString();
mcimadamore@1562 1420 }
mcimadamore@1562 1421
mcimadamore@1562 1422 boolean isLeaf(Node n) {
mcimadamore@1562 1423 //no deps, or only one self dep
mcimadamore@1562 1424 return (n.deps.isEmpty() ||
mcimadamore@1562 1425 n.deps.size() == 1 && n.deps.contains(n));
mcimadamore@1562 1426 }
mcimadamore@1562 1427
mcimadamore@1562 1428 void mergeWith(List<? extends Node> nodes) {
mcimadamore@1562 1429 for (Node n : nodes) {
mcimadamore@1562 1430 Assert.check(n.data.length() == 1, "Attempt to merge a compound node!");
mcimadamore@1562 1431 data.appendList(n.data);
mcimadamore@1562 1432 deps.addAll(n.deps);
mcimadamore@1562 1433 }
mcimadamore@1562 1434 //update deps
mcimadamore@1562 1435 Set<Node> deps2 = new HashSet<Node>();
mcimadamore@1562 1436 for (Node d : deps) {
mcimadamore@1562 1437 if (data.contains(d.data.first())) {
mcimadamore@1562 1438 deps2.add(this);
mcimadamore@1562 1439 } else {
mcimadamore@1562 1440 deps2.add(d);
mcimadamore@1562 1441 }
mcimadamore@1562 1442 }
mcimadamore@1562 1443 deps = deps2;
mcimadamore@1562 1444 }
mcimadamore@1562 1445
mcimadamore@1562 1446 void graphChanged(Node from, Node to) {
mcimadamore@1562 1447 if (deps.contains(from)) {
mcimadamore@1562 1448 deps.remove(from);
mcimadamore@1562 1449 if (to != null) {
mcimadamore@1562 1450 deps.add(to);
mcimadamore@1562 1451 }
mcimadamore@1562 1452 }
mcimadamore@1562 1453 }
mcimadamore@1562 1454 }
mcimadamore@1562 1455
mcimadamore@1562 1456 /** the nodes in the inference graph */
mcimadamore@1562 1457 ArrayList<Node> nodes;
mcimadamore@1562 1458
mcimadamore@1562 1459 InferenceGraph() {
mcimadamore@1562 1460 initNodes();
mcimadamore@1562 1461 }
mcimadamore@1562 1462
mcimadamore@1562 1463 /**
mcimadamore@1562 1464 * Delete a node from the graph. This update the underlying structure
mcimadamore@1562 1465 * of the graph (including dependencies) via listeners updates.
mcimadamore@1562 1466 */
mcimadamore@1562 1467 public void deleteNode(Node n) {
mcimadamore@1562 1468 Assert.check(nodes.contains(n));
mcimadamore@1562 1469 nodes.remove(n);
mcimadamore@1562 1470 notifyUpdate(n, null);
mcimadamore@1562 1471 }
mcimadamore@1562 1472
mcimadamore@1562 1473 /**
mcimadamore@1562 1474 * Notify all nodes of a change in the graph. If the target node is
mcimadamore@1562 1475 * {@code null} the source node is assumed to be removed.
mcimadamore@1562 1476 */
mcimadamore@1562 1477 void notifyUpdate(Node from, Node to) {
mcimadamore@1562 1478 for (Node n : nodes) {
mcimadamore@1562 1479 n.graphChanged(from, to);
mcimadamore@1562 1480 }
mcimadamore@1562 1481 }
mcimadamore@1562 1482
mcimadamore@1562 1483 /**
mcimadamore@1562 1484 * Create the graph nodes. First a simple node is created for every inference
mcimadamore@1562 1485 * variables to be solved. Then Tarjan is used to found all connected components
mcimadamore@1562 1486 * in the graph. For each component containing more than one node, a super node is
mcimadamore@1562 1487 * created, effectively replacing the original cyclic nodes.
mcimadamore@1562 1488 */
mcimadamore@1562 1489 void initNodes() {
mcimadamore@1608 1490 nodes = new ArrayList<Node>();
mcimadamore@1562 1491 for (Type t : inferenceContext.restvars()) {
mcimadamore@1562 1492 nodes.add(new Node(t));
mcimadamore@1562 1493 }
mcimadamore@1562 1494 for (Node n_i : nodes) {
mcimadamore@1562 1495 Type i = n_i.data.first();
mcimadamore@1562 1496 for (Node n_j : nodes) {
mcimadamore@1562 1497 Type j = n_j.data.first();
mcimadamore@1562 1498 UndetVar uv_i = (UndetVar)inferenceContext.asFree(i);
mcimadamore@1562 1499 if (Type.containsAny(uv_i.getBounds(InferenceBound.values()), List.of(j))) {
mcimadamore@1562 1500 //update i's deps
mcimadamore@1562 1501 n_i.deps.add(n_j);
mcimadamore@1562 1502 }
mcimadamore@1562 1503 }
mcimadamore@1562 1504 }
mcimadamore@1608 1505 ArrayList<Node> acyclicNodes = new ArrayList<Node>();
mcimadamore@1562 1506 for (List<? extends Node> conSubGraph : GraphUtils.tarjan(nodes)) {
mcimadamore@1562 1507 if (conSubGraph.length() > 1) {
mcimadamore@1562 1508 Node root = conSubGraph.head;
mcimadamore@1562 1509 root.mergeWith(conSubGraph.tail);
mcimadamore@1562 1510 for (Node n : conSubGraph) {
mcimadamore@1562 1511 notifyUpdate(n, root);
mcimadamore@1562 1512 }
mcimadamore@1562 1513 }
mcimadamore@1608 1514 acyclicNodes.add(conSubGraph.head);
mcimadamore@1562 1515 }
mcimadamore@1608 1516 nodes = acyclicNodes;
mcimadamore@1562 1517 }
mcimadamore@1562 1518
mcimadamore@1562 1519 /**
mcimadamore@1562 1520 * Debugging: dot representation of this graph
mcimadamore@1562 1521 */
mcimadamore@1562 1522 String toDot() {
mcimadamore@1562 1523 StringBuilder buf = new StringBuilder();
mcimadamore@1562 1524 for (Type t : inferenceContext.undetvars) {
mcimadamore@1562 1525 UndetVar uv = (UndetVar)t;
mcimadamore@1562 1526 buf.append(String.format("var %s - upper bounds = %s, lower bounds = %s, eq bounds = %s\\n",
mcimadamore@1562 1527 uv.qtype, uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER),
mcimadamore@1562 1528 uv.getBounds(InferenceBound.EQ)));
mcimadamore@1562 1529 }
mcimadamore@1562 1530 return GraphUtils.toDot(nodes, "inferenceGraph" + hashCode(), buf.toString());
mcimadamore@1562 1531 }
mcimadamore@1562 1532 }
mcimadamore@1562 1533 }
mcimadamore@1562 1534 // </editor-fold>
mcimadamore@1562 1535
mcimadamore@1562 1536 // <editor-fold defaultstate="collapsed" desc="Inference context">
mcimadamore@1562 1537 /**
mcimadamore@1550 1538 * Functional interface for defining inference callbacks. Certain actions
mcimadamore@1550 1539 * (i.e. subtyping checks) might need to be redone after all inference variables
mcimadamore@1550 1540 * have been fixed.
mcimadamore@1337 1541 */
mcimadamore@1550 1542 interface FreeTypeListener {
mcimadamore@1550 1543 void typesInferred(InferenceContext inferenceContext);
mcimadamore@1550 1544 }
mcimadamore@1337 1545
mcimadamore@1337 1546 /**
mcimadamore@1337 1547 * An inference context keeps track of the set of variables that are free
mcimadamore@1337 1548 * in the current context. It provides utility methods for opening/closing
mcimadamore@1337 1549 * types to their corresponding free/closed forms. It also provide hooks for
mcimadamore@1337 1550 * attaching deferred post-inference action (see PendingCheck). Finally,
mcimadamore@1337 1551 * it can be used as an entry point for performing upper/lower bound inference
mcimadamore@1337 1552 * (see InferenceKind).
mcimadamore@1337 1553 */
mcimadamore@1562 1554 class InferenceContext {
mcimadamore@1337 1555
mcimadamore@1337 1556 /** list of inference vars as undet vars */
mcimadamore@1337 1557 List<Type> undetvars;
mcimadamore@1337 1558
mcimadamore@1337 1559 /** list of inference vars in this context */
mcimadamore@1337 1560 List<Type> inferencevars;
mcimadamore@1337 1561
mcimadamore@1337 1562 java.util.Map<FreeTypeListener, List<Type>> freeTypeListeners =
mcimadamore@1337 1563 new java.util.HashMap<FreeTypeListener, List<Type>>();
mcimadamore@1337 1564
mcimadamore@1337 1565 List<FreeTypeListener> freetypeListeners = List.nil();
mcimadamore@1337 1566
mcimadamore@1550 1567 public InferenceContext(List<Type> inferencevars) {
mcimadamore@1550 1568 this.undetvars = Type.map(inferencevars, fromTypeVarFun);
mcimadamore@1337 1569 this.inferencevars = inferencevars;
mcimadamore@1337 1570 }
mcimadamore@1550 1571 //where
mcimadamore@1550 1572 Mapping fromTypeVarFun = new Mapping("fromTypeVarFunWithBounds") {
mcimadamore@1550 1573 // mapping that turns inference variables into undet vars
mcimadamore@1550 1574 public Type apply(Type t) {
mcimadamore@1898 1575 if (t.hasTag(TYPEVAR)) {
mcimadamore@1898 1576 TypeVar tv = (TypeVar)t;
mcimadamore@1898 1577 return tv.isCaptured() ?
mcimadamore@1898 1578 new CapturedUndetVar((CapturedType)tv, types) :
mcimadamore@1898 1579 new UndetVar(tv, types);
mcimadamore@1898 1580 } else {
mcimadamore@1898 1581 return t.map(this);
mcimadamore@1898 1582 }
mcimadamore@1550 1583 }
mcimadamore@1550 1584 };
mcimadamore@1337 1585
mcimadamore@1337 1586 /**
mcimadamore@1898 1587 * add a new inference var to this inference context
mcimadamore@1898 1588 */
mcimadamore@1898 1589 void addVar(TypeVar t) {
mcimadamore@1898 1590 this.undetvars = this.undetvars.prepend(fromTypeVarFun.apply(t));
mcimadamore@1898 1591 this.inferencevars = this.inferencevars.prepend(t);
mcimadamore@1898 1592 }
mcimadamore@1898 1593
mcimadamore@1898 1594 /**
mcimadamore@1337 1595 * returns the list of free variables (as type-variables) in this
mcimadamore@1337 1596 * inference context
mcimadamore@1337 1597 */
mcimadamore@1337 1598 List<Type> inferenceVars() {
mcimadamore@1337 1599 return inferencevars;
mcimadamore@1337 1600 }
mcimadamore@1337 1601
mcimadamore@1337 1602 /**
mcimadamore@1337 1603 * returns the list of uninstantiated variables (as type-variables) in this
mcimadamore@1550 1604 * inference context
mcimadamore@1337 1605 */
mcimadamore@1337 1606 List<Type> restvars() {
mcimadamore@1550 1607 return filterVars(new Filter<UndetVar>() {
mcimadamore@1550 1608 public boolean accepts(UndetVar uv) {
mcimadamore@1550 1609 return uv.inst == null;
mcimadamore@1337 1610 }
mcimadamore@1550 1611 });
mcimadamore@1550 1612 }
mcimadamore@1550 1613
mcimadamore@1550 1614 /**
mcimadamore@1550 1615 * returns the list of instantiated variables (as type-variables) in this
mcimadamore@1550 1616 * inference context
mcimadamore@1550 1617 */
mcimadamore@1550 1618 List<Type> instvars() {
mcimadamore@1550 1619 return filterVars(new Filter<UndetVar>() {
mcimadamore@1550 1620 public boolean accepts(UndetVar uv) {
mcimadamore@1550 1621 return uv.inst != null;
mcimadamore@1550 1622 }
mcimadamore@1550 1623 });
mcimadamore@1550 1624 }
mcimadamore@1550 1625
mcimadamore@1550 1626 /**
mcimadamore@1550 1627 * Get list of bounded inference variables (where bound is other than
mcimadamore@1550 1628 * declared bounds).
mcimadamore@1550 1629 */
mcimadamore@1550 1630 final List<Type> boundedVars() {
mcimadamore@1550 1631 return filterVars(new Filter<UndetVar>() {
mcimadamore@1550 1632 public boolean accepts(UndetVar uv) {
mcimadamore@1550 1633 return uv.getBounds(InferenceBound.UPPER)
mcimadamore@1550 1634 .diff(uv.getDeclaredBounds())
mcimadamore@1550 1635 .appendList(uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)).nonEmpty();
mcimadamore@1550 1636 }
mcimadamore@1550 1637 });
mcimadamore@1550 1638 }
mcimadamore@1550 1639
mcimadamore@1550 1640 private List<Type> filterVars(Filter<UndetVar> fu) {
mcimadamore@1550 1641 ListBuffer<Type> res = ListBuffer.lb();
mcimadamore@1550 1642 for (Type t : undetvars) {
mcimadamore@1550 1643 UndetVar uv = (UndetVar)t;
mcimadamore@1550 1644 if (fu.accepts(uv)) {
mcimadamore@1550 1645 res.append(uv.qtype);
mcimadamore@1550 1646 }
mcimadamore@1337 1647 }
mcimadamore@1550 1648 return res.toList();
mcimadamore@1337 1649 }
mcimadamore@1337 1650
mcimadamore@1337 1651 /**
mcimadamore@1337 1652 * is this type free?
mcimadamore@1337 1653 */
mcimadamore@1337 1654 final boolean free(Type t) {
mcimadamore@1337 1655 return t.containsAny(inferencevars);
mcimadamore@1337 1656 }
mcimadamore@1337 1657
mcimadamore@1337 1658 final boolean free(List<Type> ts) {
mcimadamore@1337 1659 for (Type t : ts) {
mcimadamore@1337 1660 if (free(t)) return true;
mcimadamore@1337 1661 }
mcimadamore@1337 1662 return false;
mcimadamore@1337 1663 }
mcimadamore@1337 1664
mcimadamore@1337 1665 /**
mcimadamore@1337 1666 * Returns a list of free variables in a given type
mcimadamore@1337 1667 */
mcimadamore@1337 1668 final List<Type> freeVarsIn(Type t) {
mcimadamore@1337 1669 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1337 1670 for (Type iv : inferenceVars()) {
mcimadamore@1337 1671 if (t.contains(iv)) {
mcimadamore@1337 1672 buf.add(iv);
mcimadamore@1337 1673 }
mcimadamore@1337 1674 }
mcimadamore@1337 1675 return buf.toList();
mcimadamore@1337 1676 }
mcimadamore@1337 1677
mcimadamore@1337 1678 final List<Type> freeVarsIn(List<Type> ts) {
mcimadamore@1337 1679 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1337 1680 for (Type t : ts) {
mcimadamore@1337 1681 buf.appendList(freeVarsIn(t));
mcimadamore@1337 1682 }
mcimadamore@1337 1683 ListBuffer<Type> buf2 = ListBuffer.lb();
mcimadamore@1337 1684 for (Type t : buf) {
mcimadamore@1337 1685 if (!buf2.contains(t)) {
mcimadamore@1337 1686 buf2.add(t);
mcimadamore@1337 1687 }
mcimadamore@1337 1688 }
mcimadamore@1337 1689 return buf2.toList();
mcimadamore@1337 1690 }
mcimadamore@1337 1691
mcimadamore@1337 1692 /**
mcimadamore@1337 1693 * Replace all free variables in a given type with corresponding
mcimadamore@1337 1694 * undet vars (used ahead of subtyping/compatibility checks to allow propagation
mcimadamore@1337 1695 * of inference constraints).
mcimadamore@1337 1696 */
mcimadamore@1550 1697 final Type asFree(Type t) {
mcimadamore@1337 1698 return types.subst(t, inferencevars, undetvars);
mcimadamore@1337 1699 }
mcimadamore@1337 1700
mcimadamore@1550 1701 final List<Type> asFree(List<Type> ts) {
mcimadamore@1337 1702 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1337 1703 for (Type t : ts) {
mcimadamore@1550 1704 buf.append(asFree(t));
mcimadamore@1337 1705 }
mcimadamore@1337 1706 return buf.toList();
mcimadamore@1337 1707 }
mcimadamore@1337 1708
mcimadamore@1337 1709 List<Type> instTypes() {
mcimadamore@1337 1710 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1337 1711 for (Type t : undetvars) {
mcimadamore@1337 1712 UndetVar uv = (UndetVar)t;
mcimadamore@1337 1713 buf.append(uv.inst != null ? uv.inst : uv.qtype);
mcimadamore@1337 1714 }
mcimadamore@1337 1715 return buf.toList();
mcimadamore@1337 1716 }
mcimadamore@1337 1717
mcimadamore@1337 1718 /**
mcimadamore@1337 1719 * Replace all free variables in a given type with corresponding
mcimadamore@1337 1720 * instantiated types - if one or more free variable has not been
mcimadamore@1337 1721 * fully instantiated, it will still be available in the resulting type.
mcimadamore@1337 1722 */
mcimadamore@1550 1723 Type asInstType(Type t) {
mcimadamore@1337 1724 return types.subst(t, inferencevars, instTypes());
mcimadamore@1337 1725 }
mcimadamore@1337 1726
mcimadamore@1550 1727 List<Type> asInstTypes(List<Type> ts) {
mcimadamore@1337 1728 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1337 1729 for (Type t : ts) {
mcimadamore@1550 1730 buf.append(asInstType(t));
mcimadamore@1337 1731 }
mcimadamore@1337 1732 return buf.toList();
mcimadamore@1337 1733 }
mcimadamore@1337 1734
mcimadamore@1337 1735 /**
mcimadamore@1337 1736 * Add custom hook for performing post-inference action
mcimadamore@1337 1737 */
mcimadamore@1337 1738 void addFreeTypeListener(List<Type> types, FreeTypeListener ftl) {
mcimadamore@1337 1739 freeTypeListeners.put(ftl, freeVarsIn(types));
mcimadamore@1337 1740 }
mcimadamore@1337 1741
mcimadamore@1337 1742 /**
mcimadamore@1337 1743 * Mark the inference context as complete and trigger evaluation
mcimadamore@1337 1744 * of all deferred checks.
mcimadamore@1337 1745 */
mcimadamore@1550 1746 void notifyChange() {
mcimadamore@1562 1747 notifyChange(inferencevars.diff(restvars()));
mcimadamore@1562 1748 }
mcimadamore@1562 1749
mcimadamore@1562 1750 void notifyChange(List<Type> inferredVars) {
mcimadamore@1337 1751 InferenceException thrownEx = null;
mcimadamore@1337 1752 for (Map.Entry<FreeTypeListener, List<Type>> entry :
mcimadamore@1337 1753 new HashMap<FreeTypeListener, List<Type>>(freeTypeListeners).entrySet()) {
mcimadamore@1562 1754 if (!Type.containsAny(entry.getValue(), inferencevars.diff(inferredVars))) {
mcimadamore@1337 1755 try {
mcimadamore@1337 1756 entry.getKey().typesInferred(this);
mcimadamore@1337 1757 freeTypeListeners.remove(entry.getKey());
mcimadamore@1337 1758 } catch (InferenceException ex) {
mcimadamore@1337 1759 if (thrownEx == null) {
mcimadamore@1337 1760 thrownEx = ex;
mcimadamore@1337 1761 }
mcimadamore@1337 1762 }
mcimadamore@1337 1763 }
mcimadamore@1337 1764 }
mcimadamore@1337 1765 //inference exception multiplexing - present any inference exception
mcimadamore@1337 1766 //thrown when processing listeners as a single one
mcimadamore@1337 1767 if (thrownEx != null) {
mcimadamore@1337 1768 throw thrownEx;
mcimadamore@1337 1769 }
mcimadamore@1337 1770 }
mcimadamore@1347 1771
mcimadamore@1562 1772 /**
mcimadamore@1562 1773 * Save the state of this inference context
mcimadamore@1562 1774 */
mcimadamore@1891 1775 List<Type> save() {
mcimadamore@1562 1776 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@1562 1777 for (Type t : undetvars) {
mcimadamore@1562 1778 UndetVar uv = (UndetVar)t;
mcimadamore@1562 1779 UndetVar uv2 = new UndetVar((TypeVar)uv.qtype, types);
mcimadamore@1562 1780 for (InferenceBound ib : InferenceBound.values()) {
mcimadamore@1562 1781 for (Type b : uv.getBounds(ib)) {
mcimadamore@1562 1782 uv2.addBound(ib, b, types);
mcimadamore@1562 1783 }
mcimadamore@1562 1784 }
mcimadamore@1562 1785 uv2.inst = uv.inst;
mcimadamore@1562 1786 buf.add(uv2);
mcimadamore@1562 1787 }
mcimadamore@1891 1788 return buf.toList();
mcimadamore@1562 1789 }
mcimadamore@1562 1790
mcimadamore@1562 1791 /**
mcimadamore@1562 1792 * Restore the state of this inference context to the previous known checkpoint
mcimadamore@1562 1793 */
mcimadamore@1891 1794 void rollback(List<Type> saved_undet) {
mcimadamore@1891 1795 Assert.check(saved_undet != null && saved_undet.length() == undetvars.length());
mcimadamore@1891 1796 //restore bounds (note: we need to preserve the old instances)
mcimadamore@1891 1797 for (Type t : undetvars) {
mcimadamore@1891 1798 UndetVar uv = (UndetVar)t;
mcimadamore@1891 1799 UndetVar uv_saved = (UndetVar)saved_undet.head;
mcimadamore@1891 1800 for (InferenceBound ib : InferenceBound.values()) {
mcimadamore@1891 1801 uv.setBounds(ib, uv_saved.getBounds(ib));
mcimadamore@1891 1802 }
mcimadamore@1891 1803 uv.inst = uv_saved.inst;
mcimadamore@1891 1804 saved_undet = saved_undet.tail;
mcimadamore@1891 1805 }
mcimadamore@1562 1806 }
mcimadamore@1562 1807
mcimadamore@1562 1808 /**
mcimadamore@1562 1809 * Copy variable in this inference context to the given context
mcimadamore@1562 1810 */
mcimadamore@1562 1811 void dupTo(final InferenceContext that) {
mcimadamore@1562 1812 that.inferencevars = that.inferencevars.appendList(inferencevars);
mcimadamore@1562 1813 that.undetvars = that.undetvars.appendList(undetvars);
mcimadamore@1562 1814 //set up listeners to notify original inference contexts as
mcimadamore@1562 1815 //propagated vars are inferred in new context
mcimadamore@1562 1816 for (Type t : inferencevars) {
mcimadamore@1562 1817 that.freeTypeListeners.put(new FreeTypeListener() {
mcimadamore@1562 1818 public void typesInferred(InferenceContext inferenceContext) {
mcimadamore@1562 1819 InferenceContext.this.notifyChange();
mcimadamore@1562 1820 }
mcimadamore@1562 1821 }, List.of(t));
mcimadamore@1562 1822 }
mcimadamore@1562 1823 }
mcimadamore@1562 1824
mcimadamore@1562 1825 /**
mcimadamore@1562 1826 * Solve with given graph strategy.
mcimadamore@1562 1827 */
mcimadamore@1562 1828 private void solve(GraphStrategy ss, Warner warn) {
mcimadamore@1562 1829 GraphSolver s = new GraphSolver(this, warn);
mcimadamore@1562 1830 s.solve(ss);
mcimadamore@1562 1831 }
mcimadamore@1562 1832
mcimadamore@1562 1833 /**
mcimadamore@1562 1834 * Solve all variables in this context.
mcimadamore@1562 1835 */
mcimadamore@1562 1836 public void solve(Warner warn) {
mcimadamore@1562 1837 solve(new LeafSolver() {
mcimadamore@1562 1838 public boolean done() {
mcimadamore@1562 1839 return restvars().isEmpty();
mcimadamore@1562 1840 }
mcimadamore@1562 1841 }, warn);
mcimadamore@1562 1842 }
mcimadamore@1562 1843
mcimadamore@1562 1844 /**
mcimadamore@1562 1845 * Solve all variables in the given list.
mcimadamore@1562 1846 */
mcimadamore@1562 1847 public void solve(final List<Type> vars, Warner warn) {
mcimadamore@1562 1848 solve(new BestLeafSolver(vars) {
mcimadamore@1562 1849 public boolean done() {
mcimadamore@1562 1850 return !free(asInstTypes(vars));
mcimadamore@1562 1851 }
mcimadamore@1562 1852 }, warn);
mcimadamore@1562 1853 }
mcimadamore@1562 1854
mcimadamore@1562 1855 /**
mcimadamore@1562 1856 * Solve at least one variable in given list.
mcimadamore@1562 1857 */
mcimadamore@1562 1858 public void solveAny(List<Type> varsToSolve, Warner warn) {
mcimadamore@1562 1859 checkWithinBounds(this, warn); //propagate bounds
mcimadamore@1562 1860 List<Type> boundedVars = boundedVars().intersect(restvars()).intersect(varsToSolve);
mcimadamore@1562 1861 if (boundedVars.isEmpty()) {
mcimadamore@1562 1862 throw inferenceException.setMessage("cyclic.inference",
mcimadamore@1562 1863 freeVarsIn(varsToSolve));
mcimadamore@1562 1864 }
mcimadamore@1562 1865 solve(new BestLeafSolver(boundedVars) {
mcimadamore@1562 1866 public boolean done() {
mcimadamore@1562 1867 return instvars().intersect(varsToSolve).nonEmpty();
mcimadamore@1562 1868 }
mcimadamore@1562 1869 }, warn);
mcimadamore@1562 1870 }
mcimadamore@1562 1871
mcimadamore@1562 1872 /**
mcimadamore@1562 1873 * Apply a set of inference steps
mcimadamore@1562 1874 */
mcimadamore@1562 1875 private boolean solveBasic(EnumSet<InferenceStep> steps) {
mcimadamore@1562 1876 return solveBasic(inferencevars, steps);
mcimadamore@1562 1877 }
mcimadamore@1562 1878
mcimadamore@1562 1879 private boolean solveBasic(List<Type> varsToSolve, EnumSet<InferenceStep> steps) {
mcimadamore@1562 1880 boolean changed = false;
mcimadamore@1562 1881 for (Type t : varsToSolve.intersect(restvars())) {
mcimadamore@1550 1882 UndetVar uv = (UndetVar)asFree(t);
mcimadamore@1562 1883 for (InferenceStep step : steps) {
mcimadamore@1562 1884 if (step.accepts(uv, this)) {
mcimadamore@1562 1885 uv.inst = step.solve(uv, this);
mcimadamore@1562 1886 changed = true;
mcimadamore@1562 1887 break;
mcimadamore@1347 1888 }
mcimadamore@1347 1889 }
mcimadamore@1347 1890 }
mcimadamore@1562 1891 return changed;
mcimadamore@1562 1892 }
mcimadamore@1562 1893
mcimadamore@1562 1894 /**
mcimadamore@1562 1895 * Instantiate inference variables in legacy mode (JLS 15.12.2.7, 15.12.2.8).
mcimadamore@1562 1896 * During overload resolution, instantiation is done by doing a partial
mcimadamore@1562 1897 * inference process using eq/lower bound instantiation. During check,
mcimadamore@1562 1898 * we also instantiate any remaining vars by repeatedly using eq/upper
mcimadamore@1562 1899 * instantiation, until all variables are solved.
mcimadamore@1562 1900 */
mcimadamore@1562 1901 public void solveLegacy(boolean partial, Warner warn, EnumSet<InferenceStep> steps) {
mcimadamore@1562 1902 while (true) {
mcimadamore@1562 1903 boolean stuck = !solveBasic(steps);
mcimadamore@1562 1904 if (restvars().isEmpty() || partial) {
mcimadamore@1562 1905 //all variables have been instantiated - exit
mcimadamore@1562 1906 break;
mcimadamore@1562 1907 } else if (stuck) {
mcimadamore@1562 1908 //some variables could not be instantiated because of cycles in
mcimadamore@1562 1909 //upper bounds - provide a (possibly recursive) default instantiation
mcimadamore@1562 1910 instantiateAsUninferredVars(restvars(), this);
mcimadamore@1562 1911 break;
mcimadamore@1562 1912 } else {
mcimadamore@1562 1913 //some variables have been instantiated - replace newly instantiated
mcimadamore@1562 1914 //variables in remaining upper bounds and continue
mcimadamore@1562 1915 for (Type t : undetvars) {
mcimadamore@1562 1916 UndetVar uv = (UndetVar)t;
mcimadamore@1562 1917 uv.substBounds(inferenceVars(), instTypes(), types);
mcimadamore@1562 1918 }
mcimadamore@1562 1919 }
mcimadamore@1347 1920 }
mcimadamore@1562 1921 checkWithinBounds(this, warn);
mcimadamore@1562 1922 }
mcimadamore@1562 1923
mcimadamore@1562 1924 private Infer infer() {
mcimadamore@1562 1925 //back-door to infer
mcimadamore@1562 1926 return Infer.this;
mcimadamore@1347 1927 }
mcimadamore@895 1928 }
mcimadamore@1337 1929
mcimadamore@1550 1930 final InferenceContext emptyContext = new InferenceContext(List.<Type>nil());
mcimadamore@1550 1931 // </editor-fold>
mcimadamore@1337 1932 }

mercurial