# HG changeset patch # User mcimadamore # Date 1370529161 -3600 # Node ID 8dd20756448c825202352c0d4fd95c17f612a2f0 # Parent 00267721fe3a9ba9ec2204edb5cb72db474458ed 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 diff -r 00267721fe3a -r 8dd20756448c langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java --- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Jun 06 15:30:14 2013 +0100 +++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Jun 06 15:32:41 2013 +0100 @@ -1172,8 +1172,11 @@ Env 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) { diff -r 00267721fe3a -r 8dd20756448c langtools/test/tools/javac/foreach/7139681/T7139681neg.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/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) { } + } +} diff -r 00267721fe3a -r 8dd20756448c langtools/test/tools/javac/foreach/7139681/T7139681neg.out --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/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 diff -r 00267721fe3a -r 8dd20756448c langtools/test/tools/javac/foreach/7139681/T7139681pos.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/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 b; + + void testArray() { + for (int a : a) { + int a2 = a; + } + } + + void testIterable() { + for (Integer b : b) { + Integer b2 = b; + } + } +}