typetools / typetools/checker-framework

Field assignment with @NotOnlyInitialized fails

Open
#3,825 1 comment 0 reactions 1 assignee View on GitHub

@smillst is already working on this.

Since Feb 22, 2021.

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

Description

When creating a circular reference between two classes using the @NotOnlyInitialized annotation on the fields the assignment fails if done inline in the field declaration, but succeeds if done in the constructor. Included code samples below to demonstrate the behavior.

This fails:

class BClass {
  @NotOnlyInitialized private final AClass other = new AClass(this);
}

But this succeeds:

class BClass {
  @NotOnlyInitialized private final AClass other;
  BClass() {
    other = new AClass(this);
  }
}

It's not clear to me that this is intended output and it's not described in the documentation, I had to discover through trial and error.

Failing example:

http://eisop.uwaterloo.ca/live#mode=display&code=import+org.checkerframework.checker.initialization.qual.NotOnlyInitialized%3B%0Aimport+org.checkerframework.checker.initialization.qual.UnknownInitialization%3B%0A%0Aclass+AClass+%7B%0A++++%40NotOnlyInitialized+private+final+BClass+other%3B%0A++++AClass(%40UnknownInitialization+BClass+other)+%7B%0A++++++++this.other+%3D+other%3B%0A++++%7D%0A%7D%0A%0Aclass+BClass+%7B%0A++++%40NotOnlyInitialized+private+final+AClass+other+%3D+new+AClass(this)%3B%0A%7D&typeSystem=nullness

Succeeding example:

http://eisop.uwaterloo.ca/live#mode=display&code=import+org.checkerframework.checker.initialization.qual.NotOnlyInitialized%3B%0Aimport+org.checkerframework.checker.initialization.qual.UnknownInitialization%3B%0A%0Aclass+AClass+%7B%0A++++%40NotOnlyInitialized+private+final+BClass+other%3B%0A++++AClass(%40UnknownInitialization+BClass+other)+%7B%0A++++++++this.other+%3D+other%3B%0A++++%7D%0A%7D%0A%0Aclass+BClass+%7B%0A++++%40NotOnlyInitialized+private+final+AClass+other%3B%0A++++BClass()+%7B%0A++++++++other+%3D+new+AClass(this)%3B%0A++++%7D%0A%7D&typeSystem=nullness

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.