typetools / typetools/checker-framework

Weird warning in NullnessChecker with code have Generic Type,Type Cast and EqualTo statement

Open
#741 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Test Version of checker framework: javac 1.8.0-jsr308-2.0.0

NullnessChecker will give a weird warning of below code:

public class Test {
    @SuppressWarnings("unchecked")
    public <T> T incompatibleTypes(Object o) {
        final T x = (T) o;
        if (x != null) {
        }
        return x;
    }
}

The warning is:

Error: [return.type.incompatible] incompatible types in return.
        return x;
               ^
  found : T extends @Initialized @Nullable Object
  required: T extends @Initialized @Nullable Object

And the more detail warning of above code with printVerboseGenerics is:

error: [return.type.incompatible] incompatible types in return.
        return x;
               ^
  found   : T[ extends @Initialized @Nullable Object super @Initialized @Nullable Void]
  required: T[ extends @Initialized @Nullable Object super @Initialized @NonNull Void]

This wired error will appear when the code has a TypeCast first and then has a NotEqualTo against with null literal.

The same problem also appears in EqualTo against with null literal:
code:

public class Test {
    @SuppressWarnings("unchecked")
    public <T> T incompatibleTypes(Object o) {
        final T x = (T) o;
        if (x == null) {
        }
        return x;
    }
}

warning with printVerboseGenerics:

error: [return.type.incompatible] incompatible types in return.
        return x;
               ^
  found   : T[ extends @Initialized @Nullable Object super @Initialized @Nullable Void]
  required: T[ extends @Initialized @Nullable Object super @Initialized @NonNull Void]

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 compiling the two provided Java reproductions with NullnessChecker and printVerboseGenerics enabled, comparing the cast followed by each null comparison. Trace how the checker reports the inferred generic bounds at the return statement. Done means both examples no longer produce the spurious return.type.incompatible warning.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.