Skip to content

Commit b77effa

Browse files
committed
6968793: issues with diagnostics
Several diagnostic improvements Reviewed-by: jjg
1 parent df54c56 commit b77effa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+351
-195
lines changed

langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,10 @@ public void visitForeachLoop(JCEnhancedForLoop tree) {
905905
// or perhaps expr implements Iterable<T>?
906906
Type base = types.asSuper(exprType, syms.iterableType.tsym);
907907
if (base == null) {
908-
log.error(tree.expr.pos(), "foreach.not.applicable.to.type");
908+
log.error(tree.expr.pos(),
909+
"foreach.not.applicable.to.type",
910+
exprType,
911+
diags.fragment("type.req.array.or.iterable"));
909912
elemtype = types.createErrorType(exprType);
910913
} else {
911914
List<Type> iterableParams = base.allparams();
@@ -970,7 +973,7 @@ public void visitSwitch(JCSwitch tree) {
970973
if (enumSwitch) {
971974
Symbol sym = enumConstant(c.pat, seltype);
972975
if (sym == null) {
973-
log.error(c.pat.pos(), "enum.const.req");
976+
log.error(c.pat.pos(), "enum.label.must.be.unqualified.enum");
974977
} else if (!labels.add(sym)) {
975978
log.error(c.pos(), "duplicate.case.label");
976979
}
@@ -2228,10 +2231,10 @@ public void visitSelect(JCFieldAccess tree) {
22282231

22292232
// Determine the symbol represented by the selection.
22302233
env.info.varArgs = false;
2231-
Symbol sym = selectSym(tree, site, env, pt, pkind);
2234+
Symbol sym = selectSym(tree, sitesym, site, env, pt, pkind);
22322235
if (sym.exists() && !isType(sym) && (pkind & (PCK | TYP)) != 0) {
22332236
site = capture(site);
2234-
sym = selectSym(tree, site, env, pt, pkind);
2237+
sym = selectSym(tree, sitesym, site, env, pt, pkind);
22352238
}
22362239
boolean varArgs = env.info.varArgs;
22372240
tree.sym = sym;
@@ -2320,6 +2323,14 @@ public void visitSelect(JCFieldAccess tree) {
23202323
* @param pkind The expected kind(s) of the Select expression.
23212324
*/
23222325
private Symbol selectSym(JCFieldAccess tree,
2326+
Type site,
2327+
Env<AttrContext> env,
2328+
Type pt,
2329+
int pkind) {
2330+
return selectSym(tree, site.tsym, site, env, pt, pkind);
2331+
}
2332+
private Symbol selectSym(JCFieldAccess tree,
2333+
Symbol location,
23232334
Type site,
23242335
Env<AttrContext> env,
23252336
Type pt,
@@ -2331,12 +2342,12 @@ private Symbol selectSym(JCFieldAccess tree,
23312342
case PACKAGE:
23322343
return rs.access(
23332344
rs.findIdentInPackage(env, site.tsym, name, pkind),
2334-
pos, site, name, true);
2345+
pos, location, site, name, true);
23352346
case ARRAY:
23362347
case CLASS:
23372348
if (pt.tag == METHOD || pt.tag == FORALL) {
23382349
return rs.resolveQualifiedMethod(
2339-
pos, env, site, name, pt.getParameterTypes(), pt.getTypeArguments());
2350+
pos, env, location, site, name, pt.getParameterTypes(), pt.getTypeArguments());
23402351
} else if (name == names._this || name == names._super) {
23412352
return rs.resolveSelf(pos, env, site.tsym, name);
23422353
} else if (name == names._class) {
@@ -2353,20 +2364,20 @@ private Symbol selectSym(JCFieldAccess tree,
23532364
// We are seeing a plain identifier as selector.
23542365
Symbol sym = rs.findIdentInType(env, site, name, pkind);
23552366
if ((pkind & ERRONEOUS) == 0)
2356-
sym = rs.access(sym, pos, site, name, true);
2367+
sym = rs.access(sym, pos, location, site, name, true);
23572368
return sym;
23582369
}
23592370
case WILDCARD:
23602371
throw new AssertionError(tree);
23612372
case TYPEVAR:
23622373
// Normally, site.getUpperBound() shouldn't be null.
23632374
// It should only happen during memberEnter/attribBase
2364-
// when determining the super type which *must* be
2375+
// when determining the super type which *must* beac
23652376
// done before attributing the type variables. In
23662377
// other words, we are seeing this illegal program:
23672378
// class B<T> extends A<T.foo> {}
23682379
Symbol sym = (site.getUpperBound() != null)
2369-
? selectSym(tree, capture(site.getUpperBound()), env, pt, pkind)
2380+
? selectSym(tree, location, capture(site.getUpperBound()), env, pt, pkind)
23702381
: null;
23712382
if (sym == null) {
23722383
log.error(pos, "type.var.cant.be.deref");
@@ -2375,7 +2386,7 @@ private Symbol selectSym(JCFieldAccess tree,
23752386
Symbol sym2 = (sym.flags() & Flags.PRIVATE) != 0 ?
23762387
rs.new AccessError(env, site, sym) :
23772388
sym;
2378-
rs.access(sym2, pos, site, name, true);
2389+
rs.access(sym2, pos, location, site, name, true);
23792390
return sym;
23802391
}
23812392
case ERROR:

langtools/src/share/classes/com/sun/tools/javac/comp/Check.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,10 +1042,13 @@ public void visitTypeApply(JCTypeApply tree) {
10421042
if (incompatibleArg != null) {
10431043
for (JCTree arg : tree.arguments) {
10441044
if (arg.type == incompatibleArg) {
1045-
log.error(arg, "not.within.bounds", incompatibleArg);
1045+
log.error(arg, "not.within.bounds", incompatibleArg, forms.head);
10461046
}
1047-
}
1048-
}
1047+
forms = forms.tail;
1048+
}
1049+
}
1050+
1051+
forms = tree.type.tsym.type.getTypeArguments();
10491052

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

langtools/src/share/classes/com/sun/tools/javac/comp/MemberEnter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ else if (tree.implementing.nonEmpty()) {
984984
c.owner.kind == PCK && c.owner != syms.unnamedPackage &&
985985
reader.packageExists(c.fullname))
986986
{
987-
log.error(tree.pos, "clash.with.pkg.of.same.name", c);
987+
log.error(tree.pos, "clash.with.pkg.of.same.name", Kinds.kindName(sym), c);
988988
}
989989

990990
} catch (CompletionFailure ex) {

langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ Symbol findIdentInType(Env<AttrContext> env, Type site,
12361236
*/
12371237
Symbol access(Symbol sym,
12381238
DiagnosticPosition pos,
1239+
Symbol location,
12391240
Type site,
12401241
Name name,
12411242
boolean qualified,
@@ -1246,25 +1247,48 @@ Symbol access(Symbol sym,
12461247
if (!site.isErroneous() &&
12471248
!Type.isErroneous(argtypes) &&
12481249
(typeargtypes==null || !Type.isErroneous(typeargtypes)))
1249-
logResolveError(errSym, pos, site, name, argtypes, typeargtypes);
1250+
logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
12501251
sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
12511252
}
12521253
return sym;
12531254
}
12541255

1255-
/** Same as above, but without type arguments and arguments.
1256+
/** Same as original access(), but without location.
12561257
*/
12571258
Symbol access(Symbol sym,
12581259
DiagnosticPosition pos,
12591260
Type site,
12601261
Name name,
1262+
boolean qualified,
1263+
List<Type> argtypes,
1264+
List<Type> typeargtypes) {
1265+
return access(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
1266+
}
1267+
1268+
/** Same as original access(), but without type arguments and arguments.
1269+
*/
1270+
Symbol access(Symbol sym,
1271+
DiagnosticPosition pos,
1272+
Symbol location,
1273+
Type site,
1274+
Name name,
12611275
boolean qualified) {
12621276
if (sym.kind >= AMBIGUOUS)
1263-
return access(sym, pos, site, name, qualified, List.<Type>nil(), null);
1277+
return access(sym, pos, location, site, name, qualified, List.<Type>nil(), null);
12641278
else
12651279
return sym;
12661280
}
12671281

1282+
/** Same as original access(), but without location, type arguments and arguments.
1283+
*/
1284+
Symbol access(Symbol sym,
1285+
DiagnosticPosition pos,
1286+
Type site,
1287+
Name name,
1288+
boolean qualified) {
1289+
return access(sym, pos, site.tsym, site, name, qualified);
1290+
}
1291+
12681292
/** Check that sym is not an abstract method.
12691293
*/
12701294
void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
@@ -1380,6 +1404,11 @@ private Symbol startResolution() {
13801404
Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
13811405
Type site, Name name, List<Type> argtypes,
13821406
List<Type> typeargtypes) {
1407+
return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes);
1408+
}
1409+
Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
1410+
Symbol location, Type site, Name name, List<Type> argtypes,
1411+
List<Type> typeargtypes) {
13831412
Symbol sym = startResolution();
13841413
List<MethodResolutionPhase> steps = methodResolutionSteps;
13851414
while (steps.nonEmpty() &&
@@ -1404,7 +1433,7 @@ Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
14041433
MethodResolutionPhase errPhase =
14051434
firstErroneousResolutionPhase();
14061435
sym = access(methodResolutionCache.get(errPhase),
1407-
pos, site, name, true, argtypes, typeargtypes);
1436+
pos, location, site, name, true, argtypes, typeargtypes);
14081437
env.info.varArgs = errPhase.isVarargsRequired;
14091438
}
14101439
} else if (allowMethodHandles && sym.isPolymorphicSignatureGeneric()) {
@@ -1471,7 +1500,7 @@ public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContex
14711500
List<Type> argtypes,
14721501
List<Type> typeargtypes) {
14731502
Symbol sym = resolveQualifiedMethod(
1474-
pos, env, site, name, argtypes, typeargtypes);
1503+
pos, env, site.tsym, site, name, argtypes, typeargtypes);
14751504
if (sym.kind == MTH) return (MethodSymbol)sym;
14761505
else throw new FatalError(
14771506
diags.fragment("fatal.err.cant.locate.meth",
@@ -1546,11 +1575,13 @@ Symbol resolveDiamond(DiagnosticPosition pos,
15461575
null;
15471576
Symbol errSym = new ResolveError(WRONG_MTH, "diamond error") {
15481577
@Override
1549-
JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
1578+
JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
1579+
Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
15501580
String key = details == null ?
15511581
"cant.apply.diamond" :
15521582
"cant.apply.diamond.1";
1553-
return diags.create(dkind, log.currentSource(), pos, key, diags.fragment("diamond", site.tsym), details);
1583+
return diags.create(dkind, log.currentSource(), pos, key,
1584+
diags.fragment("diamond", site.tsym), details);
15541585
}
15551586
};
15561587
MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
@@ -1729,17 +1760,18 @@ Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
17291760

17301761
public void logAccessError(Env<AttrContext> env, JCTree tree, Type type) {
17311762
AccessError error = new AccessError(env, type.getEnclosingType(), type.tsym);
1732-
logResolveError(error, tree.pos(), type.getEnclosingType(), null, null, null);
1763+
logResolveError(error, tree.pos(), type.getEnclosingType().tsym, type.getEnclosingType(), null, null, null);
17331764
}
17341765
//where
17351766
private void logResolveError(ResolveError error,
17361767
DiagnosticPosition pos,
1768+
Symbol location,
17371769
Type site,
17381770
Name name,
17391771
List<Type> argtypes,
17401772
List<Type> typeargtypes) {
17411773
JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
1742-
pos, site, name, argtypes, typeargtypes);
1774+
pos, location, site, name, argtypes, typeargtypes);
17431775
if (d != null) {
17441776
d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
17451777
log.report(d);
@@ -1809,6 +1841,7 @@ protected Symbol access(Name name, TypeSymbol location) {
18091841
*/
18101842
abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
18111843
DiagnosticPosition pos,
1844+
Symbol location,
18121845
Type site,
18131846
Name name,
18141847
List<Type> argtypes,
@@ -1874,6 +1907,7 @@ class SymbolNotFoundError extends ResolveError {
18741907
@Override
18751908
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
18761909
DiagnosticPosition pos,
1910+
Symbol location,
18771911
Type site,
18781912
Name name,
18791913
List<Type> argtypes,
@@ -1884,16 +1918,23 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
18841918
return null;
18851919

18861920
if (isOperator(name)) {
1921+
boolean isUnaryOp = argtypes.size() == 1;
1922+
String key = argtypes.size() == 1 ?
1923+
"operator.cant.be.applied" :
1924+
"operator.cant.be.applied.1";
1925+
Type first = argtypes.head;
1926+
Type second = !isUnaryOp ? argtypes.tail.head : null;
18871927
return diags.create(dkind, log.currentSource(), pos,
1888-
"operator.cant.be.applied", name, argtypes);
1928+
key, name, first, second);
18891929
}
18901930
boolean hasLocation = false;
1891-
if (!site.tsym.name.isEmpty()) {
1892-
if (site.tsym.kind == PCK && !site.tsym.exists()) {
1931+
if (!location.name.isEmpty()) {
1932+
if (location.kind == PCK && !site.tsym.exists()) {
18931933
return diags.create(dkind, log.currentSource(), pos,
1894-
"doesnt.exist", site.tsym);
1934+
"doesnt.exist", location);
18951935
}
1896-
hasLocation = true;
1936+
hasLocation = !location.name.equals(names._this) &&
1937+
!location.name.equals(names._super);
18971938
}
18981939
boolean isConstructor = kind == ABSENT_MTH &&
18991940
name == names.table.names.init;
@@ -1904,7 +1945,7 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
19041945
return diags.create(dkind, log.currentSource(), pos,
19051946
errKey, kindname, idname, //symbol kindname, name
19061947
typeargtypes, argtypes, //type parameters and arguments (if any)
1907-
typeKindName(site), site); //location kindname, type
1948+
getLocationDiag(location)); //location kindname, type
19081949
}
19091950
else {
19101951
return diags.create(dkind, log.currentSource(), pos,
@@ -1925,6 +1966,16 @@ private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLo
19251966
}
19261967
return key + suffix;
19271968
}
1969+
private JCDiagnostic getLocationDiag(Symbol location) {
1970+
boolean isVar = location.kind == VAR;
1971+
String key = isVar ?
1972+
"location.1" :
1973+
"location";
1974+
return diags.fragment(key,
1975+
kindName(location),
1976+
location,
1977+
isVar ? location.type : null);
1978+
}
19281979
}
19291980

19301981
/**
@@ -1965,6 +2016,7 @@ public String toString() {
19652016
@Override
19662017
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
19672018
DiagnosticPosition pos,
2019+
Symbol location,
19682020
Type site,
19692021
Name name,
19702022
List<Type> argtypes,
@@ -2016,6 +2068,7 @@ class InapplicableSymbolsError extends ResolveError {
20162068
@Override
20172069
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
20182070
DiagnosticPosition pos,
2071+
Symbol location,
20192072
Type site,
20202073
Name name,
20212074
List<Type> argtypes,
@@ -2031,7 +2084,7 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
20312084
return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(site));
20322085
} else {
20332086
return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
2034-
site, name, argtypes, typeargtypes);
2087+
location, site, name, argtypes, typeargtypes);
20352088
}
20362089
}
20372090

@@ -2131,6 +2184,7 @@ public boolean exists() {
21312184
@Override
21322185
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
21332186
DiagnosticPosition pos,
2187+
Symbol location,
21342188
Type site,
21352189
Name name,
21362190
List<Type> argtypes,
@@ -2140,7 +2194,7 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
21402194

21412195
if (sym.name == names.init && sym.owner != site.tsym) {
21422196
return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
2143-
pos, site, name, argtypes, typeargtypes);
2197+
pos, location, site, name, argtypes, typeargtypes);
21442198
}
21452199
else if ((sym.flags() & PUBLIC) != 0
21462200
|| (env != null && this.site != null
@@ -2175,6 +2229,7 @@ class StaticError extends InvalidSymbolError {
21752229
@Override
21762230
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
21772231
DiagnosticPosition pos,
2232+
Symbol location,
21782233
Type site,
21792234
Name name,
21802235
List<Type> argtypes,
@@ -2205,6 +2260,7 @@ class AmbiguityError extends InvalidSymbolError {
22052260
@Override
22062261
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
22072262
DiagnosticPosition pos,
2263+
Symbol location,
22082264
Type site,
22092265
Name name,
22102266
List<Type> argtypes,

0 commit comments

Comments
 (0)