Skip to content

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

@agrieve

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    jspecifyRelated to support for jspecify standard (see jspecify.dev)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions