typetools / typetools/checker-framework

Parameters of inherited implementations not checked for compatibility with methods they implement

Open
#2,724 4 comments 0 reactions 1 assignee View on GitHub

@wmdietl is already working on this.

Since Dec 16, 2019.

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

Description

import java.util.function.Consumer;
import java.util.function.Supplier;
import org.checkerframework.checker.nullness.qual.Nullable;

abstract class StringSupplier implements Supplier<String> {
  private String s = "";

  public void accept(String s) {
    this.s = s;
  }

  @Override
  public String get() {
    return s;
  }
}

class Holder extends StringSupplier implements Consumer<@Nullable String> {}

class Foo {
  public static void main(String[] args) {
    Holder h = new Holder();
    ((Consumer<@Nullable String>) h).accept(null);
    h.get().hashCode();
  }
}
$ checker/bin/javac -processor org.checkerframework.checker.nullness.NullnessChecker Foo.java && java Foo
Exception in thread "main" java.lang.NullPointerException
  at Foo.main(Foo.java:24)

I started down this path because I noticed that it was possible to make the same type implement, e.g., both Consumer<String> and Consumer<@Nullable String>. However, I wasn't finding a way to turn that into a NullPointerException (except by using a lambda, as in #2723). When I finally found the NullPointerException above, it turned out that I didn't need to implement Consumer twice to trigger it. (It does still seem a little dicey that it's possible to have a type that implements both Consumer<String> and Consumer<@Nullable String> (since Java doesn't allow implementing, say, Consumer<String> and Consumer<Object>), but that's the worst I can say about that at the moment.)

Source file and -version -verbose -AprintAllQualifiers output attached.

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.