Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8288130

compiler error with AP and explicit record accessor

XMLWordPrintable

    • b04

        Our team at Minecraft Forge (a modding platform for Minecraft: Java Edition) recently found a compiler bug regarding records, type parameters, and explicit types while trying to implement a new feature. I'm led to believe that this is the appropriate place to at least publicize and discuss this compiler-related issue. If not, please point me in the appropriate place to report this, thank you.

        In summary, an explicitly declared accessor method for a record component whose type is a type parameter of the record fails to compile only when an annotation processor is present during compilation. Removal of the explicit accessor or the annotation processor(s) makes the class compile as expected.

        We've assembled a small reproducible case for this issue:


            import javax.annotation.processing.AbstractProcessor;
            import javax.annotation.processing.RoundEnvironment;
            import javax.lang.model.element.TypeElement;
            import java.util.Set;

            public class EmptyProcessor extends AbstractProcessor {
                @Override
                public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
                    return false;
                }
            }

            public record TestRecord<T>(T someValue) {
                public T someValue() {
                    return this.someValue;
                }
            }

        Put each snippet into the source files `EmptyProcessor.java` and `TestRecord.java` respectively, compile the AP class using `javac EmptyProcessor.java`, and try to compile the record class with the AP present using `javac -processor EmptyProcessor TestRecord.java`. The warnings about the processor's lack of source version and supported annotations can be safely ignored (we used them to confirm that the annotation processor is present). Here is the output from running the above on Java 17:

            D:\Dev\Sandbox\javacbug> javac -processor EmptyProcessor TestRecord.java
            warning: No SupportedSourceVersion annotation found on EmptyProcessor, returning RELEASE_6.
            warning: Supported source version 'RELEASE_6' from annotation processor 'EmptyProcessor' less than -source '17'
            warning: No SupportedAnnotationTypes annotation found on EmptyProcessor, returning an empty set.
            TestRecord.java:3: error: invalid accessor method in record TestRecord
                public T someValue() {
                         ^
              (return type of accessor method someValue() must match the type of record component someValue)
              where T is a type-variable:
                T extends Object declared in record TestRecord
            1 error
            3 warnings

        We've tested this on Windows against Java 16, 17, 18, as well as the 19 EA (build 25, 2022/6/2), all reference implementations from jdk.java.net, as well as the latest Java 17 for Eclipse Temurin (17.0.3_7) and the test case fails on all those versions. We've also tested this against the Eclipse Compiler for Java, and it compiles successfully both with and without the presence of the AP.

        as reported in compiler-dev: https://mail.openjdk.java.net/pipermail/compiler-dev/2022-June/019956.html

              vromero Vicente Arturo Romero Zaldivar
              vromero Vicente Arturo Romero Zaldivar
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved: