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

Type refinement is very weak #6296

Closed
chaosflaws opened this issue Nov 12, 2023 · 1 comment
Closed

Type refinement is very weak #6296

chaosflaws opened this issue Nov 12, 2023 · 1 comment

Comments

@chaosflaws
Copy link

Running the nullness checker (checker -rpcessor nullness Test.java) on a very simple example:

public class Test {
    private void testFails1(@Nullable LocalDate x1, @Nullable LocalDate x2) {
        boolean eitherIsNull = x1 == null || x2 == null;
        if (eitherIsNull) return;
        delegate(x1, x2);
    }

    private void testFails2(@Nullable LocalDate x1, @Nullable LocalDate x2) {
        boolean firstIsNull = x1 == null;
        boolean secondIsNull = x2 == null;
        if (firstIsNull || secondIsNull) return;
        delegate(x1, x2);
    }

    private void testWorks(@Nullable LocalDate x1, @Nullable LocalDate x2) {
        if (x1 == null || x2 == null) return;
        delegate(x1, x2);
    }

    private void delegate(LocalDate x1, LocalDate x2) {
        // do something
    }
}

reports that in the two failing tests, x1 and x2 cannot be inferred to be non-null:

[...]\Test.java:11: error: [argument] incompatible argument for parameter x1 of delegate.
        delegate(x1, x2);
                 ^
  found   : @Initialized @Nullable LocalDate
  required: @Initialized @NonNull LocalDate

In the actual use case, eitherIsNull represents a more complex boolean expression that I would like to assign a name to. Currently, the checker framework prevents me from doing that.

@mernst
Copy link
Member

mernst commented Nov 13, 2023

Thank you for reporting the problem. I'm sorry you are having trouble. This issue is a duplicate of #406.

I just added a suggestion with a straightforward implementation strategy.

Issue #406 has "medium" priority, so we are unlikely to get to it very soon, unless more information/motivation comes to light about it. I'm sorry about that. However, if you would like to contribute a fix, we would be very grateful!

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

2 participants