Skip to content

Better support lambda parameters in generic method inference #1290

Closed

Description

@msridhar

is this the same case or is this a separate issue?

static <R> void invoke(Supplier<@Nullable R> supplier) {
}
invoke(() -> null);
error: [NullAway] Cannot pass parameter of type Supplier<Object>, as formal parameter has type Supplier<@org.jspecify.annotations.Nullable R>, which has mismatched type parameter nullability
        invoke(() -> null);
               ^

Originally posted by @mhalbritter in #1075

We have a unit test for this: https://github.com/uber/NullAway/blob/2e578d6314e224a83114d3ff85571a33ab4b6035/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java#L859-L859

Activity

  1. added
    jspecify
    on Sep 16, 2025
  2. msridhar commented on Dec 1, 2025

    @msridhar
    ContributorAuthor

    In NullAway 0.12.13 our support for non-void lambda parameters that return should be improved. And, we already shipped better support for void lambda parameters in an earlier release. The case we're still working on is #1350 which relates to wildcards. This is the issue related to calls to Reactor's mapNotNull method. If you notice other problems related to lambdas and generic method inference please let us know or open a new issue.

  3. sdeleuze commented on Jan 6, 2026

    @sdeleuze

    @mhalbritter Could you please check if your use case with Supplier<@Nullable R> supplier is fixed, and let us know?

  4. mhalbritter commented on Jan 7, 2026

    @mhalbritter

    NullAway 0.12.15:

        public static void main(String[] args) {
            invoke(() -> null);
        }
    
        static <R> void invoke(Supplier<@Nullable R> supplier) {
            Object foo = supplier.get();
            System.out.println(foo.toString());
        }

    compiles, so the original issue I reported is fixed.

    However, foo in invoke could be null, so the foo.toString() should fail to compile, but it doesn't. Maybe a different issue?

  5. mhalbritter commented on Jan 7, 2026

    @mhalbritter

    When combined with method references, it doesn't compile:

        public static void main(String[] args) {
            invoke(XX::returnsNull);
        }
    
        static @Nullable Object returnsNull() {
            return null;
        }
    
        static <R> void invoke(Supplier<@Nullable R> supplier) {
            Object foo = supplier.get();
            System.out.println(foo.toString());
        }

    error:

    Execution failed for task ':compileJava'.
    > Compilation failed; see the compiler output below.
      /home/mhalbritter/Downloads/nullaway-reproducer/src/main/java/com/example/nullaway_reproducer/XX.java:12: error: [NullAway] Failed to infer type argument nullability for call invoke(XX::returnsNull): Cannot treat @Nullable type as @NonNull: @org.jspecify.annotations.Nullable R
              invoke(XX::returnsNull);
                    ^
          (see http://t.uber.com/nullaway )
      2 errors
    
  6. msridhar commented on Jan 7, 2026

    @msridhar
    ContributorAuthor

    Thanks, @mhalbritter, the method reference issue is #1128 / #1307. I'm actively working on it.

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