Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ private boolean scan(Tree tree) {
/* A break statement cannot complete normally. */
@Override
public Boolean visitBreak(BreakTree tree, Void unused) {
breaks.add(skipLabel(requireNonNull(((JCTree.JCBreak) tree).target)));
// The target of a `break` with a label is the labeled statement
breaks.add(requireNonNull(((JCTree.JCBreak) tree).target));
return false;
}

Expand Down Expand Up @@ -218,9 +219,8 @@ public Boolean visitEmptyStatement(EmptyStatementTree tree, Void unused) {
public Boolean visitLabeledStatement(LabeledStatementTree tree, Void unused) {
// (1)
boolean completes = scan(tree.getStatement());
// (2) `visitBreak` records a `break` against the statement contained in its target (see
// `skipLabel`).
return completes || breaks.contains(tree.getStatement());
// (2)
return completes || breaks.contains(tree);
}

/* An expression statement can complete normally iff it is reachable. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,6 @@ void f(int x, Object o, Shape shape, Paint paint, Color color) {
*/
private CompilationTestHelper containedStatementCanCompleteNormallyTest(
boolean expected, String labeledStatement) {
return containedStatementCanCompleteNormallyTest(expected, expected, labeledStatement);
}

/**
* Like {@link #containedStatementCanCompleteNormallyTest(boolean, String)}, but documents a bug
* where {@link Reachability} disagrees with javac: {@code reachability} is what {@link
* Reachability} currently answers, and {@code javac} is what javac answers (the ground truth).
*/
private CompilationTestHelper containedStatementCanCompleteNormallyTest(
boolean reachability, boolean javac, String labeledStatement) {
String label = "l:\n";
checkArgument(labeledStatement.startsWith(label), labeledStatement);
String statement = labeledStatement.substring(label.length());
Expand All @@ -193,10 +183,10 @@ void javacAgrees(int x, Object o) {
}
"""
.formatted(
reachability,
expected,
labeledStatement,
statement,
javac ? "String s = \"\";" : "s.length();"));
expected ? "String s = \"\";" : "s.length();"));
}

@Test
Expand Down Expand Up @@ -615,11 +605,7 @@ public void breakExitsLabeledStatement_while() {
}
""";
canCompleteNormallyTest(/* expected= */ true, statement).doTest();
// BUG: Reachability credits `break l` to the statement contained in `l`, so it disagrees with
// javac, which says that statement can't complete normally.
containedStatementCanCompleteNormallyTest(
/* reachability= */ true, /* javac= */ false, statement)
.doTest();
containedStatementCanCompleteNormallyTest(/* expected= */ false, statement).doTest();
}

/**
Expand All @@ -636,11 +622,7 @@ public void breakExitsLabeledStatement_do() {
} while (true);
""";
canCompleteNormallyTest(/* expected= */ true, statement).doTest();
// BUG: Reachability credits `break l` to the statement contained in `l`, so it disagrees with
// javac, which says that statement can't complete normally.
containedStatementCanCompleteNormallyTest(
/* reachability= */ true, /* javac= */ false, statement)
.doTest();
containedStatementCanCompleteNormallyTest(/* expected= */ false, statement).doTest();
}

/**
Expand All @@ -657,11 +639,7 @@ public void breakExitsLabeledStatement_for() {
}
""";
canCompleteNormallyTest(/* expected= */ true, statement).doTest();
// BUG: Reachability credits `break l` to the statement contained in `l`, so it disagrees with
// javac, which says that statement can't complete normally.
containedStatementCanCompleteNormallyTest(
/* reachability= */ true, /* javac= */ false, statement)
.doTest();
containedStatementCanCompleteNormallyTest(/* expected= */ false, statement).doTest();
}

/**
Expand All @@ -681,11 +659,7 @@ public void breakExitsLabeledStatement_switch() {
}
""";
canCompleteNormallyTest(/* expected= */ true, statement).doTest();
// BUG: Reachability credits `break l` to the statement contained in `l`, so it disagrees with
// javac, which says that statement can't complete normally.
containedStatementCanCompleteNormallyTest(
/* reachability= */ true, /* javac= */ false, statement)
.doTest();
containedStatementCanCompleteNormallyTest(/* expected= */ false, statement).doTest();
}

/** Like {@link #breakExitsLabeledStatement_switch}, but with switch rules. */
Expand All @@ -703,11 +677,7 @@ public void breakExitsLabeledStatement_arrowSwitch() {
}
""";
canCompleteNormallyTest(/* expected= */ true, statement).doTest();
// BUG: Reachability credits `break l` to the statement contained in `l`, so it disagrees with
// javac, which says that statement can't complete normally.
containedStatementCanCompleteNormallyTest(
/* reachability= */ true, /* javac= */ false, statement)
.doTest();
containedStatementCanCompleteNormallyTest(/* expected= */ false, statement).doTest();
}

/**
Expand Down
Loading