|
42 | 42 |
|
43 | 43 | import com.sun.tools.javac.code.Attribute.RetentionPolicy;
|
44 | 44 | import com.sun.tools.javac.code.Lint.LintCategory;
|
45 |
| -import com.sun.tools.javac.code.Source.Feature; |
46 | 45 | import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
|
47 | 46 | import com.sun.tools.javac.code.TypeMetadata.Annotations;
|
48 | 47 | import com.sun.tools.javac.comp.AttrContext;
|
49 | 48 | import com.sun.tools.javac.comp.Check;
|
50 | 49 | import com.sun.tools.javac.comp.Enter;
|
51 | 50 | import com.sun.tools.javac.comp.Env;
|
52 |
| -import com.sun.tools.javac.comp.LambdaToMethod; |
53 | 51 | import com.sun.tools.javac.jvm.ClassFile;
|
54 | 52 | import com.sun.tools.javac.util.*;
|
55 | 53 |
|
@@ -2236,60 +2234,64 @@ public Type visitErrorType(ErrorType t, Symbol sym) {
|
2236 | 2234 | };
|
2237 | 2235 |
|
2238 | 2236 | /**
|
2239 |
| - * Return the base type of t or any of its outer types that starts |
2240 |
| - * with the given symbol. If none exists, return null. |
| 2237 | + * This method returns the first type in a sequence (starting at `t`) that is |
| 2238 | + * a subclass of `sym`. The next type in the sequence is obtained by calling |
| 2239 | + * `getEnclosingType()` on the previous type in the sequence. Note, this is |
| 2240 | + * typically used to compute the implicit qualifier in a method/field access |
| 2241 | + * expression. Example: |
2241 | 2242 | *
|
2242 |
| - * @param t a type |
2243 |
| - * @param sym a symbol |
| 2243 | + * static class Sup<F> { public F f; } |
| 2244 | + * class Outer { |
| 2245 | + * static class Sub extends Sup<String> { |
| 2246 | + * class I { |
| 2247 | + * void test() { |
| 2248 | + * String f2 = f; // Sup<String>::f |
| 2249 | + * } |
| 2250 | + * } |
| 2251 | + * } |
| 2252 | + * } |
| 2253 | + * |
| 2254 | + * @param t a type |
| 2255 | + * @param sym a symbol |
2244 | 2256 | */
|
2245 | 2257 | public Type asOuterSuper(Type t, Symbol sym) {
|
2246 |
| - switch (t.getTag()) { |
2247 |
| - case CLASS: |
2248 |
| - do { |
2249 |
| - Type s = asSuper(t, sym); |
2250 |
| - if (s != null) return s; |
2251 |
| - t = t.getEnclosingType(); |
2252 |
| - } while (t.hasTag(CLASS)); |
2253 |
| - return null; |
2254 |
| - case ARRAY: |
2255 |
| - return isSubtype(t, sym.type) ? sym.type : null; |
2256 |
| - case TYPEVAR: |
2257 |
| - return asSuper(t, sym); |
2258 |
| - case ERROR: |
2259 |
| - return t; |
2260 |
| - default: |
2261 |
| - return null; |
| 2258 | + Type t1 = t; |
| 2259 | + while (!t1.hasTag(NONE)) { |
| 2260 | + Type s = asSuper(t1, sym); |
| 2261 | + if (s != null) return s; |
| 2262 | + t1 = t1.getEnclosingType(); |
2262 | 2263 | }
|
| 2264 | + return null; |
2263 | 2265 | }
|
2264 | 2266 |
|
2265 | 2267 | /**
|
2266 |
| - * Return the base type of t or any of its enclosing types that |
2267 |
| - * starts with the given symbol. If none exists, return null. |
| 2268 | + * This method returns the first type in a sequence (starting at `t`) that is |
| 2269 | + * a subclass of `sym`. The next type in the sequence is obtained by obtaining |
| 2270 | + * innermost lexically enclosing class type of the previous type in the sequence. |
| 2271 | + * Note, this is typically used to compute the implicit qualifier in |
| 2272 | + * a type expression. Example: |
| 2273 | + * |
| 2274 | + * class A<T> { class B { } } |
| 2275 | + * |
| 2276 | + * class C extends A<String> { |
| 2277 | + * static class D { |
| 2278 | + * B b; // A<String>.B |
| 2279 | + * } |
| 2280 | + * } |
2268 | 2281 | *
|
2269 | 2282 | * @param t a type
|
2270 | 2283 | * @param sym a symbol
|
2271 | 2284 | */
|
2272 | 2285 | public Type asEnclosingSuper(Type t, Symbol sym) {
|
2273 |
| - switch (t.getTag()) { |
2274 |
| - case CLASS: |
2275 |
| - do { |
2276 |
| - Type s = asSuper(t, sym); |
2277 |
| - if (s != null) return s; |
2278 |
| - Type outer = t.getEnclosingType(); |
2279 |
| - t = (outer.hasTag(CLASS)) ? outer : |
2280 |
| - (t.tsym.owner.enclClass() != null) ? t.tsym.owner.enclClass().type : |
2281 |
| - Type.noType; |
2282 |
| - } while (t.hasTag(CLASS)); |
2283 |
| - return null; |
2284 |
| - case ARRAY: |
2285 |
| - return isSubtype(t, sym.type) ? sym.type : null; |
2286 |
| - case TYPEVAR: |
2287 |
| - return asSuper(t, sym); |
2288 |
| - case ERROR: |
2289 |
| - return t; |
2290 |
| - default: |
2291 |
| - return null; |
| 2286 | + Type t1 = t; |
| 2287 | + while (!t1.hasTag(NONE)) { |
| 2288 | + Type s = asSuper(t1, sym); |
| 2289 | + if (s != null) return s; |
| 2290 | + t1 = (t1.tsym.owner.enclClass() != null) |
| 2291 | + ? t1.tsym.owner.enclClass().type |
| 2292 | + : noType; |
2292 | 2293 | }
|
| 2294 | + return null; |
2293 | 2295 | }
|
2294 | 2296 | // </editor-fold>
|
2295 | 2297 |
|
@@ -4514,7 +4516,7 @@ private boolean sideCast(Type from, Type to, Warner warn) {
|
4514 | 4516 | to = from;
|
4515 | 4517 | from = target;
|
4516 | 4518 | }
|
4517 |
| - List<Type> commonSupers = superClosure(to, erasure(from)); |
| 4519 | + List<Type> commonSupers = supertypeClosure(to, erasure(from)); |
4518 | 4520 | boolean giveWarning = commonSupers.isEmpty();
|
4519 | 4521 | // The arguments to the supers could be unified here to
|
4520 | 4522 | // get a more accurate analysis
|
@@ -4572,13 +4574,13 @@ private boolean giveWarning(Type from, Type to) {
|
4572 | 4574 | return false;
|
4573 | 4575 | }
|
4574 | 4576 |
|
4575 |
| - private List<Type> superClosure(Type t, Type s) { |
| 4577 | + private List<Type> supertypeClosure(Type t, Type s) { |
4576 | 4578 | List<Type> cl = List.nil();
|
4577 | 4579 | for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
|
4578 | 4580 | if (isSubtype(s, erasure(l.head))) {
|
4579 | 4581 | cl = insert(cl, l.head);
|
4580 | 4582 | } else {
|
4581 |
| - cl = union(cl, superClosure(l.head, s)); |
| 4583 | + cl = union(cl, supertypeClosure(l.head, s)); |
4582 | 4584 | }
|
4583 | 4585 | }
|
4584 | 4586 | return cl;
|
|
0 commit comments