typetools / typetools/checker-framework

Ternary expression with @MonotonicNonNull fails

Open
#1,256 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I would expect the following to check successfully, with bar receiving the inferred type @Nullable Object:

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

public class MonotonicNonNullTernaryIf {

  @MonotonicNonNull Object defaultFoo;

  public void test(Object foo, boolean condition) {
    Object bar = condition ? foo : defaultFoo;
  }
}

But instead I get the following error:

$ java -jar checker.jar -processor org.checkerframework.checker.nullness.NullnessChecker -version MonotonicNonNullTernaryIf.java 
javac 1.8.0-jsr308-2.1.10
MonotonicNonNullTernaryIf.java:8: error: [monotonic.type.incompatible] cannot assign org.checkerframework.checker.nullness.qual.MonotonicNonNull to org.checkerframework.checker.nullness.qual.MonotonicNonNull (monotonic type).
    Object bar = condition ? foo : defaultFoo;
                                   ^
  full type found: @Initialized @MonotonicNonNull Object
1 error

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 by reproducing the issue with the MonotonicNonNullTernaryIf example and the NullnessChecker command shown in the report. Trace how the checker handles the ternary expression and the monotonic.type.incompatible diagnostic. Done means this example checks successfully with bar inferred as @Nullable Object, without weakening the intended monotonicity checks.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.