typetools / typetools/checker-framework

EnsuresNonNull failing on generic method parameters

Open
#6,478 3 comments 0 reactions 1 assignee View on GitHub

@smillst is already working on this.

Since Mar 8, 2024.

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.