Tue, 10 Aug 2010 14:52:34 +0100
6975275: diamond implementation needs some cleanup
Summary: resolution issues during diamond inference should be reported through Resolve.logResolveError()
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Aug 05 09:45:25 2010 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Aug 10 14:52:34 2010 +0100 1.3 @@ -1554,7 +1554,7 @@ 1.4 List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv); 1.5 1.6 if (TreeInfo.isDiamond(tree)) { 1.7 - clazztype = attribDiamond(localEnv, tree, clazztype, mapping, argtypes, typeargtypes, true); 1.8 + clazztype = attribDiamond(localEnv, tree, clazztype, mapping, argtypes, typeargtypes); 1.9 clazz.type = clazztype; 1.10 } 1.11 1.12 @@ -1692,8 +1692,7 @@ 1.13 Type clazztype, 1.14 Pair<Scope, Scope> mapping, 1.15 List<Type> argtypes, 1.16 - List<Type> typeargtypes, 1.17 - boolean reportErrors) { 1.18 + List<Type> typeargtypes) { 1.19 if (clazztype.isErroneous() || mapping == erroneousMapping) { 1.20 //if the type of the instance creation expression is erroneous, 1.21 //or something prevented us to form a valid mapping, return the 1.22 @@ -1731,7 +1730,7 @@ 1.23 env, 1.24 clazztype.tsym.type, 1.25 argtypes, 1.26 - typeargtypes, reportErrors); 1.27 + typeargtypes); 1.28 } finally { 1.29 ((ClassSymbol) clazztype.tsym).members_field = mapping.fst; 1.30 } 1.31 @@ -1760,42 +1759,37 @@ 1.32 Warner.noWarnings); 1.33 } catch (Infer.InferenceException ex) { 1.34 //an error occurred while inferring uninstantiated type-variables 1.35 - //we need to optionally report an error 1.36 - if (reportErrors) { 1.37 - log.error(tree.clazz.pos(), 1.38 + log.error(tree.clazz.pos(), 1.39 + "cant.apply.diamond.1", 1.40 + diags.fragment("diamond", clazztype.tsym), 1.41 + ex.diagnostic); 1.42 + } 1.43 + } 1.44 + clazztype = chk.checkClassType(tree.clazz.pos(), 1.45 + clazztype, 1.46 + true); 1.47 + if (clazztype.tag == CLASS) { 1.48 + List<Type> invalidDiamondArgs = chk.checkDiamond((ClassType)clazztype); 1.49 + if (!clazztype.isErroneous() && invalidDiamondArgs.nonEmpty()) { 1.50 + //one or more types inferred in the previous steps is either a 1.51 + //captured type or an intersection type --- we need to report an error. 1.52 + String subkey = invalidDiamondArgs.size() > 1 ? 1.53 + "diamond.invalid.args" : 1.54 + "diamond.invalid.arg"; 1.55 + //The error message is of the kind: 1.56 + // 1.57 + //cannot infer type arguments for {clazztype}<>; 1.58 + //reason: {subkey} 1.59 + // 1.60 + //where subkey is a fragment of the kind: 1.61 + // 1.62 + //type argument(s) {invalidDiamondArgs} inferred for {clazztype}<> is not allowed in this context 1.63 + log.error(tree.clazz.pos(), 1.64 "cant.apply.diamond.1", 1.65 diags.fragment("diamond", clazztype.tsym), 1.66 - ex.diagnostic); 1.67 - } 1.68 - } 1.69 - } 1.70 - if (reportErrors) { 1.71 - clazztype = chk.checkClassType(tree.clazz.pos(), 1.72 - clazztype, 1.73 - true); 1.74 - if (clazztype.tag == CLASS) { 1.75 - List<Type> invalidDiamondArgs = chk.checkDiamond((ClassType)clazztype); 1.76 - if (!clazztype.isErroneous() && invalidDiamondArgs.nonEmpty()) { 1.77 - //one or more types inferred in the previous steps is either a 1.78 - //captured type or an intersection type --- we need to report an error. 1.79 - String subkey = invalidDiamondArgs.size() > 1 ? 1.80 - "diamond.invalid.args" : 1.81 - "diamond.invalid.arg"; 1.82 - //The error message is of the kind: 1.83 - // 1.84 - //cannot infer type arguments for {clazztype}<>; 1.85 - //reason: {subkey} 1.86 - // 1.87 - //where subkey is a fragment of the kind: 1.88 - // 1.89 - //type argument(s) {invalidDiamondArgs} inferred for {clazztype}<> is not allowed in this context 1.90 - log.error(tree.clazz.pos(), 1.91 - "cant.apply.diamond.1", 1.92 - diags.fragment("diamond", clazztype.tsym), 1.93 - diags.fragment(subkey, 1.94 - invalidDiamondArgs, 1.95 - diags.fragment("diamond", clazztype.tsym))); 1.96 - } 1.97 + diags.fragment(subkey, 1.98 + invalidDiamondArgs, 1.99 + diags.fragment("diamond", clazztype.tsym))); 1.100 } 1.101 } 1.102 return clazztype;
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java Thu Aug 05 09:45:25 2010 +0100 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java Tue Aug 10 14:52:34 2010 +0100 2.3 @@ -40,6 +40,7 @@ 2.4 import static com.sun.tools.javac.code.Flags.*; 2.5 import static com.sun.tools.javac.code.Kinds.*; 2.6 import static com.sun.tools.javac.code.TypeTags.*; 2.7 +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; 2.8 import javax.lang.model.element.ElementVisitor; 2.9 2.10 import java.util.Map; 2.11 @@ -1447,7 +1448,7 @@ 2.12 Env<AttrContext> env, 2.13 Type site, 2.14 List<Type> argtypes, 2.15 - List<Type> typeargtypes, boolean reportErrors) { 2.16 + List<Type> typeargtypes) { 2.17 Symbol sym = methodNotFound; 2.18 JCDiagnostic explanation = null; 2.19 List<MethodResolutionPhase> steps = methodResolutionSteps; 2.20 @@ -1466,11 +1467,20 @@ 2.21 } 2.22 steps = steps.tail; 2.23 } 2.24 - if (sym.kind >= AMBIGUOUS && reportErrors) { 2.25 - String key = explanation == null ? 2.26 - "cant.apply.diamond" : 2.27 - "cant.apply.diamond.1"; 2.28 - log.error(pos, key, diags.fragment("diamond", site.tsym), explanation); 2.29 + if (sym.kind >= AMBIGUOUS) { 2.30 + final JCDiagnostic details = explanation; 2.31 + Symbol errSym = new ResolveError(WRONG_MTH, "diamond error") { 2.32 + @Override 2.33 + JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) { 2.34 + String key = details == null ? 2.35 + "cant.apply.diamond" : 2.36 + "cant.apply.diamond.1"; 2.37 + return diags.create(dkind, log.currentSource(), pos, key, diags.fragment("diamond", site.tsym), details); 2.38 + } 2.39 + }; 2.40 + MethodResolutionPhase errPhase = firstErroneousResolutionPhase(); 2.41 + sym = access(errSym, pos, site, names.init, true, argtypes, typeargtypes); 2.42 + env.info.varArgs = errPhase.isVarargsRequired(); 2.43 } 2.44 return sym; 2.45 }