diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java index 76c88cc9c7a24..9b4653a039a7f 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -4173,17 +4173,34 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, Pair 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 diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties index 13c3099c790a6..3a51112a495ff 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties @@ -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\ diff --git a/test/langtools/tools/javac/Diagnostics/8043251/T8043251.java b/test/langtools/tools/javac/Diagnostics/8043251/T8043251.java new file mode 100644 index 0000000000000..dcf3586a507f2 --- /dev/null +++ b/test/langtools/tools/javac/Diagnostics/8043251/T8043251.java @@ -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 f = Function.identity(); +} diff --git a/test/langtools/tools/javac/Diagnostics/8043251/T8043251.out b/test/langtools/tools/javac/Diagnostics/8043251/T8043251.out new file mode 100644 index 0000000000000..2b17605d7dec3 --- /dev/null +++ b/test/langtools/tools/javac/Diagnostics/8043251/T8043251.out @@ -0,0 +1,2 @@ +T8043251.java:9:42: compiler.err.cant.apply.symbol.noargs: kindname.method, identity, kindname.interface, java.util.function.Function, (compiler.misc.wrong.number.type.args: 1) +1 error diff --git a/test/langtools/tools/javac/diags/examples/ExplicitParamsDoNotConformToBounds.java b/test/langtools/tools/javac/diags/examples/ExplicitParamsDoNotConformToBounds.java index cd10b13180d22..6f8047af3aa6b 100644 --- a/test/langtools/tools/javac/diags/examples/ExplicitParamsDoNotConformToBounds.java +++ b/test/langtools/tools/javac/diags/examples/ExplicitParamsDoNotConformToBounds.java @@ -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 { diff --git a/test/langtools/tools/javac/diags/examples/WrongNumberTypeArgsFragment.java b/test/langtools/tools/javac/diags/examples/WrongNumberTypeArgsFragment.java index c1777bba20fb1..8b0cc90c6940e 100644 --- a/test/langtools/tools/javac/diags/examples/WrongNumberTypeArgsFragment.java +++ b/test/langtools/tools/javac/diags/examples/WrongNumberTypeArgsFragment.java @@ -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.*;