typetools / typetools/checker-framework

`Map.merge` and other methods now always return `@Nullable`

Open
#7,340 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

In version 3.49.3, Map.merge, Map.compute, and Map.computeIfAbsent used @PolyNull in their signatures. This gave them a non-null return type when they were used with functions that operated on non-null types. That allowed code like this:

class MapMerge {
  Integer doMerge(java.util.Map<String, Integer> m) {
    return m.merge("", 1, Integer::sum);
  }
}

When we upgrade to version 3.49.4, we see the new Map signatures from https://github.com/typetools/checker-framework/issues/7082 / https://github.com/typetools/jdk/pull/227. Those signatures use @Nullable instead of @PolyNull. That leads to a number of new errors in our codebase on code that we believe is safe. Here is a minimized example:

checker-framework-3.49.4 $ checker/bin/javac -processor nullness MapMerge.java
MapMerge.java:3: error: [return] incompatible types in return.
    return m.merge("", 1, Integer::sum);
                  ^
  type of expression: @Initialized @Nullable Integer
  method return type: @Initialized @NonNull Integer
1 error

There is currently a tradeoff between fixing #7082 and keeping the above code working.

But it may be possible for the checker to support both callers: The problem in #7082 might also be possible to fix by keeping the old Map signatures but changing the algorithm for type inference: The checker accepts the code from #7082 if we explicitly cast the MyFunction::mergeFunction method reference to the type (BiFunction<Integer, Integer, @Nullable Integer>).

(There was a previous report of type-inference problems with @PolyNull back in https://github.com/typetools/checker-framework/issues/2429. But that one looks to be fixed in recent versions, so this issue is separate.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimized MapMerge.java example and the checker/bin/javac invocation. Read issues #7082 and #2429, then investigate whether type inference can support both the updated Map signatures and the non-null caller shown here. Done means the safe Map.merge, Map.compute, and Map.computeIfAbsent use cases work without reintroducing the reported issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.