46 |
46 |
47 import static com.sun.tools.javac.code.Flags.*; |
47 import static com.sun.tools.javac.code.Flags.*; |
48 import static com.sun.tools.javac.code.Flags.ANNOTATION; |
48 import static com.sun.tools.javac.code.Flags.ANNOTATION; |
49 import static com.sun.tools.javac.code.Flags.SYNCHRONIZED; |
49 import static com.sun.tools.javac.code.Flags.SYNCHRONIZED; |
50 import static com.sun.tools.javac.code.Kinds.*; |
50 import static com.sun.tools.javac.code.Kinds.*; |
51 import static com.sun.tools.javac.code.TypeTags.*; |
51 import static com.sun.tools.javac.code.TypeTag.*; |
52 import static com.sun.tools.javac.code.TypeTags.WILDCARD; |
52 import static com.sun.tools.javac.code.TypeTag.WILDCARD; |
53 |
53 |
54 import static com.sun.tools.javac.tree.JCTree.Tag.*; |
54 import static com.sun.tools.javac.tree.JCTree.Tag.*; |
55 |
55 |
56 /** Type checking helper class for the attribution phase. |
56 /** Type checking helper class for the attribution phase. |
57 * |
57 * |
278 * @param found The type that was found. |
278 * @param found The type that was found. |
279 */ |
279 */ |
280 Type typeTagError(DiagnosticPosition pos, Object required, Object found) { |
280 Type typeTagError(DiagnosticPosition pos, Object required, Object found) { |
281 // this error used to be raised by the parser, |
281 // this error used to be raised by the parser, |
282 // but has been delayed to this point: |
282 // but has been delayed to this point: |
283 if (found instanceof Type && ((Type)found).tag == VOID) { |
283 if (found instanceof Type && ((Type)found).hasTag(VOID)) { |
284 log.error(pos, "illegal.start.of.type"); |
284 log.error(pos, "illegal.start.of.type"); |
285 return syms.errType; |
285 return syms.errType; |
286 } |
286 } |
287 log.error(pos, "type.found.req", found, required); |
287 log.error(pos, "type.found.req", found, required); |
288 return types.createErrorType(found instanceof Type ? (Type)found : syms.errType); |
288 return types.createErrorType(found instanceof Type ? (Type)found : syms.errType); |
356 void checkTransparentClass(DiagnosticPosition pos, ClassSymbol c, Scope s) { |
356 void checkTransparentClass(DiagnosticPosition pos, ClassSymbol c, Scope s) { |
357 if (s.next != null) { |
357 if (s.next != null) { |
358 for (Scope.Entry e = s.next.lookup(c.name); |
358 for (Scope.Entry e = s.next.lookup(c.name); |
359 e.scope != null && e.sym.owner == c.owner; |
359 e.scope != null && e.sym.owner == c.owner; |
360 e = e.next()) { |
360 e = e.next()) { |
361 if (e.sym.kind == TYP && e.sym.type.tag != TYPEVAR && |
361 if (e.sym.kind == TYP && !e.sym.type.hasTag(TYPEVAR) && |
362 (e.sym.owner.kind & (VAR | MTH)) != 0 && |
362 (e.sym.owner.kind & (VAR | MTH)) != 0 && |
363 c.name != names.error) { |
363 c.name != names.error) { |
364 duplicateError(pos, e.sym); |
364 duplicateError(pos, e.sym); |
365 return; |
365 return; |
366 } |
366 } |
525 public void typesInferred(InferenceContext inferenceContext) { |
525 public void typesInferred(InferenceContext inferenceContext) { |
526 checkType(pos, found, inferenceContext.asInstType(req, types), checkContext); |
526 checkType(pos, found, inferenceContext.asInstType(req, types), checkContext); |
527 } |
527 } |
528 }); |
528 }); |
529 } |
529 } |
530 if (req.tag == ERROR) |
530 if (req.hasTag(ERROR)) |
531 return req; |
531 return req; |
532 if (req.tag == NONE) |
532 if (req.hasTag(NONE)) |
533 return found; |
533 return found; |
534 if (checkContext.compatible(found, req, checkContext.checkWarner(pos, found, req))) { |
534 if (checkContext.compatible(found, req, checkContext.checkWarner(pos, found, req))) { |
535 return found; |
535 return found; |
536 } else { |
536 } else { |
537 if (found.tag <= DOUBLE && req.tag <= DOUBLE) { |
537 if (found.getTag().isSubRangeOf(DOUBLE) && req.getTag().isSubRangeOf(DOUBLE)) { |
538 checkContext.report(pos, diags.fragment("possible.loss.of.precision", found, req)); |
538 checkContext.report(pos, diags.fragment("possible.loss.of.precision", found, req)); |
539 return types.createErrorType(found); |
539 return types.createErrorType(found); |
540 } |
540 } |
541 checkContext.report(pos, diags.fragment("inconvertible.types", found, req)); |
541 checkContext.report(pos, diags.fragment("inconvertible.types", found, req)); |
542 return types.createErrorType(found); |
542 return types.createErrorType(found); |
592 //where |
592 //where |
593 /** Is type a type variable, or a (possibly multi-dimensional) array of |
593 /** Is type a type variable, or a (possibly multi-dimensional) array of |
594 * type variables? |
594 * type variables? |
595 */ |
595 */ |
596 boolean isTypeVar(Type t) { |
596 boolean isTypeVar(Type t) { |
597 return t.tag == TYPEVAR || t.tag == ARRAY && isTypeVar(types.elemtype(t)); |
597 return t.hasTag(TYPEVAR) || t.hasTag(ARRAY) && isTypeVar(types.elemtype(t)); |
598 } |
598 } |
599 |
599 |
600 /** Check that a type is within some bounds. |
600 /** Check that a type is within some bounds. |
601 * |
601 * |
602 * Used in TypeApply to verify that, e.g., X in {@code V<X>} is a valid |
602 * Used in TypeApply to verify that, e.g., X in {@code V<X>} is a valid |
605 * @param bound The bound. |
605 * @param bound The bound. |
606 */ |
606 */ |
607 private boolean checkExtends(Type a, Type bound) { |
607 private boolean checkExtends(Type a, Type bound) { |
608 if (a.isUnbound()) { |
608 if (a.isUnbound()) { |
609 return true; |
609 return true; |
610 } else if (a.tag != WILDCARD) { |
610 } else if (!a.hasTag(WILDCARD)) { |
611 a = types.upperBound(a); |
611 a = types.upperBound(a); |
612 return types.isSubtype(a, bound); |
612 return types.isSubtype(a, bound); |
613 } else if (a.isExtendsBound()) { |
613 } else if (a.isExtendsBound()) { |
614 return types.isCastable(bound, types.upperBound(a), Warner.noWarnings); |
614 return types.isCastable(bound, types.upperBound(a), Warner.noWarnings); |
615 } else if (a.isSuperBound()) { |
615 } else if (a.isSuperBound()) { |
621 /** Check that type is different from 'void'. |
621 /** Check that type is different from 'void'. |
622 * @param pos Position to be used for error reporting. |
622 * @param pos Position to be used for error reporting. |
623 * @param t The type to be checked. |
623 * @param t The type to be checked. |
624 */ |
624 */ |
625 Type checkNonVoid(DiagnosticPosition pos, Type t) { |
625 Type checkNonVoid(DiagnosticPosition pos, Type t) { |
626 if (t.tag == VOID) { |
626 if (t.hasTag(VOID)) { |
627 log.error(pos, "void.not.allowed.here"); |
627 log.error(pos, "void.not.allowed.here"); |
628 return types.createErrorType(t); |
628 return types.createErrorType(t); |
629 } else { |
629 } else { |
630 return t; |
630 return t; |
631 } |
631 } |
634 /** Check that type is a class or interface type. |
634 /** Check that type is a class or interface type. |
635 * @param pos Position to be used for error reporting. |
635 * @param pos Position to be used for error reporting. |
636 * @param t The type to be checked. |
636 * @param t The type to be checked. |
637 */ |
637 */ |
638 Type checkClassType(DiagnosticPosition pos, Type t) { |
638 Type checkClassType(DiagnosticPosition pos, Type t) { |
639 if (t.tag != CLASS && t.tag != ERROR) |
639 if (!t.hasTag(CLASS) && !t.hasTag(ERROR)) |
640 return typeTagError(pos, |
640 return typeTagError(pos, |
641 diags.fragment("type.req.class"), |
641 diags.fragment("type.req.class"), |
642 (t.tag == TYPEVAR) |
642 (t.hasTag(TYPEVAR)) |
643 ? diags.fragment("type.parameter", t) |
643 ? diags.fragment("type.parameter", t) |
644 : t); |
644 : t); |
645 else |
645 else |
646 return t; |
646 return t; |
647 } |
647 } |
648 |
648 |
649 /** Check that type is a valid qualifier for a constructor reference expression |
649 /** Check that type is a valid qualifier for a constructor reference expression |
650 */ |
650 */ |
651 Type checkConstructorRefType(DiagnosticPosition pos, Type t) { |
651 Type checkConstructorRefType(DiagnosticPosition pos, Type t) { |
652 t = checkClassType(pos, t); |
652 t = checkClassType(pos, t); |
653 if (t.tag == CLASS) { |
653 if (t.hasTag(CLASS)) { |
654 if ((t.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) { |
654 if ((t.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) { |
655 log.error(pos, "abstract.cant.be.instantiated"); |
655 log.error(pos, "abstract.cant.be.instantiated"); |
656 t = types.createErrorType(t); |
656 t = types.createErrorType(t); |
657 } else if ((t.tsym.flags() & ENUM) != 0) { |
657 } else if ((t.tsym.flags() & ENUM) != 0) { |
658 log.error(pos, "enum.cant.be.instantiated"); |
658 log.error(pos, "enum.cant.be.instantiated"); |
670 Type checkClassType(DiagnosticPosition pos, Type t, boolean noBounds) { |
670 Type checkClassType(DiagnosticPosition pos, Type t, boolean noBounds) { |
671 t = checkClassType(pos, t); |
671 t = checkClassType(pos, t); |
672 if (noBounds && t.isParameterized()) { |
672 if (noBounds && t.isParameterized()) { |
673 List<Type> args = t.getTypeArguments(); |
673 List<Type> args = t.getTypeArguments(); |
674 while (args.nonEmpty()) { |
674 while (args.nonEmpty()) { |
675 if (args.head.tag == WILDCARD) |
675 if (args.head.hasTag(WILDCARD)) |
676 return typeTagError(pos, |
676 return typeTagError(pos, |
677 diags.fragment("type.req.exact"), |
677 diags.fragment("type.req.exact"), |
678 args.head); |
678 args.head); |
679 args = args.tail; |
679 args = args.tail; |
680 } |
680 } |
685 /** Check that type is a reifiable class, interface or array type. |
685 /** Check that type is a reifiable class, interface or array type. |
686 * @param pos Position to be used for error reporting. |
686 * @param pos Position to be used for error reporting. |
687 * @param t The type to be checked. |
687 * @param t The type to be checked. |
688 */ |
688 */ |
689 Type checkReifiableReferenceType(DiagnosticPosition pos, Type t) { |
689 Type checkReifiableReferenceType(DiagnosticPosition pos, Type t) { |
690 if (t.tag != CLASS && t.tag != ARRAY && t.tag != ERROR) { |
690 if (!t.hasTag(CLASS) && !t.hasTag(ARRAY) && !t.hasTag(ERROR)) { |
691 return typeTagError(pos, |
691 return typeTagError(pos, |
692 diags.fragment("type.req.class.array"), |
692 diags.fragment("type.req.class.array"), |
693 t); |
693 t); |
694 } else if (!types.isReifiable(t)) { |
694 } else if (!types.isReifiable(t)) { |
695 log.error(pos, "illegal.generic.type.for.instof"); |
695 log.error(pos, "illegal.generic.type.for.instof"); |
703 * or a type variable. |
703 * or a type variable. |
704 * @param pos Position to be used for error reporting. |
704 * @param pos Position to be used for error reporting. |
705 * @param t The type to be checked. |
705 * @param t The type to be checked. |
706 */ |
706 */ |
707 Type checkRefType(DiagnosticPosition pos, Type t) { |
707 Type checkRefType(DiagnosticPosition pos, Type t) { |
708 switch (t.tag) { |
708 if (t.isReference()) |
709 case CLASS: |
|
710 case ARRAY: |
|
711 case TYPEVAR: |
|
712 case WILDCARD: |
|
713 case ERROR: |
|
714 return t; |
709 return t; |
715 default: |
710 else |
716 return typeTagError(pos, |
711 return typeTagError(pos, |
717 diags.fragment("type.req.ref"), |
712 diags.fragment("type.req.ref"), |
718 t); |
713 t); |
719 } |
|
720 } |
714 } |
721 |
715 |
722 /** Check that each type is a reference type, i.e. a class, interface or array type |
716 /** Check that each type is a reference type, i.e. a class, interface or array type |
723 * or a type variable. |
717 * or a type variable. |
724 * @param trees Original trees, used for error reporting. |
718 * @param trees Original trees, used for error reporting. |
736 /** Check that type is a null or reference type. |
730 /** Check that type is a null or reference type. |
737 * @param pos Position to be used for error reporting. |
731 * @param pos Position to be used for error reporting. |
738 * @param t The type to be checked. |
732 * @param t The type to be checked. |
739 */ |
733 */ |
740 Type checkNullOrRefType(DiagnosticPosition pos, Type t) { |
734 Type checkNullOrRefType(DiagnosticPosition pos, Type t) { |
741 switch (t.tag) { |
735 if (t.isNullOrReference()) |
742 case CLASS: |
|
743 case ARRAY: |
|
744 case TYPEVAR: |
|
745 case WILDCARD: |
|
746 case BOT: |
|
747 case ERROR: |
|
748 return t; |
736 return t; |
749 default: |
737 else |
750 return typeTagError(pos, |
738 return typeTagError(pos, |
751 diags.fragment("type.req.ref"), |
739 diags.fragment("type.req.ref"), |
752 t); |
740 t); |
753 } |
|
754 } |
741 } |
755 |
742 |
756 /** Check that flag set does not contain elements of two conflicting sets. s |
743 /** Check that flag set does not contain elements of two conflicting sets. s |
757 * Return true if it doesn't. |
744 * Return true if it doesn't. |
758 * @param pos Position to be used for error reporting. |
745 * @param pos Position to be used for error reporting. |
1052 |
1039 |
1053 args = type.getTypeArguments(); |
1040 args = type.getTypeArguments(); |
1054 bounds = bounds_buf.toList(); |
1041 bounds = bounds_buf.toList(); |
1055 |
1042 |
1056 for (Type arg : types.capture(type).getTypeArguments()) { |
1043 for (Type arg : types.capture(type).getTypeArguments()) { |
1057 if (arg.tag == TYPEVAR && |
1044 if (arg.hasTag(TYPEVAR) && |
1058 arg.getUpperBound().isErroneous() && |
1045 arg.getUpperBound().isErroneous() && |
1059 !bounds.head.isErroneous() && |
1046 !bounds.head.isErroneous() && |
1060 !isTypeArgErroneous(args.head)) { |
1047 !isTypeArgErroneous(args.head)) { |
1061 return args.head; |
1048 return args.head; |
1062 } |
1049 } |
1307 tree.elemtype.accept(this); |
1294 tree.elemtype.accept(this); |
1308 } |
1295 } |
1309 |
1296 |
1310 @Override |
1297 @Override |
1311 public void visitTypeApply(JCTypeApply tree) { |
1298 public void visitTypeApply(JCTypeApply tree) { |
1312 if (tree.type.tag == CLASS) { |
1299 if (tree.type.hasTag(CLASS)) { |
1313 List<JCExpression> args = tree.arguments; |
1300 List<JCExpression> args = tree.arguments; |
1314 List<Type> forms = tree.type.tsym.type.getTypeArguments(); |
1301 List<Type> forms = tree.type.tsym.type.getTypeArguments(); |
1315 |
1302 |
1316 Type incompatibleArg = firstIncompatibleTypeArg(tree.type); |
1303 Type incompatibleArg = firstIncompatibleTypeArg(tree.type); |
1317 if (incompatibleArg != null) { |
1304 if (incompatibleArg != null) { |
1358 validateTree(tree.inner, true, isOuter); |
1345 validateTree(tree.inner, true, isOuter); |
1359 } |
1346 } |
1360 |
1347 |
1361 @Override |
1348 @Override |
1362 public void visitSelect(JCFieldAccess tree) { |
1349 public void visitSelect(JCFieldAccess tree) { |
1363 if (tree.type.tag == CLASS) { |
1350 if (tree.type.hasTag(CLASS)) { |
1364 visitSelectInternal(tree); |
1351 visitSelectInternal(tree); |
1365 |
1352 |
1366 // Check that this type is either fully parameterized, or |
1353 // Check that this type is either fully parameterized, or |
1367 // not parameterized at all. |
1354 // not parameterized at all. |
1368 if (tree.selected.type.isParameterized() && tree.type.tsym.type.getTypeArguments().nonEmpty()) |
1355 if (tree.selected.type.isParameterized() && tree.type.tsym.type.getTypeArguments().nonEmpty()) |
1407 validateTree(l.head, checkRaw, isOuter); |
1394 validateTree(l.head, checkRaw, isOuter); |
1408 } |
1395 } |
1409 |
1396 |
1410 void checkRaw(JCTree tree, Env<AttrContext> env) { |
1397 void checkRaw(JCTree tree, Env<AttrContext> env) { |
1411 if (lint.isEnabled(LintCategory.RAW) && |
1398 if (lint.isEnabled(LintCategory.RAW) && |
1412 tree.type.tag == CLASS && |
1399 tree.type.hasTag(CLASS) && |
1413 !TreeInfo.isDiamond(tree) && |
1400 !TreeInfo.isDiamond(tree) && |
1414 !withinAnonConstr(env) && |
1401 !withinAnonConstr(env) && |
1415 tree.type.isRaw()) { |
1402 tree.type.isRaw()) { |
1416 log.warning(LintCategory.RAW, |
1403 log.warning(LintCategory.RAW, |
1417 tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type); |
1404 tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type); |
1509 |
1496 |
1510 /** Is exc an exception type that need not be declared? |
1497 /** Is exc an exception type that need not be declared? |
1511 */ |
1498 */ |
1512 boolean isUnchecked(Type exc) { |
1499 boolean isUnchecked(Type exc) { |
1513 return |
1500 return |
1514 (exc.tag == TYPEVAR) ? isUnchecked(types.supertype(exc)) : |
1501 (exc.hasTag(TYPEVAR)) ? isUnchecked(types.supertype(exc)) : |
1515 (exc.tag == CLASS) ? isUnchecked((ClassSymbol)exc.tsym) : |
1502 (exc.hasTag(CLASS)) ? isUnchecked((ClassSymbol)exc.tsym) : |
1516 exc.tag == BOT; |
1503 exc.hasTag(BOT); |
1517 } |
1504 } |
1518 |
1505 |
1519 /** Same, but handling completion failures. |
1506 /** Same, but handling completion failures. |
1520 */ |
1507 */ |
1521 boolean isUnchecked(DiagnosticPosition pos, Type exc) { |
1508 boolean isUnchecked(DiagnosticPosition pos, Type exc) { |
1757 // If the method, m, is not defined in an interface, then the only time we need to |
1744 // If the method, m, is not defined in an interface, then the only time we need to |
1758 // address the issue is when the method is the supertype implemementation: any other |
1745 // address the issue is when the method is the supertype implemementation: any other |
1759 // case, we will have dealt with when examining the supertype classes |
1746 // case, we will have dealt with when examining the supertype classes |
1760 ClassSymbol mc = m.enclClass(); |
1747 ClassSymbol mc = m.enclClass(); |
1761 Type st = types.supertype(origin.type); |
1748 Type st = types.supertype(origin.type); |
1762 if (st.tag != CLASS) |
1749 if (!st.hasTag(CLASS)) |
1763 return true; |
1750 return true; |
1764 MethodSymbol stimpl = m.implementation((ClassSymbol)st.tsym, types, false); |
1751 MethodSymbol stimpl = m.implementation((ClassSymbol)st.tsym, types, false); |
1765 |
1752 |
1766 if (mc != null && ((mc.flags() & INTERFACE) != 0)) { |
1753 if (mc != null && ((mc.flags() & INTERFACE) != 0)) { |
1767 List<Type> intfs = types.interfaces(origin.type); |
1754 List<Type> intfs = types.interfaces(origin.type); |
1780 * @param pos Position to be used for error reporting. |
1767 * @param pos Position to be used for error reporting. |
1781 * @param site The class type to be checked. |
1768 * @param site The class type to be checked. |
1782 */ |
1769 */ |
1783 public void checkCompatibleConcretes(DiagnosticPosition pos, Type site) { |
1770 public void checkCompatibleConcretes(DiagnosticPosition pos, Type site) { |
1784 Type sup = types.supertype(site); |
1771 Type sup = types.supertype(site); |
1785 if (sup.tag != CLASS) return; |
1772 if (!sup.hasTag(CLASS)) return; |
1786 |
1773 |
1787 for (Type t1 = sup; |
1774 for (Type t1 = sup; |
1788 t1.tsym.type.isParameterized(); |
1775 t1.tsym.type.isParameterized(); |
1789 t1 = types.supertype(t1)) { |
1776 t1 = types.supertype(t1)) { |
1790 for (Scope.Entry e1 = t1.tsym.members().elems; |
1777 for (Scope.Entry e1 = t1.tsym.members().elems; |
1801 Type st1 = types.memberType(t1, s1); |
1788 Type st1 = types.memberType(t1, s1); |
1802 int s1ArgsLength = st1.getParameterTypes().length(); |
1789 int s1ArgsLength = st1.getParameterTypes().length(); |
1803 if (st1 == s1.type) continue; |
1790 if (st1 == s1.type) continue; |
1804 |
1791 |
1805 for (Type t2 = sup; |
1792 for (Type t2 = sup; |
1806 t2.tag == CLASS; |
1793 t2.hasTag(CLASS); |
1807 t2 = types.supertype(t2)) { |
1794 t2 = types.supertype(t2)) { |
1808 for (Scope.Entry e2 = t2.tsym.members().lookup(s1.name); |
1795 for (Scope.Entry e2 = t2.tsym.members().lookup(s1.name); |
1809 e2.scope != null; |
1796 e2.scope != null; |
1810 e2 = e2.next()) { |
1797 e2 = e2.next()) { |
1811 Symbol s2 = e2.sym; |
1798 Symbol s2 = e2.sym; |
1874 return null; |
1861 return null; |
1875 } |
1862 } |
1876 |
1863 |
1877 /** Compute all the supertypes of t, indexed by type symbol. */ |
1864 /** Compute all the supertypes of t, indexed by type symbol. */ |
1878 private void closure(Type t, Map<TypeSymbol,Type> typeMap) { |
1865 private void closure(Type t, Map<TypeSymbol,Type> typeMap) { |
1879 if (t.tag != CLASS) return; |
1866 if (!t.hasTag(CLASS)) return; |
1880 if (typeMap.put(t.tsym, t) == null) { |
1867 if (typeMap.put(t.tsym, t) == null) { |
1881 closure(types.supertype(t), typeMap); |
1868 closure(types.supertype(t), typeMap); |
1882 for (Type i : types.interfaces(t)) |
1869 for (Type i : types.interfaces(t)) |
1883 closure(i, typeMap); |
1870 closure(i, typeMap); |
1884 } |
1871 } |
1885 } |
1872 } |
1886 |
1873 |
1887 /** Compute all the supertypes of t, indexed by type symbol (except thise in typesSkip). */ |
1874 /** Compute all the supertypes of t, indexed by type symbol (except thise in typesSkip). */ |
1888 private void closure(Type t, Map<TypeSymbol,Type> typesSkip, Map<TypeSymbol,Type> typeMap) { |
1875 private void closure(Type t, Map<TypeSymbol,Type> typesSkip, Map<TypeSymbol,Type> typeMap) { |
1889 if (t.tag != CLASS) return; |
1876 if (!t.hasTag(CLASS)) return; |
1890 if (typesSkip.get(t.tsym) != null) return; |
1877 if (typesSkip.get(t.tsym) != null) return; |
1891 if (typeMap.put(t.tsym, t) == null) { |
1878 if (typeMap.put(t.tsym, t) == null) { |
1892 closure(types.supertype(t), typesSkip, typeMap); |
1879 closure(types.supertype(t), typesSkip, typeMap); |
1893 for (Type i : types.interfaces(t)) |
1880 for (Type i : types.interfaces(t)) |
1894 closure(i, typesSkip, typeMap); |
1881 closure(i, typesSkip, typeMap); |
1914 List<Type> tvars2 = st2.getTypeArguments(); |
1901 List<Type> tvars2 = st2.getTypeArguments(); |
1915 Type rt1 = st1.getReturnType(); |
1902 Type rt1 = st1.getReturnType(); |
1916 Type rt2 = types.subst(st2.getReturnType(), tvars2, tvars1); |
1903 Type rt2 = types.subst(st2.getReturnType(), tvars2, tvars1); |
1917 boolean compat = |
1904 boolean compat = |
1918 types.isSameType(rt1, rt2) || |
1905 types.isSameType(rt1, rt2) || |
1919 rt1.tag >= CLASS && rt2.tag >= CLASS && |
1906 !rt1.isPrimitiveOrVoid() && |
|
1907 !rt2.isPrimitiveOrVoid() && |
1920 (types.covariantReturnType(rt1, rt2, Warner.noWarnings) || |
1908 (types.covariantReturnType(rt1, rt2, Warner.noWarnings) || |
1921 types.covariantReturnType(rt2, rt1, Warner.noWarnings)) || |
1909 types.covariantReturnType(rt2, rt1, Warner.noWarnings)) || |
1922 checkCommonOverriderIn(s1,s2,site); |
1910 checkCommonOverriderIn(s1,s2,site); |
1923 if (!compat) { |
1911 if (!compat) { |
1924 log.error(pos, "types.incompatible.diff.ret", |
1912 log.error(pos, "types.incompatible.diff.ret", |
1959 Type rt1 = st1.getReturnType(); |
1947 Type rt1 = st1.getReturnType(); |
1960 Type rt2 = st2.getReturnType(); |
1948 Type rt2 = st2.getReturnType(); |
1961 Type rt13 = types.subst(st3.getReturnType(), tvars3, tvars1); |
1949 Type rt13 = types.subst(st3.getReturnType(), tvars3, tvars1); |
1962 Type rt23 = types.subst(st3.getReturnType(), tvars3, tvars2); |
1950 Type rt23 = types.subst(st3.getReturnType(), tvars3, tvars2); |
1963 boolean compat = |
1951 boolean compat = |
1964 rt13.tag >= CLASS && rt23.tag >= CLASS && |
1952 !rt13.isPrimitiveOrVoid() && |
|
1953 !rt23.isPrimitiveOrVoid() && |
1965 (types.covariantReturnType(rt13, rt1, Warner.noWarnings) && |
1954 (types.covariantReturnType(rt13, rt1, Warner.noWarnings) && |
1966 types.covariantReturnType(rt23, rt2, Warner.noWarnings)); |
1955 types.covariantReturnType(rt23, rt2, Warner.noWarnings)); |
1967 if (compat) |
1956 if (compat) |
1968 return true; |
1957 return true; |
1969 } |
1958 } |
1982 if ((origin.flags() & ENUM) != 0 && names.finalize.equals(m.name)) |
1971 if ((origin.flags() & ENUM) != 0 && names.finalize.equals(m.name)) |
1983 if (m.overrides(syms.enumFinalFinalize, origin, types, false)) { |
1972 if (m.overrides(syms.enumFinalFinalize, origin, types, false)) { |
1984 log.error(tree.pos(), "enum.no.finalize"); |
1973 log.error(tree.pos(), "enum.no.finalize"); |
1985 return; |
1974 return; |
1986 } |
1975 } |
1987 for (Type t = origin.type; t.tag == CLASS; |
1976 for (Type t = origin.type; t.hasTag(CLASS); |
1988 t = types.supertype(t)) { |
1977 t = types.supertype(t)) { |
1989 if (t != origin.type) { |
1978 if (t != origin.type) { |
1990 checkOverride(tree, t, origin, m); |
1979 checkOverride(tree, t, origin, m); |
1991 } |
1980 } |
1992 for (Type t2 : types.interfaces(t)) { |
1981 for (Type t2 : types.interfaces(t)) { |
2062 undef = absmeth; |
2051 undef = absmeth; |
2063 } |
2052 } |
2064 } |
2053 } |
2065 if (undef == null) { |
2054 if (undef == null) { |
2066 Type st = types.supertype(c.type); |
2055 Type st = types.supertype(c.type); |
2067 if (st.tag == CLASS) |
2056 if (st.hasTag(CLASS)) |
2068 undef = firstUndef(impl, (ClassSymbol)st.tsym); |
2057 undef = firstUndef(impl, (ClassSymbol)st.tsym); |
2069 } |
2058 } |
2070 for (List<Type> l = types.interfaces(c.type); |
2059 for (List<Type> l = types.interfaces(c.type); |
2071 undef == null && l.nonEmpty(); |
2060 undef == null && l.nonEmpty(); |
2072 l = l.tail) { |
2061 l = l.tail) { |
2153 errorFound = true; |
2142 errorFound = true; |
2154 noteCyclic(pos, (ClassSymbol)c); |
2143 noteCyclic(pos, (ClassSymbol)c); |
2155 } else if (!c.type.isErroneous()) { |
2144 } else if (!c.type.isErroneous()) { |
2156 try { |
2145 try { |
2157 seenClasses = seenClasses.prepend(c); |
2146 seenClasses = seenClasses.prepend(c); |
2158 if (c.type.tag == CLASS) { |
2147 if (c.type.hasTag(CLASS)) { |
2159 if (supertypes.nonEmpty()) { |
2148 if (supertypes.nonEmpty()) { |
2160 scan(supertypes); |
2149 scan(supertypes); |
2161 } |
2150 } |
2162 else { |
2151 else { |
2163 ClassType ct = (ClassType)c.type; |
2152 ClassType ct = (ClassType)c.type; |
2198 checkNonCyclic1(pos, t, List.<TypeVar>nil()); |
2187 checkNonCyclic1(pos, t, List.<TypeVar>nil()); |
2199 } |
2188 } |
2200 |
2189 |
2201 private void checkNonCyclic1(DiagnosticPosition pos, Type t, List<TypeVar> seen) { |
2190 private void checkNonCyclic1(DiagnosticPosition pos, Type t, List<TypeVar> seen) { |
2202 final TypeVar tv; |
2191 final TypeVar tv; |
2203 if (t.tag == TYPEVAR && (t.tsym.flags() & UNATTRIBUTED) != 0) |
2192 if (t.hasTag(TYPEVAR) && (t.tsym.flags() & UNATTRIBUTED) != 0) |
2204 return; |
2193 return; |
2205 if (seen.contains(t)) { |
2194 if (seen.contains(t)) { |
2206 tv = (TypeVar)t; |
2195 tv = (TypeVar)t; |
2207 tv.bound = types.createErrorType(t); |
2196 tv.bound = types.createErrorType(t); |
2208 log.error(pos, "cyclic.inheritance", t); |
2197 log.error(pos, "cyclic.inheritance", t); |
2209 } else if (t.tag == TYPEVAR) { |
2198 } else if (t.hasTag(TYPEVAR)) { |
2210 tv = (TypeVar)t; |
2199 tv = (TypeVar)t; |
2211 seen = seen.prepend(tv); |
2200 seen = seen.prepend(tv); |
2212 for (Type b : types.getBounds(tv)) |
2201 for (Type b : types.getBounds(tv)) |
2213 checkNonCyclic1(pos, b, seen); |
2202 checkNonCyclic1(pos, b, seen); |
2214 } |
2203 } |
2230 if ((c.flags_field & LOCKED) != 0) { |
2219 if ((c.flags_field & LOCKED) != 0) { |
2231 noteCyclic(pos, (ClassSymbol)c); |
2220 noteCyclic(pos, (ClassSymbol)c); |
2232 } else if (!c.type.isErroneous()) { |
2221 } else if (!c.type.isErroneous()) { |
2233 try { |
2222 try { |
2234 c.flags_field |= LOCKED; |
2223 c.flags_field |= LOCKED; |
2235 if (c.type.tag == CLASS) { |
2224 if (c.type.hasTag(CLASS)) { |
2236 ClassType clazz = (ClassType)c.type; |
2225 ClassType clazz = (ClassType)c.type; |
2237 if (clazz.interfaces_field != null) |
2226 if (clazz.interfaces_field != null) |
2238 for (List<Type> l=clazz.interfaces_field; l.nonEmpty(); l=l.tail) |
2227 for (List<Type> l=clazz.interfaces_field; l.nonEmpty(); l=l.tail) |
2239 complete &= checkNonCyclicInternal(pos, l.head); |
2228 complete &= checkNonCyclicInternal(pos, l.head); |
2240 if (clazz.supertype_field != null) { |
2229 if (clazz.supertype_field != null) { |
2241 Type st = clazz.supertype_field; |
2230 Type st = clazz.supertype_field; |
2242 if (st != null && st.tag == CLASS) |
2231 if (st != null && st.hasTag(CLASS)) |
2243 complete &= checkNonCyclicInternal(pos, st); |
2232 complete &= checkNonCyclicInternal(pos, st); |
2244 } |
2233 } |
2245 if (c.owner.kind == TYP) |
2234 if (c.owner.kind == TYP) |
2246 complete &= checkNonCyclicInternal(pos, c.owner.type); |
2235 complete &= checkNonCyclicInternal(pos, c.owner.type); |
2247 } |
2236 } |
2259 private void noteCyclic(DiagnosticPosition pos, ClassSymbol c) { |
2248 private void noteCyclic(DiagnosticPosition pos, ClassSymbol c) { |
2260 log.error(pos, "cyclic.inheritance", c); |
2249 log.error(pos, "cyclic.inheritance", c); |
2261 for (List<Type> l=types.interfaces(c.type); l.nonEmpty(); l=l.tail) |
2250 for (List<Type> l=types.interfaces(c.type); l.nonEmpty(); l=l.tail) |
2262 l.head = types.createErrorType((ClassSymbol)l.head.tsym, Type.noType); |
2251 l.head = types.createErrorType((ClassSymbol)l.head.tsym, Type.noType); |
2263 Type st = types.supertype(c.type); |
2252 Type st = types.supertype(c.type); |
2264 if (st.tag == CLASS) |
2253 if (st.hasTag(CLASS)) |
2265 ((ClassType)c.type).supertype_field = types.createErrorType((ClassSymbol)st.tsym, Type.noType); |
2254 ((ClassType)c.type).supertype_field = types.createErrorType((ClassSymbol)st.tsym, Type.noType); |
2266 c.type = types.createErrorType(c, c.type); |
2255 c.type = types.createErrorType(c, c.type); |
2267 c.flags_field |= ACYCLIC; |
2256 c.flags_field |= ACYCLIC; |
2268 } |
2257 } |
2269 |
2258 |
2311 * @param c The class whose interfaces are checked. |
2300 * @param c The class whose interfaces are checked. |
2312 */ |
2301 */ |
2313 void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) { |
2302 void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) { |
2314 List<Type> supertypes = types.interfaces(c); |
2303 List<Type> supertypes = types.interfaces(c); |
2315 Type supertype = types.supertype(c); |
2304 Type supertype = types.supertype(c); |
2316 if (supertype.tag == CLASS && |
2305 if (supertype.hasTag(CLASS) && |
2317 (supertype.tsym.flags() & ABSTRACT) != 0) |
2306 (supertype.tsym.flags() & ABSTRACT) != 0) |
2318 supertypes = supertypes.prepend(supertype); |
2307 supertypes = supertypes.prepend(supertype); |
2319 for (List<Type> l = supertypes; l.nonEmpty(); l = l.tail) { |
2308 for (List<Type> l = supertypes; l.nonEmpty(); l = l.tail) { |
2320 if (allowGenerics && !l.head.getTypeArguments().isEmpty() && |
2309 if (allowGenerics && !l.head.getTypeArguments().isEmpty() && |
2321 !checkCompatibleAbstracts(pos, l.head, l.head, c)) |
2310 !checkCompatibleAbstracts(pos, l.head, l.head, c)) |
2540 * or in the interface annotation.Annotation." |
2529 * or in the interface annotation.Annotation." |
2541 * |
2530 * |
2542 * @jls 9.6 Annotation Types |
2531 * @jls 9.6 Annotation Types |
2543 */ |
2532 */ |
2544 void validateAnnotationMethod(DiagnosticPosition pos, MethodSymbol m) { |
2533 void validateAnnotationMethod(DiagnosticPosition pos, MethodSymbol m) { |
2545 for (Type sup = syms.annotationType; sup.tag == CLASS; sup = types.supertype(sup)) { |
2534 for (Type sup = syms.annotationType; sup.hasTag(CLASS); sup = types.supertype(sup)) { |
2546 Scope s = sup.tsym.members(); |
2535 Scope s = sup.tsym.members(); |
2547 for (Scope.Entry e = s.lookup(m.name); e.scope != null; e = e.next()) { |
2536 for (Scope.Entry e = s.lookup(m.name); e.scope != null; e = e.next()) { |
2548 if (e.sym.kind == MTH && |
2537 if (e.sym.kind == MTH && |
2549 (e.sym.flags() & (PUBLIC | PROTECTED)) != 0 && |
2538 (e.sym.flags() & (PUBLIC | PROTECTED)) != 0 && |
2550 types.overrideEquivalent(m.type, e.sym.type)) |
2539 types.overrideEquivalent(m.type, e.sym.type)) |
2853 { if (s.kind == PCK) return true; } |
2842 { if (s.kind == PCK) return true; } |
2854 else if (e.value.name == names.TYPE_USE) |
2843 else if (e.value.name == names.TYPE_USE) |
2855 { if (s.kind == TYP || |
2844 { if (s.kind == TYP || |
2856 s.kind == VAR || |
2845 s.kind == VAR || |
2857 (s.kind == MTH && !s.isConstructor() && |
2846 (s.kind == MTH && !s.isConstructor() && |
2858 s.type.getReturnType().tag != VOID)) |
2847 !s.type.getReturnType().hasTag(VOID))) |
2859 return true; |
2848 return true; |
2860 } |
2849 } |
2861 else |
2850 else |
2862 return true; // recovery |
2851 return true; // recovery |
2863 } |
2852 } |
3014 tsym.flags_field |= ACYCLIC_ANN; |
3003 tsym.flags_field |= ACYCLIC_ANN; |
3015 } |
3004 } |
3016 } |
3005 } |
3017 |
3006 |
3018 void checkAnnotationResType(DiagnosticPosition pos, Type type) { |
3007 void checkAnnotationResType(DiagnosticPosition pos, Type type) { |
3019 switch (type.tag) { |
3008 switch (type.getTag()) { |
3020 case TypeTags.CLASS: |
3009 case CLASS: |
3021 if ((type.tsym.flags() & ANNOTATION) != 0) |
3010 if ((type.tsym.flags() & ANNOTATION) != 0) |
3022 checkNonCyclicElementsInternal(pos, type.tsym); |
3011 checkNonCyclicElementsInternal(pos, type.tsym); |
3023 break; |
3012 break; |
3024 case TypeTags.ARRAY: |
3013 case ARRAY: |
3025 checkAnnotationResType(pos, types.elemtype(type)); |
3014 checkAnnotationResType(pos, types.elemtype(type)); |
3026 break; |
3015 break; |
3027 default: |
3016 default: |
3028 break; // int etc |
3017 break; // int etc |
3029 } |
3018 } |
3112 * @param operand The right hand operand for the expression |
3101 * @param operand The right hand operand for the expression |
3113 */ |
3102 */ |
3114 void checkDivZero(DiagnosticPosition pos, Symbol operator, Type operand) { |
3103 void checkDivZero(DiagnosticPosition pos, Symbol operator, Type operand) { |
3115 if (operand.constValue() != null |
3104 if (operand.constValue() != null |
3116 && lint.isEnabled(LintCategory.DIVZERO) |
3105 && lint.isEnabled(LintCategory.DIVZERO) |
3117 && operand.tag <= LONG |
3106 && (operand.getTag().isSubRangeOf(LONG)) |
3118 && ((Number) (operand.constValue())).longValue() == 0) { |
3107 && ((Number) (operand.constValue())).longValue() == 0) { |
3119 int opc = ((OperatorSymbol)operator).opcode; |
3108 int opc = ((OperatorSymbol)operator).opcode; |
3120 if (opc == ByteCodes.idiv || opc == ByteCodes.imod |
3109 if (opc == ByteCodes.idiv || opc == ByteCodes.imod |
3121 || opc == ByteCodes.ldiv || opc == ByteCodes.lmod) { |
3110 || opc == ByteCodes.ldiv || opc == ByteCodes.lmod) { |
3122 log.warning(LintCategory.DIVZERO, pos, "div.zero"); |
3111 log.warning(LintCategory.DIVZERO, pos, "div.zero"); |