changeset 978:c15d788cb381

7023703: Valid code doesn't compile Summary: leftovers cause problems when analyzing loops in Flow.java Reviewed-by: jjg
author mcimadamore
date Thu, 03 Mar 2011 17:32:35 +0000
parents e9b8fbb30f5a
children 32565546784b
files src/share/classes/com/sun/tools/javac/comp/Flow.java test/tools/javac/7023703/T7023703neg.java test/tools/javac/7023703/T7023703neg.out test/tools/javac/7023703/T7023703pos.java
diffstat 4 files changed, 164 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Mar 03 09:43:24 2011 +0000
+++ b/src/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Mar 03 17:32:35 2011 +0000
@@ -804,14 +804,16 @@
         ListBuffer<PendingExit> prevPendingExits = pendingExits;
         boolean prevLoopPassTwo = loopPassTwo;
         pendingExits = new ListBuffer<PendingExit>();
+        int prevErrors = log.nerrors;
         do {
             Bits uninitsEntry = uninits.dup();
+            uninitsEntry.excludeFrom(nextadr);
             scanStat(tree.body);
             alive |= resolveContinues(tree);
             scanCond(tree.cond);
-            if (log.nerrors != 0 ||
+            if (log.nerrors !=  prevErrors ||
                 loopPassTwo ||
-                uninitsEntry.diffSet(uninitsWhenTrue).nextBit(firstadr)==-1)
+                uninitsEntry.dup().diffSet(uninitsWhenTrue).nextBit(firstadr)==-1)
                 break;
             inits = initsWhenTrue;
             uninits = uninitsEntry.andSet(uninitsWhenTrue);
@@ -831,8 +833,10 @@
         Bits initsCond;
         Bits uninitsCond;
         pendingExits = new ListBuffer<PendingExit>();
+        int prevErrors = log.nerrors;
         do {
             Bits uninitsEntry = uninits.dup();
+            uninitsEntry.excludeFrom(nextadr);
             scanCond(tree.cond);
             initsCond = initsWhenFalse;
             uninitsCond = uninitsWhenFalse;
@@ -841,9 +845,9 @@
             alive = !tree.cond.type.isFalse();
             scanStat(tree.body);
             alive |= resolveContinues(tree);
-            if (log.nerrors != 0 ||
+            if (log.nerrors != prevErrors ||
                 loopPassTwo ||
-                uninitsEntry.diffSet(uninits).nextBit(firstadr) == -1)
+                uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
                 break;
             uninits = uninitsEntry.andSet(uninits);
             loopPassTwo = true;
@@ -864,8 +868,10 @@
         Bits initsCond;
         Bits uninitsCond;
         pendingExits = new ListBuffer<PendingExit>();
+        int prevErrors = log.nerrors;
         do {
             Bits uninitsEntry = uninits.dup();
+            uninitsEntry.excludeFrom(nextadr);
             if (tree.cond != null) {
                 scanCond(tree.cond);
                 initsCond = initsWhenFalse;
@@ -883,7 +889,7 @@
             scanStat(tree.body);
             alive |= resolveContinues(tree);
             scan(tree.step);
-            if (log.nerrors != 0 ||
+            if (log.nerrors != prevErrors ||
                 loopPassTwo ||
                 uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
                 break;
@@ -897,8 +903,6 @@
         alive = resolveBreaks(tree, prevPendingExits) ||
             tree.cond != null && !tree.cond.type.isTrue();
         nextadr = nextadrPrev;
-        inits.excludeFrom(nextadr);
-        uninits.excludeFrom(nextadr);
     }
 
     public void visitForeachLoop(JCEnhancedForLoop tree) {
@@ -913,13 +917,15 @@
 
         letInit(tree.pos(), tree.var.sym);
         pendingExits = new ListBuffer<PendingExit>();
+        int prevErrors = log.nerrors;
         do {
             Bits uninitsEntry = uninits.dup();
+            uninitsEntry.excludeFrom(nextadr);
             scanStat(tree.body);
             alive |= resolveContinues(tree);
-            if (log.nerrors != 0 ||
+            if (log.nerrors != prevErrors ||
                 loopPassTwo ||
-                uninitsEntry.diffSet(uninits).nextBit(firstadr) == -1)
+                uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
                 break;
             uninits = uninitsEntry.andSet(uninits);
             loopPassTwo = true;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/7023703/T7023703neg.java	Thu Mar 03 17:32:35 2011 +0000
@@ -0,0 +1,71 @@
+/*
+ * 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
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/7023703/T7023703neg.out	Thu Mar 03 17:32:35 2011 +0000
@@ -0,0 +1,5 @@
+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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/7023703/T7023703pos.java	Thu Mar 03 17:32:35 2011 +0000
@@ -0,0 +1,73 @@
+/*
+ * 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;
+        }
+    }
+}