Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] @With requires inner records to be explicitly static #3497

Closed
pmenke-de opened this issue Sep 12, 2023 · 1 comment · Fixed by #3571
Closed

[BUG] @With requires inner records to be explicitly static #3497

pmenke-de opened this issue Sep 12, 2023 · 1 comment · Fixed by #3571
Milestone

Comments

@pmenke-de
Copy link

pmenke-de commented Sep 12, 2023

Describe the bug
Compilation of a java-source file fails, if

  • There is a class C1 with a generic parameter
  • Class C1 contains an inner record-class C2
  • C2 is annotated with @With
  • C2 contains at least on record-component
  • C2 is not explicitly marked as static

This seems to be related to #3140 but still occurs in lombok 1.18.28

To Reproduce
Compile the following source file

package de.pmenke.test;

import lombok.With;

public class OuterClass<V> {
    private InnerRecord rec;

    @With
    /* static */ record InnerRecord(int aComponent) {}
}

Observe compilation failure

[ERROR] ./src/main/java/de/pmenke/test/OuterClass.java:[9,41] non-static type variable V cannot be referenced from a static context

A reproduction example project is available in gist https://gist.github.com/pmenke-de/28d8793052f51cc1f6d16c09b6ced1c4

If the keyword static is added to the record-class definition, the code compiles as expected.

Expected behavior
The code should compile without the static keyword as well, as record-classes are static implicitly.

Version info (please complete the following information):

  • Lombok version 1.18.28
  • Platform mac-os / java 17
    openjdk version "17.0.5" 2022-10-18
    OpenJDK Runtime Environment JBR-17.0.5+8-738-jcef (build 17.0.5+8-b738)
    OpenJDK 64-Bit Server VM JBR-17.0.5+8-738-jcef (build 17.0.5+8-b738, mixed mode)
    

Additional context
delomboking via the lombok.jar shows the following variants between explicit-static and implicit-static

implicit-static

    record InnerRecord(int aComponent) {
        /**
         * @return a clone of this object, except with this updated property (returns {@code this} if an identical value is passed).
         */
        @java.lang.SuppressWarnings("all")
        public OuterClass<V>.InnerRecord withAComponent(final int aComponent) {
            return this.aComponent == aComponent ? this : new OuterClass<V>.InnerRecord(aComponent);
        }
    }

explicit-static

    static record InnerRecord(int aComponent) {
        /**
         * @return a clone of this object, except with this updated property (returns {@code this} if an identical value is passed).
         */
        @java.lang.SuppressWarnings("all")
        public OuterClass.InnerRecord withAComponent(final int aComponent) {
            return this.aComponent == aComponent ? this : new OuterClass.InnerRecord(aComponent);
        }
    }

It seems to be the case, that the implicitly-static record isn't detected as being static, causing lombok to produce references to OuterClass including the generic parameter V, which would only be bound in a non-static context.

@rspilker
Copy link
Collaborator

This is part of the latest edge release, all feedback is welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants