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