6569404: Cannot instantiate an inner class of a type variable
authormcimadamore
Tue, 11 Aug 2009 01:14:06 +0100
changeset 3559 58cfcc0f1aa9
parent 3558 56be54d60f36
child 3560 bbfccbd92afe
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
langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/test/tools/javac/generics/typevars/6569404/T6569404a.java
langtools/test/tools/javac/generics/typevars/6569404/T6569404b.java
langtools/test/tools/javac/generics/typevars/6569404/T6569404b.out
langtools/test/tools/javac/generics/typevars/6569404/T6569404c.java
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Aug 11 01:13:42 2009 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Aug 11 01:14:06 2009 +0100
@@ -1239,7 +1239,10 @@
                 }
 
                 if (site.tag == CLASS) {
-                    if (site.getEnclosingType().tag == CLASS) {
+                    Type encl = site.getEnclosingType();
+                    while (encl != null && encl.tag == TYPEVAR)
+                        encl = encl.getUpperBound();
+                    if (encl.tag == CLASS) {
                         // we are calling a nested class
 
                         if (tree.meth.getTag() == JCTree.SELECT) {
@@ -1251,7 +1254,7 @@
                             // to the outer instance type of the class.
                             chk.checkRefType(qualifier.pos(),
                                              attribExpr(qualifier, localEnv,
-                                                        site.getEnclosingType()));
+                                                        encl));
                         } else if (methName == names._super) {
                             // qualifier omitted; check for existence
                             // of an appropriate implicit qualifier.
@@ -2042,7 +2045,7 @@
                 Symbol sym = (site.getUpperBound() != null)
                     ? selectSym(tree, capture(site.getUpperBound()), env, pt, pkind)
                     : null;
-                if (sym == null || isType(sym)) {
+                if (sym == null) {
                     log.error(pos, "type.var.cant.be.deref");
                     return syms.errSymbol;
                 } else {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/typevars/6569404/T6569404a.java	Tue Aug 11 01:14:06 2009 +0100
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug     6569404
+ * @summary Regression: Cannot instantiate an inner class of a type variable
+ * @author  mcimadamore
+ */
+
+public class T6569404a {
+
+    static class Outer {
+      public class Inner {}
+    }
+
+    static class Test<T extends Outer> {
+       public Test(T t) {
+          Outer.Inner inner = t.new Inner();
+       }
+    }
+
+    public static void main(String[] args) {
+       new Test<Outer>(new Outer());
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/typevars/6569404/T6569404b.java	Tue Aug 11 01:14:06 2009 +0100
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug     6569404
+ * @summary Regression: Cannot instantiate an inner class of a type variable
+ * @author  mcimadamore
+ * @compile/fail/ref=T6569404b.out T6569404b.java -XDrawDiagnostics
+ */
+
+class T6569404b {
+
+    static class A<X> {}
+
+    static class B<T extends Outer> extends A<T.Inner> {}
+
+    static class Outer {
+        public class Inner {}
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/typevars/6569404/T6569404b.out	Tue Aug 11 01:14:06 2009 +0100
@@ -0,0 +1,2 @@
+T6569404b.java:36:48: compiler.err.type.var.cant.be.deref
+1 error
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/typevars/6569404/T6569404c.java	Tue Aug 11 01:14:06 2009 +0100
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug     6569404
+ * @summary Regression: Cannot instantiate an inner class of a type variable
+ * @author  mcimadamore
+ */
+
+public class T6569404c {
+    static class Outer {
+      class Inner {}
+    }
+
+    static class Test<X extends Outer>  {
+       class InnerTest extends X.Inner { InnerTest(Outer o) {o.super();} }
+    }
+
+    public static void main(String[] args) {
+       new Test<Outer>().new InnerTest(new Outer());
+    }
+}