Skip to content

Nullness Checker: spurious return.type.incompatible for a local array returned from an implicit lambda inside a generic method call #2134

Open

Description

@wmdietl

The Nullness Checker reports a false-positive return.type.incompatible on a block-bodied lambda that assigns a local variable and returns it, when the lambda is passed as an argument to a generic method whose type argument is inferred. javac alone compiles it.

import java.util.List;
import java.util.function.Function;

class H2Local {
    static <A, R> R of(A a, Function<A, R> f) {
        throw new Error();
    }

    Object[] localVar(List<String> list) {
        return of(
                list,
                l -> {
                    Object[] x = new Object[0];
                    return x;
                });
    }
}
javac -processor nullness H2Local.java
error: [return.type.incompatible] incompatible types in return.
        return of(
                 ^
  type of expression: @NonNull Object @Nullable []
  method return type: @NonNull Object @NonNull []
1 error

The lambda parameter l is not used in the body at all, so this is not related to #2084. x is a fresh, non-null array (new Object[0]), so the refined type of x at the return x; statement should be @NonNull Object @NonNull [], not @Nullable.

Variant Result
as above false positive
same lambda body, but as a plain field initializer (Supplier<Object[]> s = () -> { Object[] x = new Object[0]; return x; };), not an argument to a generic method ok

So the false positive appears specific to a lambda's block body being checked while it's the argument to a generic method invocation with its own type argument inference, not to block-bodied lambdas returning locals in general.

Expected: no error.

Environment: master 3591d45bec, JDK 25. Confirmed present identically on both master and the #2084 fix branch (unaffected by that fix).

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions