6723444: javac fails to substitute type variables into a constructor's throws clause
authormcimadamore
Tue, 13 Jan 2009 13:28:42 +0000
changeset 1791 d378f023c36d
parent 1790 7182011ee8a6
child 1792 985e9406d42a
6723444: javac fails to substitute type variables into a constructor's throws clause Summary: Added constructor's actual type info to NewClass AST node Reviewed-by: jjg Contributed-by: mark@twistedbanana.demon.co.uk
langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java
langtools/src/share/classes/com/sun/tools/javac/tree/JCTree.java
langtools/test/tools/javac/generics/6723444/T6723444.java
langtools/test/tools/javac/generics/6723444/T6723444.out
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Jan 13 13:28:20 2009 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Jan 13 13:28:42 2009 +0000
@@ -1457,7 +1457,7 @@
                 localEnv.info.varArgs = false;
                 tree.constructor = rs.resolveConstructor(
                     tree.pos(), localEnv, clazztype, argtypes, typeargtypes);
-                Type ctorType = checkMethod(clazztype,
+                tree.constructorType = checkMethod(clazztype,
                                             tree.constructor,
                                             localEnv,
                                             tree.args,
@@ -1465,7 +1465,7 @@
                                             typeargtypes,
                                             localEnv.info.varArgs);
                 if (localEnv.info.varArgs)
-                    assert ctorType.isErroneous() || tree.varargsElement != null;
+                    assert tree.constructorType.isErroneous() || tree.varargsElement != null;
             }
 
             if (cdef != null) {
@@ -1527,6 +1527,13 @@
                     typeargtypes, true, tree.varargsElement != null);
                 assert sym.kind < AMBIGUOUS || tree.constructor.type.isErroneous();
                 tree.constructor = sym;
+                tree.constructorType = checkMethod(clazztype,
+                                            tree.constructor,
+                                            localEnv,
+                                            tree.args,
+                                            argtypes,
+                                            typeargtypes,
+                                            localEnv.info.varArgs);
             }
 
             if (tree.constructor != null && tree.constructor.kind == MTH)
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java	Tue Jan 13 13:28:20 2009 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java	Tue Jan 13 13:28:42 2009 +0000
@@ -1136,11 +1136,32 @@
         scanExpr(tree.encl);
         scanExprs(tree.args);
        // scan(tree.def);
-        for (List<Type> l = tree.constructor.type.getThrownTypes();
+        for (List<Type> l = tree.constructorType.getThrownTypes();
              l.nonEmpty();
-             l = l.tail)
+             l = l.tail) {
             markThrown(tree, l.head);
-        scan(tree.def);
+        }
+        List<Type> caughtPrev = caught;
+        try {
+            // If the new class expression defines an anonymous class,
+            // analysis of the anonymous constructor may encounter thrown
+            // types which are unsubstituted type variables.
+            // However, since the constructor's actual thrown types have
+            // already been marked as thrown, it is safe to simply include
+            // each of the constructor's formal thrown types in the set of
+            // 'caught/declared to be thrown' types, for the duration of
+            // the class def analysis.
+            if (tree.def != null)
+                for (List<Type> l = tree.constructor.type.getThrownTypes();
+                     l.nonEmpty();
+                     l = l.tail) {
+                    caught = chk.incl(l.head, caught);
+                }
+            scan(tree.def);
+        }
+        finally {
+            caught = caughtPrev;
+        }
     }
 
     public void visitNewArray(JCNewArray tree) {
--- a/langtools/src/share/classes/com/sun/tools/javac/tree/JCTree.java	Tue Jan 13 13:28:20 2009 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/tree/JCTree.java	Tue Jan 13 13:28:42 2009 +0000
@@ -1326,6 +1326,7 @@
         public JCClassDecl def;
         public Symbol constructor;
         public Type varargsElement;
+        public Type constructorType;
         protected JCNewClass(JCExpression encl,
                            List<JCExpression> typeargs,
                            JCExpression clazz,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/6723444/T6723444.java	Tue Jan 13 13:28:42 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
+ * @bug 6723444
+ *
+ * @summary javac fails to substitute type variables into a constructor's throws clause
+ * @author Mark Mahieu
+ * @compile/fail/ref=T6723444.out -XDstdout -XDrawDiagnostics T6723444.java
+ *
+ */
+public class T6723444 {
+
+    static class Foo<X extends Throwable> {
+        Foo() throws X {}
+    }
+
+    <X extends Throwable> T6723444()
+        throws X {}
+
+    <X extends Throwable> T6723444(Foo<X> foo)
+        throws X {}
+
+    <X1 extends Throwable, X2 extends Throwable> T6723444(Foo<X1> foo, int i)
+        throws X1, X2 {}
+
+    public static void main(String[] args) throws Exception {
+
+        // the following 8 statements should compile without error
+
+        Foo<Exception> exFoo = new Foo<Exception>();
+        exFoo = new Foo<Exception>() {};
+
+        new<Exception> T6723444();
+        new<Exception> T6723444() {};
+        new T6723444(exFoo);
+        new T6723444(exFoo) {};
+        new<Exception, Exception> T6723444(exFoo, 1);
+        new<Exception, Exception> T6723444(exFoo, 1) {};
+
+        // the remaining statements should all raise an
+        // unreported exception error
+
+        new T6723444(exFoo, 1);
+        new T6723444(exFoo, 1) {};
+
+        Foo<Throwable> thFoo = new Foo<Throwable>();
+        thFoo = new Foo<Throwable>() {};
+
+        new<Throwable> T6723444();
+        new<Throwable> T6723444() {};
+        new T6723444(thFoo);
+        new T6723444(thFoo) {};
+        new T6723444(thFoo, 1);
+        new T6723444(thFoo, 1) {};
+        new<Throwable, Throwable> T6723444(thFoo, 1);
+        new<Throwable, Throwable> T6723444(thFoo, 1) {};
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/6723444/T6723444.out	Tue Jan 13 13:28:42 2009 +0000
@@ -0,0 +1,13 @@
+T6723444.java:65:9: compiler.err.unreported.exception.need.to.catch.or.throw: X2
+T6723444.java:66:9: compiler.err.unreported.exception.need.to.catch.or.throw: X2
+T6723444.java:68:32: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Throwable
+T6723444.java:69:17: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Throwable
+T6723444.java:71:9: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Throwable
+T6723444.java:72:9: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Throwable
+T6723444.java:73:9: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Throwable
+T6723444.java:74:9: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Throwable
+T6723444.java:75:9: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Throwable
+T6723444.java:76:9: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Throwable
+T6723444.java:77:9: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Throwable
+T6723444.java:78:9: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Throwable
+12 errors
\ No newline at end of file