Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CFG for while (true) #3249

Closed
mernst opened this issue Apr 11, 2020 · 0 comments · Fixed by #3389
Closed

CFG for while (true) #3249

mernst opened this issue Apr 11, 2020 · 0 comments · Fixed by #3389

Comments

@mernst
Copy link
Member

mernst commented Apr 11, 2020

The CFG generator should treat true specially when used as a while condition.

Consider the following class:

public class WhileTrueInitialization {

  private final double field;

  WhileTrueInitialization() {
    double local;
    while (true) {
      local = 1;
      break;
    }
    field = local;
  }
}

Running

javacheck -g WhileTrueInitialization.java -processor nullness

yields

WhileTrueInitialization.java:11: error: [assignment.type.incompatible] incompatible types in assignment.
    field = local;
            ^
  found   : @UnknownInitialization @NonNull double
  required: @Initialized @NonNull double
1 error

The problem is apparent from viewing the control flow graph: while (true) is treated like any other while construct in that there is a branch that skips the entire body. That is not possible when the condition is true, so the CFG should not include that branch.

This is a duplicate of #1727, but with a smaller test case.

@mernst mernst added this to the Low milestone Apr 11, 2020
@wmdietl wmdietl linked a pull request Jun 18, 2020 that will close this issue
smillst pushed a commit that referenced this issue Jun 30, 2020
This PR fixes both issue #3249 and issue #1727, by adapting the CFG in the following way:

If the loop condition is checked to be a constant true  (according to the same logic in javac flow analysis), then the loop condition does not have an else-branch, i.e. the block holding the loop condition is directly followed by the one holding the loop entry, instead of followed by a conditional block.

Fixes #1727 and Fixes #3249`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant