Skip to content

JSpecify: false positive when ? super T is on the both sides of call and T is nullable #1500

Closed

Description

@unv-unv
@NullMarked
public class Test {
    static class Foo<T extends @Nullable Object> {
        public static <T extends @Nullable Object> Foo<T> of(Foo<? super T> foo) {
            return new Foo<>();
        }

        public Foo<T> or(Foo<? super T> other) {
            return this;
        }
    }

    // BUG: [NullAway] incompatible types: Foo<Void> cannot be converted to Foo<@Nullable Void>
    static final Foo<@Nullable Void> FOO = Foo.of(new Foo<@Nullable Void>()).or(new Foo<@Nullable Void>());
}

Note that if I remove call for .or() everything compiles OK.

static final Foo<@Nullable Void> FOO = Foo.of(new Foo<@Nullable Void>());

The following one is OK too:

static final Foo<@Nullable Void> FOO_BASE = Foo.of(new Foo<@Nullable Void>());
static final Foo<@Nullable Void> FOO = FOO_BASE.or(new Foo<@Nullable Void>());

So the problem appears only if on the both sides of .or() call is ? super T.

NullAway 0.13.1, Error Prone 2.48.0

Activity

  1. changed the title [-]False positive when ? super T is on the both sides of call and T is nullable in JSpecify Mode[/-] [+]JSpecify: false positive when ? super T is on the both sides of call and T is nullable[/+] on Mar 27, 2026
  2. msridhar commented on Apr 3, 2026

    @msridhar
    Contributor

    Thanks for the report, this is most likely due to incomplete support for wildcards; see #1360 and also #1462. I'll leave this one open and we should check that it's fixed after adding more wildcards support

  3. unv-unv commented on Apr 14, 2026

    @unv-unv
    Author

    Checked in 0.13.2. Problem is still here.

  4. msridhar commented on Apr 14, 2026

    @msridhar
    Contributor

    Yeah, we'll update / close this issue when this one is fixed, but it will take some time.

  5. msridhar commented on May 1, 2026

    @msridhar
    Contributor

    @unv-unv I wrote a Java equivalent of this test and it does not compile:

    class Test {
    
        static class Foo<T> {
        public static <T> Foo<T> of(Foo<? super T> foo) {
            return new Foo<>();
        }
    
        public Foo<T> or(Foo<? super T> other) {
            return this;
        }
        }
        static final Foo<String> FOO = Foo.of(new Foo<Object>()).or(new Foo<Object>());
    }

    The following modified FOO declaration does compile:

        static final Foo<String> FOO = Foo.<String>of(new Foo<Object>()).or(new Foo<Object>());

    (Godbolt link)

    Basically, javac cannot infer that Foo.of(new Foo<Object>()) should have type Foo<String> based on FOO's type, since it goes through the call to or. Right now, for similar reasons, NullAway does not know that Foo.of(new Foo<@Nullable Void>()) "should" have type Foo<@Nullable Void> and infers Foo<Void> instead.

    Now, for the Void type, one could argue we just should always infer @Nullable Void and add some special treatment. We could think about that, though it'd be low priority. But, I don't think this issue was about the Void type specifically.

    @unv-unv thoughts?

  6. added a commit that references this issue on May 3, 2026
  7. added a commit that references this issue on May 25, 2026
    c315bd5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions