Tue, 16 Sep 2014 13:59:19 -0700
Merge
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Sep 11 15:33:00 2014 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Sep 16 13:59:19 2014 -0700 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 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java Thu Sep 11 15:33:00 2014 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java Tue Sep 16 13:59:19 2014 -0700 2.3 @@ -3045,7 +3045,7 @@ 2.4 /** 2.5 * Should lookup stop at given phase with given result 2.6 */ 2.7 - protected boolean shouldStop(Symbol sym, MethodResolutionPhase phase) { 2.8 + final boolean shouldStop(Symbol sym, MethodResolutionPhase phase) { 2.9 return phase.ordinal() > maxPhase.ordinal() || 2.10 sym.kind < ERRONEOUS || sym.kind == AMBIGUOUS; 2.11 } 2.12 @@ -4228,15 +4228,39 @@ 2.13 VARARITY(true, true) { 2.14 @Override 2.15 public Symbol mergeResults(Symbol bestSoFar, Symbol sym) { 2.16 - switch (sym.kind) { 2.17 - case WRONG_MTH: 2.18 - return (bestSoFar.kind == WRONG_MTH || bestSoFar.kind == WRONG_MTHS) ? 2.19 - bestSoFar : 2.20 - sym; 2.21 - case ABSENT_MTH: 2.22 - return bestSoFar; 2.23 - default: 2.24 - return sym; 2.25 + //Check invariants (see {@code LookupHelper.shouldStop}) 2.26 + Assert.check(bestSoFar.kind >= ERRONEOUS && bestSoFar.kind != AMBIGUOUS); 2.27 + if (sym.kind < ERRONEOUS) { 2.28 + //varargs resolution successful 2.29 + return sym; 2.30 + } else { 2.31 + //pick best error 2.32 + switch (bestSoFar.kind) { 2.33 + case WRONG_MTH: 2.34 + case WRONG_MTHS: 2.35 + //Override previous errors if they were caused by argument mismatch. 2.36 + //This generally means preferring current symbols - but we need to pay 2.37 + //attention to the fact that the varargs lookup returns 'less' candidates 2.38 + //than the previous rounds, and adjust that accordingly. 2.39 + switch (sym.kind) { 2.40 + case WRONG_MTH: 2.41 + //if the previous round matched more than one method, return that 2.42 + //result instead 2.43 + return bestSoFar.kind == WRONG_MTHS ? 2.44 + bestSoFar : sym; 2.45 + case ABSENT_MTH: 2.46 + //do not override erroneous symbol if the arity lookup did not 2.47 + //match any method 2.48 + return bestSoFar; 2.49 + case WRONG_MTHS: 2.50 + default: 2.51 + //safe to override 2.52 + return sym; 2.53 + } 2.54 + default: 2.55 + //otherwise, return first error 2.56 + return bestSoFar; 2.57 + } 2.58 } 2.59 } 2.60 };
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/lambda/8051958/T8051958.java Tue Sep 16 13:59:19 2014 -0700 3.3 @@ -0,0 +1,71 @@ 3.4 +/* 3.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. Oracle designates this 3.11 + * particular file as subject to the "Classpath" exception as provided 3.12 + * by Oracle in the LICENSE file that accompanied this code. 3.13 + * 3.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.17 + * version 2 for more details (a copy is included in the LICENSE file that 3.18 + * accompanied this code). 3.19 + * 3.20 + * You should have received a copy of the GNU General Public License version 3.21 + * 2 along with this work; if not, write to the Free Software Foundation, 3.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.23 + * 3.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.25 + * or visit www.oracle.com if you need additional information or have any 3.26 + * questions. 3.27 + */ 3.28 + 3.29 +/* 3.30 + * @test 3.31 + * @bug 8051958 3.32 + * @summary Cannot assign a value to final variable in lambda 3.33 + * @compile T8051958.java 3.34 + */ 3.35 + 3.36 +class T8051958 { 3.37 + Runnable inst_r = ()-> { 3.38 + final int x; 3.39 + x = 1; 3.40 + }; 3.41 + 3.42 + Runnable static_r = ()-> { 3.43 + final int x; 3.44 + x = 1; 3.45 + }; 3.46 + 3.47 + { 3.48 + Runnable inst_r = ()-> { 3.49 + final int x; 3.50 + x = 1; 3.51 + }; 3.52 + } 3.53 + 3.54 + static { 3.55 + Runnable static_r = ()-> { 3.56 + final int x; 3.57 + x = 1; 3.58 + }; 3.59 + } 3.60 + 3.61 + void instTest() { 3.62 + Runnable static_r = ()-> { 3.63 + final int x; 3.64 + x = 1; 3.65 + }; 3.66 + } 3.67 + 3.68 + static void staticTest() { 3.69 + Runnable static_r = ()-> { 3.70 + final int x; 3.71 + x = 1; 3.72 + }; 3.73 + } 3.74 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/varargs/8055514/T8055514.java Tue Sep 16 13:59:19 2014 -0700 4.3 @@ -0,0 +1,26 @@ 4.4 +/* 4.5 + * @test /nodynamiccopyright/ 4.6 + * @bug 8055514 4.7 + * @summary Wrong, confusing error when non-static varargs referenced in static context 4.8 + * @compile/fail/ref=T8055514.out -Xlint:varargs -Werror -XDrawDiagnostics T8055514.java 4.9 + */ 4.10 +class T8055514 { 4.11 + void m(int... args) { } 4.12 + 4.13 + void m2(int... args) { } 4.14 + static void m2(String s) { } 4.15 + 4.16 + void m3(int... args) { } 4.17 + static void m3(String s) { } 4.18 + static void m3(Runnable r) { } 4.19 + 4.20 + void m4(int... args) { } 4.21 + void m4(int i1, int i2, int i3) { } 4.22 + 4.23 + static void test() { 4.24 + m(1,2,3); //only one candidate (varargs) - varargs error wins 4.25 + m2(1,2,3); //two candidates - only one applicable (varargs) - varargs error wins 4.26 + m3(1,2,3); //three candidates - only one applicable (varargs) - varargs error wins 4.27 + m4(1,2,3); //two candidates - both applicable - basic error wins 4.28 + } 4.29 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/varargs/8055514/T8055514.out Tue Sep 16 13:59:19 2014 -0700 5.3 @@ -0,0 +1,5 @@ 5.4 +T8055514.java:21:9: compiler.err.non-static.cant.be.ref: kindname.method, m(int...) 5.5 +T8055514.java:22:9: compiler.err.non-static.cant.be.ref: kindname.method, m2(int...) 5.6 +T8055514.java:23:9: compiler.err.non-static.cant.be.ref: kindname.method, m3(int...) 5.7 +T8055514.java:24:9: compiler.err.non-static.cant.be.ref: kindname.method, m4(int,int,int) 5.8 +4 errors