changeset 1809:7889d1fe2597

7139681: Enhanced for loop: local variable scope inconsistent with JLS Summary: For-each loop variable is incorrectly visible from the for-each expression Reviewed-by: jjg, vromero
author mcimadamore
date Thu, 06 Jun 2013 15:32:41 +0100
parents 6e30a513c945
children 349160289ba2
files src/share/classes/com/sun/tools/javac/comp/Attr.java test/tools/javac/foreach/7139681/T7139681neg.java test/tools/javac/foreach/7139681/T7139681neg.out test/tools/javac/foreach/7139681/T7139681pos.java
diffstat 4 files changed, 69 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Jun 06 15:30:14 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Jun 06 15:32:41 2013 +0100
@@ -1172,8 +1172,11 @@
         Env<AttrContext> loopEnv =
             env.dup(env.tree, env.info.dup(env.info.scope.dup()));
         try {
+            //the Formal Parameter of a for-each loop is not in the scope when
+            //attributing the for-each expression; we mimick this by attributing
+            //the for-each expression first (against original scope).
+            Type exprType = types.upperBound(attribExpr(tree.expr, loopEnv));
             attribStat(tree.var, loopEnv);
-            Type exprType = types.upperBound(attribExpr(tree.expr, loopEnv));
             chk.checkNonVoid(tree.pos(), exprType);
             Type elemtype = types.elemtype(exprType); // perhaps expr is an array?
             if (elemtype == null) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/foreach/7139681/T7139681neg.java	Thu Jun 06 15:32:41 2013 +0100
@@ -0,0 +1,16 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 7139681
+ * @summary Enhanced for loop: local variable scope inconsistent with JLS
+ *
+ * @compile/fail/ref=T7139681neg.out -XDrawDiagnostics T7139681neg.java
+ */
+class T7139681neg {
+    void testArray() {
+        for (int a : a) { }
+    }
+
+    void testIterable() {
+        for (Integer b : b) { }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/foreach/7139681/T7139681neg.out	Thu Jun 06 15:32:41 2013 +0100
@@ -0,0 +1,3 @@
+T7139681neg.java:10:22: compiler.err.cant.resolve.location: kindname.variable, a, , , (compiler.misc.location: kindname.class, T7139681neg, null)
+T7139681neg.java:14:26: compiler.err.cant.resolve.location: kindname.variable, b, , , (compiler.misc.location: kindname.class, T7139681neg, null)
+2 errors
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/foreach/7139681/T7139681pos.java	Thu Jun 06 15:32:41 2013 +0100
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2013, 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 7139681
+ * @summary Enhanced for loop: local variable scope inconsistent with JLS
+ *
+ * @compile T7139681pos.java
+ */
+class T7139681pos {
+    int[] a;
+    Iterable<Integer> b;
+
+    void testArray() {
+        for (int a : a) {
+            int a2 = a;
+        }
+    }
+
+    void testIterable() {
+        for (Integer b : b) {
+            Integer b2 = b;
+        }
+    }
+}