Wed, 10 Sep 2014 10:50:59 +0100
8051958: Cannot assign a value to final variable in lambda
Summary: Remove Attr.owner and refactor code for detecting forward field references
Reviewed-by: vromero
src/share/classes/com/sun/tools/javac/comp/Attr.java | file | annotate | diff | comparison | revisions | |
test/tools/javac/lambda/8051958/T8051958.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Wed Sep 03 13:20:04 2014 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Wed Sep 10 10:50:59 2014 +0100 1.3 @@ -287,7 +287,7 @@ 1.4 * @param env The current environment. 1.5 */ 1.6 boolean isAssignableAsBlankFinal(VarSymbol v, Env<AttrContext> env) { 1.7 - Symbol owner = owner(env); 1.8 + Symbol owner = env.info.scope.owner; 1.9 // owner refers to the innermost variable, method or 1.10 // initializer block declaration at this point. 1.11 return 1.12 @@ -302,41 +302,6 @@ 1.13 ((v.flags() & STATIC) != 0) == Resolve.isStatic(env)); 1.14 } 1.15 1.16 - /** 1.17 - * Return the innermost enclosing owner symbol in a given attribution context 1.18 - */ 1.19 - Symbol owner(Env<AttrContext> env) { 1.20 - while (true) { 1.21 - switch (env.tree.getTag()) { 1.22 - case VARDEF: 1.23 - //a field can be owner 1.24 - VarSymbol vsym = ((JCVariableDecl)env.tree).sym; 1.25 - if (vsym.owner.kind == TYP) { 1.26 - return vsym; 1.27 - } 1.28 - break; 1.29 - case METHODDEF: 1.30 - //method def is always an owner 1.31 - return ((JCMethodDecl)env.tree).sym; 1.32 - case CLASSDEF: 1.33 - //class def is always an owner 1.34 - return ((JCClassDecl)env.tree).sym; 1.35 - case BLOCK: 1.36 - //static/instance init blocks are owner 1.37 - Symbol blockSym = env.info.scope.owner; 1.38 - if ((blockSym.flags() & BLOCK) != 0) { 1.39 - return blockSym; 1.40 - } 1.41 - break; 1.42 - case TOPLEVEL: 1.43 - //toplevel is always an owner (for pkge decls) 1.44 - return env.info.scope.owner; 1.45 - } 1.46 - Assert.checkNonNull(env.next); 1.47 - env = env.next; 1.48 - } 1.49 - } 1.50 - 1.51 /** Check that variable can be assigned to. 1.52 * @param pos The current source code position. 1.53 * @param v The assigned varaible 1.54 @@ -3660,7 +3625,7 @@ 1.55 // and are subject to definite assignment checking. 1.56 if ((env.info.enclVar == v || v.pos > tree.pos) && 1.57 v.owner.kind == TYP && 1.58 - canOwnInitializer(owner(env)) && 1.59 + enclosingInitEnv(env) != null && 1.60 v.owner == env.info.scope.owner.enclClass() && 1.61 ((v.flags() & STATIC) != 0) == Resolve.isStatic(env) && 1.62 (!env.tree.hasTag(ASSIGN) || 1.63 @@ -3680,6 +3645,36 @@ 1.64 } 1.65 1.66 /** 1.67 + * Returns the enclosing init environment associated with this env (if any). An init env 1.68 + * can be either a field declaration env or a static/instance initializer env. 1.69 + */ 1.70 + Env<AttrContext> enclosingInitEnv(Env<AttrContext> env) { 1.71 + while (true) { 1.72 + switch (env.tree.getTag()) { 1.73 + case VARDEF: 1.74 + JCVariableDecl vdecl = (JCVariableDecl)env.tree; 1.75 + if (vdecl.sym.owner.kind == TYP) { 1.76 + //field 1.77 + return env; 1.78 + } 1.79 + break; 1.80 + case BLOCK: 1.81 + if (env.next.tree.hasTag(CLASSDEF)) { 1.82 + //instance/static initializer 1.83 + return env; 1.84 + } 1.85 + break; 1.86 + case METHODDEF: 1.87 + case CLASSDEF: 1.88 + case TOPLEVEL: 1.89 + return null; 1.90 + } 1.91 + Assert.checkNonNull(env.next); 1.92 + env = env.next; 1.93 + } 1.94 + } 1.95 + 1.96 + /** 1.97 * Check for illegal references to static members of enum. In 1.98 * an enum type, constructors and initializers may not 1.99 * reference its static members unless they are constant. 1.100 @@ -3732,17 +3727,6 @@ 1.101 v.name != names._class; 1.102 } 1.103 1.104 - /** Can the given symbol be the owner of code which forms part 1.105 - * if class initialization? This is the case if the symbol is 1.106 - * a type or field, or if the symbol is the synthetic method. 1.107 - * owning a block. 1.108 - */ 1.109 - private boolean canOwnInitializer(Symbol sym) { 1.110 - return 1.111 - (sym.kind & (VAR | TYP)) != 0 || 1.112 - (sym.kind == MTH && (sym.flags() & BLOCK) != 0); 1.113 - } 1.114 - 1.115 Warner noteWarner = new Warner(); 1.116 1.117 /**
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/lambda/8051958/T8051958.java Wed Sep 10 10:50:59 2014 +0100 2.3 @@ -0,0 +1,71 @@ 2.4 +/* 2.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. Oracle designates this 2.11 + * particular file as subject to the "Classpath" exception as provided 2.12 + * by Oracle in the LICENSE file that accompanied this code. 2.13 + * 2.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.17 + * version 2 for more details (a copy is included in the LICENSE file that 2.18 + * accompanied this code). 2.19 + * 2.20 + * You should have received a copy of the GNU General Public License version 2.21 + * 2 along with this work; if not, write to the Free Software Foundation, 2.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.23 + * 2.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.25 + * or visit www.oracle.com if you need additional information or have any 2.26 + * questions. 2.27 + */ 2.28 + 2.29 +/* 2.30 + * @test 2.31 + * @bug 8051958 2.32 + * @summary Cannot assign a value to final variable in lambda 2.33 + * @compile T8051958.java 2.34 + */ 2.35 + 2.36 +class T8051958 { 2.37 + Runnable inst_r = ()-> { 2.38 + final int x; 2.39 + x = 1; 2.40 + }; 2.41 + 2.42 + Runnable static_r = ()-> { 2.43 + final int x; 2.44 + x = 1; 2.45 + }; 2.46 + 2.47 + { 2.48 + Runnable inst_r = ()-> { 2.49 + final int x; 2.50 + x = 1; 2.51 + }; 2.52 + } 2.53 + 2.54 + static { 2.55 + Runnable static_r = ()-> { 2.56 + final int x; 2.57 + x = 1; 2.58 + }; 2.59 + } 2.60 + 2.61 + void instTest() { 2.62 + Runnable static_r = ()-> { 2.63 + final int x; 2.64 + x = 1; 2.65 + }; 2.66 + } 2.67 + 2.68 + static void staticTest() { 2.69 + Runnable static_r = ()-> { 2.70 + final int x; 2.71 + x = 1; 2.72 + }; 2.73 + } 2.74 +}