changeset 1790:7182011ee8a6

6665356: Cast not allowed when both qualifying type and inner class are parameterized Summary: Fixed parser and cats conversion in order to allow cast between generic inner classes Reviewed-by: jjg
author mcimadamore
date Tue, 13 Jan 2009 13:28:20 +0000
parents 7ac8c0815000
children d378f023c36d
files langtools/src/share/classes/com/sun/tools/javac/code/Types.java langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java langtools/test/tools/javac/cast/6665356/T6665356.java langtools/test/tools/javac/cast/6665356/T6665356.out langtools/test/tools/javac/generics/rare/6665356/T6665356.java langtools/test/tools/javac/generics/rare/6665356/T6665356.out
diffstat 6 files changed, 162 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Types.java	Tue Jan 13 13:27:14 2009 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Types.java	Tue Jan 13 13:28:20 2009 +0000
@@ -880,12 +880,12 @@
         if (warn != warnStack.head) {
             try {
                 warnStack = warnStack.prepend(warn);
-                return isCastable.visit(t, s);
+                return isCastable.visit(t,s);
             } finally {
                 warnStack = warnStack.tail;
             }
         } else {
-            return isCastable.visit(t, s);
+            return isCastable.visit(t,s);
         }
     }
     // where
@@ -983,10 +983,10 @@
                         if (highSub != null) {
                             assert a.tsym == highSub.tsym && a.tsym == lowSub.tsym
                                 : a.tsym + " != " + highSub.tsym + " != " + lowSub.tsym;
-                            if (!disjointTypes(aHigh.getTypeArguments(), highSub.getTypeArguments())
-                                && !disjointTypes(aHigh.getTypeArguments(), lowSub.getTypeArguments())
-                                && !disjointTypes(aLow.getTypeArguments(), highSub.getTypeArguments())
-                                && !disjointTypes(aLow.getTypeArguments(), lowSub.getTypeArguments())) {
+                            if (!disjointTypes(aHigh.allparams(), highSub.allparams())
+                                && !disjointTypes(aHigh.allparams(), lowSub.allparams())
+                                && !disjointTypes(aLow.allparams(), highSub.allparams())
+                                && !disjointTypes(aLow.allparams(), lowSub.allparams())) {
                                 if (upcast ? giveWarning(a, highSub) || giveWarning(a, lowSub)
                                            : giveWarning(highSub, a) || giveWarning(lowSub, a))
                                     warnStack.head.warnUnchecked();
@@ -1197,6 +1197,7 @@
             s = upperBound(s);
         if (s.tag == TYPEVAR)
             s = s.getUpperBound();
+
         return !isSubtype(t, s);
     }
     // </editor-fold>
@@ -3189,7 +3190,7 @@
     private boolean giveWarning(Type from, Type to) {
         // To and from are (possibly different) parameterizations
         // of the same class or interface
-        return to.isParameterized() && !containsType(to.getTypeArguments(), from.getTypeArguments());
+        return to.isParameterized() && !containsType(to.allparams(), from.allparams());
     }
 
     private List<Type> superClosure(Type t, Type s) {
--- a/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Tue Jan 13 13:27:14 2009 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Tue Jan 13 13:28:20 2009 +0000
@@ -864,6 +864,12 @@
                         t = F.at(pos1).TypeApply(t, args.toList());
                         checkGenerics();
                         t = bracketsOpt(toP(t));
+                        while (S.token() == DOT) {
+                            S.nextToken();
+                            mode = TYPE;
+                            t = toP(F.at(S.pos()).Select(t, ident()));
+                            t = typeArgumentsOpt(t);
+                        }
                     } else if ((mode & EXPR) != 0) {
                         mode = EXPR;
                         t = F.at(pos1).Binary(op, t, term2Rest(t1, TreeInfo.shiftPrec));
@@ -871,7 +877,8 @@
                     } else {
                         accept(GT);
                     }
-                } else {
+                }
+                else {
                     t = termRest(term1Rest(term2Rest(t, TreeInfo.orPrec)));
                 }
                 accept(RPAREN);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/6665356/T6665356.java	Tue Jan 13 13:28:20 2009 +0000
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2008 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
+ * @author Maurizio Cimadamore
+ * @bug     6665356
+ * @summary Cast not allowed when both qualifying type and inner class are parameterized
+ * @compile/fail/ref=T6665356.out -XDrawDiagnostics T6665356.java
+ */
+
+class T6665356 {
+    class Outer<S> {
+        class Inner<T> {}
+    }
+
+    void cast1(Outer<Integer>.Inner<Long> p) {
+        Object o = (Outer<Integer>.Inner<Long>)p;
+    }
+
+    void cast2(Outer<Integer>.Inner<Long> p) {
+        Object o = (Outer<? extends Number>.Inner<Long>)p;
+    }
+
+    void cast3(Outer<Integer>.Inner<Long> p) {
+        Object o = (Outer<Integer>.Inner<? extends Number>)p;
+    }
+
+    void cast4(Outer<Integer>.Inner<Long> p) {
+        Object o = (Outer<? extends Number>.Inner<? extends Number>)p;
+    }
+
+    void cast5(Outer<Integer>.Inner<Long> p) {
+        Object o = (Outer<? super Number>.Inner<Long>)p;
+    }
+
+    void cast6(Outer<Integer>.Inner<Long> p) {
+        Object o = (Outer<Integer>.Inner<? super Number>)p;
+    }
+
+    void cast7(Outer<Integer>.Inner<Long> p) {
+        Object o = (Outer<? super Number>.Inner<? super Number>)p;
+    }
+
+    void cast8(Outer<Integer>.Inner<Long> p) {
+        Object o = (Outer<? extends String>.Inner<Long>)p;
+    }
+
+    void cast9(Outer<Integer>.Inner<Long> p) {
+        Object o = (Outer<Integer>.Inner<? extends String>)p;
+    }
+
+    void cast10(Outer<Integer>.Inner<Long> p) {
+        Object o = (Outer<? super String>.Inner<Long>)p;
+    }
+
+    void cast11(Outer<Integer>.Inner<Long> p) {
+        Object o = (Outer<Integer>.Inner<? super String>)p;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/cast/6665356/T6665356.out	Tue Jan 13 13:28:20 2009 +0000
@@ -0,0 +1,8 @@
+T6665356.java:54:55: compiler.err.prob.found.req: (- compiler.misc.inconvertible.types), T6665356.Outer<java.lang.Integer>.Inner<java.lang.Long>, T6665356.Outer<? super java.lang.Number>.Inner<java.lang.Long>
+T6665356.java:58:58: compiler.err.prob.found.req: (- compiler.misc.inconvertible.types), T6665356.Outer<java.lang.Integer>.Inner<java.lang.Long>, T6665356.Outer<java.lang.Integer>.Inner<? super java.lang.Number>
+T6665356.java:62:65: compiler.err.prob.found.req: (- compiler.misc.inconvertible.types), T6665356.Outer<java.lang.Integer>.Inner<java.lang.Long>, T6665356.Outer<? super java.lang.Number>.Inner<? super java.lang.Number>
+T6665356.java:66:57: compiler.err.prob.found.req: (- compiler.misc.inconvertible.types), T6665356.Outer<java.lang.Integer>.Inner<java.lang.Long>, T6665356.Outer<? extends java.lang.String>.Inner<java.lang.Long>
+T6665356.java:70:60: compiler.err.prob.found.req: (- compiler.misc.inconvertible.types), T6665356.Outer<java.lang.Integer>.Inner<java.lang.Long>, T6665356.Outer<java.lang.Integer>.Inner<? extends java.lang.String>
+T6665356.java:74:55: compiler.err.prob.found.req: (- compiler.misc.inconvertible.types), T6665356.Outer<java.lang.Integer>.Inner<java.lang.Long>, T6665356.Outer<? super java.lang.String>.Inner<java.lang.Long>
+T6665356.java:78:58: compiler.err.prob.found.req: (- compiler.misc.inconvertible.types), T6665356.Outer<java.lang.Integer>.Inner<java.lang.Long>, T6665356.Outer<java.lang.Integer>.Inner<? super java.lang.String>
+7 errors
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/rare/6665356/T6665356.java	Tue Jan 13 13:28:20 2009 +0000
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2008 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
+ * @author Maurizio Cimadamore
+ * @bug     6665356
+ * @summary Cast not allowed when both qualifying type and inner class are parameterized
+ * @compile/fail/ref=T6665356.out -XDrawDiagnostics T6665356.java
+ */
+
+class T6665356 {
+    class Outer<S> {
+        class Inner<T> {}
+    }
+
+    void test1() {
+        boolean b;
+        b = null instanceof Outer.Inner;
+        b = null instanceof Outer<?>.Inner;
+        b = null instanceof Outer.Inner<?>;
+        b = null instanceof Outer<?>.Inner<?>;
+    }
+
+    void test2() {
+        boolean b;
+        Object o;
+        o = (Outer.Inner)null;
+        o = (Outer<?>.Inner)null;
+        o = (Outer.Inner<?>)null;
+        o = (Outer<?>.Inner<?>)null;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/rare/6665356/T6665356.out	Tue Jan 13 13:28:20 2009 +0000
@@ -0,0 +1,5 @@
+T6665356.java:40:37: compiler.err.improperly.formed.type.param.missing
+T6665356.java:41:40: compiler.err.improperly.formed.type.inner.raw.param
+T6665356.java:49:23: compiler.err.improperly.formed.type.param.missing
+T6665356.java:50:25: compiler.err.improperly.formed.type.inner.raw.param
+4 errors
\ No newline at end of file