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

confusing error message: return type of accessor method is not compatible with type of record component

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 14
    • 14
    • tools
    • b31

        Consider the following code sample:

        public record R(Number number) {
           public Integer number() {
               return 42;
           }
        }

        When compiling it with jdk14b28

        javac --enable-preview --release 14 R.java

        the following error occurs:

        R.java:2: error: invalid accessor method in record R
           public Integer number() {
                          ^
          (return type of accessor method number() is not compatible with type of record component number)
        Note: R.java uses preview language features.
        Note: Recompile with -Xlint:preview for details.
        1 error
        ============================================

        However JLS update (http://cr.openjdk.java.net/~gbierman/jep359/jep359-20191125/specs/records-jls.html) says:
        "If an accessor method is declared explicitly, then it must additionally satisfy the following rules; otherwise a compile-time error occurs:
            The return type of the accessor method must be identical to the declared type of the corresponding record component"

        Compiler error message could be improved by replacing:
        "is not compatible with type of record component number"
        with
        "is not identical to the declared type of the corresponding record component"

              vromero Vicente Arturo Romero Zaldivar
              dbessono Dmitry Bessonov
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: