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

changeset 1562
2154ed9ff6c8
parent 1551
8cdd96f2fdb9
child 1608
133a0a0c2cbc
equal deleted inserted replaced
1561:073696f59241 1562:2154ed9ff6c8
1 /* 1 /*
2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
23 * questions. 23 * questions.
24 */ 24 */
25 25
26 package com.sun.tools.javac.comp; 26 package com.sun.tools.javac.comp;
27 27
28 import com.sun.tools.javac.code.*;
29 import com.sun.tools.javac.code.Symbol.*;
30 import com.sun.tools.javac.code.Type.*;
31 import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
32 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
33 import com.sun.tools.javac.comp.Resolve.InapplicableMethodException;
34 import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode;
35 import com.sun.tools.javac.tree.JCTree; 28 import com.sun.tools.javac.tree.JCTree;
36 import com.sun.tools.javac.tree.JCTree.JCTypeCast; 29 import com.sun.tools.javac.tree.JCTree.JCTypeCast;
37 import com.sun.tools.javac.tree.TreeInfo; 30 import com.sun.tools.javac.tree.TreeInfo;
38 import com.sun.tools.javac.util.*; 31 import com.sun.tools.javac.util.*;
32 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
39 import com.sun.tools.javac.util.List; 33 import com.sun.tools.javac.util.List;
40 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 34 import com.sun.tools.javac.code.*;
35 import com.sun.tools.javac.code.Type.*;
36 import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
37 import com.sun.tools.javac.code.Symbol.*;
38 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
39 import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph;
40 import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph.Node;
41 import com.sun.tools.javac.comp.Resolve.InapplicableMethodException;
42 import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode;
41 43
42 import java.util.HashMap; 44 import java.util.HashMap;
43 import java.util.Map; 45 import java.util.Map;
46 import java.util.Set;
47
48 import java.util.ArrayList;
49 import java.util.Collections;
50 import java.util.EnumSet;
51 import java.util.HashSet;
44 52
45 import static com.sun.tools.javac.code.TypeTag.*; 53 import static com.sun.tools.javac.code.TypeTag.*;
46 54
47 /** Helper class for type parameter inference, used by the attribution phase. 55 /** Helper class for type parameter inference, used by the attribution phase.
48 * 56 *
53 */ 61 */
54 public class Infer { 62 public class Infer {
55 protected static final Context.Key<Infer> inferKey = 63 protected static final Context.Key<Infer> inferKey =
56 new Context.Key<Infer>(); 64 new Context.Key<Infer>();
57 65
58 /** A value for prototypes that admit any type, including polymorphic ones. */ 66 Resolve rs;
59 public static final Type anyPoly = new Type(NONE, null); 67 Check chk;
60
61 Symtab syms; 68 Symtab syms;
62 Types types; 69 Types types;
63 Check chk; 70 JCDiagnostic.Factory diags;
64 Resolve rs;
65 DeferredAttr deferredAttr;
66 Log log; 71 Log log;
67 JCDiagnostic.Factory diags; 72
68 73 /** should the graph solver be used? */
69 /** Should we inject return-type constraints earlier? */ 74 boolean allowGraphInference;
70 boolean allowEarlyReturnConstraints;
71 75
72 public static Infer instance(Context context) { 76 public static Infer instance(Context context) {
73 Infer instance = context.get(inferKey); 77 Infer instance = context.get(inferKey);
74 if (instance == null) 78 if (instance == null)
75 instance = new Infer(context); 79 instance = new Infer(context);
76 return instance; 80 return instance;
77 } 81 }
78 82
79 protected Infer(Context context) { 83 protected Infer(Context context) {
80 context.put(inferKey, this); 84 context.put(inferKey, this);
85
86 rs = Resolve.instance(context);
87 chk = Check.instance(context);
81 syms = Symtab.instance(context); 88 syms = Symtab.instance(context);
82 types = Types.instance(context); 89 types = Types.instance(context);
83 rs = Resolve.instance(context); 90 diags = JCDiagnostic.Factory.instance(context);
84 deferredAttr = DeferredAttr.instance(context);
85 log = Log.instance(context); 91 log = Log.instance(context);
86 chk = Check.instance(context);
87 diags = JCDiagnostic.Factory.instance(context);
88 inferenceException = new InferenceException(diags); 92 inferenceException = new InferenceException(diags);
89 allowEarlyReturnConstraints = Source.instance(context).allowEarlyReturnConstraints(); 93 Options options = Options.instance(context);
90 } 94 allowGraphInference = Source.instance(context).allowGraphInference()
95 && options.isUnset("useLegacyInference");
96 }
97
98 /** A value for prototypes that admit any type, including polymorphic ones. */
99 public static final Type anyPoly = new Type(NONE, null);
91 100
92 /** 101 /**
93 * This exception class is design to store a list of diagnostics corresponding 102 * This exception class is design to store a list of diagnostics corresponding
94 * to inference errors that can arise during a method applicability check. 103 * to inference errors that can arise during a method applicability check.
95 */ 104 */
116 void clear() { 125 void clear() {
117 messages = List.nil(); 126 messages = List.nil();
118 } 127 }
119 } 128 }
120 129
121 final InferenceException inferenceException; 130 protected final InferenceException inferenceException;
122 131
123 /*************************************************************************** 132 // <editor-fold defaultstate="collapsed" desc="Inference routines">
124 * Mini/Maximization of UndetVars 133 /**
125 ***************************************************************************/ 134 * Main inference entry point - instantiate a generic method type
126 135 * using given argument types and (possibly) an expected target-type.
127 /** Instantiate undetermined type variable to its minimal upper bound.
128 * Throw a NoInstanceException if this not possible.
129 */
130 void maximizeInst(UndetVar that, Warner warn) throws InferenceException {
131 List<Type> hibounds = Type.filter(that.getBounds(InferenceBound.UPPER), boundFilter);
132 if (that.getBounds(InferenceBound.EQ).isEmpty()) {
133 if (hibounds.isEmpty())
134 that.inst = syms.objectType;
135 else if (hibounds.tail.isEmpty())
136 that.inst = hibounds.head;
137 else
138 that.inst = types.glb(hibounds);
139 } else {
140 that.inst = that.getBounds(InferenceBound.EQ).head;
141 }
142 if (that.inst == null ||
143 that.inst.isErroneous())
144 throw inferenceException
145 .setMessage("no.unique.maximal.instance.exists",
146 that.qtype, hibounds);
147 }
148
149 private Filter<Type> boundFilter = new Filter<Type>() {
150 @Override
151 public boolean accepts(Type t) {
152 return !t.isErroneous() && !t.hasTag(BOT);
153 }
154 };
155
156 /** Instantiate undetermined type variable to the lub of all its lower bounds.
157 * Throw a NoInstanceException if this not possible.
158 */
159 void minimizeInst(UndetVar that, Warner warn) throws InferenceException {
160 List<Type> lobounds = Type.filter(that.getBounds(InferenceBound.LOWER), boundFilter);
161 if (that.getBounds(InferenceBound.EQ).isEmpty()) {
162 if (lobounds.isEmpty()) {
163 //do nothing - the inference variable is under-constrained
164 return;
165 } else if (lobounds.tail.isEmpty())
166 that.inst = lobounds.head.isPrimitive() ? syms.errType : lobounds.head;
167 else {
168 that.inst = types.lub(lobounds);
169 }
170 if (that.inst == null || that.inst.hasTag(ERROR))
171 throw inferenceException
172 .setMessage("no.unique.minimal.instance.exists",
173 that.qtype, lobounds);
174 } else {
175 that.inst = that.getBounds(InferenceBound.EQ).head;
176 }
177 }
178
179 /***************************************************************************
180 * Exported Methods
181 ***************************************************************************/
182
183 /**
184 * Instantiate uninferred inference variables (JLS 15.12.2.8). First
185 * if the method return type is non-void, we derive constraints from the
186 * expected type - then we use declared bound well-formedness to derive additional
187 * constraints. If no instantiation exists, or if several incomparable
188 * best instantiations exist throw a NoInstanceException.
189 */
190 public void instantiateUninferred(DiagnosticPosition pos,
191 InferenceContext inferenceContext,
192 MethodType mtype,
193 Attr.ResultInfo resultInfo,
194 Warner warn) throws InferenceException {
195 while (true) {
196 boolean stuck = true;
197 for (Type t : inferenceContext.undetvars) {
198 UndetVar uv = (UndetVar)t;
199 if (uv.inst == null && (uv.getBounds(InferenceBound.EQ).nonEmpty() ||
200 !inferenceContext.free(uv.getBounds(InferenceBound.UPPER)))) {
201 maximizeInst((UndetVar)t, warn);
202 stuck = false;
203 }
204 }
205 if (inferenceContext.restvars().isEmpty()) {
206 //all variables have been instantiated - exit
207 break;
208 } else if (stuck) {
209 //some variables could not be instantiated because of cycles in
210 //upper bounds - provide a (possibly recursive) default instantiation
211 instantiateAsUninferredVars(inferenceContext);
212 break;
213 } else {
214 //some variables have been instantiated - replace newly instantiated
215 //variables in remaining upper bounds and continue
216 for (Type t : inferenceContext.undetvars) {
217 UndetVar uv = (UndetVar)t;
218 uv.substBounds(inferenceContext.inferenceVars(), inferenceContext.instTypes(), types);
219 }
220 }
221 }
222 }
223
224 /**
225 * Infer cyclic inference variables as described in 15.12.2.8.
226 */
227 private void instantiateAsUninferredVars(InferenceContext inferenceContext) {
228 ListBuffer<Type> todo = ListBuffer.lb();
229 //step 1 - create fresh tvars
230 for (Type t : inferenceContext.undetvars) {
231 UndetVar uv = (UndetVar)t;
232 if (uv.inst == null) {
233 TypeSymbol fresh_tvar = new TypeSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner);
234 fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null);
235 todo.append(uv);
236 uv.inst = fresh_tvar.type;
237 }
238 }
239 //step 2 - replace fresh tvars in their bounds
240 List<Type> formals = inferenceContext.inferenceVars();
241 for (Type t : todo) {
242 UndetVar uv = (UndetVar)t;
243 TypeVar ct = (TypeVar)uv.inst;
244 ct.bound = types.glb(inferenceContext.asInstTypes(types.getBounds(ct)));
245 if (ct.bound.isErroneous()) {
246 //report inference error if glb fails
247 reportBoundError(uv, BoundErrorKind.BAD_UPPER);
248 }
249 formals = formals.tail;
250 }
251 }
252
253 /** Instantiate a generic method type by finding instantiations for all its
254 * inference variables so that it can be applied to a given argument type list.
255 */ 136 */
256 public Type instantiateMethod(Env<AttrContext> env, 137 public Type instantiateMethod(Env<AttrContext> env,
257 List<Type> tvars, 138 List<Type> tvars,
258 MethodType mt, 139 MethodType mt,
259 Attr.ResultInfo resultInfo, 140 Attr.ResultInfo resultInfo,
265 Resolve.MethodCheck methodCheck, 146 Resolve.MethodCheck methodCheck,
266 Warner warn) throws InferenceException { 147 Warner warn) throws InferenceException {
267 //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG 148 //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
268 final InferenceContext inferenceContext = new InferenceContext(tvars); 149 final InferenceContext inferenceContext = new InferenceContext(tvars);
269 inferenceException.clear(); 150 inferenceException.clear();
270
271 DeferredAttr.DeferredAttrContext deferredAttrContext =
272 resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn);
273
274 try { 151 try {
275 methodCheck.argumentsAcceptable(env, deferredAttrContext, argtypes, mt.getParameterTypes(), warn); 152 DeferredAttr.DeferredAttrContext deferredAttrContext =
276 153 resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn);
277 if (resultInfo != null && allowEarlyReturnConstraints && 154
155 methodCheck.argumentsAcceptable(env, deferredAttrContext,
156 argtypes, mt.getParameterTypes(), warn);
157
158 if (allowGraphInference &&
159 resultInfo != null &&
278 !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { 160 !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
279 generateReturnConstraints(mt, inferenceContext, resultInfo); 161 //inject return constraints earlier
162 checkWithinBounds(inferenceContext, warn); //propagation
163 generateReturnConstraints(resultInfo, mt, inferenceContext);
164 //propagate outwards if needed
165 if (resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) {
166 //propagate inference context outwards and exit
167 inferenceContext.dupTo(resultInfo.checkContext.inferenceContext());
168 deferredAttrContext.complete();
169 return mt;
170 }
280 } 171 }
281 172
282 deferredAttrContext.complete(); 173 deferredAttrContext.complete();
283 174
284 // minimize as yet undetermined type variables 175 // minimize as yet undetermined type variables
285 for (Type t : inferenceContext.undetvars) { 176 if (allowGraphInference) {
286 minimizeInst((UndetVar)t, warn); 177 inferenceContext.solve(warn);
287 } 178 } else {
288 179 inferenceContext.solveLegacy(true, warn, LegacyInferenceSteps.EQ_LOWER.steps); //minimizeInst
289 checkWithinBounds(inferenceContext, warn); 180 }
290 181
291 mt = (MethodType)inferenceContext.asInstType(mt); 182 mt = (MethodType)inferenceContext.asInstType(mt);
292 183
293 List<Type> restvars = inferenceContext.restvars(); 184 if (!allowGraphInference &&
294 185 inferenceContext.restvars().nonEmpty() &&
295 if (!restvars.isEmpty()) { 186 resultInfo != null &&
296 if (resultInfo != null && !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { 187 !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
297 if (!allowEarlyReturnConstraints) { 188 generateReturnConstraints(resultInfo, mt, inferenceContext);
298 generateReturnConstraints(mt, inferenceContext, resultInfo); 189 inferenceContext.solveLegacy(false, warn, LegacyInferenceSteps.EQ_UPPER.steps); //maximizeInst
299 } 190 mt = (MethodType)inferenceContext.asInstType(mt);
300 instantiateUninferred(env.tree.pos(), inferenceContext, mt, resultInfo, warn); 191 }
301 checkWithinBounds(inferenceContext, warn); 192
302 mt = (MethodType)inferenceContext.asInstType(mt); 193 if (resultInfo != null && rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) {
303 if (rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) { 194 log.note(env.tree.pos, "deferred.method.inst", msym, mt, resultInfo.pt);
304 log.note(env.tree.pos, "deferred.method.inst", msym, mt, resultInfo.pt);
305 }
306 }
307 } 195 }
308 196
309 // return instantiated version of method type 197 // return instantiated version of method type
310 return mt; 198 return mt;
311 } finally { 199 } finally {
312 inferenceContext.notifyChange(); 200 if (resultInfo != null || !allowGraphInference) {
201 inferenceContext.notifyChange();
202 } else {
203 inferenceContext.notifyChange(inferenceContext.boundedVars());
204 }
205 }
206 }
207
208 /**
209 * Generate constraints from the generic method's return type. If the method
210 * call occurs in a context where a type T is expected, use the expected
211 * type to derive more constraints on the generic method inference variables.
212 */
213 void generateReturnConstraints(Attr.ResultInfo resultInfo,
214 MethodType mt, InferenceContext inferenceContext) {
215 Type qtype1 = inferenceContext.asFree(mt.getReturnType());
216 Type to = returnConstraintTarget(qtype1, resultInfo.pt);
217 Assert.check(allowGraphInference || !resultInfo.checkContext.inferenceContext().free(to),
218 "legacy inference engine cannot handle constraints on both sides of a subtyping assertion");
219 //we need to skip capture?
220 Warner retWarn = new Warner();
221 if (!resultInfo.checkContext.compatible(qtype1, resultInfo.checkContext.inferenceContext().asFree(to), retWarn) ||
222 //unchecked conversion is not allowed
223 retWarn.hasLint(Lint.LintCategory.UNCHECKED)) {
224 throw inferenceException
225 .setMessage("infer.no.conforming.instance.exists",
226 inferenceContext.restvars(), mt.getReturnType(), to);
313 } 227 }
314 } 228 }
315 //where 229 //where
316 void generateReturnConstraints(Type mt, InferenceContext inferenceContext, Attr.ResultInfo resultInfo) { 230 private Type returnConstraintTarget(Type from, Type to) {
317 if (resultInfo != null) { 231 if (from.hasTag(VOID)) {
318 Type to = resultInfo.pt; 232 return syms.voidType;
319 if (to.hasTag(NONE) || resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) { 233 } else if (to.hasTag(NONE)) {
320 to = mt.getReturnType().isPrimitiveOrVoid() ? 234 return from.isPrimitive() ? from : syms.objectType;
321 mt.getReturnType() : syms.objectType; 235 } else if (from.hasTag(UNDETVAR) && to.isPrimitive()) {
322 } 236 if (!allowGraphInference) {
323 Type qtype1 = inferenceContext.asFree(mt.getReturnType()); 237 //if legacy, just return boxed type
324 Warner retWarn = new Warner(); 238 return types.boxedClass(to).type;
325 if (!resultInfo.checkContext.compatible(qtype1, qtype1.hasTag(UNDETVAR) ? types.boxedTypeOrType(to) : to, retWarn) || 239 }
326 //unchecked conversion is not allowed 240 //if graph inference we need to skip conflicting boxed bounds...
327 retWarn.hasLint(Lint.LintCategory.UNCHECKED)) { 241 UndetVar uv = (UndetVar)from;
328 throw inferenceException 242 for (Type t : uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) {
329 .setMessage("infer.no.conforming.instance.exists", 243 Type boundAsPrimitive = types.unboxedType(t);
330 inferenceContext.restvars(), mt.getReturnType(), to); 244 if (boundAsPrimitive == null) continue;
331 } 245 if (types.isConvertible(boundAsPrimitive, to)) {
332 } 246 //effectively skip return-type constraint generation (compatibility)
333 } 247 return syms.objectType;
334 248 }
335 /** check that type parameters are within their bounds. 249 }
336 */ 250 return types.boxedClass(to).type;
337 void checkWithinBounds(InferenceContext inferenceContext, 251 } else {
338 Warner warn) throws InferenceException { 252 return to;
339 //step 1 - check compatibility of instantiated type w.r.t. initial bounds 253 }
340 for (Type t : inferenceContext.undetvars) { 254 }
255
256 /**
257 * Infer cyclic inference variables as described in 15.12.2.8.
258 */
259 private void instantiateAsUninferredVars(List<Type> vars, InferenceContext inferenceContext) {
260 ListBuffer<Type> todo = ListBuffer.lb();
261 //step 1 - create fresh tvars
262 for (Type t : vars) {
263 UndetVar uv = (UndetVar)inferenceContext.asFree(t);
264 List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER);
265 if (Type.containsAny(upperBounds, vars)) {
266 TypeSymbol fresh_tvar = new TypeSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner);
267 fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null);
268 todo.append(uv);
269 uv.inst = fresh_tvar.type;
270 } else if (upperBounds.nonEmpty()) {
271 uv.inst = types.glb(upperBounds);
272 } else {
273 uv.inst = syms.objectType;
274 }
275 }
276 //step 2 - replace fresh tvars in their bounds
277 List<Type> formals = vars;
278 for (Type t : todo) {
341 UndetVar uv = (UndetVar)t; 279 UndetVar uv = (UndetVar)t;
342 uv.substBounds(inferenceContext.inferenceVars(), inferenceContext.instTypes(), types); 280 TypeVar ct = (TypeVar)uv.inst;
343 checkCompatibleUpperBounds(uv, inferenceContext.inferenceVars()); 281 ct.bound = types.glb(inferenceContext.asInstTypes(types.getBounds(ct)));
344 if (!inferenceContext.restvars().contains(uv.qtype)) { 282 if (ct.bound.isErroneous()) {
345 Type inst = inferenceContext.asInstType(t); 283 //report inference error if glb fails
346 for (Type u : uv.getBounds(InferenceBound.UPPER)) { 284 reportBoundError(uv, BoundErrorKind.BAD_UPPER);
347 if (!types.isSubtypeUnchecked(inst, inferenceContext.asFree(u), warn)) { 285 }
348 reportBoundError(uv, BoundErrorKind.UPPER); 286 formals = formals.tail;
349 } 287 }
350 } 288 }
351 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
352 Assert.check(!inferenceContext.free(l));
353 if (!types.isSubtypeUnchecked(l, inst, warn)) {
354 reportBoundError(uv, BoundErrorKind.LOWER);
355 }
356 }
357 for (Type e : uv.getBounds(InferenceBound.EQ)) {
358 Assert.check(!inferenceContext.free(e));
359 if (!types.isSameType(inst, e)) {
360 reportBoundError(uv, BoundErrorKind.EQ);
361 }
362 }
363 }
364 }
365
366 //step 2 - check that eq bounds are consistent w.r.t. eq/lower bounds
367 for (Type t : inferenceContext.undetvars) {
368 UndetVar uv = (UndetVar)t;
369 //check eq bounds consistency
370 Type eq = null;
371 for (Type e : uv.getBounds(InferenceBound.EQ)) {
372 Assert.check(!inferenceContext.free(e));
373 if (eq != null && !types.isSameType(e, eq)) {
374 reportBoundError(uv, BoundErrorKind.EQ);
375 }
376 eq = e;
377 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
378 Assert.check(!inferenceContext.free(l));
379 if (!types.isSubtypeUnchecked(l, e, warn)) {
380 reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
381 }
382 }
383 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
384 if (inferenceContext.free(u)) continue;
385 if (!types.isSubtypeUnchecked(e, u, warn)) {
386 reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
387 }
388 }
389 }
390 }
391 }
392
393 void checkCompatibleUpperBounds(UndetVar uv, List<Type> tvars) {
394 // VGJ: sort of inlined maximizeInst() below. Adding
395 // bounds can cause lobounds that are above hibounds.
396 ListBuffer<Type> hiboundsNoVars = ListBuffer.lb();
397 for (Type t : Type.filter(uv.getBounds(InferenceBound.UPPER), boundFilter)) {
398 if (!t.containsAny(tvars)) {
399 hiboundsNoVars.append(t);
400 }
401 }
402 List<Type> hibounds = hiboundsNoVars.toList();
403 Type hb = null;
404 if (hibounds.isEmpty())
405 hb = syms.objectType;
406 else if (hibounds.tail.isEmpty())
407 hb = hibounds.head;
408 else
409 hb = types.glb(hibounds);
410 if (hb == null || hb.isErroneous())
411 reportBoundError(uv, BoundErrorKind.BAD_UPPER);
412 }
413
414 enum BoundErrorKind {
415 BAD_UPPER() {
416 @Override
417 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
418 return ex.setMessage("incompatible.upper.bounds", uv.qtype,
419 uv.getBounds(InferenceBound.UPPER));
420 }
421 },
422 BAD_EQ_UPPER() {
423 @Override
424 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
425 return ex.setMessage("incompatible.eq.upper.bounds", uv.qtype,
426 uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.UPPER));
427 }
428 },
429 BAD_EQ_LOWER() {
430 @Override
431 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
432 return ex.setMessage("incompatible.eq.lower.bounds", uv.qtype,
433 uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.LOWER));
434 }
435 },
436 UPPER() {
437 @Override
438 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
439 return ex.setMessage("inferred.do.not.conform.to.upper.bounds", uv.inst,
440 uv.getBounds(InferenceBound.UPPER));
441 }
442 },
443 LOWER() {
444 @Override
445 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
446 return ex.setMessage("inferred.do.not.conform.to.lower.bounds", uv.inst,
447 uv.getBounds(InferenceBound.LOWER));
448 }
449 },
450 EQ() {
451 @Override
452 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
453 return ex.setMessage("inferred.do.not.conform.to.eq.bounds", uv.inst,
454 uv.getBounds(InferenceBound.EQ));
455 }
456 };
457
458 abstract InapplicableMethodException setMessage(InferenceException ex, UndetVar uv);
459 }
460 //where
461 void reportBoundError(UndetVar uv, BoundErrorKind bk) {
462 throw bk.setMessage(inferenceException, uv);
463 }
464
465 // <editor-fold desc="functional interface instantiation">
466 /**
467 * This method is used to infer a suitable target functional interface in case
468 * the original parameterized interface contains wildcards. An inference process
469 * is applied so that wildcard bounds, as well as explicit lambda/method ref parameters
470 * (where applicable) are used to constraint the solution.
471 */
472 public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface,
473 List<Type> paramTypes, Check.CheckContext checkContext) {
474 if (types.capture(funcInterface) == funcInterface) {
475 //if capture doesn't change the type then return the target unchanged
476 //(this means the target contains no wildcards!)
477 return funcInterface;
478 } else {
479 Type formalInterface = funcInterface.tsym.type;
480 InferenceContext funcInterfaceContext =
481 new InferenceContext(funcInterface.tsym.type.getTypeArguments());
482 Assert.check(paramTypes != null);
483 //get constraints from explicit params (this is done by
484 //checking that explicit param types are equal to the ones
485 //in the functional interface descriptors)
486 List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes();
487 if (descParameterTypes.size() != paramTypes.size()) {
488 checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda"));
489 return types.createErrorType(funcInterface);
490 }
491 for (Type p : descParameterTypes) {
492 if (!types.isSameType(funcInterfaceContext.asFree(p), paramTypes.head)) {
493 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
494 return types.createErrorType(funcInterface);
495 }
496 paramTypes = paramTypes.tail;
497 }
498 List<Type> actualTypeargs = funcInterface.getTypeArguments();
499 for (Type t : funcInterfaceContext.undetvars) {
500 UndetVar uv = (UndetVar)t;
501 if (funcInterfaceContext.boundedVars().contains(uv.qtype)) {
502 minimizeInst(uv, types.noWarnings);
503 if (uv.inst == null &&
504 Type.filter(uv.getBounds(InferenceBound.UPPER), boundFilter).nonEmpty()) {
505 maximizeInst(uv, types.noWarnings);
506 }
507 } else {
508 uv.inst = actualTypeargs.head;
509 }
510 Assert.check(uv.inst != null);
511 actualTypeargs = actualTypeargs.tail;
512 }
513 Type owntype = funcInterfaceContext.asInstType(formalInterface);
514 if (!chk.checkValidGenericType(owntype)) {
515 //if the inferred functional interface type is not well-formed,
516 //or if it's not a subtype of the original target, issue an error
517 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
518 }
519 return owntype;
520 }
521 }
522 // </editor-fold>
523 289
524 /** 290 /**
525 * Compute a synthetic method type corresponding to the requested polymorphic 291 * Compute a synthetic method type corresponding to the requested polymorphic
526 * method signature. The target return type is computed from the immediately 292 * method signature. The target return type is computed from the immediately
527 * enclosing scope surrounding the polymorphic-signature call. 293 * enclosing scope surrounding the polymorphic-signature call.
571 } 337 }
572 //where 338 //where
573 class ImplicitArgType extends DeferredAttr.DeferredTypeMap { 339 class ImplicitArgType extends DeferredAttr.DeferredTypeMap {
574 340
575 public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) { 341 public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) {
576 deferredAttr.super(AttrMode.SPECULATIVE, msym, phase); 342 rs.deferredAttr.super(AttrMode.SPECULATIVE, msym, phase);
577 } 343 }
578 344
579 public Type apply(Type t) { 345 public Type apply(Type t) {
580 t = types.erasure(super.apply(t)); 346 t = types.erasure(super.apply(t));
581 if (t.hasTag(BOT)) 347 if (t.hasTag(BOT))
585 return t; 351 return t;
586 } 352 }
587 } 353 }
588 354
589 /** 355 /**
356 * This method is used to infer a suitable target SAM in case the original
357 * SAM type contains one or more wildcards. An inference process is applied
358 * so that wildcard bounds, as well as explicit lambda/method ref parameters
359 * (where applicable) are used to constraint the solution.
360 */
361 public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface,
362 List<Type> paramTypes, Check.CheckContext checkContext) {
363 if (types.capture(funcInterface) == funcInterface) {
364 //if capture doesn't change the type then return the target unchanged
365 //(this means the target contains no wildcards!)
366 return funcInterface;
367 } else {
368 Type formalInterface = funcInterface.tsym.type;
369 InferenceContext funcInterfaceContext =
370 new InferenceContext(funcInterface.tsym.type.getTypeArguments());
371
372 Assert.check(paramTypes != null);
373 //get constraints from explicit params (this is done by
374 //checking that explicit param types are equal to the ones
375 //in the functional interface descriptors)
376 List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes();
377 if (descParameterTypes.size() != paramTypes.size()) {
378 checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda"));
379 return types.createErrorType(funcInterface);
380 }
381 for (Type p : descParameterTypes) {
382 if (!types.isSameType(funcInterfaceContext.asFree(p), paramTypes.head)) {
383 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
384 return types.createErrorType(funcInterface);
385 }
386 paramTypes = paramTypes.tail;
387 }
388
389 try {
390 funcInterfaceContext.solve(funcInterfaceContext.boundedVars(), types.noWarnings);
391 } catch (InferenceException ex) {
392 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
393 }
394
395 List<Type> actualTypeargs = funcInterface.getTypeArguments();
396 for (Type t : funcInterfaceContext.undetvars) {
397 UndetVar uv = (UndetVar)t;
398 if (uv.inst == null) {
399 uv.inst = actualTypeargs.head;
400 }
401 actualTypeargs = actualTypeargs.tail;
402 }
403
404 Type owntype = funcInterfaceContext.asInstType(formalInterface);
405 if (!chk.checkValidGenericType(owntype)) {
406 //if the inferred functional interface type is not well-formed,
407 //or if it's not a subtype of the original target, issue an error
408 checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
409 }
410 return owntype;
411 }
412 }
413 // </editor-fold>
414
415 // <editor-fold defaultstate="collapsed" desc="Bound checking">
416 /**
417 * Check bounds and perform incorporation
418 */
419 void checkWithinBounds(InferenceContext inferenceContext,
420 Warner warn) throws InferenceException {
421 MultiUndetVarListener mlistener = new MultiUndetVarListener(inferenceContext.undetvars);
422 try {
423 while (true) {
424 mlistener.reset();
425 if (!allowGraphInference) {
426 //in legacy mode we lack of transitivity, so bound check
427 //cannot be run in parallel with other incoprporation rounds
428 for (Type t : inferenceContext.undetvars) {
429 UndetVar uv = (UndetVar)t;
430 IncorporationStep.CHECK_BOUNDS.apply(uv, inferenceContext, warn);
431 }
432 }
433 for (Type t : inferenceContext.undetvars) {
434 UndetVar uv = (UndetVar)t;
435 //bound incorporation
436 EnumSet<IncorporationStep> incorporationSteps = allowGraphInference ?
437 incorporationStepsGraph : incorporationStepsLegacy;
438 for (IncorporationStep is : incorporationSteps) {
439 is.apply(uv, inferenceContext, warn);
440 }
441 }
442 if (!mlistener.changed || !allowGraphInference) break;
443 }
444 }
445 finally {
446 mlistener.detach();
447 }
448 }
449 //where
450 /**
451 * This listener keeps track of changes on a group of inference variable
452 * bounds. Note: the listener must be detached (calling corresponding
453 * method) to make sure that the underlying inference variable is
454 * left in a clean state.
455 */
456 class MultiUndetVarListener implements UndetVar.UndetVarListener {
457
458 int rounds;
459 boolean changed;
460 List<Type> undetvars;
461
462 public MultiUndetVarListener(List<Type> undetvars) {
463 this.undetvars = undetvars;
464 for (Type t : undetvars) {
465 UndetVar uv = (UndetVar)t;
466 uv.listener = this;
467 }
468 }
469
470 public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
471 //avoid non-termination
472 if (rounds < MAX_INCORPORATION_STEPS) {
473 changed = true;
474 }
475 }
476
477 void reset() {
478 rounds++;
479 changed = false;
480 }
481
482 void detach() {
483 for (Type t : undetvars) {
484 UndetVar uv = (UndetVar)t;
485 uv.listener = null;
486 }
487 }
488 };
489
490 /** max number of incorporation rounds */
491 static final int MAX_INCORPORATION_STEPS = 100;
492
493 /**
494 * This enumeration defines an entry point for doing inference variable
495 * bound incorporation - it can be used to inject custom incorporation
496 * logic into the basic bound checking routine
497 */
498 enum IncorporationStep {
499 /**
500 * Performs basic bound checking - i.e. is the instantiated type for a given
501 * inference variable compatible with its bounds?
502 */
503 CHECK_BOUNDS() {
504 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
505 Infer infer = inferenceContext.infer();
506 uv.substBounds(inferenceContext.inferenceVars(), inferenceContext.instTypes(), infer.types);
507 infer.checkCompatibleUpperBounds(uv, inferenceContext);
508 if (uv.inst != null) {
509 Type inst = uv.inst;
510 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
511 if (!infer.types.isSubtypeUnchecked(inst, inferenceContext.asFree(u), warn)) {
512 infer.reportBoundError(uv, BoundErrorKind.UPPER);
513 }
514 }
515 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
516 if (!infer.types.isSubtypeUnchecked(inferenceContext.asFree(l), inst, warn)) {
517 infer.reportBoundError(uv, BoundErrorKind.LOWER);
518 }
519 }
520 for (Type e : uv.getBounds(InferenceBound.EQ)) {
521 if (!infer.types.isSameType(inst, inferenceContext.asFree(e))) {
522 infer.reportBoundError(uv, BoundErrorKind.EQ);
523 }
524 }
525 }
526 }
527 },
528 /**
529 * Check consistency of equality constraints. This is a slightly more aggressive
530 * inference routine that is designed as to maximize compatibility with JDK 7.
531 * Note: this is not used in graph mode.
532 */
533 EQ_CHECK_LEGACY() {
534 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
535 Infer infer = inferenceContext.infer();
536 Type eq = null;
537 for (Type e : uv.getBounds(InferenceBound.EQ)) {
538 Assert.check(!inferenceContext.free(e));
539 if (eq != null && !infer.types.isSameType(e, eq)) {
540 infer.reportBoundError(uv, BoundErrorKind.EQ);
541 }
542 eq = e;
543 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
544 Assert.check(!inferenceContext.free(l));
545 if (!infer.types.isSubtypeUnchecked(l, e, warn)) {
546 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
547 }
548 }
549 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
550 if (inferenceContext.free(u)) continue;
551 if (!infer.types.isSubtypeUnchecked(e, u, warn)) {
552 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
553 }
554 }
555 }
556 }
557 },
558 /**
559 * Check consistency of equality constraints.
560 */
561 EQ_CHECK() {
562 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
563 Infer infer = inferenceContext.infer();
564 for (Type e : uv.getBounds(InferenceBound.EQ)) {
565 if (e.containsAny(inferenceContext.inferenceVars())) continue;
566 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
567 if (!infer.types.isSubtypeUnchecked(e, inferenceContext.asFree(u), warn)) {
568 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
569 }
570 }
571 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
572 if (!infer.types.isSubtypeUnchecked(inferenceContext.asFree(l), e, warn)) {
573 infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
574 }
575 }
576 }
577 }
578 },
579 /**
580 * Given a bound set containing {@code alpha <: T} and {@code alpha :> S}
581 * perform {@code S <: T} (which could lead to new bounds).
582 */
583 CROSS_UPPER_LOWER() {
584 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
585 Infer infer = inferenceContext.infer();
586 for (Type b1 : uv.getBounds(InferenceBound.UPPER)) {
587 for (Type b2 : uv.getBounds(InferenceBound.LOWER)) {
588 if (!inferenceContext.inferenceVars().contains(b1) &&
589 !inferenceContext.inferenceVars().contains(b2) &&
590 infer.types.asSuper(b2, b1.tsym) != null) {
591 infer.types.isSubtypeUnchecked(inferenceContext.asFree(b2), inferenceContext.asFree(b1));
592 }
593 }
594 }
595 }
596 },
597 /**
598 * Given a bound set containing {@code alpha <: T} and {@code alpha == S}
599 * perform {@code S <: T} (which could lead to new bounds).
600 */
601 CROSS_UPPER_EQ() {
602 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
603 Infer infer = inferenceContext.infer();
604 for (Type b1 : uv.getBounds(InferenceBound.UPPER)) {
605 for (Type b2 : uv.getBounds(InferenceBound.EQ)) {
606 if (!inferenceContext.inferenceVars().contains(b1) &&
607 !inferenceContext.inferenceVars().contains(b2) &&
608 infer.types.asSuper(b2, b1.tsym) != null) {
609 infer.types.isSubtypeUnchecked(inferenceContext.asFree(b2), inferenceContext.asFree(b1));
610 }
611 }
612 }
613 }
614 },
615 /**
616 * Given a bound set containing {@code alpha :> S} and {@code alpha == T}
617 * perform {@code S <: T} (which could lead to new bounds).
618 */
619 CROSS_EQ_LOWER() {
620 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
621 Infer infer = inferenceContext.infer();
622 for (Type b1 : uv.getBounds(InferenceBound.EQ)) {
623 for (Type b2 : uv.getBounds(InferenceBound.LOWER)) {
624 if (!inferenceContext.inferenceVars().contains(b1) &&
625 !inferenceContext.inferenceVars().contains(b2) &&
626 infer.types.asSuper(b2, b1.tsym) != null) {
627 infer.types.isSubtypeUnchecked(inferenceContext.asFree(b2), inferenceContext.asFree(b1));
628 }
629 }
630 }
631 }
632 },
633 /**
634 * Given a bound set containing {@code alpha <: beta} propagate lower bounds
635 * from alpha to beta; also propagate upper bounds from beta to alpha.
636 */
637 PROP_UPPER() {
638 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
639 Infer infer = inferenceContext.infer();
640 for (Type b : uv.getBounds(InferenceBound.UPPER)) {
641 if (inferenceContext.inferenceVars().contains(b)) {
642 UndetVar uv2 = (UndetVar)inferenceContext.asFree(b);
643 //alpha <: beta
644 //1. copy alpha's lower to beta's
645 for (Type l : uv.getBounds(InferenceBound.LOWER)) {
646 uv2.addBound(InferenceBound.LOWER, inferenceContext.asInstType(l), infer.types);
647 }
648 //2. copy beta's upper to alpha's
649 for (Type u : uv2.getBounds(InferenceBound.UPPER)) {
650 uv.addBound(InferenceBound.UPPER, inferenceContext.asInstType(u), infer.types);
651 }
652 }
653 }
654 }
655 },
656 /**
657 * Given a bound set containing {@code alpha :> beta} propagate lower bounds
658 * from beta to alpha; also propagate upper bounds from alpha to beta.
659 */
660 PROP_LOWER() {
661 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
662 Infer infer = inferenceContext.infer();
663 for (Type b : uv.getBounds(InferenceBound.LOWER)) {
664 if (inferenceContext.inferenceVars().contains(b)) {
665 UndetVar uv2 = (UndetVar)inferenceContext.asFree(b);
666 //alpha :> beta
667 //1. copy alpha's upper to beta's
668 for (Type u : uv.getBounds(InferenceBound.UPPER)) {
669 uv2.addBound(InferenceBound.UPPER, inferenceContext.asInstType(u), infer.types);
670 }
671 //2. copy beta's lower to alpha's
672 for (Type l : uv2.getBounds(InferenceBound.LOWER)) {
673 uv.addBound(InferenceBound.LOWER, inferenceContext.asInstType(l), infer.types);
674 }
675 }
676 }
677 }
678 },
679 /**
680 * Given a bound set containing {@code alpha == beta} propagate lower/upper
681 * bounds from alpha to beta and back.
682 */
683 PROP_EQ() {
684 public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
685 Infer infer = inferenceContext.infer();
686 for (Type b : uv.getBounds(InferenceBound.EQ)) {
687 if (inferenceContext.inferenceVars().contains(b)) {
688 UndetVar uv2 = (UndetVar)inferenceContext.asFree(b);
689 //alpha == beta
690 //1. copy all alpha's bounds to beta's
691 for (InferenceBound ib : InferenceBound.values()) {
692 for (Type b2 : uv.getBounds(ib)) {
693 if (b2 != uv2) {
694 uv2.addBound(ib, inferenceContext.asInstType(b2), infer.types);
695 }
696 }
697 }
698 //2. copy all beta's bounds to alpha's
699 for (InferenceBound ib : InferenceBound.values()) {
700 for (Type b2 : uv2.getBounds(ib)) {
701 if (b2 != uv) {
702 uv.addBound(ib, inferenceContext.asInstType(b2), infer.types);
703 }
704 }
705 }
706 }
707 }
708 }
709 };
710
711 abstract void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn);
712 }
713
714 /** incorporation steps to be executed when running in legacy mode */
715 EnumSet<IncorporationStep> incorporationStepsLegacy = EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY);
716
717 /** incorporation steps to be executed when running in graph mode */
718 EnumSet<IncorporationStep> incorporationStepsGraph =
719 EnumSet.complementOf(EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY));
720
721 /**
722 * Make sure that the upper bounds we got so far lead to a solvable inference
723 * variable by making sure that a glb exists.
724 */
725 void checkCompatibleUpperBounds(UndetVar uv, InferenceContext inferenceContext) {
726 List<Type> hibounds =
727 Type.filter(uv.getBounds(InferenceBound.UPPER), new BoundFilter(inferenceContext));
728 Type hb = null;
729 if (hibounds.isEmpty())
730 hb = syms.objectType;
731 else if (hibounds.tail.isEmpty())
732 hb = hibounds.head;
733 else
734 hb = types.glb(hibounds);
735 if (hb == null || hb.isErroneous())
736 reportBoundError(uv, BoundErrorKind.BAD_UPPER);
737 }
738 //where
739 protected static class BoundFilter implements Filter<Type> {
740
741 InferenceContext inferenceContext;
742
743 public BoundFilter(InferenceContext inferenceContext) {
744 this.inferenceContext = inferenceContext;
745 }
746
747 @Override
748 public boolean accepts(Type t) {
749 return !t.isErroneous() && !inferenceContext.free(t) &&
750 !t.hasTag(BOT);
751 }
752 };
753
754 /**
755 * This enumeration defines all possible bound-checking related errors.
756 */
757 enum BoundErrorKind {
758 /**
759 * The (uninstantiated) inference variable has incompatible upper bounds.
760 */
761 BAD_UPPER() {
762 @Override
763 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
764 return ex.setMessage("incompatible.upper.bounds", uv.qtype,
765 uv.getBounds(InferenceBound.UPPER));
766 }
767 },
768 /**
769 * An equality constraint is not compatible with an upper bound.
770 */
771 BAD_EQ_UPPER() {
772 @Override
773 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
774 return ex.setMessage("incompatible.eq.upper.bounds", uv.qtype,
775 uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.UPPER));
776 }
777 },
778 /**
779 * An equality constraint is not compatible with a lower bound.
780 */
781 BAD_EQ_LOWER() {
782 @Override
783 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
784 return ex.setMessage("incompatible.eq.lower.bounds", uv.qtype,
785 uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.LOWER));
786 }
787 },
788 /**
789 * Instantiated inference variable is not compatible with an upper bound.
790 */
791 UPPER() {
792 @Override
793 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
794 return ex.setMessage("inferred.do.not.conform.to.upper.bounds", uv.inst,
795 uv.getBounds(InferenceBound.UPPER));
796 }
797 },
798 /**
799 * Instantiated inference variable is not compatible with a lower bound.
800 */
801 LOWER() {
802 @Override
803 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
804 return ex.setMessage("inferred.do.not.conform.to.lower.bounds", uv.inst,
805 uv.getBounds(InferenceBound.LOWER));
806 }
807 },
808 /**
809 * Instantiated inference variable is not compatible with an equality constraint.
810 */
811 EQ() {
812 @Override
813 InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
814 return ex.setMessage("inferred.do.not.conform.to.eq.bounds", uv.inst,
815 uv.getBounds(InferenceBound.EQ));
816 }
817 };
818
819 abstract InapplicableMethodException setMessage(InferenceException ex, UndetVar uv);
820 }
821
822 /**
823 * Report a bound-checking error of given kind
824 */
825 void reportBoundError(UndetVar uv, BoundErrorKind bk) {
826 throw bk.setMessage(inferenceException, uv);
827 }
828 // </editor-fold>
829
830 // <editor-fold defaultstate="collapsed" desc="Inference engine">
831 /**
832 * Graph inference strategy - act as an input to the inference solver; a strategy is
833 * composed of two ingredients: (i) find a node to solve in the inference graph,
834 * and (ii) tell th engine when we are done fixing inference variables
835 */
836 interface GraphStrategy {
837 /**
838 * Pick the next node (leaf) to solve in the graph
839 */
840 Node pickNode(InferenceGraph g);
841 /**
842 * Is this the last step?
843 */
844 boolean done();
845 }
846
847 /**
848 * Simple solver strategy class that locates all leaves inside a graph
849 * and picks the first leaf as the next node to solve
850 */
851 abstract class LeafSolver implements GraphStrategy {
852 public Node pickNode(InferenceGraph g) {
853 Assert.check(!g.nodes.isEmpty(), "No nodes to solve!");
854 return g.nodes.get(0);
855 }
856 }
857
858 /**
859 * This solver uses an heuristic to pick the best leaf - the heuristic
860 * tries to select the node that has maximal probability to contain one
861 * or more inference variables in a given list
862 */
863 abstract class BestLeafSolver extends LeafSolver {
864
865 List<Type> varsToSolve;
866
867 BestLeafSolver(List<Type> varsToSolve) {
868 this.varsToSolve = varsToSolve;
869 }
870
871 /**
872 * Computes the cost associated with a given node; the cost is computed
873 * as the total number of type-variables that should be eagerly instantiated
874 * in order to get to some of the variables in {@code varsToSolve} from
875 * a given node
876 */
877 void computeCostIfNeeded(Node n, Map<Node, Integer> costMap) {
878 if (costMap.containsKey(n)) {
879 return;
880 } else if (!Collections.disjoint(n.data, varsToSolve)) {
881 costMap.put(n, n.data.size());
882 } else {
883 int subcost = Integer.MAX_VALUE;
884 costMap.put(n, subcost); //avoid loops
885 for (Node n2 : n.getDependencies()) {
886 computeCostIfNeeded(n2, costMap);
887 subcost = Math.min(costMap.get(n2), subcost);
888 }
889 //update cost map to reflect real cost
890 costMap.put(n, subcost == Integer.MAX_VALUE ?
891 Integer.MAX_VALUE :
892 n.data.size() + subcost);
893 }
894 }
895
896 /**
897 * Pick the leaf that minimize cost
898 */
899 @Override
900 public Node pickNode(final InferenceGraph g) {
901 final Map<Node, Integer> costMap = new HashMap<Node, Integer>();
902 ArrayList<Node> leaves = new ArrayList<Node>();
903 for (Node n : g.nodes) {
904 computeCostIfNeeded(n, costMap);
905 if (n.isLeaf(n)) {
906 leaves.add(n);
907 }
908 }
909 Assert.check(!leaves.isEmpty(), "No nodes to solve!");
910 Collections.sort(leaves, new java.util.Comparator<Node>() {
911 public int compare(Node n1, Node n2) {
912 return costMap.get(n1) - costMap.get(n2);
913 }
914 });
915 return leaves.get(0);
916 }
917 }
918
919 /**
920 * The inference process can be thought of as a sequence of steps. Each step
921 * instantiates an inference variable using a subset of the inference variable
922 * bounds, if certain condition are met. Decisions such as the sequence in which
923 * steps are applied, or which steps are to be applied are left to the inference engine.
924 */
925 enum InferenceStep {
926
927 /**
928 * Instantiate an inference variables using one of its (ground) equality
929 * constraints
930 */
931 EQ(InferenceBound.EQ) {
932 @Override
933 Type solve(UndetVar uv, InferenceContext inferenceContext) {
934 return filterBounds(uv, inferenceContext).head;
935 }
936 },
937 /**
938 * Instantiate an inference variables using its (ground) lower bounds. Such
939 * bounds are merged together using lub().
940 */
941 LOWER(InferenceBound.LOWER) {
942 @Override
943 Type solve(UndetVar uv, InferenceContext inferenceContext) {
944 Infer infer = inferenceContext.infer();
945 List<Type> lobounds = filterBounds(uv, inferenceContext);
946 Type owntype = infer.types.lub(lobounds);
947 if (owntype.hasTag(ERROR)) {
948 throw infer.inferenceException
949 .setMessage("no.unique.minimal.instance.exists",
950 uv.qtype, lobounds);
951 } else {
952 return owntype;
953 }
954 }
955 },
956 /**
957 * Instantiate an inference variables using its (ground) upper bounds. Such
958 * bounds are merged together using glb().
959 */
960 UPPER(InferenceBound.UPPER) {
961 @Override
962 Type solve(UndetVar uv, InferenceContext inferenceContext) {
963 Infer infer = inferenceContext.infer();
964 List<Type> hibounds = filterBounds(uv, inferenceContext);
965 Type owntype = infer.types.glb(hibounds);
966 if (owntype.isErroneous()) {
967 throw infer.inferenceException
968 .setMessage("no.unique.maximal.instance.exists",
969 uv.qtype, hibounds);
970 } else {
971 return owntype;
972 }
973 }
974 },
975 /**
976 * Like the former; the only difference is that this step can only be applied
977 * if all upper bounds are ground.
978 */
979 UPPER_LEGACY(InferenceBound.UPPER) {
980 @Override
981 public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
982 return !inferenceContext.free(t.getBounds(ib));
983 }
984
985 @Override
986 Type solve(UndetVar uv, InferenceContext inferenceContext) {
987 return UPPER.solve(uv, inferenceContext);
988 }
989 };
990
991 final InferenceBound ib;
992
993 InferenceStep(InferenceBound ib) {
994 this.ib = ib;
995 }
996
997 /**
998 * Find an instantiated type for a given inference variable within
999 * a given inference context
1000 */
1001 abstract Type solve(UndetVar uv, InferenceContext inferenceContext);
1002
1003 /**
1004 * Can the inference variable be instantiated using this step?
1005 */
1006 public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
1007 return filterBounds(t, inferenceContext).nonEmpty();
1008 }
1009
1010 /**
1011 * Return the subset of ground bounds in a given bound set (i.e. eq/lower/upper)
1012 */
1013 List<Type> filterBounds(UndetVar uv, InferenceContext inferenceContext) {
1014 return Type.filter(uv.getBounds(ib), new BoundFilter(inferenceContext));
1015 }
1016 }
1017
1018 /**
1019 * This enumeration defines the sequence of steps to be applied when the
1020 * solver works in legacy mode. The steps in this enumeration reflect
1021 * the behavior of old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8).
1022 */
1023 enum LegacyInferenceSteps {
1024
1025 EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
1026 EQ_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.UPPER_LEGACY));
1027
1028 final EnumSet<InferenceStep> steps;
1029
1030 LegacyInferenceSteps(EnumSet<InferenceStep> steps) {
1031 this.steps = steps;
1032 }
1033 }
1034
1035 /**
1036 * This enumeration defines the sequence of steps to be applied when the
1037 * graph solver is used. This order is defined so as to maximize compatibility
1038 * w.r.t. old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8).
1039 */
1040 enum GraphInferenceSteps {
1041
1042 EQ(EnumSet.of(InferenceStep.EQ)),
1043 EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
1044 EQ_LOWER_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER, InferenceStep.UPPER));
1045
1046 final EnumSet<InferenceStep> steps;
1047
1048 GraphInferenceSteps(EnumSet<InferenceStep> steps) {
1049 this.steps = steps;
1050 }
1051 }
1052
1053 /**
1054 * This is the graph inference solver - the solver organizes all inference variables in
1055 * a given inference context by bound dependencies - in the general case, such dependencies
1056 * would lead to a cyclic directed graph (hence the name); the dependency info is used to build
1057 * an acyclic graph, where all cyclic variables are bundled together. An inference
1058 * step corresponds to solving a node in the acyclic graph - this is done by
1059 * relying on a given strategy (see GraphStrategy).
1060 */
1061 class GraphSolver {
1062
1063 InferenceContext inferenceContext;
1064 Warner warn;
1065
1066 GraphSolver(InferenceContext inferenceContext, Warner warn) {
1067 this.inferenceContext = inferenceContext;
1068 this.warn = warn;
1069 }
1070
1071 /**
1072 * Solve variables in a given inference context. The amount of variables
1073 * to be solved, and the way in which the underlying acyclic graph is explored
1074 * depends on the selected solver strategy.
1075 */
1076 void solve(GraphStrategy sstrategy) {
1077 checkWithinBounds(inferenceContext, warn); //initial propagation of bounds
1078 InferenceGraph inferenceGraph = new InferenceGraph();
1079 while (!sstrategy.done()) {
1080 InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph);
1081 List<Type> varsToSolve = List.from(nodeToSolve.data);
1082 inferenceContext.save();
1083 try {
1084 //repeat until all variables are solved
1085 outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) {
1086 //for each inference phase
1087 for (GraphInferenceSteps step : GraphInferenceSteps.values()) {
1088 if (inferenceContext.solveBasic(varsToSolve, step.steps)) {
1089 checkWithinBounds(inferenceContext, warn);
1090 continue outer;
1091 }
1092 }
1093 //no progress
1094 throw inferenceException;
1095 }
1096 }
1097 catch (InferenceException ex) {
1098 inferenceContext.rollback();
1099 instantiateAsUninferredVars(varsToSolve, inferenceContext);
1100 checkWithinBounds(inferenceContext, warn);
1101 }
1102 inferenceGraph.deleteNode(nodeToSolve);
1103 }
1104 }
1105
1106 /**
1107 * The dependencies between the inference variables that need to be solved
1108 * form a (possibly cyclic) graph. This class reduces the original dependency graph
1109 * to an acyclic version, where cyclic nodes are folded into a single 'super node'.
1110 */
1111 class InferenceGraph {
1112
1113 /**
1114 * This class represents a node in the graph. Each node corresponds
1115 * to an inference variable and has edges (dependencies) on other
1116 * nodes. The node defines an entry point that can be used to receive
1117 * updates on the structure of the graph this node belongs to (used to
1118 * keep dependencies in sync).
1119 */
1120 class Node extends GraphUtils.TarjanNode<ListBuffer<Type>> {
1121
1122 Set<Node> deps;
1123
1124 Node(Type ivar) {
1125 super(ListBuffer.of(ivar));
1126 this.deps = new HashSet<Node>();
1127 }
1128
1129 @Override
1130 public Iterable<? extends Node> getDependencies() {
1131 return deps;
1132 }
1133
1134 @Override
1135 public String printDependency(GraphUtils.Node<ListBuffer<Type>> to) {
1136 StringBuilder buf = new StringBuilder();
1137 String sep = "";
1138 for (Type from : data) {
1139 UndetVar uv = (UndetVar)inferenceContext.asFree(from);
1140 for (Type bound : uv.getBounds(InferenceBound.values())) {
1141 if (bound.containsAny(List.from(to.data))) {
1142 buf.append(sep);
1143 buf.append(bound);
1144 sep = ",";
1145 }
1146 }
1147 }
1148 return buf.toString();
1149 }
1150
1151 boolean isLeaf(Node n) {
1152 //no deps, or only one self dep
1153 return (n.deps.isEmpty() ||
1154 n.deps.size() == 1 && n.deps.contains(n));
1155 }
1156
1157 void mergeWith(List<? extends Node> nodes) {
1158 for (Node n : nodes) {
1159 Assert.check(n.data.length() == 1, "Attempt to merge a compound node!");
1160 data.appendList(n.data);
1161 deps.addAll(n.deps);
1162 }
1163 //update deps
1164 Set<Node> deps2 = new HashSet<Node>();
1165 for (Node d : deps) {
1166 if (data.contains(d.data.first())) {
1167 deps2.add(this);
1168 } else {
1169 deps2.add(d);
1170 }
1171 }
1172 deps = deps2;
1173 }
1174
1175 void graphChanged(Node from, Node to) {
1176 if (deps.contains(from)) {
1177 deps.remove(from);
1178 if (to != null) {
1179 deps.add(to);
1180 }
1181 }
1182 }
1183 }
1184
1185 /** the nodes in the inference graph */
1186 ArrayList<Node> nodes;
1187
1188 InferenceGraph() {
1189 initNodes();
1190 }
1191
1192 /**
1193 * Delete a node from the graph. This update the underlying structure
1194 * of the graph (including dependencies) via listeners updates.
1195 */
1196 public void deleteNode(Node n) {
1197 Assert.check(nodes.contains(n));
1198 nodes.remove(n);
1199 notifyUpdate(n, null);
1200 }
1201
1202 /**
1203 * Notify all nodes of a change in the graph. If the target node is
1204 * {@code null} the source node is assumed to be removed.
1205 */
1206 void notifyUpdate(Node from, Node to) {
1207 for (Node n : nodes) {
1208 n.graphChanged(from, to);
1209 }
1210 }
1211
1212 /**
1213 * Create the graph nodes. First a simple node is created for every inference
1214 * variables to be solved. Then Tarjan is used to found all connected components
1215 * in the graph. For each component containing more than one node, a super node is
1216 * created, effectively replacing the original cyclic nodes.
1217 */
1218 void initNodes() {
1219 ArrayList<Node> nodes = new ArrayList<Node>();
1220 for (Type t : inferenceContext.restvars()) {
1221 nodes.add(new Node(t));
1222 }
1223 for (Node n_i : nodes) {
1224 Type i = n_i.data.first();
1225 for (Node n_j : nodes) {
1226 Type j = n_j.data.first();
1227 UndetVar uv_i = (UndetVar)inferenceContext.asFree(i);
1228 if (Type.containsAny(uv_i.getBounds(InferenceBound.values()), List.of(j))) {
1229 //update i's deps
1230 n_i.deps.add(n_j);
1231 //update j's deps - only if i's bounds contain _exactly_ j
1232 if (uv_i.getBounds(InferenceBound.values()).contains(j)) {
1233 n_j.deps.add(n_i);
1234 }
1235 }
1236 }
1237 }
1238 this.nodes = new ArrayList<Node>();
1239 for (List<? extends Node> conSubGraph : GraphUtils.tarjan(nodes)) {
1240 if (conSubGraph.length() > 1) {
1241 Node root = conSubGraph.head;
1242 root.mergeWith(conSubGraph.tail);
1243 for (Node n : conSubGraph) {
1244 notifyUpdate(n, root);
1245 }
1246 }
1247 this.nodes.add(conSubGraph.head);
1248 }
1249 }
1250
1251 /**
1252 * Debugging: dot representation of this graph
1253 */
1254 String toDot() {
1255 StringBuilder buf = new StringBuilder();
1256 for (Type t : inferenceContext.undetvars) {
1257 UndetVar uv = (UndetVar)t;
1258 buf.append(String.format("var %s - upper bounds = %s, lower bounds = %s, eq bounds = %s\\n",
1259 uv.qtype, uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER),
1260 uv.getBounds(InferenceBound.EQ)));
1261 }
1262 return GraphUtils.toDot(nodes, "inferenceGraph" + hashCode(), buf.toString());
1263 }
1264 }
1265 }
1266 // </editor-fold>
1267
1268 // <editor-fold defaultstate="collapsed" desc="Inference context">
1269 /**
590 * Functional interface for defining inference callbacks. Certain actions 1270 * Functional interface for defining inference callbacks. Certain actions
591 * (i.e. subtyping checks) might need to be redone after all inference variables 1271 * (i.e. subtyping checks) might need to be redone after all inference variables
592 * have been fixed. 1272 * have been fixed.
593 */ 1273 */
594 interface FreeTypeListener { 1274 interface FreeTypeListener {
601 * types to their corresponding free/closed forms. It also provide hooks for 1281 * types to their corresponding free/closed forms. It also provide hooks for
602 * attaching deferred post-inference action (see PendingCheck). Finally, 1282 * attaching deferred post-inference action (see PendingCheck). Finally,
603 * it can be used as an entry point for performing upper/lower bound inference 1283 * it can be used as an entry point for performing upper/lower bound inference
604 * (see InferenceKind). 1284 * (see InferenceKind).
605 */ 1285 */
606 class InferenceContext { 1286 class InferenceContext {
607 1287
608 /** list of inference vars as undet vars */ 1288 /** list of inference vars as undet vars */
609 List<Type> undetvars; 1289 List<Type> undetvars;
610 1290
611 /** list of inference vars in this context */ 1291 /** list of inference vars in this context */
612 List<Type> inferencevars; 1292 List<Type> inferencevars;
1293
1294 /** backed up inference variables */
1295 List<Type> saved_undet;
613 1296
614 java.util.Map<FreeTypeListener, List<Type>> freeTypeListeners = 1297 java.util.Map<FreeTypeListener, List<Type>> freeTypeListeners =
615 new java.util.HashMap<FreeTypeListener, List<Type>>(); 1298 new java.util.HashMap<FreeTypeListener, List<Type>>();
616 1299
617 List<FreeTypeListener> freetypeListeners = List.nil(); 1300 List<FreeTypeListener> freetypeListeners = List.nil();
780 /** 1463 /**
781 * Mark the inference context as complete and trigger evaluation 1464 * Mark the inference context as complete and trigger evaluation
782 * of all deferred checks. 1465 * of all deferred checks.
783 */ 1466 */
784 void notifyChange() { 1467 void notifyChange() {
1468 notifyChange(inferencevars.diff(restvars()));
1469 }
1470
1471 void notifyChange(List<Type> inferredVars) {
785 InferenceException thrownEx = null; 1472 InferenceException thrownEx = null;
786 for (Map.Entry<FreeTypeListener, List<Type>> entry : 1473 for (Map.Entry<FreeTypeListener, List<Type>> entry :
787 new HashMap<FreeTypeListener, List<Type>>(freeTypeListeners).entrySet()) { 1474 new HashMap<FreeTypeListener, List<Type>>(freeTypeListeners).entrySet()) {
788 if (!Type.containsAny(entry.getValue(), restvars())) { 1475 if (!Type.containsAny(entry.getValue(), inferencevars.diff(inferredVars))) {
789 try { 1476 try {
790 entry.getKey().typesInferred(this); 1477 entry.getKey().typesInferred(this);
791 freeTypeListeners.remove(entry.getKey()); 1478 freeTypeListeners.remove(entry.getKey());
792 } catch (InferenceException ex) { 1479 } catch (InferenceException ex) {
793 if (thrownEx == null) { 1480 if (thrownEx == null) {
801 if (thrownEx != null) { 1488 if (thrownEx != null) {
802 throw thrownEx; 1489 throw thrownEx;
803 } 1490 }
804 } 1491 }
805 1492
806 void solveAny(List<Type> varsToSolve) { 1493 /**
807 boolean progress = false; 1494 * Save the state of this inference context
808 for (Type t : varsToSolve) { 1495 */
1496 void save() {
1497 ListBuffer<Type> buf = ListBuffer.lb();
1498 for (Type t : undetvars) {
1499 UndetVar uv = (UndetVar)t;
1500 UndetVar uv2 = new UndetVar((TypeVar)uv.qtype, types);
1501 for (InferenceBound ib : InferenceBound.values()) {
1502 for (Type b : uv.getBounds(ib)) {
1503 uv2.addBound(ib, b, types);
1504 }
1505 }
1506 uv2.inst = uv.inst;
1507 buf.add(uv2);
1508 }
1509 saved_undet = buf.toList();
1510 }
1511
1512 /**
1513 * Restore the state of this inference context to the previous known checkpoint
1514 */
1515 void rollback() {
1516 Assert.check(saved_undet != null && saved_undet.length() == undetvars.length());
1517 undetvars = saved_undet;
1518 saved_undet = null;
1519 }
1520
1521 /**
1522 * Copy variable in this inference context to the given context
1523 */
1524 void dupTo(final InferenceContext that) {
1525 that.inferencevars = that.inferencevars.appendList(inferencevars);
1526 that.undetvars = that.undetvars.appendList(undetvars);
1527 //set up listeners to notify original inference contexts as
1528 //propagated vars are inferred in new context
1529 for (Type t : inferencevars) {
1530 that.freeTypeListeners.put(new FreeTypeListener() {
1531 public void typesInferred(InferenceContext inferenceContext) {
1532 InferenceContext.this.notifyChange();
1533 }
1534 }, List.of(t));
1535 }
1536 }
1537
1538 /**
1539 * Solve with given graph strategy.
1540 */
1541 private void solve(GraphStrategy ss, Warner warn) {
1542 GraphSolver s = new GraphSolver(this, warn);
1543 s.solve(ss);
1544 }
1545
1546 /**
1547 * Solve all variables in this context.
1548 */
1549 public void solve(Warner warn) {
1550 solve(new LeafSolver() {
1551 public boolean done() {
1552 return restvars().isEmpty();
1553 }
1554 }, warn);
1555 }
1556
1557 /**
1558 * Solve all variables in the given list.
1559 */
1560 public void solve(final List<Type> vars, Warner warn) {
1561 solve(new BestLeafSolver(vars) {
1562 public boolean done() {
1563 return !free(asInstTypes(vars));
1564 }
1565 }, warn);
1566 }
1567
1568 /**
1569 * Solve at least one variable in given list.
1570 */
1571 public void solveAny(List<Type> varsToSolve, Warner warn) {
1572 checkWithinBounds(this, warn); //propagate bounds
1573 List<Type> boundedVars = boundedVars().intersect(restvars()).intersect(varsToSolve);
1574 if (boundedVars.isEmpty()) {
1575 throw inferenceException.setMessage("cyclic.inference",
1576 freeVarsIn(varsToSolve));
1577 }
1578 solve(new BestLeafSolver(boundedVars) {
1579 public boolean done() {
1580 return instvars().intersect(varsToSolve).nonEmpty();
1581 }
1582 }, warn);
1583 }
1584
1585 /**
1586 * Apply a set of inference steps
1587 */
1588 private boolean solveBasic(EnumSet<InferenceStep> steps) {
1589 return solveBasic(inferencevars, steps);
1590 }
1591
1592 private boolean solveBasic(List<Type> varsToSolve, EnumSet<InferenceStep> steps) {
1593 boolean changed = false;
1594 for (Type t : varsToSolve.intersect(restvars())) {
809 UndetVar uv = (UndetVar)asFree(t); 1595 UndetVar uv = (UndetVar)asFree(t);
810 if (uv.inst == null) { 1596 for (InferenceStep step : steps) {
811 minimizeInst(uv, types.noWarnings); 1597 if (step.accepts(uv, this)) {
812 if (uv.inst != null) { 1598 uv.inst = step.solve(uv, this);
813 progress = true; 1599 changed = true;
814 } 1600 break;
815 } 1601 }
816 } 1602 }
817 if (!progress) { 1603 }
818 throw inferenceException.setMessage("cyclic.inference", varsToSolve); 1604 return changed;
819 } 1605 }
1606
1607 /**
1608 * Instantiate inference variables in legacy mode (JLS 15.12.2.7, 15.12.2.8).
1609 * During overload resolution, instantiation is done by doing a partial
1610 * inference process using eq/lower bound instantiation. During check,
1611 * we also instantiate any remaining vars by repeatedly using eq/upper
1612 * instantiation, until all variables are solved.
1613 */
1614 public void solveLegacy(boolean partial, Warner warn, EnumSet<InferenceStep> steps) {
1615 while (true) {
1616 boolean stuck = !solveBasic(steps);
1617 if (restvars().isEmpty() || partial) {
1618 //all variables have been instantiated - exit
1619 break;
1620 } else if (stuck) {
1621 //some variables could not be instantiated because of cycles in
1622 //upper bounds - provide a (possibly recursive) default instantiation
1623 instantiateAsUninferredVars(restvars(), this);
1624 break;
1625 } else {
1626 //some variables have been instantiated - replace newly instantiated
1627 //variables in remaining upper bounds and continue
1628 for (Type t : undetvars) {
1629 UndetVar uv = (UndetVar)t;
1630 uv.substBounds(inferenceVars(), instTypes(), types);
1631 }
1632 }
1633 }
1634 checkWithinBounds(this, warn);
1635 }
1636
1637 private Infer infer() {
1638 //back-door to infer
1639 return Infer.this;
820 } 1640 }
821 } 1641 }
822 1642
823 final InferenceContext emptyContext = new InferenceContext(List.<Type>nil()); 1643 final InferenceContext emptyContext = new InferenceContext(List.<Type>nil());
824 // </editor-fold> 1644 // </editor-fold>

mercurial