Skip to content

Commit 91f407d

Browse files
archiecobbsVicente Romero
authored and
Vicente Romero
committed
8029301: Confusing error message for array creation method reference
Reviewed-by: vromero
1 parent e73411a commit 91f407d

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -4174,6 +4174,8 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
41744174

41754175
Pair<Symbol, JCDiagnostic> c = errCandidate();
41764176
Symbol ws = c.fst.asMemberOf(site, types);
4177+
UnaryOperator<JCDiagnostic> rewriter = compactMethodDiags ?
4178+
d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null;
41774179

41784180
// If the problem is due to type arguments, then the method parameters aren't relevant,
41794181
// so use the error message that omits them to avoid confusion.
@@ -4182,18 +4184,25 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
41824184
case "compiler.misc.explicit.param.do.not.conform.to.bounds":
41834185
return diags.create(dkind, log.currentSource(), pos,
41844186
"cant.apply.symbol.noargs",
4185-
compactMethodDiags ?
4186-
d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null,
4187+
rewriter,
41874188
kindName(ws),
41884189
ws.name == names.init ? ws.owner.name : ws.name,
41894190
kindName(ws.owner),
41904191
ws.owner.type,
41914192
c.snd);
41924193
default:
4194+
// Avoid saying "constructor Array in class Array"
4195+
if (ws.owner == syms.arrayClass && ws.name == names.init) {
4196+
return diags.create(dkind, log.currentSource(), pos,
4197+
"cant.apply.array.ctor",
4198+
rewriter,
4199+
methodArguments(ws.type.getParameterTypes()),
4200+
methodArguments(argtypes),
4201+
c.snd);
4202+
}
41934203
return diags.create(dkind, log.currentSource(), pos,
41944204
"cant.apply.symbol",
4195-
compactMethodDiags ?
4196-
d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null,
4205+
rewriter,
41974206
kindName(ws),
41984207
ws.name == names.init ? ws.owner.name : ws.name,
41994208
methodArguments(ws.type.getParameterTypes()),

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

+7
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ compiler.misc.cant.apply.symbol=\
260260
found: {3}\n\
261261
reason: {6}
262262

263+
# 0: list of type or message segment, 1: list of type or message segment, 2: message segment
264+
compiler.misc.cant.apply.array.ctor=\
265+
cannot create array from given types\n\
266+
required: {0}\n\
267+
found: {1}\n\
268+
reason: {2}
269+
263270
# 0: symbol kind, 1: name, 2: list of type
264271
compiler.misc.cant.apply.symbols=\
265272
no suitable {0} found for {1}({2})

test/langtools/tools/javac/diags/examples/CantApplySymbolFragment.java

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
// key: compiler.misc.no.conforming.assignment.exists
2727
// key: compiler.misc.cant.apply.symbol
2828
// key: compiler.misc.invalid.mref
29+
// key: compiler.misc.cant.apply.array.ctor
30+
// key: compiler.misc.arg.length.mismatch
2931

3032
class CantApplySymbolFragment {
3133

@@ -38,4 +40,8 @@ static void f(String s) { }
3840
void test() {
3941
SAM s = CantApplySymbolFragment::f;
4042
}
43+
44+
void test2() {
45+
Runnable x = String[]::new;
46+
}
4147
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
MethodReference60.java:26:44: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbol: kindname.constructor, Array, int, compiler.misc.no.args, kindname.class, Array, (compiler.misc.arg.length.mismatch)))
2-
MethodReference60.java:27:44: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbol: kindname.constructor, Array, int, int,int, kindname.class, Array, (compiler.misc.arg.length.mismatch)))
3-
MethodReference60.java:28:44: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbol: kindname.constructor, Array, int, java.lang.String, kindname.class, Array, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: java.lang.String, int))))
1+
MethodReference60.java:26:44: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.array.ctor: int, compiler.misc.no.args, (compiler.misc.arg.length.mismatch)))
2+
MethodReference60.java:27:44: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.array.ctor: int, int,int, (compiler.misc.arg.length.mismatch)))
3+
MethodReference60.java:28:44: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.array.ctor: int, java.lang.String, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: java.lang.String, int))))
44
MethodReference60.java:29:42: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.mref: (compiler.misc.inconvertible.types: int[], java.lang.Integer))
55
MethodReference60.java:30:44: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.mref: (compiler.misc.inconvertible.types: int[], java.lang.Integer[]))
66
5 errors

0 commit comments

Comments
 (0)