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 a generic method call with explicit type arguments #1035

Closed
beatbrot opened this issue Sep 9, 2024 · 2 comments · Fixed by #1074
Closed

JSpecify: support a generic method call with explicit type arguments #1035

beatbrot opened this issue Sep 9, 2024 · 2 comments · Fixed by #1074
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://github.com/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
msridhar added a commit that referenced this issue Oct 30, 2024
…e arguments at calls) (#1053)

- [ ] Added support for generic identity functions with explicitly-typed
nullness.

  - [ ] Related to the issue #1035.

- [ ] void genericNonNullIdentityFunction() and void
genericNullAllowingIdentityFunction() unit tests are added to
GenericMethodTests.java file.

---------

Co-authored-by: Manu Sridharan <[email protected]>
@msridhar
Copy link
Collaborator

I'm happy to report that with the latest fixes from @haewiful the test case in this issue now passes in JSpecify mode! See #1074. Note that the test only passes with an explicit type argument at the call. I'm going to close this issue with that test, and open a new issue for supporting inference so that the type argument need not be written.

@msridhar msridhar changed the title JSpecify: support generic methods JSpecify: support a generic method call with explicit type arguments Nov 14, 2024
msridhar added a commit that referenced this issue Nov 14, 2024
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

Successfully merging a pull request may close this issue.

2 participants