RFR: 8291643: Consider omitting type annotations from type error diagnostics

Liam Miller-Cushon cushon at openjdk.org
Wed Mar 6 20:35:20 UTC 2024


On Thu, 9 Nov 2023 00:47:10 GMT, Liam Miller-Cushon <cushon at openjdk.org> wrote:

> Hi,
> 
> Please consider this fix for [JDK-8291643](https://bugs.openjdk.org/browse/JDK-8291643), which causes javac to remove type-use annotations when formatting types in diagnostics.
> 
> For the example like the one in the bug, this change causes javac to emit the diagnostic like `incompatible types: List<String> cannot be converted to List<Number>` rather than `incompatible types: List<String> cannot be converted to List<@Nullable Number>`. Including the type annotations can be confusing, because they do not impact the type checking done by the compiler.
> 
> An alternative I considered was to remove type annotations from types for specific diagnostics, instead of doing it unconditionally in diagnostic formatting. I can revisit that if the current approach seems too broad.
> 
> The test update to `test/langtools/tools/javac/lambda/LambdaConv25.out` is because a `ForAll` is being formatted, and `stripMetadata()` uses a `StructuralTypeMapping` which rewrites away the `ForAll`.

I pushed a demo of some of the changes mentioned earlier, including an implementation of a `stripAnnotations` method that uses the approach I mentioned in https://github.com/openjdk/jdk/pull/8984#issuecomment-1806211798

> One possibility would be to have a trivial wrapper around the types used in diagnostic arguments, and then have the diagnostic formatter unwrap them and leave the annotations on

`AbstractDiagnosticFormatter` now removes type annotations from printed types by default, and there's a `JCDiagnostic.AnnotatedType` wrapper for diagnostics that want to opt-in to printing types with annotations. This isn't currently used in the PR, but https://github.com/openjdk/jdk/pull/16592 is an example of a diagnostic that wants to include type annotations.

Please keep this open for now

I updated this to use the `Type#stripMetadata` method added in https://bugs.openjdk.org/browse/JDK-8042981 to remove the type annotations, now that change has landed.

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

PR Comment: https://git.openjdk.org/jdk/pull/16578#issuecomment-1811517580
PR Comment: https://git.openjdk.org/jdk/pull/16578#issuecomment-1884004866
PR Comment: https://git.openjdk.org/jdk/pull/16578#issuecomment-1981730529


More information about the compiler-dev mailing list