6975275: diamond implementation needs some cleanup
Summary: resolution issues during diamond inference should be reported through Resolve.logResolveError()
Reviewed-by: jjg
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Aug 05 09:45:25 2010 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Aug 10 14:52:34 2010 +0100
@@ -1554,7 +1554,7 @@
List<Type> 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<Scope, Scope> mapping,
List<Type> argtypes,
- List<Type> typeargtypes,
- boolean reportErrors) {
+ List<Type> 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(),
- "cant.apply.diamond.1",
- diags.fragment("diamond", clazztype.tsym),
- ex.diagnostic);
- }
+ 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<Type> 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)));
- }
+ clazztype = chk.checkClassType(tree.clazz.pos(),
+ clazztype,
+ true);
+ if (clazztype.tag == CLASS) {
+ List<Type> 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)));
}
}
return clazztype;
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java Thu Aug 05 09:45:25 2010 +0100
+++ b/langtools/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<AttrContext> env,
Type site,
List<Type> argtypes,
- List<Type> typeargtypes, boolean reportErrors) {
+ List<Type> typeargtypes) {
Symbol sym = methodNotFound;
JCDiagnostic explanation = null;
List<MethodResolutionPhase> 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<Type> argtypes, List<Type> 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;
}