2710 argtypes, |
2711 argtypes, |
2711 typeargtypes, |
2712 typeargtypes, |
2712 true, |
2713 true, |
2713 useVarargs, |
2714 useVarargs, |
2714 noteWarner); |
2715 noteWarner); |
2715 boolean warned = noteWarner.hasNonSilentLint(LintCategory.UNCHECKED); |
|
2716 |
2716 |
2717 // If this fails, something went wrong; we should not have |
2717 // If this fails, something went wrong; we should not have |
2718 // found the identifier in the first place. |
2718 // found the identifier in the first place. |
2719 if (owntype == null) { |
2719 if (owntype == null) { |
2720 if (!pt.isErroneous()) |
2720 if (!pt.isErroneous()) |
2721 log.error(env.tree.pos(), |
2721 log.error(env.tree.pos(), |
2722 "internal.error.cant.instantiate", |
2722 "internal.error.cant.instantiate", |
2723 sym, site, |
2723 sym, site, |
2724 Type.toString(pt.getParameterTypes())); |
2724 Type.toString(pt.getParameterTypes())); |
2725 owntype = types.createErrorType(site); |
2725 owntype = types.createErrorType(site); |
|
2726 return types.createErrorType(site); |
|
2727 } else if (owntype.getReturnType().tag == FORALL) { |
|
2728 return owntype; |
2726 } else { |
2729 } else { |
2727 // System.out.println("call : " + env.tree); |
2730 return chk.checkMethod(owntype, sym, env, argtrees, argtypes, useVarargs); |
2728 // System.out.println("method : " + owntype); |
2731 } |
2729 // System.out.println("actuals: " + argtypes); |
2732 } |
2730 List<Type> formals = owntype.getParameterTypes(); |
2733 |
2731 Type last = useVarargs ? formals.last() : null; |
2734 /** |
2732 if (sym.name==names.init && |
2735 * Check that constructor arguments conform to its instantiation. |
2733 sym.owner == syms.enumSym) |
2736 **/ |
2734 formals = formals.tail.tail; |
2737 public Type checkConstructor(Type site, |
2735 List<JCExpression> args = argtrees; |
2738 Symbol sym, |
2736 while (formals.head != last) { |
2739 Env<AttrContext> env, |
2737 JCTree arg = args.head; |
2740 final List<JCExpression> argtrees, |
2738 Warner warn = chk.convertWarner(arg.pos(), arg.type, formals.head); |
2741 List<Type> argtypes, |
2739 assertConvertible(arg, arg.type, formals.head, warn); |
2742 List<Type> typeargtypes, |
2740 warned |= warn.hasNonSilentLint(LintCategory.UNCHECKED); |
2743 boolean useVarargs) { |
2741 args = args.tail; |
2744 Type owntype = checkMethod(site, sym, env, argtrees, argtypes, typeargtypes, useVarargs); |
2742 formals = formals.tail; |
2745 chk.checkType(env.tree.pos(), owntype.getReturnType(), syms.voidType); |
2743 } |
|
2744 if (useVarargs) { |
|
2745 Type varArg = types.elemtype(last); |
|
2746 while (args.tail != null) { |
|
2747 JCTree arg = args.head; |
|
2748 Warner warn = chk.convertWarner(arg.pos(), arg.type, varArg); |
|
2749 assertConvertible(arg, arg.type, varArg, warn); |
|
2750 warned |= warn.hasNonSilentLint(LintCategory.UNCHECKED); |
|
2751 args = args.tail; |
|
2752 } |
|
2753 } else if ((sym.flags() & VARARGS) != 0 && allowVarargs) { |
|
2754 // non-varargs call to varargs method |
|
2755 Type varParam = owntype.getParameterTypes().last(); |
|
2756 Type lastArg = argtypes.last(); |
|
2757 if (types.isSubtypeUnchecked(lastArg, types.elemtype(varParam)) && |
|
2758 !types.isSameType(types.erasure(varParam), types.erasure(lastArg))) |
|
2759 log.warning(argtrees.last().pos(), "inexact.non-varargs.call", |
|
2760 types.elemtype(varParam), |
|
2761 varParam); |
|
2762 } |
|
2763 |
|
2764 if (warned && sym.type.tag == FORALL) { |
|
2765 chk.warnUnchecked(env.tree.pos(), |
|
2766 "unchecked.meth.invocation.applied", |
|
2767 kindName(sym), |
|
2768 sym.name, |
|
2769 rs.methodArguments(sym.type.getParameterTypes()), |
|
2770 rs.methodArguments(argtypes), |
|
2771 kindName(sym.location()), |
|
2772 sym.location()); |
|
2773 owntype = new MethodType(owntype.getParameterTypes(), |
|
2774 types.erasure(owntype.getReturnType()), |
|
2775 types.erasure(owntype.getThrownTypes()), |
|
2776 syms.methodClass); |
|
2777 } |
|
2778 if (useVarargs) { |
|
2779 JCTree tree = env.tree; |
|
2780 Type argtype = owntype.getParameterTypes().last(); |
|
2781 if (owntype.getReturnType().tag != FORALL || warned) { |
|
2782 chk.checkVararg(env.tree.pos(), owntype.getParameterTypes(), sym); |
|
2783 } |
|
2784 Type elemtype = types.elemtype(argtype); |
|
2785 switch (tree.getTag()) { |
|
2786 case APPLY: |
|
2787 ((JCMethodInvocation) tree).varargsElement = elemtype; |
|
2788 break; |
|
2789 case NEWCLASS: |
|
2790 ((JCNewClass) tree).varargsElement = elemtype; |
|
2791 break; |
|
2792 default: |
|
2793 throw new AssertionError(""+tree); |
|
2794 } |
|
2795 } |
|
2796 } |
|
2797 return owntype; |
2746 return owntype; |
2798 } |
|
2799 |
|
2800 private void assertConvertible(JCTree tree, Type actual, Type formal, Warner warn) { |
|
2801 if (types.isConvertible(actual, formal, warn)) |
|
2802 return; |
|
2803 |
|
2804 if (formal.isCompound() |
|
2805 && types.isSubtype(actual, types.supertype(formal)) |
|
2806 && types.isSubtypeUnchecked(actual, types.interfaces(formal), warn)) |
|
2807 return; |
|
2808 |
|
2809 if (false) { |
|
2810 // TODO: make assertConvertible work |
|
2811 chk.typeError(tree.pos(), diags.fragment("incompatible.types"), actual, formal); |
|
2812 throw new AssertionError("Tree: " + tree |
|
2813 + " actual:" + actual |
|
2814 + " formal: " + formal); |
|
2815 } |
|
2816 } |
2747 } |
2817 |
2748 |
2818 public void visitLiteral(JCLiteral tree) { |
2749 public void visitLiteral(JCLiteral tree) { |
2819 result = check( |
2750 result = check( |
2820 tree, litType(tree.typetag).constType(tree.value), VAL, pkind, pt); |
2751 tree, litType(tree.typetag).constType(tree.value), VAL, pkind, pt); |