Skip to content
Permalink
Browse files

8234922: No compilation error reported not reported for a binding var…

…iable when loop broken with label

Any break outside of a loop should confine the binding variables from the loop's condition to the loop

Reviewed-by: mcimadamore
  • Loading branch information
Jan Lahoda
Jan Lahoda committed Dec 4, 2019
1 parent f26bdf8 commit 07be23513b057de06363efce38b78dc2334597f2
@@ -280,8 +280,7 @@ public boolean breaksOutOf(Env<AttrContext> env, JCTree loop, JCTree body, TreeM
//related errors, which will allow for more errors to be detected
Log.DiagnosticHandler diagHandler = new Log.DiscardDiagnosticHandler(log);
try {
boolean[] breaksOut = new boolean[1];
SnippetBreakAnalyzer analyzer = new SnippetBreakAnalyzer(loop);
SnippetBreakAnalyzer analyzer = new SnippetBreakAnalyzer();

analyzer.analyzeTree(env, body, make);
return analyzer.breaksOut();
@@ -1515,16 +1514,46 @@ public boolean isAlive() {
}

class SnippetBreakAnalyzer extends AliveAnalyzer {
private final JCTree loop;
private final Set<JCTree> seenTrees = new HashSet<>();
private boolean breaksOut;

public SnippetBreakAnalyzer(JCTree loop) {
this.loop = loop;
public SnippetBreakAnalyzer() {
}

@Override
public void visitLabelled(JCTree.JCLabeledStatement tree) {
seenTrees.add(tree);
super.visitLabelled(tree);
}

@Override
public void visitWhileLoop(JCTree.JCWhileLoop tree) {
seenTrees.add(tree);
super.visitWhileLoop(tree);
}

@Override
public void visitForLoop(JCTree.JCForLoop tree) {
seenTrees.add(tree);
super.visitForLoop(tree);
}

@Override
public void visitForeachLoop(JCTree.JCEnhancedForLoop tree) {
seenTrees.add(tree);
super.visitForeachLoop(tree);
}

@Override
public void visitDoLoop(JCTree.JCDoWhileLoop tree) {
seenTrees.add(tree);
super.visitDoLoop(tree);
}

@Override
public void visitBreak(JCBreak tree) {
breaksOut |= (super.alive == Liveness.ALIVE && tree.target == loop);
breaksOut |= (super.alive == Liveness.ALIVE &&
!seenTrees.contains(tree.target));
super.visitBreak(tree);
}

@@ -146,6 +146,30 @@ public static void main(String[] args) {
s.length();
}

{
while (!(o1 instanceof String s)) {
L8: break L8;
}

s.length();
}

{
for ( ;!(o1 instanceof String s); ) {
L9: break L9;
}

s.length();
}

{
do {
L10: break L10;
} while (!(o1 instanceof String s));

s.length();
}

if (o1 instanceof String s) {
Runnable r1 = new Runnable() {
@Override
@@ -187,5 +187,59 @@ public static void main(String[] args) {

s.length();
}

{
L: while (!(o1 instanceof String s)) {
break L;
}

s.length();
}

{
L: for (; !(o1 instanceof String s); ) {
break L;
}

s.length();
}

{
L: do {
break L;
} while (!(o1 instanceof String s));

s.length();
}

{
L: {
while (!(o1 instanceof String s)) {
break L;
}

s.length();
}
}

{
L: {
for (; !(o1 instanceof String s); ) {
break L;
}

s.length();
}
}

{
L: {
do {
break L;
} while (!(o1 instanceof String s));

s.length();
}
}
}
}
@@ -40,9 +40,15 @@ BindingsTest2.java:146:13: compiler.err.cant.resolve.location: kindname.variable
BindingsTest2.java:154:13: compiler.err.cant.resolve.location: kindname.variable, s, , , (compiler.misc.location: kindname.class, BindingsTest2, null)
BindingsTest2.java:171:13: compiler.err.cant.resolve.location: kindname.variable, s, , , (compiler.misc.location: kindname.class, BindingsTest2, null)
BindingsTest2.java:179:13: compiler.err.cant.resolve.location: kindname.variable, s, , , (compiler.misc.location: kindname.class, BindingsTest2, null)
BindingsTest2.java:196:13: compiler.err.cant.resolve.location: kindname.variable, s, , , (compiler.misc.location: kindname.class, BindingsTest2, null)
BindingsTest2.java:204:13: compiler.err.cant.resolve.location: kindname.variable, s, , , (compiler.misc.location: kindname.class, BindingsTest2, null)
BindingsTest2.java:212:13: compiler.err.cant.resolve.location: kindname.variable, s, , , (compiler.misc.location: kindname.class, BindingsTest2, null)
BindingsTest2.java:221:17: compiler.err.cant.resolve.location: kindname.variable, s, , , (compiler.misc.location: kindname.class, BindingsTest2, null)
BindingsTest2.java:231:17: compiler.err.cant.resolve.location: kindname.variable, s, , , (compiler.misc.location: kindname.class, BindingsTest2, null)
BindingsTest2.java:241:17: compiler.err.cant.resolve.location: kindname.variable, s, , , (compiler.misc.location: kindname.class, BindingsTest2, null)
BindingsTest2.java:135:17: compiler.err.unreachable.stmt
BindingsTest2.java:160:17: compiler.err.unreachable.stmt
BindingsTest2.java:185:17: compiler.err.unreachable.stmt
- compiler.note.preview.filename: BindingsTest2.java
- compiler.note.preview.recompile
45 errors
51 errors

0 comments on commit 07be235

Please sign in to comment.
You can’t perform that action at this time.