Skip to content

JSpecifyExperimental: false positive for addAll with ? element type #1822

Open

Description

@unv-unv

JSpecifyExperimental mode on.

@NullMarked
class Test {
    public Collection<Object> foo(List<?> list) {
        List<Object> result = new ArrayList<>();
        // BUG: List<?> cannot be converted to Collection<? extends Object> (List<?> is a subtype of Collection<?>)
        result.addAll(list);
        return result;
    }
}

Shows compilation error:

[ERROR]Test.java[6,22]: [NullAway] incompatible types: List<?> cannot be converted to Collection<? extends Object> (List<?> is a subtype of Collection<?>)

NullAway 0.14.1, Error Prone 2.50.0.

Activity

  1. msridhar commented on Sep 7, 2026

    @msridhar
    Contributor

    The error message is bad on this one, but I think it's a real issue, since List<?> could contain null values. That's because its type variable has a nullable upper bound. Does that make sense? Sorry can provide links later if needed, I'm on my phone

  2. unv-unv commented on Sep 7, 2026

    @unv-unv
    Author

    But I have @NullMarked annotation. So List<?> should be non-nullable.

  3. msridhar commented on Sep 7, 2026

    @msridhar
    Contributor

    See https://jspecify.dev/docs/user-guide/#wildcard-bounds. For unbounded wildcards in @NullMarked code, the default upper bound is the same as the upper bound of the corresponding type variable, which in this case is @Nullable Object, taking into account the JSpecify JDK model of List.

  4. unv-unv commented on Sep 7, 2026

    @unv-unv
    Author

    Is it possible to specify non-null question mark?

  5. msridhar commented on Sep 7, 2026

    @msridhar
    Contributor

    Yes! Write List<? extends Object>. Object here is implicitly non-null

  6. added
    jspecify
    on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jdk-modelsjspecify

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions