8043251: Bogus javac error: required: no arguments, found: no arguments

Reviewed-by: vromero
This commit is contained in:
Archie L. Cobbs 2023-01-09 18:38:15 +00:00 committed by Vicente Romero
parent cd10c7278d
commit 679e485838
6 changed files with 47 additions and 13 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac
test/langtools/tools/javac

@ -4173,17 +4173,34 @@ public class Resolve {
Pair<Symbol, JCDiagnostic> c = errCandidate();
Symbol ws = c.fst.asMemberOf(site, types);
return diags.create(dkind, log.currentSource(), pos,
"cant.apply.symbol",
compactMethodDiags ?
d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null,
kindName(ws),
ws.name == names.init ? ws.owner.name : ws.name,
methodArguments(ws.type.getParameterTypes()),
methodArguments(argtypes),
kindName(ws.owner),
ws.owner.type,
c.snd);
// If the problem is due to type arguments, then the method parameters aren't relevant,
// so use the error message that omits them to avoid confusion.
switch (c.snd.getCode()) {
case "compiler.misc.wrong.number.type.args":
case "compiler.misc.explicit.param.do.not.conform.to.bounds":
return diags.create(dkind, log.currentSource(), pos,
"cant.apply.symbol.noargs",
compactMethodDiags ?
d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null,
kindName(ws),
ws.name == names.init ? ws.owner.name : ws.name,
kindName(ws.owner),
ws.owner.type,
c.snd);
default:
return diags.create(dkind, log.currentSource(), pos,
"cant.apply.symbol",
compactMethodDiags ?
d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null,
kindName(ws),
ws.name == names.init ? ws.owner.name : ws.name,
methodArguments(ws.type.getParameterTypes()),
methodArguments(argtypes),
kindName(ws.owner),
ws.owner.type,
c.snd);
}
}
@Override

@ -233,6 +233,11 @@ compiler.err.switch.expression.no.result.expressions=\
compiler.err.call.must.be.first.stmt.in.ctor=\
call to {0} must be first statement in constructor
# 0: symbol kind, 1: name, 2: symbol kind, 3: type, 4: message segment
compiler.err.cant.apply.symbol.noargs=\
{0} {1} in {2} {3} cannot be applied to given types;\n\
reason: {4}
# 0: symbol kind, 1: name, 2: list of type or message segment, 3: list of type or message segment, 4: symbol kind, 5: type, 6: message segment
compiler.err.cant.apply.symbol=\
{0} {1} in {4} {5} cannot be applied to given types;\n\

@ -0,0 +1,10 @@
/**
* @test /nodynamiccopyright/
* @bug 8043251
* @summary Confusing error message with wrong number of type parameters
* @compile/fail/ref=T8043251.out -XDrawDiagnostics T8043251.java
*/
import java.util.function.Function;
class T8043251 {
Function<String, String> f = Function.<String, String>identity();
}

@ -0,0 +1,2 @@
T8043251.java:9:42: compiler.err.cant.apply.symbol.noargs: kindname.method, identity, kindname.interface, java.util.function.Function<T,R>, (compiler.misc.wrong.number.type.args: 1)
1 error

@ -21,7 +21,7 @@
* questions.
*/
// key: compiler.err.cant.apply.symbol
// key: compiler.err.cant.apply.symbol.noargs
// key: compiler.misc.explicit.param.do.not.conform.to.bounds
class ExplicitParamsDoNotConformToBounds {

@ -21,7 +21,7 @@
* questions.
*/
// key: compiler.err.cant.apply.symbol
// key: compiler.err.cant.apply.symbol.noargs
// key: compiler.misc.wrong.number.type.args
import java.util.*;