提交 fc53d8b5 编写于 作者: M mcimadamore

6975275: diamond implementation needs some cleanup

Summary: resolution issues during diamond inference should be reported through Resolve.logResolveError()
Reviewed-by: jjg
上级 de1d34cb
...@@ -1554,7 +1554,7 @@ public class Attr extends JCTree.Visitor { ...@@ -1554,7 +1554,7 @@ public class Attr extends JCTree.Visitor {
List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv); List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv);
if (TreeInfo.isDiamond(tree)) { if (TreeInfo.isDiamond(tree)) {
clazztype = attribDiamond(localEnv, tree, clazztype, mapping, argtypes, typeargtypes, true); clazztype = attribDiamond(localEnv, tree, clazztype, mapping, argtypes, typeargtypes);
clazz.type = clazztype; clazz.type = clazztype;
} }
...@@ -1692,8 +1692,7 @@ public class Attr extends JCTree.Visitor { ...@@ -1692,8 +1692,7 @@ public class Attr extends JCTree.Visitor {
Type clazztype, Type clazztype,
Pair<Scope, Scope> mapping, Pair<Scope, Scope> mapping,
List<Type> argtypes, List<Type> argtypes,
List<Type> typeargtypes, List<Type> typeargtypes) {
boolean reportErrors) {
if (clazztype.isErroneous() || mapping == erroneousMapping) { if (clazztype.isErroneous() || mapping == erroneousMapping) {
//if the type of the instance creation expression is erroneous, //if the type of the instance creation expression is erroneous,
//or something prevented us to form a valid mapping, return the //or something prevented us to form a valid mapping, return the
...@@ -1731,7 +1730,7 @@ public class Attr extends JCTree.Visitor { ...@@ -1731,7 +1730,7 @@ public class Attr extends JCTree.Visitor {
env, env,
clazztype.tsym.type, clazztype.tsym.type,
argtypes, argtypes,
typeargtypes, reportErrors); typeargtypes);
} finally { } finally {
((ClassSymbol) clazztype.tsym).members_field = mapping.fst; ((ClassSymbol) clazztype.tsym).members_field = mapping.fst;
} }
...@@ -1760,42 +1759,37 @@ public class Attr extends JCTree.Visitor { ...@@ -1760,42 +1759,37 @@ public class Attr extends JCTree.Visitor {
Warner.noWarnings); Warner.noWarnings);
} catch (Infer.InferenceException ex) { } catch (Infer.InferenceException ex) {
//an error occurred while inferring uninstantiated type-variables //an error occurred while inferring uninstantiated type-variables
//we need to optionally report an error log.error(tree.clazz.pos(),
if (reportErrors) { "cant.apply.diamond.1",
log.error(tree.clazz.pos(), diags.fragment("diamond", clazztype.tsym),
"cant.apply.diamond.1", ex.diagnostic);
diags.fragment("diamond", clazztype.tsym),
ex.diagnostic);
}
} }
} }
if (reportErrors) { clazztype = chk.checkClassType(tree.clazz.pos(),
clazztype = chk.checkClassType(tree.clazz.pos(), clazztype,
clazztype, true);
true); if (clazztype.tag == CLASS) {
if (clazztype.tag == CLASS) { List<Type> invalidDiamondArgs = chk.checkDiamond((ClassType)clazztype);
List<Type> invalidDiamondArgs = chk.checkDiamond((ClassType)clazztype); if (!clazztype.isErroneous() && invalidDiamondArgs.nonEmpty()) {
if (!clazztype.isErroneous() && invalidDiamondArgs.nonEmpty()) { //one or more types inferred in the previous steps is either a
//one or more types inferred in the previous steps is either a //captured type or an intersection type --- we need to report an error.
//captured type or an intersection type --- we need to report an error. String subkey = invalidDiamondArgs.size() > 1 ?
String subkey = invalidDiamondArgs.size() > 1 ? "diamond.invalid.args" :
"diamond.invalid.args" : "diamond.invalid.arg";
"diamond.invalid.arg"; //The error message is of the kind:
//The error message is of the kind: //
// //cannot infer type arguments for {clazztype}<>;
//cannot infer type arguments for {clazztype}<>; //reason: {subkey}
//reason: {subkey} //
// //where subkey is a fragment of the kind:
//where subkey is a fragment of the kind: //
// //type argument(s) {invalidDiamondArgs} inferred for {clazztype}<> is not allowed in this context
//type argument(s) {invalidDiamondArgs} inferred for {clazztype}<> is not allowed in this context log.error(tree.clazz.pos(),
log.error(tree.clazz.pos(), "cant.apply.diamond.1",
"cant.apply.diamond.1", diags.fragment("diamond", clazztype.tsym),
diags.fragment("diamond", clazztype.tsym), diags.fragment(subkey,
diags.fragment(subkey, invalidDiamondArgs,
invalidDiamondArgs, diags.fragment("diamond", clazztype.tsym)));
diags.fragment("diamond", clazztype.tsym)));
}
} }
} }
return clazztype; return clazztype;
......
...@@ -40,6 +40,7 @@ import com.sun.tools.javac.tree.JCTree.*; ...@@ -40,6 +40,7 @@ import com.sun.tools.javac.tree.JCTree.*;
import static com.sun.tools.javac.code.Flags.*; import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Kinds.*; import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.TypeTags.*; import static com.sun.tools.javac.code.TypeTags.*;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
import javax.lang.model.element.ElementVisitor; import javax.lang.model.element.ElementVisitor;
import java.util.Map; import java.util.Map;
...@@ -1447,7 +1448,7 @@ public class Resolve { ...@@ -1447,7 +1448,7 @@ public class Resolve {
Env<AttrContext> env, Env<AttrContext> env,
Type site, Type site,
List<Type> argtypes, List<Type> argtypes,
List<Type> typeargtypes, boolean reportErrors) { List<Type> typeargtypes) {
Symbol sym = methodNotFound; Symbol sym = methodNotFound;
JCDiagnostic explanation = null; JCDiagnostic explanation = null;
List<MethodResolutionPhase> steps = methodResolutionSteps; List<MethodResolutionPhase> steps = methodResolutionSteps;
...@@ -1466,11 +1467,20 @@ public class Resolve { ...@@ -1466,11 +1467,20 @@ public class Resolve {
} }
steps = steps.tail; steps = steps.tail;
} }
if (sym.kind >= AMBIGUOUS && reportErrors) { if (sym.kind >= AMBIGUOUS) {
String key = explanation == null ? final JCDiagnostic details = explanation;
"cant.apply.diamond" : Symbol errSym = new ResolveError(WRONG_MTH, "diamond error") {
"cant.apply.diamond.1"; @Override
log.error(pos, key, diags.fragment("diamond", site.tsym), explanation); 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; return sym;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册