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

EnsuresNonNull failing on generic method parameters #6478

Open
Johannes-Rost opened this issue Mar 6, 2024 · 3 comments
Open

EnsuresNonNull failing on generic method parameters #6478

Johannes-Rost opened this issue Mar 6, 2024 · 3 comments
Assignees

Comments

@Johannes-Rost
Copy link

Hello,

I have come across an issue while using @EnsuresNonNull on method parameters with certain generic types.

If I have the following method

public class CollectionContracts {

    @EnsuresNonNull("#1")
    public static <T extends Collection<E>, E> @NonNull T requireNotEmpty(T collection) {
        if(collection == null) {
            throw new IllegalArgumentException("Collection was null");
        }
        if(collection.isEmpty()) {
            throw new IllegalArgumentException("Collection was empty");
        }
        return collection;
    }
}

the compilation fails with the following error message:

[...]/CollectionContracts.java:4:59
java: [contracts.postcondition] postcondition of requireNotEmpty is not satisfied.
  found   : no information about collection
  required: collection is @NonNull

If I change the signature to

    @EnsuresNonNull("#1")
    public static <E> @NonNull Collection<E> requireNotEmpty(Collection<E> collection) {

everything work as I would expect.

I am using checker-framework version 3.42.0 with maven-compiler-plugin 3.12.1

@Johannes-Rost
Copy link
Author

@Johannes-Rost
Copy link
Author

Johannes-Rost commented Mar 8, 2024

If I add an @nullable to the first argument, everthing works as expected:

public class CollectionContracts {

    @EnsuresNonNull("#1")
    public static <T extends Collection<E>, E> @NonNull T requireNotEmpty(@Nullable T collection) {
        if(collection == null) {
            throw new IllegalArgumentException("Collection was null");
        }
        if(collection.isEmpty()) {
            throw new IllegalArgumentException("Collection was empty");
        }
        return collection;
    }
}

What I still do not know is, why there is a difference between my two examples in the first post...

@mernst
Copy link
Member

mernst commented Mar 8, 2024

Thanks for letting us know the workaround. I would like @smillst to take a quick look regardless, so I'm re-opening this.

@mernst mernst reopened this Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants