typetools / typetools/checker-framework

Permit assignment to @MonotonicNonNull in constructor

Open
#2,215 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

Every possibly-nullable assignment to a @MonotonicNonNull field is prohibited.
However, such an assignment should be permitted as the first assignment to the field in a constructor, if the field was not initialized to a possibly non-null value at its declaration. (That is, it was not initialized, or it was initialized to null.)
Here is an example:

import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

class Issue2215Parent {
    @MonotonicNonNull Object a;
    @MonotonicNonNull Object b = null;
    @MonotonicNonNull Object c = possiblyNull();
    @MonotonicNonNull Object d = new Object();

    static @Nullable Object possiblyNull() {
        return null;
    }
}

public class Issue2215 extends Issue2215Parent {

  @MonotonicNonNull Object f;
  @MonotonicNonNull Object g = null;
  @MonotonicNonNull Object h = Issue2215Parent.possiblyNull();
  @MonotonicNonNull Object i = new Object();

  Issue2215(@Nullable Object o) {

    this.a = o;
    this.b = o;
    // :: error: (assignment.type.incompatible)
    this.c = o;
    // :: error: (assignment.type.incompatible)
    this.d = o;
    // :: error: (assignment.type.incompatible)
    this.a = o;
    // :: error: (assignment.type.incompatible)
    this.b = o;
    // :: error: (assignment.type.incompatible)
    this.c = o;
    // :: error: (assignment.type.incompatible)
    this.d = o;

    this.f = o;
    this.g = o;
    // :: error: (assignment.type.incompatible)
    this.h = o;
    // :: error: (assignment.type.incompatible)
    this.i = o;
    // :: error: (assignment.type.incompatible)
    this.f = o;
    // :: error: (assignment.type.incompatible)
    this.g = o;
    // :: error: (assignment.type.incompatible)
    this.h = o;
    // :: error: (assignment.type.incompatible)
    this.i = o;

  }

}

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

Use the Java reproducer in the issue as the starting point and inspect the nullness-checker handling of @MonotonicNonNull field assignments during constructors. Verify that the first possibly-null assignment is accepted only for fields uninitialized or initialized to null, while later assignments and fields initialized to possibly or definitely non-null values still produce the shown diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.