typetools / typetools/checker-framework

@Nullable result inferred for @NonNull returning method

Open
#6,756 0 comments 0 reactions 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

abstract class Test {

  abstract <T> @NonNull T get(Class<T> type);

  Collection<Map.Entry<String, String>> getEndpointMap(Stream<Test> tests) {
    return tests
        .map(val -> Map.entry(val.get(String.class), val.get(String.class)))
        .toList();
  }
}

compilation fails with

C:\Projects\pvyscomponents\java\libs\kernel\repository\api\src\main\java\com\provys\repository\Test.java:22:30
java: [return] incompatible types in return.
  type of expression: @Initialized @NonNull Entry<@Initialized @NonNull String, @Initialized @Nullable String>
  method return type: @Initialized @NonNull Entry<@Initialized @NonNull String, @Initialized @NonNull String>

It is a bit strange that if type arguments for Map.entry are specified in offending line, code compiles without warnings
.map(val -> Map.<String, String>entry(val.get(String.class), val.get(String.class)))

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 by running the Java reproducer and comparing inference for Map.entry with and without explicit type arguments. Trace the Checker Framework handling of @NonNull and @Nullable type arguments, then add a regression test showing that the inferred result matches the declared non-null return type.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.