typetools / typetools/checker-framework

Inference is not able to use return type in @Nullable type inference

Open
#6,748 0 comments 0 reactions 1 assignee View on GitHub

@smillst is already working on this.

Since Aug 8, 2024.

False Positive (false warning or imprecision)
Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

In this example

@SuppressWarnings("nullity")
class Test {

  abstract static class Typed<T> {

    abstract T get();
  }

  <T> Optional<T> method1(Typed<? extends @Nullable T> arg) {
    return Optional.ofNullable(arg.get());
  }

  <T> Optional<T> method2(Typed<? extends @Nullable T> arg) {
    return method1(arg);
  }
}

compilation fails with error in method2

C:\Projects\pvyscomponents\java\libs\kernel\repository\api\src\main\java\com\provys\repository\Test.java:23:19
java: [return] incompatible types in return.
  type of expression: @Initialized @NonNull Optional<T extends @Initialized @NonNull Object>
  method return type: @Initialized @NonNull Optional<T extends @Initialized @Nullable Object>

I believe it should compile without warning, as if it used T as type argument in call to method1, all should be fine

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.