提交 71cc3199 编写于 作者: M mcimadamore

7023703: Valid code doesn't compile

Summary: leftovers cause problems when analyzing loops in Flow.java
Reviewed-by: jjg
上级 e7767409
...@@ -804,14 +804,16 @@ public class Flow extends TreeScanner { ...@@ -804,14 +804,16 @@ public class Flow extends TreeScanner {
ListBuffer<PendingExit> prevPendingExits = pendingExits; ListBuffer<PendingExit> prevPendingExits = pendingExits;
boolean prevLoopPassTwo = loopPassTwo; boolean prevLoopPassTwo = loopPassTwo;
pendingExits = new ListBuffer<PendingExit>(); pendingExits = new ListBuffer<PendingExit>();
int prevErrors = log.nerrors;
do { do {
Bits uninitsEntry = uninits.dup(); Bits uninitsEntry = uninits.dup();
uninitsEntry.excludeFrom(nextadr);
scanStat(tree.body); scanStat(tree.body);
alive |= resolveContinues(tree); alive |= resolveContinues(tree);
scanCond(tree.cond); scanCond(tree.cond);
if (log.nerrors != 0 || if (log.nerrors != prevErrors ||
loopPassTwo || loopPassTwo ||
uninitsEntry.diffSet(uninitsWhenTrue).nextBit(firstadr)==-1) uninitsEntry.dup().diffSet(uninitsWhenTrue).nextBit(firstadr)==-1)
break; break;
inits = initsWhenTrue; inits = initsWhenTrue;
uninits = uninitsEntry.andSet(uninitsWhenTrue); uninits = uninitsEntry.andSet(uninitsWhenTrue);
...@@ -831,8 +833,10 @@ public class Flow extends TreeScanner { ...@@ -831,8 +833,10 @@ public class Flow extends TreeScanner {
Bits initsCond; Bits initsCond;
Bits uninitsCond; Bits uninitsCond;
pendingExits = new ListBuffer<PendingExit>(); pendingExits = new ListBuffer<PendingExit>();
int prevErrors = log.nerrors;
do { do {
Bits uninitsEntry = uninits.dup(); Bits uninitsEntry = uninits.dup();
uninitsEntry.excludeFrom(nextadr);
scanCond(tree.cond); scanCond(tree.cond);
initsCond = initsWhenFalse; initsCond = initsWhenFalse;
uninitsCond = uninitsWhenFalse; uninitsCond = uninitsWhenFalse;
...@@ -841,9 +845,9 @@ public class Flow extends TreeScanner { ...@@ -841,9 +845,9 @@ public class Flow extends TreeScanner {
alive = !tree.cond.type.isFalse(); alive = !tree.cond.type.isFalse();
scanStat(tree.body); scanStat(tree.body);
alive |= resolveContinues(tree); alive |= resolveContinues(tree);
if (log.nerrors != 0 || if (log.nerrors != prevErrors ||
loopPassTwo || loopPassTwo ||
uninitsEntry.diffSet(uninits).nextBit(firstadr) == -1) uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
break; break;
uninits = uninitsEntry.andSet(uninits); uninits = uninitsEntry.andSet(uninits);
loopPassTwo = true; loopPassTwo = true;
...@@ -864,8 +868,10 @@ public class Flow extends TreeScanner { ...@@ -864,8 +868,10 @@ public class Flow extends TreeScanner {
Bits initsCond; Bits initsCond;
Bits uninitsCond; Bits uninitsCond;
pendingExits = new ListBuffer<PendingExit>(); pendingExits = new ListBuffer<PendingExit>();
int prevErrors = log.nerrors;
do { do {
Bits uninitsEntry = uninits.dup(); Bits uninitsEntry = uninits.dup();
uninitsEntry.excludeFrom(nextadr);
if (tree.cond != null) { if (tree.cond != null) {
scanCond(tree.cond); scanCond(tree.cond);
initsCond = initsWhenFalse; initsCond = initsWhenFalse;
...@@ -883,7 +889,7 @@ public class Flow extends TreeScanner { ...@@ -883,7 +889,7 @@ public class Flow extends TreeScanner {
scanStat(tree.body); scanStat(tree.body);
alive |= resolveContinues(tree); alive |= resolveContinues(tree);
scan(tree.step); scan(tree.step);
if (log.nerrors != 0 || if (log.nerrors != prevErrors ||
loopPassTwo || loopPassTwo ||
uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1) uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
break; break;
...@@ -897,8 +903,6 @@ public class Flow extends TreeScanner { ...@@ -897,8 +903,6 @@ public class Flow extends TreeScanner {
alive = resolveBreaks(tree, prevPendingExits) || alive = resolveBreaks(tree, prevPendingExits) ||
tree.cond != null && !tree.cond.type.isTrue(); tree.cond != null && !tree.cond.type.isTrue();
nextadr = nextadrPrev; nextadr = nextadrPrev;
inits.excludeFrom(nextadr);
uninits.excludeFrom(nextadr);
} }
public void visitForeachLoop(JCEnhancedForLoop tree) { public void visitForeachLoop(JCEnhancedForLoop tree) {
...@@ -913,13 +917,15 @@ public class Flow extends TreeScanner { ...@@ -913,13 +917,15 @@ public class Flow extends TreeScanner {
letInit(tree.pos(), tree.var.sym); letInit(tree.pos(), tree.var.sym);
pendingExits = new ListBuffer<PendingExit>(); pendingExits = new ListBuffer<PendingExit>();
int prevErrors = log.nerrors;
do { do {
Bits uninitsEntry = uninits.dup(); Bits uninitsEntry = uninits.dup();
uninitsEntry.excludeFrom(nextadr);
scanStat(tree.body); scanStat(tree.body);
alive |= resolveContinues(tree); alive |= resolveContinues(tree);
if (log.nerrors != 0 || if (log.nerrors != prevErrors ||
loopPassTwo || loopPassTwo ||
uninitsEntry.diffSet(uninits).nextBit(firstadr) == -1) uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
break; break;
uninits = uninitsEntry.andSet(uninits); uninits = uninitsEntry.andSet(uninits);
loopPassTwo = true; loopPassTwo = true;
......
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 7023703
* @summary Valid code doesn't compile
* @compile/fail/ref=T7023703neg.out -XDrawDiagnostics T7023703neg.java
*/
class T7023703neg {
void testForLoop(boolean cond) {
final int bug;
final int bug2;
for (;cond;) {
final int item = 0;
bug2 = 1; //error
}
bug = 0; //ok
}
void testForEachLoop(java.util.Collection<Integer> c) {
final int bug;
final int bug2;
for (Integer i : c) {
final int item = 0;
bug2 = 1; //error
}
bug = 0; //ok
}
void testWhileLoop(boolean cond) {
final int bug;
final int bug2;
while (cond) {
final int item = 0;
bug2 = 1; //error
}
bug = 0; //ok
}
void testDoWhileLoop(boolean cond) {
final int bug;
final int bug2;
do {
final int item = 0;
bug2 = 1; //error
} while (cond);
bug = 0; //ok
}
}
T7023703neg.java:37:13: compiler.err.var.might.be.assigned.in.loop: bug2
T7023703neg.java:47:13: compiler.err.var.might.be.assigned.in.loop: bug2
T7023703neg.java:57:13: compiler.err.var.might.be.assigned.in.loop: bug2
T7023703neg.java:67:13: compiler.err.var.might.be.assigned.in.loop: bug2
4 errors
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 7023703
* @summary Valid code doesn't compile
* @compile T7023703pos.java
*/
class T7023703pos {
void testForLoop() {
final int bug;
for (;"a".equals("b");) {
final int item = 0;
}
bug = 0; //ok
}
void testForEachLoop(boolean cond, java.util.Collection<Integer> c) {
final int bug;
for (Integer i : c) {
if (cond) {
final int item = 0;
}
}
bug = 0; //ok
}
void testWhileLoop() {
final int bug;
while ("a".equals("b")) {
final int item = 0;
}
bug = 0; //ok
}
void testDoWhileLoop() {
final int bug;
do {
final int item = 0;
} while ("a".equals("b"));
bug = 0; //ok
}
private static class Inner {
private final int a, b, c, d, e;
public Inner() {
a = b = c = d = e = 0;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册