# HG changeset patch # User mcimadamore # Date 1281448354 -3600 # Node ID a2d8c7071f24f86e0213718c2b44acae4106e465 # Parent 237f3bd52242afda5ea825f6026bc6af258db983 6975275: diamond implementation needs some cleanup Summary: resolution issues during diamond inference should be reported through Resolve.logResolveError() Reviewed-by: jjg diff -r 237f3bd52242 -r a2d8c7071f24 src/share/classes/com/sun/tools/javac/comp/Attr.java --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Aug 05 09:45:25 2010 +0100 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Aug 10 14:52:34 2010 +0100 @@ -1554,7 +1554,7 @@ List typeargtypes = attribTypes(tree.typeargs, localEnv); if (TreeInfo.isDiamond(tree)) { - clazztype = attribDiamond(localEnv, tree, clazztype, mapping, argtypes, typeargtypes, true); + clazztype = attribDiamond(localEnv, tree, clazztype, mapping, argtypes, typeargtypes); clazz.type = clazztype; } @@ -1692,8 +1692,7 @@ Type clazztype, Pair mapping, List argtypes, - List typeargtypes, - boolean reportErrors) { + List typeargtypes) { if (clazztype.isErroneous() || mapping == erroneousMapping) { //if the type of the instance creation expression is erroneous, //or something prevented us to form a valid mapping, return the @@ -1731,7 +1730,7 @@ env, clazztype.tsym.type, argtypes, - typeargtypes, reportErrors); + typeargtypes); } finally { ((ClassSymbol) clazztype.tsym).members_field = mapping.fst; } @@ -1760,42 +1759,37 @@ Warner.noWarnings); } catch (Infer.InferenceException ex) { //an error occurred while inferring uninstantiated type-variables - //we need to optionally report an error - if (reportErrors) { - log.error(tree.clazz.pos(), + log.error(tree.clazz.pos(), + "cant.apply.diamond.1", + diags.fragment("diamond", clazztype.tsym), + ex.diagnostic); + } + } + clazztype = chk.checkClassType(tree.clazz.pos(), + clazztype, + true); + if (clazztype.tag == CLASS) { + List invalidDiamondArgs = chk.checkDiamond((ClassType)clazztype); + if (!clazztype.isErroneous() && invalidDiamondArgs.nonEmpty()) { + //one or more types inferred in the previous steps is either a + //captured type or an intersection type --- we need to report an error. + String subkey = invalidDiamondArgs.size() > 1 ? + "diamond.invalid.args" : + "diamond.invalid.arg"; + //The error message is of the kind: + // + //cannot infer type arguments for {clazztype}<>; + //reason: {subkey} + // + //where subkey is a fragment of the kind: + // + //type argument(s) {invalidDiamondArgs} inferred for {clazztype}<> is not allowed in this context + log.error(tree.clazz.pos(), "cant.apply.diamond.1", diags.fragment("diamond", clazztype.tsym), - ex.diagnostic); - } - } - } - if (reportErrors) { - clazztype = chk.checkClassType(tree.clazz.pos(), - clazztype, - true); - if (clazztype.tag == CLASS) { - List invalidDiamondArgs = chk.checkDiamond((ClassType)clazztype); - if (!clazztype.isErroneous() && invalidDiamondArgs.nonEmpty()) { - //one or more types inferred in the previous steps is either a - //captured type or an intersection type --- we need to report an error. - String subkey = invalidDiamondArgs.size() > 1 ? - "diamond.invalid.args" : - "diamond.invalid.arg"; - //The error message is of the kind: - // - //cannot infer type arguments for {clazztype}<>; - //reason: {subkey} - // - //where subkey is a fragment of the kind: - // - //type argument(s) {invalidDiamondArgs} inferred for {clazztype}<> is not allowed in this context - log.error(tree.clazz.pos(), - "cant.apply.diamond.1", - diags.fragment("diamond", clazztype.tsym), - diags.fragment(subkey, - invalidDiamondArgs, - diags.fragment("diamond", clazztype.tsym))); - } + diags.fragment(subkey, + invalidDiamondArgs, + diags.fragment("diamond", clazztype.tsym))); } } return clazztype; diff -r 237f3bd52242 -r a2d8c7071f24 src/share/classes/com/sun/tools/javac/comp/Resolve.java --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java Thu Aug 05 09:45:25 2010 +0100 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java Tue Aug 10 14:52:34 2010 +0100 @@ -40,6 +40,7 @@ import static com.sun.tools.javac.code.Flags.*; import static com.sun.tools.javac.code.Kinds.*; import static com.sun.tools.javac.code.TypeTags.*; +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; import javax.lang.model.element.ElementVisitor; import java.util.Map; @@ -1447,7 +1448,7 @@ Env env, Type site, List argtypes, - List typeargtypes, boolean reportErrors) { + List typeargtypes) { Symbol sym = methodNotFound; JCDiagnostic explanation = null; List steps = methodResolutionSteps; @@ -1466,11 +1467,20 @@ } steps = steps.tail; } - if (sym.kind >= AMBIGUOUS && reportErrors) { - String key = explanation == null ? - "cant.apply.diamond" : - "cant.apply.diamond.1"; - log.error(pos, key, diags.fragment("diamond", site.tsym), explanation); + if (sym.kind >= AMBIGUOUS) { + final JCDiagnostic details = explanation; + Symbol errSym = new ResolveError(WRONG_MTH, "diamond error") { + @Override + JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Type site, Name name, List argtypes, List typeargtypes) { + String key = details == null ? + "cant.apply.diamond" : + "cant.apply.diamond.1"; + return diags.create(dkind, log.currentSource(), pos, key, diags.fragment("diamond", site.tsym), details); + } + }; + MethodResolutionPhase errPhase = firstErroneousResolutionPhase(); + sym = access(errSym, pos, site, names.init, true, argtypes, typeargtypes); + env.info.varArgs = errPhase.isVarargsRequired(); } return sym; }