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

Fri, 08 Aug 2008 17:43:24 +0100

author
mcimadamore
date
Fri, 08 Aug 2008 17:43:24 +0100
changeset 94
6542933af8f4
parent 89
b6d5f53b3b29
child 104
5e89c4ca637c
permissions
-rw-r--r--

6676362: Spurious forward reference error with final var + instance variable initializer
Summary: Some javac forward reference errors aren't compliant with the JLS
Reviewed-by: jjg

duke@1 1 /*
xdono@54 2 * Copyright 1999-2008 Sun Microsystems, Inc. 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
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun 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 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.comp;
duke@1 27
duke@1 28 import com.sun.tools.javac.util.*;
duke@1 29 import com.sun.tools.javac.util.List;
duke@1 30 import com.sun.tools.javac.code.*;
duke@1 31 import com.sun.tools.javac.code.Type.*;
mcimadamore@89 32 import com.sun.tools.javac.util.JCDiagnostic;
duke@1 33
duke@1 34 import static com.sun.tools.javac.code.Flags.*;
duke@1 35 import static com.sun.tools.javac.code.Kinds.*;
duke@1 36 import static com.sun.tools.javac.code.TypeTags.*;
duke@1 37
duke@1 38 /** Helper class for type parameter inference, used by the attribution phase.
duke@1 39 *
duke@1 40 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 41 * you write code that depends on this, you do so at your own risk.
duke@1 42 * This code and its internal interfaces are subject to change or
duke@1 43 * deletion without notice.</b>
duke@1 44 */
duke@1 45 public class Infer {
duke@1 46 protected static final Context.Key<Infer> inferKey =
duke@1 47 new Context.Key<Infer>();
duke@1 48
duke@1 49 /** A value for prototypes that admit any type, including polymorphic ones. */
duke@1 50 public static final Type anyPoly = new Type(NONE, null);
duke@1 51
duke@1 52 Symtab syms;
duke@1 53 Types types;
mcimadamore@89 54 JCDiagnostic.Factory diags;
duke@1 55
duke@1 56 public static Infer instance(Context context) {
duke@1 57 Infer instance = context.get(inferKey);
duke@1 58 if (instance == null)
duke@1 59 instance = new Infer(context);
duke@1 60 return instance;
duke@1 61 }
duke@1 62
duke@1 63 protected Infer(Context context) {
duke@1 64 context.put(inferKey, this);
duke@1 65 syms = Symtab.instance(context);
duke@1 66 types = Types.instance(context);
mcimadamore@89 67 diags = JCDiagnostic.Factory.instance(context);
mcimadamore@89 68 ambiguousNoInstanceException =
mcimadamore@89 69 new NoInstanceException(true, diags);
mcimadamore@89 70 unambiguousNoInstanceException =
mcimadamore@89 71 new NoInstanceException(false, diags);
duke@1 72 }
duke@1 73
duke@1 74 public static class NoInstanceException extends RuntimeException {
duke@1 75 private static final long serialVersionUID = 0;
duke@1 76
duke@1 77 boolean isAmbiguous; // exist several incomparable best instances?
duke@1 78
duke@1 79 JCDiagnostic diagnostic;
mcimadamore@89 80 JCDiagnostic.Factory diags;
duke@1 81
mcimadamore@89 82 NoInstanceException(boolean isAmbiguous, JCDiagnostic.Factory diags) {
duke@1 83 this.diagnostic = null;
duke@1 84 this.isAmbiguous = isAmbiguous;
mcimadamore@89 85 this.diags = diags;
duke@1 86 }
duke@1 87 NoInstanceException setMessage(String key) {
mcimadamore@89 88 this.diagnostic = diags.fragment(key);
duke@1 89 return this;
duke@1 90 }
duke@1 91 NoInstanceException setMessage(String key, Object arg1) {
mcimadamore@89 92 this.diagnostic = diags.fragment(key, arg1);
duke@1 93 return this;
duke@1 94 }
duke@1 95 NoInstanceException setMessage(String key, Object arg1, Object arg2) {
mcimadamore@89 96 this.diagnostic = diags.fragment(key, arg1, arg2);
duke@1 97 return this;
duke@1 98 }
duke@1 99 NoInstanceException setMessage(String key, Object arg1, Object arg2, Object arg3) {
mcimadamore@89 100 this.diagnostic = diags.fragment(key, arg1, arg2, arg3);
duke@1 101 return this;
duke@1 102 }
duke@1 103 public JCDiagnostic getDiagnostic() {
duke@1 104 return diagnostic;
duke@1 105 }
duke@1 106 }
mcimadamore@89 107 private final NoInstanceException ambiguousNoInstanceException;
mcimadamore@89 108 private final NoInstanceException unambiguousNoInstanceException;
duke@1 109
duke@1 110 /***************************************************************************
duke@1 111 * Auxiliary type values and classes
duke@1 112 ***************************************************************************/
duke@1 113
duke@1 114 /** A mapping that turns type variables into undetermined type variables.
duke@1 115 */
duke@1 116 Mapping fromTypeVarFun = new Mapping("fromTypeVarFun") {
duke@1 117 public Type apply(Type t) {
duke@1 118 if (t.tag == TYPEVAR) return new UndetVar(t);
duke@1 119 else return t.map(this);
duke@1 120 }
duke@1 121 };
duke@1 122
duke@1 123 /** A mapping that returns its type argument with every UndetVar replaced
duke@1 124 * by its `inst' field. Throws a NoInstanceException
duke@1 125 * if this not possible because an `inst' field is null.
duke@1 126 */
duke@1 127 Mapping getInstFun = new Mapping("getInstFun") {
duke@1 128 public Type apply(Type t) {
duke@1 129 switch (t.tag) {
duke@1 130 case UNKNOWN:
duke@1 131 throw ambiguousNoInstanceException
duke@1 132 .setMessage("undetermined.type");
duke@1 133 case UNDETVAR:
duke@1 134 UndetVar that = (UndetVar) t;
duke@1 135 if (that.inst == null)
duke@1 136 throw ambiguousNoInstanceException
duke@1 137 .setMessage("type.variable.has.undetermined.type",
duke@1 138 that.qtype);
duke@1 139 return apply(that.inst);
duke@1 140 default:
duke@1 141 return t.map(this);
duke@1 142 }
duke@1 143 }
duke@1 144 };
duke@1 145
duke@1 146 /***************************************************************************
duke@1 147 * Mini/Maximization of UndetVars
duke@1 148 ***************************************************************************/
duke@1 149
duke@1 150 /** Instantiate undetermined type variable to its minimal upper bound.
duke@1 151 * Throw a NoInstanceException if this not possible.
duke@1 152 */
duke@1 153 void maximizeInst(UndetVar that, Warner warn) throws NoInstanceException {
duke@1 154 if (that.inst == null) {
duke@1 155 if (that.hibounds.isEmpty())
duke@1 156 that.inst = syms.objectType;
duke@1 157 else if (that.hibounds.tail.isEmpty())
duke@1 158 that.inst = that.hibounds.head;
duke@1 159 else {
duke@1 160 for (List<Type> bs = that.hibounds;
duke@1 161 bs.nonEmpty() && that.inst == null;
duke@1 162 bs = bs.tail) {
duke@1 163 // System.out.println("hibounds = " + that.hibounds);//DEBUG
duke@1 164 if (isSubClass(bs.head, that.hibounds))
duke@1 165 that.inst = types.fromUnknownFun.apply(bs.head);
duke@1 166 }
mcimadamore@17 167 if (that.inst == null) {
mcimadamore@17 168 int classCount = 0, interfaceCount = 0;
mcimadamore@17 169 for (Type t : that.hibounds) {
mcimadamore@17 170 if (t.tag == CLASS) {
mcimadamore@17 171 if (t.isInterface())
mcimadamore@17 172 interfaceCount++;
mcimadamore@17 173 else
mcimadamore@17 174 classCount++;
mcimadamore@17 175 }
mcimadamore@17 176 }
mcimadamore@17 177 if ((that.hibounds.size() == classCount + interfaceCount) && classCount == 1)
mcimadamore@17 178 that.inst = types.makeCompoundType(that.hibounds);
mcimadamore@17 179 }
duke@1 180 if (that.inst == null || !types.isSubtypeUnchecked(that.inst, that.hibounds, warn))
duke@1 181 throw ambiguousNoInstanceException
duke@1 182 .setMessage("no.unique.maximal.instance.exists",
duke@1 183 that.qtype, that.hibounds);
duke@1 184 }
duke@1 185 }
duke@1 186 }
duke@1 187 //where
duke@1 188 private boolean isSubClass(Type t, final List<Type> ts) {
duke@1 189 t = t.baseType();
duke@1 190 if (t.tag == TYPEVAR) {
duke@1 191 List<Type> bounds = types.getBounds((TypeVar)t);
duke@1 192 for (Type s : ts) {
duke@1 193 if (!types.isSameType(t, s.baseType())) {
duke@1 194 for (Type bound : bounds) {
duke@1 195 if (!isSubClass(bound, List.of(s.baseType())))
duke@1 196 return false;
duke@1 197 }
duke@1 198 }
duke@1 199 }
duke@1 200 } else {
duke@1 201 for (Type s : ts) {
duke@1 202 if (!t.tsym.isSubClass(s.baseType().tsym, types))
duke@1 203 return false;
duke@1 204 }
duke@1 205 }
duke@1 206 return true;
duke@1 207 }
duke@1 208
duke@1 209 /** Instaniate undetermined type variable to the lub of all its lower bounds.
duke@1 210 * Throw a NoInstanceException if this not possible.
duke@1 211 */
duke@1 212 void minimizeInst(UndetVar that, Warner warn) throws NoInstanceException {
duke@1 213 if (that.inst == null) {
duke@1 214 if (that.lobounds.isEmpty())
duke@1 215 that.inst = syms.botType;
duke@1 216 else if (that.lobounds.tail.isEmpty())
mcimadamore@5 217 that.inst = that.lobounds.head.isPrimitive() ? syms.errType : that.lobounds.head;
duke@1 218 else {
duke@1 219 that.inst = types.lub(that.lobounds);
mcimadamore@5 220 }
mcimadamore@5 221 if (that.inst == null || that.inst == syms.errType)
duke@1 222 throw ambiguousNoInstanceException
duke@1 223 .setMessage("no.unique.minimal.instance.exists",
duke@1 224 that.qtype, that.lobounds);
duke@1 225 // VGJ: sort of inlined maximizeInst() below. Adding
duke@1 226 // bounds can cause lobounds that are above hibounds.
duke@1 227 if (that.hibounds.isEmpty())
duke@1 228 return;
duke@1 229 Type hb = null;
duke@1 230 if (that.hibounds.tail.isEmpty())
duke@1 231 hb = that.hibounds.head;
duke@1 232 else for (List<Type> bs = that.hibounds;
duke@1 233 bs.nonEmpty() && hb == null;
duke@1 234 bs = bs.tail) {
duke@1 235 if (isSubClass(bs.head, that.hibounds))
duke@1 236 hb = types.fromUnknownFun.apply(bs.head);
duke@1 237 }
duke@1 238 if (hb == null ||
duke@1 239 !types.isSubtypeUnchecked(hb, that.hibounds, warn) ||
duke@1 240 !types.isSubtypeUnchecked(that.inst, hb, warn))
duke@1 241 throw ambiguousNoInstanceException;
duke@1 242 }
duke@1 243 }
duke@1 244
duke@1 245 /***************************************************************************
duke@1 246 * Exported Methods
duke@1 247 ***************************************************************************/
duke@1 248
duke@1 249 /** Try to instantiate expression type `that' to given type `to'.
duke@1 250 * If a maximal instantiation exists which makes this type
duke@1 251 * a subtype of type `to', return the instantiated type.
duke@1 252 * If no instantiation exists, or if several incomparable
duke@1 253 * best instantiations exist throw a NoInstanceException.
duke@1 254 */
duke@1 255 public Type instantiateExpr(ForAll that,
duke@1 256 Type to,
duke@1 257 Warner warn) throws NoInstanceException {
duke@1 258 List<Type> undetvars = Type.map(that.tvars, fromTypeVarFun);
duke@1 259 for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) {
duke@1 260 UndetVar v = (UndetVar) l.head;
duke@1 261 ListBuffer<Type> hibounds = new ListBuffer<Type>();
duke@1 262 for (List<Type> l1 = types.getBounds((TypeVar) v.qtype); l1.nonEmpty(); l1 = l1.tail) {
duke@1 263 if (!l1.head.containsSome(that.tvars)) {
duke@1 264 hibounds.append(l1.head);
duke@1 265 }
duke@1 266 }
duke@1 267 v.hibounds = hibounds.toList();
duke@1 268 }
duke@1 269 Type qtype1 = types.subst(that.qtype, that.tvars, undetvars);
duke@1 270 if (!types.isSubtype(qtype1, to)) {
duke@1 271 throw unambiguousNoInstanceException
duke@1 272 .setMessage("no.conforming.instance.exists",
duke@1 273 that.tvars, that.qtype, to);
duke@1 274 }
duke@1 275 for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail)
duke@1 276 maximizeInst((UndetVar) l.head, warn);
duke@1 277 // System.out.println(" = " + qtype1.map(getInstFun));//DEBUG
duke@1 278
duke@1 279 // check bounds
duke@1 280 List<Type> targs = Type.map(undetvars, getInstFun);
duke@1 281 targs = types.subst(targs, that.tvars, targs);
duke@1 282 checkWithinBounds(that.tvars, targs, warn);
duke@1 283
duke@1 284 return getInstFun.apply(qtype1);
duke@1 285 }
duke@1 286
duke@1 287 /** Instantiate method type `mt' by finding instantiations of
duke@1 288 * `tvars' so that method can be applied to `argtypes'.
duke@1 289 */
duke@1 290 public Type instantiateMethod(List<Type> tvars,
duke@1 291 MethodType mt,
duke@1 292 List<Type> argtypes,
duke@1 293 boolean allowBoxing,
duke@1 294 boolean useVarargs,
duke@1 295 Warner warn) throws NoInstanceException {
duke@1 296 //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
duke@1 297 List<Type> undetvars = Type.map(tvars, fromTypeVarFun);
duke@1 298 List<Type> formals = mt.argtypes;
duke@1 299
duke@1 300 // instantiate all polymorphic argument types and
duke@1 301 // set up lower bounds constraints for undetvars
duke@1 302 Type varargsFormal = useVarargs ? formals.last() : null;
duke@1 303 while (argtypes.nonEmpty() && formals.head != varargsFormal) {
duke@1 304 Type ft = formals.head;
duke@1 305 Type at = argtypes.head.baseType();
duke@1 306 if (at.tag == FORALL)
duke@1 307 at = instantiateArg((ForAll) at, ft, tvars, warn);
duke@1 308 Type sft = types.subst(ft, tvars, undetvars);
duke@1 309 boolean works = allowBoxing
duke@1 310 ? types.isConvertible(at, sft, warn)
duke@1 311 : types.isSubtypeUnchecked(at, sft, warn);
duke@1 312 if (!works) {
duke@1 313 throw unambiguousNoInstanceException
duke@1 314 .setMessage("no.conforming.assignment.exists",
duke@1 315 tvars, at, ft);
duke@1 316 }
duke@1 317 formals = formals.tail;
duke@1 318 argtypes = argtypes.tail;
duke@1 319 }
duke@1 320 if (formals.head != varargsFormal || // not enough args
duke@1 321 !useVarargs && argtypes.nonEmpty()) { // too many args
duke@1 322 // argument lists differ in length
duke@1 323 throw unambiguousNoInstanceException
duke@1 324 .setMessage("arg.length.mismatch");
duke@1 325 }
duke@1 326
duke@1 327 // for varargs arguments as well
duke@1 328 if (useVarargs) {
duke@1 329 Type elt = types.elemtype(varargsFormal);
duke@1 330 Type sft = types.subst(elt, tvars, undetvars);
duke@1 331 while (argtypes.nonEmpty()) {
duke@1 332 Type ft = sft;
duke@1 333 Type at = argtypes.head.baseType();
duke@1 334 if (at.tag == FORALL)
duke@1 335 at = instantiateArg((ForAll) at, ft, tvars, warn);
duke@1 336 boolean works = types.isConvertible(at, sft, warn);
duke@1 337 if (!works) {
duke@1 338 throw unambiguousNoInstanceException
duke@1 339 .setMessage("no.conforming.assignment.exists",
duke@1 340 tvars, at, ft);
duke@1 341 }
duke@1 342 argtypes = argtypes.tail;
duke@1 343 }
duke@1 344 }
duke@1 345
duke@1 346 // minimize as yet undetermined type variables
duke@1 347 for (Type t : undetvars)
duke@1 348 minimizeInst((UndetVar) t, warn);
duke@1 349
duke@1 350 /** Type variables instantiated to bottom */
duke@1 351 ListBuffer<Type> restvars = new ListBuffer<Type>();
duke@1 352
duke@1 353 /** Instantiated types or TypeVars if under-constrained */
duke@1 354 ListBuffer<Type> insttypes = new ListBuffer<Type>();
duke@1 355
duke@1 356 /** Instantiated types or UndetVars if under-constrained */
duke@1 357 ListBuffer<Type> undettypes = new ListBuffer<Type>();
duke@1 358
duke@1 359 for (Type t : undetvars) {
duke@1 360 UndetVar uv = (UndetVar)t;
duke@1 361 if (uv.inst.tag == BOT) {
duke@1 362 restvars.append(uv.qtype);
duke@1 363 insttypes.append(uv.qtype);
duke@1 364 undettypes.append(uv);
duke@1 365 uv.inst = null;
duke@1 366 } else {
duke@1 367 insttypes.append(uv.inst);
duke@1 368 undettypes.append(uv.inst);
duke@1 369 }
duke@1 370 }
duke@1 371 checkWithinBounds(tvars, undettypes.toList(), warn);
duke@1 372
duke@1 373 if (!restvars.isEmpty()) {
duke@1 374 // if there are uninstantiated variables,
duke@1 375 // quantify result type with them
duke@1 376 mt = new MethodType(mt.argtypes,
duke@1 377 new ForAll(restvars.toList(), mt.restype),
duke@1 378 mt.thrown, syms.methodClass);
duke@1 379 }
duke@1 380
duke@1 381 // return instantiated version of method type
duke@1 382 return types.subst(mt, tvars, insttypes.toList());
duke@1 383 }
duke@1 384 //where
duke@1 385
duke@1 386 /** Try to instantiate argument type `that' to given type `to'.
duke@1 387 * If this fails, try to insantiate `that' to `to' where
duke@1 388 * every occurrence of a type variable in `tvars' is replaced
duke@1 389 * by an unknown type.
duke@1 390 */
duke@1 391 private Type instantiateArg(ForAll that,
duke@1 392 Type to,
duke@1 393 List<Type> tvars,
duke@1 394 Warner warn) throws NoInstanceException {
duke@1 395 List<Type> targs;
duke@1 396 try {
duke@1 397 return instantiateExpr(that, to, warn);
duke@1 398 } catch (NoInstanceException ex) {
duke@1 399 Type to1 = to;
duke@1 400 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail)
duke@1 401 to1 = types.subst(to1, List.of(l.head), List.of(syms.unknownType));
duke@1 402 return instantiateExpr(that, to1, warn);
duke@1 403 }
duke@1 404 }
duke@1 405
duke@1 406 /** check that type parameters are within their bounds.
duke@1 407 */
duke@1 408 private void checkWithinBounds(List<Type> tvars,
duke@1 409 List<Type> arguments,
duke@1 410 Warner warn)
duke@1 411 throws NoInstanceException {
duke@1 412 for (List<Type> tvs = tvars, args = arguments;
duke@1 413 tvs.nonEmpty();
duke@1 414 tvs = tvs.tail, args = args.tail) {
duke@1 415 if (args.head instanceof UndetVar) continue;
duke@1 416 List<Type> bounds = types.subst(types.getBounds((TypeVar)tvs.head), tvars, arguments);
duke@1 417 if (!types.isSubtypeUnchecked(args.head, bounds, warn))
duke@1 418 throw unambiguousNoInstanceException
duke@1 419 .setMessage("inferred.do.not.conform.to.bounds",
duke@1 420 arguments, tvars);
duke@1 421 }
duke@1 422 }
duke@1 423 }

mercurial