Tue, 11 Aug 2009 01:14:06 +0100
6569404: Cannot instantiate an inner class of a type variable
Summary: javac is too strict in rejecting member selction from a type-var
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Aug 11 01:13:42 2009 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Aug 11 01:14:06 2009 +0100 1.3 @@ -1239,7 +1239,10 @@ 1.4 } 1.5 1.6 if (site.tag == CLASS) { 1.7 - if (site.getEnclosingType().tag == CLASS) { 1.8 + Type encl = site.getEnclosingType(); 1.9 + while (encl != null && encl.tag == TYPEVAR) 1.10 + encl = encl.getUpperBound(); 1.11 + if (encl.tag == CLASS) { 1.12 // we are calling a nested class 1.13 1.14 if (tree.meth.getTag() == JCTree.SELECT) { 1.15 @@ -1251,7 +1254,7 @@ 1.16 // to the outer instance type of the class. 1.17 chk.checkRefType(qualifier.pos(), 1.18 attribExpr(qualifier, localEnv, 1.19 - site.getEnclosingType())); 1.20 + encl)); 1.21 } else if (methName == names._super) { 1.22 // qualifier omitted; check for existence 1.23 // of an appropriate implicit qualifier. 1.24 @@ -2042,7 +2045,7 @@ 1.25 Symbol sym = (site.getUpperBound() != null) 1.26 ? selectSym(tree, capture(site.getUpperBound()), env, pt, pkind) 1.27 : null; 1.28 - if (sym == null || isType(sym)) { 1.29 + if (sym == null) { 1.30 log.error(pos, "type.var.cant.be.deref"); 1.31 return syms.errSymbol; 1.32 } else {
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/generics/typevars/6569404/T6569404a.java Tue Aug 11 01:14:06 2009 +0100 2.3 @@ -0,0 +1,46 @@ 2.4 +/* 2.5 + * Copyright 2009 Sun Microsystems, Inc. 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. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 2.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 2.24 + * have any questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 6569404 2.30 + * @summary Regression: Cannot instantiate an inner class of a type variable 2.31 + * @author mcimadamore 2.32 + */ 2.33 + 2.34 +public class T6569404a { 2.35 + 2.36 + static class Outer { 2.37 + public class Inner {} 2.38 + } 2.39 + 2.40 + static class Test<T extends Outer> { 2.41 + public Test(T t) { 2.42 + Outer.Inner inner = t.new Inner(); 2.43 + } 2.44 + } 2.45 + 2.46 + public static void main(String[] args) { 2.47 + new Test<Outer>(new Outer()); 2.48 + } 2.49 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/generics/typevars/6569404/T6569404b.java Tue Aug 11 01:14:06 2009 +0100 3.3 @@ -0,0 +1,41 @@ 3.4 +/* 3.5 + * Copyright 2009 Sun Microsystems, Inc. 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. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 3.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 3.24 + * have any questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test 3.29 + * @bug 6569404 3.30 + * @summary Regression: Cannot instantiate an inner class of a type variable 3.31 + * @author mcimadamore 3.32 + * @compile/fail/ref=T6569404b.out T6569404b.java -XDrawDiagnostics 3.33 + */ 3.34 + 3.35 +class T6569404b { 3.36 + 3.37 + static class A<X> {} 3.38 + 3.39 + static class B<T extends Outer> extends A<T.Inner> {} 3.40 + 3.41 + static class Outer { 3.42 + public class Inner {} 3.43 + } 3.44 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/generics/typevars/6569404/T6569404b.out Tue Aug 11 01:14:06 2009 +0100 4.3 @@ -0,0 +1,2 @@ 4.4 +T6569404b.java:36:48: compiler.err.type.var.cant.be.deref 4.5 +1 error
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/generics/typevars/6569404/T6569404c.java Tue Aug 11 01:14:06 2009 +0100 5.3 @@ -0,0 +1,43 @@ 5.4 +/* 5.5 + * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. 5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 + * 5.8 + * This code is free software; you can redistribute it and/or modify it 5.9 + * under the terms of the GNU General Public License version 2 only, as 5.10 + * published by the Free Software Foundation. 5.11 + * 5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 + * version 2 for more details (a copy is included in the LICENSE file that 5.16 + * accompanied this code). 5.17 + * 5.18 + * You should have received a copy of the GNU General Public License version 5.19 + * 2 along with this work; if not, write to the Free Software Foundation, 5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 + * 5.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 5.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 5.24 + * have any questions. 5.25 + */ 5.26 + 5.27 +/* 5.28 + * @test 5.29 + * @bug 6569404 5.30 + * @summary Regression: Cannot instantiate an inner class of a type variable 5.31 + * @author mcimadamore 5.32 + */ 5.33 + 5.34 +public class T6569404c { 5.35 + static class Outer { 5.36 + class Inner {} 5.37 + } 5.38 + 5.39 + static class Test<X extends Outer> { 5.40 + class InnerTest extends X.Inner { InnerTest(Outer o) {o.super();} } 5.41 + } 5.42 + 5.43 + public static void main(String[] args) { 5.44 + new Test<Outer>().new InnerTest(new Outer()); 5.45 + } 5.46 +}