RFR: 8268312: Compilation error with nested generic functional interface [v10]

Vicente Romero vromero at openjdk.org
Thu Jul 14 22:46:58 UTC 2022

> Please review this PR, which is my proposal to fix an existing regression. This code:
> import java.util.Optional;
> class App {
>     public static void main(String[] args) {
>         Optional.of("").map(outer -> {
>             Optional.of("")
>                 .map(inner -> returnGeneric(outer))
>                 .ifPresent(String::toString);
>             return "";
>         });
>     }
>     private static <RG> RG returnGeneric(RG generic) {
>         return generic;
>     }
> }
> is not accepted by javac but if the user passes the `-Xdiags:verbose` option then the code compiles. I tracked down the reason for this puzzling difference and I found that it is due to our diagnostic rewriters which can generate more detailed positions for error messages but in cases like the one above can trick the compiler to generate an error message too early. The code deciding if an error message should be deferred or not, depending on the position, is at `DeferredDiagnosticHandler::report`. We decide to do the rewriting if we are in diagnostics compact mode, this is why the error doesn't occur with the `-Xdiags:verbose` option. This fix will made some diagnostics to appear at a slightly different position, but won't make the compiler reject correct code. Comments?

Vicente Romero has updated the pull request incrementally with two additional commits since the last revision:

 - Update src/jdk.compiler/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java
   Co-authored-by: Maurizio Cimadamore <54672762+mcimadamore at users.noreply.github.com>
 - Update test/langtools/tools/javac/diags/DiagnosticRewriterTest3.java
   Co-authored-by: Maurizio Cimadamore <54672762+mcimadamore at users.noreply.github.com>


  - all: https://git.openjdk.org/jdk/pull/5586/files
  - new: https://git.openjdk.org/jdk/pull/5586/files/a00c8dab..a913b305

 - full: https://webrevs.openjdk.org/?repo=jdk&pr=5586&range=09
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=5586&range=08-09

  Stats: 2 lines in 2 files changed: 0 ins; 0 del; 2 mod
  Patch: https://git.openjdk.org/jdk/pull/5586.diff
  Fetch: git fetch https://git.openjdk.org/jdk pull/5586/head:pull/5586

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

More information about the compiler-dev mailing list