typetools / typetools/checker-framework

False positive in the Optional Checker

Open
#6,848 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

False Positive (false warning or imprecision)
Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

Overview

The updated (to-be-released) Optional Checker has increased precision in its analysis of Optional values resulting from operations on container types. For example, it is able to deduce that the terminal .get() operation below is safe (i.e., will not throw a NoSuchElementException):

@NonEmpty Stream<Integer> lens = List.of("foo").stream()
    .map(String::length);
// Calling `Stream.max` on a non-empty container will always yield a present `Optional`
lens.max(Integer::compare).get();

Issue

The updated Optional Checker suffers false positives when the Optional-resulting operation depends on the type of the earliest leftmost receiver whose type is @PolyNonEmpty.

For example, the following minimal example (also attached as Main.java) should cleanly type-check:

import java.util.Optional;
import java.util.List;

import org.checkerframework.checker.nonempty.qual.*;

class Main {

  class Container {

    @NonEmpty List<String> getStrs() {
      throw new Error();
    }
  }

  void foo() {
    Container container = new Container();
    container.getStrs().stream().findAny().get();
  }
}

But it fails with the error message:

Main.java:17: error: [method.invocation] call to get() not allowed on the given receiver.
    container.getStrs().stream().findAny().get();
                                              ^
  found   : @MaybePresent Optional<@MaybePresent String>
  required: @Present Optional<@MaybePresent String>
1 error

Steps to Reproduce

Execute the test case with the following command:

javacheck -processor nonempty -ArunAsOptionalChecker -AassumePure Main.java

Expected Behavior: Main.java should cleanly type-check.

Actual Behavior: Main.java does not cleanly type-check.

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.

Research direction

Start with the attached Main.java and run the issue's javacheck command using the nonempty processor and Optional Checker options. Trace how the Optional Checker handles a chain whose earliest leftmost receiver has a @PolyNonEmpty type. Done means Main.java type-checks without the false-positive error on findAny().get().

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.