# HG changeset patch # User dlsmith # Date 1429282559 21600 # Node ID f08330fad3416af428491c0dcafec974e95ee723 # Parent 84eb517777335f079ba16c1fa49e7c36f0c444aa 8075520: Varargs access check mishandles capture variables 8077786: Check varargs access against inferred signature Reviewed-by: vromero diff -r 84eb51777733 -r f08330fad341 src/share/classes/com/sun/tools/javac/comp/Resolve.java --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java Wed Apr 15 14:45:24 2015 -0700 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java Fri Apr 17 08:55:59 2015 -0600 @@ -836,20 +836,19 @@ List formals, Warner warn) { super.argumentsAcceptable(env, deferredAttrContext, argtypes, formals, warn); - //should we expand formals? + // should we check varargs element type accessibility? if (deferredAttrContext.phase.isVarargsRequired()) { - Type typeToCheck = null; - if (!checkVarargsAccessAfterResolution) { - typeToCheck = types.elemtype(formals.last()); - } else if (deferredAttrContext.mode == AttrMode.CHECK) { - typeToCheck = types.erasure(types.elemtype(formals.last())); - } - if (typeToCheck != null) { - varargsAccessible(env, typeToCheck, deferredAttrContext.inferenceContext); + if (deferredAttrContext.mode == AttrMode.CHECK || !checkVarargsAccessAfterResolution) { + varargsAccessible(env, types.elemtype(formals.last()), deferredAttrContext.inferenceContext); } } } + /** + * Test that the runtime array element type corresponding to 't' is accessible. 't' should be the + * varargs element type of either the method invocation type signature (after inference completes) + * or the method declaration signature (before inference completes). + */ private void varargsAccessible(final Env env, final Type t, final InferenceContext inferenceContext) { if (inferenceContext.free(t)) { inferenceContext.addFreeTypeListener(List.of(t), new FreeTypeListener() { @@ -859,7 +858,7 @@ } }); } else { - if (!isAccessible(env, t)) { + if (!isAccessible(env, types.erasure(t))) { Symbol location = env.enclClass.sym; reportMC(env.tree, MethodCheckDiag.INACCESSIBLE_VARARGS, inferenceContext, t, Kinds.kindName(location), location); } diff -r 84eb51777733 -r f08330fad341 test/tools/javac/varargs/T8049075/VarargsAndWildcardParameterizedTypeTest.java --- a/test/tools/javac/varargs/T8049075/VarargsAndWildcardParameterizedTypeTest.java Wed Apr 15 14:45:24 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2014, 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 8049075 - * @summary javac, wildcards and generic vararg method invocation not accepted - * @compile VarargsAndWildcardParameterizedTypeTest.java - */ - -class VarargsAndWildcardParameterizedTypeTest { - interface I { - String m(T... t); - } - - void m() { - I i = null; - i.m(Integer.valueOf(1), Integer.valueOf(1)); - } -} diff -r 84eb51777733 -r f08330fad341 test/tools/javac/varargs/access/OtherPackage.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/varargs/access/OtherPackage.java Fri Apr 17 08:55:59 2015 -0600 @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2015, 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. + */ + +/* + * Auxiliary file for VarargsInferredPrivateType + */ + +package otherpackage; + +public class OtherPackage { + public static Private getPrivate() { + return new Private(); + } + + private static class Private {} +} \ No newline at end of file diff -r 84eb51777733 -r f08330fad341 test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest.java Fri Apr 17 08:55:59 2015 -0600 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2014, 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 8049075 + * @summary javac, wildcards and generic vararg method invocation not accepted + * @compile VarargsAndWildcardParameterizedTypeTest.java + * @compile -source 8 VarargsAndWildcardParameterizedTypeTest.java + * @compile -source 7 VarargsAndWildcardParameterizedTypeTest.java + */ + +class VarargsAndWildcardParameterizedTypeTest { + interface I { + String m(T... t); + } + + void m() { + I i = null; + i.m(Integer.valueOf(1), Integer.valueOf(1)); + } +} diff -r 84eb51777733 -r f08330fad341 test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest2.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest2.java Fri Apr 17 08:55:59 2015 -0600 @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2015, 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 8075520 + * @summary Varargs access check mishandles capture variables + * @compile VarargsAndWildcardParameterizedTypeTest2.java + * @compile -source 8 VarargsAndWildcardParameterizedTypeTest2.java + * @compile -source 7 VarargsAndWildcardParameterizedTypeTest2.java + */ + +class VarargsAndWildcardParameterizedTypeTest2 { + interface I { + void m(T... t); + } + + interface Box { + T get(); + } + + void m(I i, Box b) { + i.m(b.get()); + } +} diff -r 84eb51777733 -r f08330fad341 test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest3.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest3.java Fri Apr 17 08:55:59 2015 -0600 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2015, 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 8075520 + * @summary Varargs access check mishandles capture variables + * @compile VarargsAndWildcardParameterizedTypeTest3.java + * @compile -source 8 VarargsAndWildcardParameterizedTypeTest3.java + * @compile -source 7 VarargsAndWildcardParameterizedTypeTest3.java + */ + +class VarargsAndWildcardParameterizedTypeTest2 { + interface I { + void m(Box iter, T... t); + } + + interface Box {} + + void m(I i, Box b) { + i.m(b); + } +} diff -r 84eb51777733 -r f08330fad341 test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest4.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest4.java Fri Apr 17 08:55:59 2015 -0600 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2015, 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 8075520 + * @summary Varargs access check mishandles capture variables + * @compile VarargsAndWildcardParameterizedTypeTest4.java + * @compile -source 8 VarargsAndWildcardParameterizedTypeTest4.java + * @compile -source 7 VarargsAndWildcardParameterizedTypeTest4.java + */ + +class VarargsAndWildcardParameterizedTypeTest2 { + interface I { + void m(Box iter, T... t); + } + + interface Box {} + + void m(I i, Box b) { + i.m(b); + } +} diff -r 84eb51777733 -r f08330fad341 test/tools/javac/varargs/access/VarargsInferredPrivateType-source7.out --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/varargs/access/VarargsInferredPrivateType-source7.out Fri Apr 17 08:55:59 2015 -0600 @@ -0,0 +1,4 @@ +VarargsInferredPrivateType.java:16:10: compiler.err.cant.apply.symbol: kindname.method, m, T[], otherpackage.OtherPackage.Private, kindname.interface, VarargsInferredPrivateType.I, (compiler.misc.inaccessible.varargs.type: otherpackage.OtherPackage.Private, kindname.class, VarargsInferredPrivateType) +- compiler.note.unchecked.filename: VarargsInferredPrivateType.java +- compiler.note.unchecked.recompile +1 error diff -r 84eb51777733 -r f08330fad341 test/tools/javac/varargs/access/VarargsInferredPrivateType.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/varargs/access/VarargsInferredPrivateType.java Fri Apr 17 08:55:59 2015 -0600 @@ -0,0 +1,18 @@ +/* + * @test /nodynamiccopyright/ + * @bug 8077786 + * @summary Check varargs access against inferred signature + * @compile/fail/ref=VarargsInferredPrivateType.out -nowarn -XDrawDiagnostics VarargsInferredPrivateType.java OtherPackage.java + * @compile/fail/ref=VarargsInferredPrivateType.out -source 8 -nowarn -XDrawDiagnostics VarargsInferredPrivateType.java OtherPackage.java + * @compile/fail/ref=VarargsInferredPrivateType-source7.out -source 7 -nowarn -XDrawDiagnostics VarargsInferredPrivateType.java OtherPackage.java + */ + +class VarargsInferredPrivateType { + interface I { + void m(T... t); + } + + void m(I i) { + i.m(otherpackage.OtherPackage.getPrivate()); + } +} diff -r 84eb51777733 -r f08330fad341 test/tools/javac/varargs/access/VarargsInferredPrivateType.out --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/varargs/access/VarargsInferredPrivateType.out Fri Apr 17 08:55:59 2015 -0600 @@ -0,0 +1,4 @@ +VarargsInferredPrivateType.java:16:12: compiler.err.prob.found.req: (compiler.misc.inaccessible.varargs.type: otherpackage.OtherPackage.Private, kindname.class, VarargsInferredPrivateType) +- compiler.note.unchecked.filename: VarargsInferredPrivateType.java +- compiler.note.unchecked.recompile +1 error