typetools / typetools/checker-framework

Variable still under initialization after constructor completes

Open
#4,568 6 comments 0 reactions 1 assignee View on GitHub

@mernst is already working on this.

Since Apr 19, 2021.

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

Description

Using checker 3.12.0. I would not expect f to still be under initialization when calling add. The constructor for the class Foo has completed, therefore f should be fully initialized.

/home/jpschewe/projects/checker-bugs/src/main/java/net/mtu/eggplant/checker/UnderInitializationAfterConstruction.java:16: error: [argument.type.incompatible] incompatible argument for parameter arg0 of add.
    l.add(f);
          ^
  found   : @UnderInitialization(net.mtu.eggplant.checker.UnderInitializationAfterConstruction.Foo.class) @NonNull Foo
  required: @Initialized @NonNull Foo
package net.mtu.eggplant.checker;

import java.util.List;
import java.util.LinkedList;

import org.checkerframework.checker.initialization.qual.UnknownInitialization;
import org.checkerframework.checker.initialization.qual.NotOnlyInitialized;

public class UnderInitializationAfterConstruction {

  private final List<Foo> l = new LinkedList<>();
  
  public UnderInitializationAfterConstruction() {
    final Foo f = new Foo(this);
    // at this point f should not be under initialization
    l.add(f);
  }


  private static final class Foo {
    private final @NotOnlyInitialized UnderInitializationAfterConstruction ref;
    public Foo(@UnknownInitialization UnderInitializationAfterConstruction ref) {
      this.ref = ref;
    }
  }
}

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.