typetools / typetools/checker-framework

Nullness checker handling of implicit type variable bounds - difference between <T> and <T extends @Nullable Object>

Open
#5,075 3 comments 0 reactions 1 assignee View on GitHub

@smillst is already working on this.

Since Jan 1, 2023.

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

Description

In the following example, the nullness checker rejects an input that contains a type variable with an explicit extends @Nullable Object bound, and accepts the same example if the explicit bound is removed.

The CLIMB-to-top defaults say implicit type variable bounds are interpreted as equivalent extends @Nullable Object, is this a bug?

import org.checkerframework.checker.nullness.qual.Nullable;

class A<V extends @Nullable Object> {
  I<V> i() {
    return new B<>(this);
  }
}

class B<K, V> implements I<V> {
  B(A<V> a) {}
}

interface I<V> {}

With Checker Framework 3.21.3:

$ ./checker-framework-3.21.3/checker/bin/javac -processor nullness A.java
A.java:5: error: [return] incompatible types in return.
    return new B<>(this);
           ^
  type of expression: @Initialized @NonNull B<@Initialized @NonNull Object, V extends @Initialized @NonNull Object>
  method return type: @Initialized @NonNull I<V extends @Initialized @Nullable Object>
A.java:5: error: [argument] incompatible argument for parameter a of B.
    return new B<>(this);
                   ^
  found   : @Initialized @NonNull A<V extends @Initialized @Nullable Object>
  required: @Initialized @NonNull A<V extends @Initialized @NonNull Object>
2 errors

The code compiles without error if extends @Nullable Object is removed.

diff A.java B.java
3c3
< class A<V extends @Nullable Object> {
---
> class A<V> {
$ ./checker-framework-3.21.3/checker/bin/javac -processor nullness B.java

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.