Skip to content

Commit

Permalink
6968793: issues with diagnostics
Browse files Browse the repository at this point in the history
Several diagnostic improvements

Reviewed-by: jjg
  • Loading branch information
mcimadamore committed Jan 24, 2011
1 parent df54c56 commit b77effa
Show file tree
Hide file tree
Showing 46 changed files with 351 additions and 195 deletions.
31 changes: 21 additions & 10 deletions langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,10 @@ public void visitForeachLoop(JCEnhancedForLoop tree) {
// or perhaps expr implements Iterable<T>?
Type base = types.asSuper(exprType, syms.iterableType.tsym);
if (base == null) {
log.error(tree.expr.pos(), "foreach.not.applicable.to.type");
log.error(tree.expr.pos(),
"foreach.not.applicable.to.type",
exprType,
diags.fragment("type.req.array.or.iterable"));
elemtype = types.createErrorType(exprType);
} else {
List<Type> iterableParams = base.allparams();
Expand Down Expand Up @@ -970,7 +973,7 @@ public void visitSwitch(JCSwitch tree) {
if (enumSwitch) {
Symbol sym = enumConstant(c.pat, seltype);
if (sym == null) {
log.error(c.pat.pos(), "enum.const.req");
log.error(c.pat.pos(), "enum.label.must.be.unqualified.enum");
} else if (!labels.add(sym)) {
log.error(c.pos(), "duplicate.case.label");
}
Expand Down Expand Up @@ -2228,10 +2231,10 @@ public void visitSelect(JCFieldAccess tree) {

// Determine the symbol represented by the selection.
env.info.varArgs = false;
Symbol sym = selectSym(tree, site, env, pt, pkind);
Symbol sym = selectSym(tree, sitesym, site, env, pt, pkind);
if (sym.exists() && !isType(sym) && (pkind & (PCK | TYP)) != 0) {
site = capture(site);
sym = selectSym(tree, site, env, pt, pkind);
sym = selectSym(tree, sitesym, site, env, pt, pkind);
}
boolean varArgs = env.info.varArgs;
tree.sym = sym;
Expand Down Expand Up @@ -2320,6 +2323,14 @@ public void visitSelect(JCFieldAccess tree) {
* @param pkind The expected kind(s) of the Select expression.
*/
private Symbol selectSym(JCFieldAccess tree,
Type site,
Env<AttrContext> env,
Type pt,
int pkind) {
return selectSym(tree, site.tsym, site, env, pt, pkind);
}
private Symbol selectSym(JCFieldAccess tree,
Symbol location,
Type site,
Env<AttrContext> env,
Type pt,
Expand All @@ -2331,12 +2342,12 @@ private Symbol selectSym(JCFieldAccess tree,
case PACKAGE:
return rs.access(
rs.findIdentInPackage(env, site.tsym, name, pkind),
pos, site, name, true);
pos, location, site, name, true);
case ARRAY:
case CLASS:
if (pt.tag == METHOD || pt.tag == FORALL) {
return rs.resolveQualifiedMethod(
pos, env, site, name, pt.getParameterTypes(), pt.getTypeArguments());
pos, env, location, site, name, pt.getParameterTypes(), pt.getTypeArguments());
} else if (name == names._this || name == names._super) {
return rs.resolveSelf(pos, env, site.tsym, name);
} else if (name == names._class) {
Expand All @@ -2353,20 +2364,20 @@ private Symbol selectSym(JCFieldAccess tree,
// We are seeing a plain identifier as selector.
Symbol sym = rs.findIdentInType(env, site, name, pkind);
if ((pkind & ERRONEOUS) == 0)
sym = rs.access(sym, pos, site, name, true);
sym = rs.access(sym, pos, location, site, name, true);
return sym;
}
case WILDCARD:
throw new AssertionError(tree);
case TYPEVAR:
// Normally, site.getUpperBound() shouldn't be null.
// It should only happen during memberEnter/attribBase
// when determining the super type which *must* be
// when determining the super type which *must* beac
// done before attributing the type variables. In
// other words, we are seeing this illegal program:
// class B<T> extends A<T.foo> {}
Symbol sym = (site.getUpperBound() != null)
? selectSym(tree, capture(site.getUpperBound()), env, pt, pkind)
? selectSym(tree, location, capture(site.getUpperBound()), env, pt, pkind)
: null;
if (sym == null) {
log.error(pos, "type.var.cant.be.deref");
Expand All @@ -2375,7 +2386,7 @@ private Symbol selectSym(JCFieldAccess tree,
Symbol sym2 = (sym.flags() & Flags.PRIVATE) != 0 ?
rs.new AccessError(env, site, sym) :
sym;
rs.access(sym2, pos, site, name, true);
rs.access(sym2, pos, location, site, name, true);
return sym;
}
case ERROR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1042,10 +1042,13 @@ public void visitTypeApply(JCTypeApply tree) {
if (incompatibleArg != null) {
for (JCTree arg : tree.arguments) {
if (arg.type == incompatibleArg) {
log.error(arg, "not.within.bounds", incompatibleArg);
log.error(arg, "not.within.bounds", incompatibleArg, forms.head);
}
}
}
forms = forms.tail;
}
}

forms = tree.type.tsym.type.getTypeArguments();

boolean is_java_lang_Class = tree.type.tsym.flatName() == names.java_lang_Class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ else if (tree.implementing.nonEmpty()) {
c.owner.kind == PCK && c.owner != syms.unnamedPackage &&
reader.packageExists(c.fullname))
{
log.error(tree.pos, "clash.with.pkg.of.same.name", c);
log.error(tree.pos, "clash.with.pkg.of.same.name", Kinds.kindName(sym), c);
}

} catch (CompletionFailure ex) {
Expand Down
90 changes: 73 additions & 17 deletions langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@ Symbol findIdentInType(Env<AttrContext> env, Type site,
*/
Symbol access(Symbol sym,
DiagnosticPosition pos,
Symbol location,
Type site,
Name name,
boolean qualified,
Expand All @@ -1246,25 +1247,48 @@ Symbol access(Symbol sym,
if (!site.isErroneous() &&
!Type.isErroneous(argtypes) &&
(typeargtypes==null || !Type.isErroneous(typeargtypes)))
logResolveError(errSym, pos, site, name, argtypes, typeargtypes);
logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
}
return sym;
}

/** Same as above, but without type arguments and arguments.
/** Same as original access(), but without location.
*/
Symbol access(Symbol sym,
DiagnosticPosition pos,
Type site,
Name name,
boolean qualified,
List<Type> argtypes,
List<Type> typeargtypes) {
return access(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
}

/** Same as original access(), but without type arguments and arguments.
*/
Symbol access(Symbol sym,
DiagnosticPosition pos,
Symbol location,
Type site,
Name name,
boolean qualified) {
if (sym.kind >= AMBIGUOUS)
return access(sym, pos, site, name, qualified, List.<Type>nil(), null);
return access(sym, pos, location, site, name, qualified, List.<Type>nil(), null);
else
return sym;
}

/** Same as original access(), but without location, type arguments and arguments.
*/
Symbol access(Symbol sym,
DiagnosticPosition pos,
Type site,
Name name,
boolean qualified) {
return access(sym, pos, site.tsym, site, name, qualified);
}

/** Check that sym is not an abstract method.
*/
void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
Expand Down Expand Up @@ -1380,6 +1404,11 @@ private Symbol startResolution() {
Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
Type site, Name name, List<Type> argtypes,
List<Type> typeargtypes) {
return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes);
}
Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
Symbol location, Type site, Name name, List<Type> argtypes,
List<Type> typeargtypes) {
Symbol sym = startResolution();
List<MethodResolutionPhase> steps = methodResolutionSteps;
while (steps.nonEmpty() &&
Expand All @@ -1404,7 +1433,7 @@ Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
MethodResolutionPhase errPhase =
firstErroneousResolutionPhase();
sym = access(methodResolutionCache.get(errPhase),
pos, site, name, true, argtypes, typeargtypes);
pos, location, site, name, true, argtypes, typeargtypes);
env.info.varArgs = errPhase.isVarargsRequired;
}
} else if (allowMethodHandles && sym.isPolymorphicSignatureGeneric()) {
Expand Down Expand Up @@ -1471,7 +1500,7 @@ public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContex
List<Type> argtypes,
List<Type> typeargtypes) {
Symbol sym = resolveQualifiedMethod(
pos, env, site, name, argtypes, typeargtypes);
pos, env, site.tsym, site, name, argtypes, typeargtypes);
if (sym.kind == MTH) return (MethodSymbol)sym;
else throw new FatalError(
diags.fragment("fatal.err.cant.locate.meth",
Expand Down Expand Up @@ -1546,11 +1575,13 @@ Symbol resolveDiamond(DiagnosticPosition pos,
null;
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) {
JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
Symbol location, 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);
return diags.create(dkind, log.currentSource(), pos, key,
diags.fragment("diamond", site.tsym), details);
}
};
MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
Expand Down Expand Up @@ -1729,17 +1760,18 @@ Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {

public void logAccessError(Env<AttrContext> env, JCTree tree, Type type) {
AccessError error = new AccessError(env, type.getEnclosingType(), type.tsym);
logResolveError(error, tree.pos(), type.getEnclosingType(), null, null, null);
logResolveError(error, tree.pos(), type.getEnclosingType().tsym, type.getEnclosingType(), null, null, null);
}
//where
private void logResolveError(ResolveError error,
DiagnosticPosition pos,
Symbol location,
Type site,
Name name,
List<Type> argtypes,
List<Type> typeargtypes) {
JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
pos, site, name, argtypes, typeargtypes);
pos, location, site, name, argtypes, typeargtypes);
if (d != null) {
d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
log.report(d);
Expand Down Expand Up @@ -1809,6 +1841,7 @@ protected Symbol access(Name name, TypeSymbol location) {
*/
abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
DiagnosticPosition pos,
Symbol location,
Type site,
Name name,
List<Type> argtypes,
Expand Down Expand Up @@ -1874,6 +1907,7 @@ class SymbolNotFoundError extends ResolveError {
@Override
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
DiagnosticPosition pos,
Symbol location,
Type site,
Name name,
List<Type> argtypes,
Expand All @@ -1884,16 +1918,23 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
return null;

if (isOperator(name)) {
boolean isUnaryOp = argtypes.size() == 1;
String key = argtypes.size() == 1 ?
"operator.cant.be.applied" :
"operator.cant.be.applied.1";
Type first = argtypes.head;
Type second = !isUnaryOp ? argtypes.tail.head : null;
return diags.create(dkind, log.currentSource(), pos,
"operator.cant.be.applied", name, argtypes);
key, name, first, second);
}
boolean hasLocation = false;
if (!site.tsym.name.isEmpty()) {
if (site.tsym.kind == PCK && !site.tsym.exists()) {
if (!location.name.isEmpty()) {
if (location.kind == PCK && !site.tsym.exists()) {
return diags.create(dkind, log.currentSource(), pos,
"doesnt.exist", site.tsym);
"doesnt.exist", location);
}
hasLocation = true;
hasLocation = !location.name.equals(names._this) &&
!location.name.equals(names._super);
}
boolean isConstructor = kind == ABSENT_MTH &&
name == names.table.names.init;
Expand All @@ -1904,7 +1945,7 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
return diags.create(dkind, log.currentSource(), pos,
errKey, kindname, idname, //symbol kindname, name
typeargtypes, argtypes, //type parameters and arguments (if any)
typeKindName(site), site); //location kindname, type
getLocationDiag(location)); //location kindname, type
}
else {
return diags.create(dkind, log.currentSource(), pos,
Expand All @@ -1925,6 +1966,16 @@ private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLo
}
return key + suffix;
}
private JCDiagnostic getLocationDiag(Symbol location) {
boolean isVar = location.kind == VAR;
String key = isVar ?
"location.1" :
"location";
return diags.fragment(key,
kindName(location),
location,
isVar ? location.type : null);
}
}

/**
Expand Down Expand Up @@ -1965,6 +2016,7 @@ public String toString() {
@Override
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
DiagnosticPosition pos,
Symbol location,
Type site,
Name name,
List<Type> argtypes,
Expand Down Expand Up @@ -2016,6 +2068,7 @@ class InapplicableSymbolsError extends ResolveError {
@Override
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
DiagnosticPosition pos,
Symbol location,
Type site,
Name name,
List<Type> argtypes,
Expand All @@ -2031,7 +2084,7 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(site));
} else {
return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
site, name, argtypes, typeargtypes);
location, site, name, argtypes, typeargtypes);
}
}

Expand Down Expand Up @@ -2131,6 +2184,7 @@ public boolean exists() {
@Override
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
DiagnosticPosition pos,
Symbol location,
Type site,
Name name,
List<Type> argtypes,
Expand All @@ -2140,7 +2194,7 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,

if (sym.name == names.init && sym.owner != site.tsym) {
return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
pos, site, name, argtypes, typeargtypes);
pos, location, site, name, argtypes, typeargtypes);
}
else if ((sym.flags() & PUBLIC) != 0
|| (env != null && this.site != null
Expand Down Expand Up @@ -2175,6 +2229,7 @@ class StaticError extends InvalidSymbolError {
@Override
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
DiagnosticPosition pos,
Symbol location,
Type site,
Name name,
List<Type> argtypes,
Expand Down Expand Up @@ -2205,6 +2260,7 @@ class AmbiguityError extends InvalidSymbolError {
@Override
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
DiagnosticPosition pos,
Symbol location,
Type site,
Name name,
List<Type> argtypes,
Expand Down
Loading

0 comments on commit b77effa

Please sign in to comment.