提交 72b8dbb5 编写于 作者: M mcimadamore

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
上级 395ad251
...@@ -1172,8 +1172,11 @@ public class Attr extends JCTree.Visitor { ...@@ -1172,8 +1172,11 @@ public class Attr extends JCTree.Visitor {
Env<AttrContext> loopEnv = Env<AttrContext> loopEnv =
env.dup(env.tree, env.info.dup(env.info.scope.dup())); env.dup(env.tree, env.info.dup(env.info.scope.dup()));
try { try {
attribStat(tree.var, loopEnv); //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)); Type exprType = types.upperBound(attribExpr(tree.expr, loopEnv));
attribStat(tree.var, loopEnv);
chk.checkNonVoid(tree.pos(), exprType); chk.checkNonVoid(tree.pos(), exprType);
Type elemtype = types.elemtype(exprType); // perhaps expr is an array? Type elemtype = types.elemtype(exprType); // perhaps expr is an array?
if (elemtype == null) { if (elemtype == null) {
......
/*
* @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) { }
}
}
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
/*
* 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;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册