Skip to content

JSpecify: handle stream values mapped to @Nullable #1462

@msridhar

Description

@msridhar

A self-contained test (to separate from library modeling issues):

import org.jspecify.annotations.*;
@NullMarked
class Test {
    interface List<T extends @Nullable Object> {
        Stream<T> stream();
    }
    interface Stream<T extends @Nullable Object> {
        <R extends @Nullable Object> Stream<R> map(Function<? super T, ? extends R> mapper);
        void forEach(Consumer<? super T> action);
    }
    interface Function<T extends @Nullable Object, R extends @Nullable Object> {
        R apply(T t);
    }
    interface Consumer<T extends @Nullable Object> {
        void accept(T t);
    }
    static @Nullable String mapToNull(String s) {
        return null;
    }
    static void test(List<String> list) {
        // legal, should infer R -> @Nullable String
        list.stream().map(Test::mapToNull).forEach(s -> {
            // BUG: Diagnostic contains: dereferenced expression s is @Nullable
            s.hashCode();
        });
    }
}

Right now, even in JSpecify mode, we don't report a warning on the s.hashCode() call. Hopefully, better handling of wildcards will get us there.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions