# HG changeset patch # User mcimadamore # Date 1231853322 0 # Node ID d378f023c36dc81e79902d3c26b232feb03b18d8 # Parent 7182011ee8a659149b9c8c1ff0d2f3acfb9d535e 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 diff -r 7182011ee8a6 -r d378f023c36d langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java --- 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) diff -r 7182011ee8a6 -r d378f023c36d langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java --- 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 l = tree.constructor.type.getThrownTypes(); + for (List l = tree.constructorType.getThrownTypes(); l.nonEmpty(); - l = l.tail) + l = l.tail) { markThrown(tree, l.head); - scan(tree.def); + } + List 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 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) { diff -r 7182011ee8a6 -r d378f023c36d langtools/src/share/classes/com/sun/tools/javac/tree/JCTree.java --- 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 typeargs, JCExpression clazz, diff -r 7182011ee8a6 -r d378f023c36d langtools/test/tools/javac/generics/6723444/T6723444.java --- /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 { + Foo() throws X {} + } + + T6723444() + throws X {} + + T6723444(Foo foo) + throws X {} + + T6723444(Foo foo, int i) + throws X1, X2 {} + + public static void main(String[] args) throws Exception { + + // the following 8 statements should compile without error + + Foo exFoo = new Foo(); + exFoo = new Foo() {}; + + new T6723444(); + new T6723444() {}; + new T6723444(exFoo); + new T6723444(exFoo) {}; + new T6723444(exFoo, 1); + new T6723444(exFoo, 1) {}; + + // the remaining statements should all raise an + // unreported exception error + + new T6723444(exFoo, 1); + new T6723444(exFoo, 1) {}; + + Foo thFoo = new Foo(); + thFoo = new Foo() {}; + + new T6723444(); + new T6723444() {}; + new T6723444(thFoo); + new T6723444(thFoo) {}; + new T6723444(thFoo, 1); + new T6723444(thFoo, 1) {}; + new T6723444(thFoo, 1); + new T6723444(thFoo, 1) {}; + } +} diff -r 7182011ee8a6 -r d378f023c36d langtools/test/tools/javac/generics/6723444/T6723444.out --- /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