Skip to content

False negative with type argument inference? #219

@msridhar

Description

@msridhar
import java.util.*;
import org.jspecify.annotations.*;
@NullMarked
class Test {
  public static <T extends @Nullable Object> T firstOrDefault(List<T> list, T defaultValue) {
    return list.isEmpty() ? defaultValue : list.get(0);
  }

  // expected an error here
  String field = firstOrDefault(Collections.singletonList(null), "hello");

  void takesNonNullString(String s) {}

  void test() {
    // expected an error here
    takesNonNullString(firstOrDefault(Collections.singletonList(null), "hello"));
  }
}

I would expect an error since at both call sites, making the type argument @Nullable doesn't work (since call result is assigned to a @NonNull location), but making it @NonNull doesn't work either (since we're putting null in the singleton List). Am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions