Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSpecify: support generic methods #1035

Open
beatbrot opened this issue Sep 9, 2024 · 1 comment
Open

JSpecify: support generic methods #1035

beatbrot opened this issue Sep 9, 2024 · 1 comment
Labels
jspecify Related to support for jspecify standard (see jspecify.dev)

Comments

@beatbrot
Copy link

beatbrot commented Sep 9, 2024

According to the JSpecify Spec, it is possible to add extends @Nullable Object to type parameters to siginify that T can be a nullable type.

Consider the following sample:

import org.jspecify.annotations.*;

@NullMarked
public class Todo {
    public static <T extends @Nullable Object> T foo(NullableSupplier<T> code) {
        return code.get();
    }

    public static void main(String[] args) {
        Todo.<@Nullable Object> foo(() -> null);
    }

    @NullMarked
    @FunctionalInterface
    public interface NullableSupplier<T extends @Nullable Object> {
        T get();
    }
}

According to JSpecify Spec, this is valid since the call to foo in main explicitly specifies the type parameter T as nullable Object. The JSpecify Reference Checker also accepts this.
NullAway however falsly gives us the following error without JSpecifyMode:

Todo.java:13: error: [NullAway] returning @Nullable expression from method with @NonNull return type
        Todo.<@Nullable Object> foo(() -> null);
                                    ^
    (see http://t.uber.com/nullaway )
1 error

And this error with JSpecifyMode:

Todo.java:13: error: [NullAway] Cannot pass parameter of type NullableSupplier<@Nullable Object>, as formal parameter has type NullableSupplier<T>, which has mismatched type parameter nullability
        Todo.<@Nullable Object> foo(() -> null);
                                    ^
    (see http://t.uber.com/nullaway )
1 error

This issue seems similar to #660 and others, but I feel like my example gives a proper way to verify that this is a Bug and that the Bug would be resolved if my sample compiles.

@msridhar
Copy link
Collaborator

msridhar commented Sep 9, 2024

Hi @beatbrot thanks for the report. We currently have no support for generic methods in JSpecify mode, only generic classes. See further details here: https:/uber/NullAway/wiki/JSpecify-Support We are actively working on this and hope to have more support within the next few months. I will update this issue when something is implemented.

@msridhar msridhar added the jspecify Related to support for jspecify standard (see jspecify.dev) label Sep 9, 2024
@msridhar msridhar changed the title JSpecify Nullable annotations are not respected for Generic Type Parameters JSpecify: support generic methods Sep 9, 2024
@haewiful haewiful mentioned this issue Oct 8, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
jspecify Related to support for jspecify standard (see jspecify.dev)
Projects
None yet
Development

No branches or pull requests

2 participants