Skip to content

JSpecify mode: SubL<?> is rejected as Base<? extends List<String>> when SubL's type parameter has a narrower bound than Base's #2206

Backlog

Description

@vlsi

Kind of problem

False positive (NullAway reports an error on correct code)

Minimal reproducer

import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class Test {
  static class Base<U extends @Nullable Object> {}
  static class SubL<T extends List<String>> extends Base<T> {}

  void viewL(SubL<?> s) {
    Base<? extends List<String>> t = s;
  }
}

Actual behavior

incompatible types: SubL<?> cannot be converted to Base<? extends List<String>> (SubL<?> is a subtype of Base<?>)

Expected behavior

No error. By capture conversion (JLS 5.1.10) the ? of SubL<?> takes the bound of T, which is List<String>, so the source is SubL<CAP extends List<String>>, whose supertype Base<CAP> is contained in Base<? extends List<String>>. NullAway appears to compare the wildcard after substituting it into Base, where it takes the implicit @Nullable Object bound of U instead of the bound of T.

The same happens one level down: with static class SubW<T> extends Base<List<T>> {}, SubW<?> s; Base<? extends List<? extends Object>> t = s; is reported, though T's bound Object is non-null in @NullMarked code.

NullAway configuration

JSpecify mode, -XepOpt:NullAway:OnlyNullMarked=true

NullAway version

master at af5ba08

Error Prone version

2.53.0

JDK version

21.0.9

Additional context

Found in the review of #1828, which reports this case with the new message and pins it in a test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions