Skip to content

JSpecify: inference for calls to generic methods not working for lambda arguments #1294

Closed

Description

@agrieve

Follow-up to #1075

@NullMarked
class Foo {

    public interface Callback<T extends @Nullable Object> {
      void onResult(T thing);
    }

    public static <T extends @Nullable Object> Callback<T> wrap(Callback<T> thing) {
      return thing;
    }

    public static void test() {
      Callback<@Nullable String> ret1 = wrap((@Nullable String s) -> {});

      Callback<@Nullable String> ret2 = wrap(s -> {});
    }
}

Yields:

Foo.java:18: warning: [NullAway] incompatible types: Callback<String> cannot be converted to Callback<@Nullable String>
      Callback<@Nullable String> ret1 = wrap((@Nullable String s) -> {});
                                 ^
    (see http://t.uber.com/nullaway )
Foo.java:20: warning: [NullAway] incompatible types: Callback<String> cannot be converted to Callback<@Nullable String>
      Callback<@Nullable String> ret2 = wrap(s -> {});
                                 ^
    (see http://t.uber.com/nullaway )

Guessing ret2 is harder to make work than ret1, but it'd be great if ret1 could be made to work.

As a work-around, assigning to a local variable works:

      Callback<@Nullable String> tmp = s -> {};
      Callback<@Nullable String> ret1 = wrap(tmp);

Activity

  1. added
    jspecify
    on Sep 17, 2025
  2. msridhar commented on Sep 17, 2025

    @msridhar
    Contributor

    This might be the same as #1290 but I'll leave it open to make sure these additional tests pass.

  3. added a commit that references this issue on Oct 19, 2025
    0a2e2b0
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

    jspecify

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions