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

Incorrect warning for nullable type parameter in Map.computeIfAbsent #5176

Closed
amalloy opened this issue Jun 25, 2022 · 1 comment
Closed
Assignees

Comments

@amalloy
Copy link

amalloy commented Jun 25, 2022

Commands

$ ./checker-framework-3.18.0/checker/bin/javac -processor nullness CFRepro.java
$ ./checker-framework-3.22.1/checker/bin/javac -processor nullness CFRepro.java

Inputs

File CFRepro.java:

import java.util.Map;
import java.util.HashMap;
import org.checkerframework.checker.nullness.qual.Nullable;

class CFRepro {
  final Map<Integer, Box<@Nullable String>> m = new HashMap<>();
  Box<@Nullable String> get(Integer k) {
    return m.computeIfAbsent(k, unused -> new Box<>(null));
  }
}

class Box<T> {
  Box(T t) {}
}

Outputs

3.18.0 produces no errors. 3.22.1 produces the following errors:

CFRepro.java:15: error: [return] incompatible types in return.                  
    return m.computeIfAbsent(k, unused -> new Box<>(null));                     
                                          ^
  type of expression: @Initialized @NonNull Box<@Initialized @NonNull String>
  method return type: @Initialized @NonNull Box<@Initialized @Nullable String>
CFRepro.java:15: error: [argument] incompatible argument for parameter t of Box.
    return m.computeIfAbsent(k, unused -> new Box<>(null));
                                                    ^
  found   : null (NullType)
  required: @Initialized @NonNull String 
2 errors

Expectation

3.22.1 should behave like 3.18.0, because this code is not erroneous. We have a Map<K, Box<@Nullable V>>, and new Box(null) is s valid Box<@Nullable V>. I don't see why CF infers that new Box<>(null) has type @Initialized @NonNull Box<@Initialized @NonNull String>.

3.18.0 is an arbitary older version I happen to have handy for testing. I have not tried to identify the specific version or commit between 3.18.0 and 3.22.1 that introduced the regression.

@smillst
Copy link
Member

smillst commented May 21, 2024

This was fixed with #979.

@smillst smillst closed this as completed May 21, 2024
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