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

Archie L. Cobbs duke at openjdk.org
Mon Nov 7 16:22:18 UTC 2022


We have an error message `compiler.err.cant.apply.symbol` for cases where a method invocation doesn't resolve. It shows the "required" parameters and the "found" parameters, plus a further description of the problem.

This message being used inappropriately when the problem is actually due to explicitly passed method type parameters instead of regular method parameters.

For example, if you do this:

Function<String, String> f = Function.<String, String>identity();

the error reported is this:

error: method identity in interface Function<T,R> cannot be applied to given types;
    Function<String, String> f = Function.<String, String>identity();
                                         ^
  required: no arguments
  found:    no arguments
  reason: wrong number of type arguments; required 1
  where T,R are type-variables:
    T extends Object declared in interface Function
    R extends Object declared in interface Function

The real error here is `wrong number of type arguments; required 1`, but the `compiler.err.cant.apply.symbol` error message that assumes the problem is with the regular parameters, which it displays. The result is the appearance of the useless and confusing `required` and `found` lines.

This patch creates an alternate version of that error message (`compiler.err.cant.apply.symbol.noargs`) that omits the `required` and `found` lines, and makes the compiler use this alternate message when the error is due to type parameters (specifically, when the underlying error is "wrong number of type arguments" or "explicit type argument X does not conform to declared bound(s)").

This improves the error for example above to this:

error: method identity in interface Function<T,R> cannot be applied to given types;
    Function<String, String> f = Function.<String, String>identity();
                                         ^
  reason: wrong number of type arguments; required 1
  where T,R are type-variables:
    T extends Object declared in interface Function
    R extends Object declared in interface Function
1 error

-------------

Commit messages:
 - Fix confusing error message regarding wrong number of type parameters.

Changes: https://git.openjdk.org/jdk/pull/10799/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=10799&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8043251
  Stats: 47 lines in 6 files changed: 34 ins; 0 del; 13 mod
  Patch: https://git.openjdk.org/jdk/pull/10799.diff
  Fetch: git fetch https://git.openjdk.org/jdk pull/10799/head:pull/10799

PR: https://git.openjdk.org/jdk/pull/10799


More information about the compiler-dev mailing list