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

Static factory parameter ordering changed in 2.8.x with inherited parameters #1488

Open
marquiswang opened this issue Oct 18, 2023 · 5 comments

Comments

@marquiswang
Copy link

marquiswang commented Oct 18, 2023

We noticed a behavioral change in the generated static factory in 2.8.x, when inheriting fields from a parent class.

For example, with the following code:

public interface Foo {
    @Value.Parameter
    int a();
}

@Value.Immutable
public interface Bar extends Foo {
    @Value.Parameter
    int b();
}

In 2.7.4 and below, Immutables generates

public static ImmutableBar of(int b, int a) {
    return new ImmutableBar(b, a);
}

and in 2.7.5 and above, it generates:

public static ImmutableBar of(int a, int b) {
    return new ImmutableBar(a, b);
}

I think that the new behavior is better, and in a few cases in our codebase we'd explicitly changed the subclass to be something like the following in order to force the parameter order.

@Value.Immutable
public interface Bar extends Foo {
    @Override
    @Value.Parameter
    int a();

    @Value.Parameter
    int b();
}

However, we have great concerns about the safety of this migration - if the parameters are all of the same type, then reordering parameters can lead to really dangerous bugs.

Do you have any suggestions for how to migrate to 2.8.x and higher? I'm thinking that some kind of flag that we can pass to Immutables to detect if all fields are the same type AND there is a parent class that provides some fields, then emit a warning unless some annotation is added to the class indicating that it was checked?

@elucash
Copy link
Member

elucash commented Feb 18, 2024

I was hoping we have such a change under a feature flag. But I cannot find such thing. I might need to look into how it was before and why it's changed (probably when we've discontinued some hacks for older compilers, this might have been a collateral). I hope we can return that older behavior under a feature flag. Will be looking into it, but no estimates :-(

@marquiswang
Copy link
Author

Thanks @elucash!

I think it was this commit: 11cc457

... if that helps at all.

@MendelMonteiro
Copy link

MendelMonteiro commented May 3, 2024

Is there any progress on adding an option to use the older behaviour?

@MendelMonteiro
Copy link

@elucash Is this something you would accept a PR for?

@elucash
Copy link
Member

elucash commented May 21, 2024

Sorry for delayed response. Sure, if PR would have a feature/style flag and will be able to turn that older sorting, that would be great and will be merged. I can look into this too, probably, this or next weekend. It seems like not that hard, but definitely, not trivial. It would be really easy if just we can easily change the order in which supertypes are considered, but I'm a bit anxious if there's more to it

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

No branches or pull requests

3 participants