7002070: If catch clause has an incompatible type, error pointer points to first exception type in list
authormcimadamore
Mon, 06 Dec 2010 11:50:37 +0000
changeset 7622 ee16e77d07f0
parent 7621 9f2901d1f92f
child 7623 66d98f7ba8c7
7002070: If catch clause has an incompatible type, error pointer points to first exception type in list Summary: Attribution should check each component of a disjunctive type separately, rather than checking the corresponding lub() Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/test/tools/javac/multicatch/Neg06.java
langtools/test/tools/javac/multicatch/Neg06.out
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Dec 06 11:49:00 2010 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Dec 06 11:50:37 2010 +0000
@@ -2889,8 +2889,15 @@
     }
 
     public void visitTypeDisjunction(JCTypeDisjunction tree) {
-        List<Type> alternatives = attribTypes(tree.alternatives, env);
-        tree.type = result = check(tree, types.lub(alternatives), TYP, pkind, pt);
+        ListBuffer<Type> multicatchTypes = ListBuffer.lb();
+        for (JCExpression typeTree : tree.alternatives) {
+            Type ctype = attribType(typeTree, env);
+            ctype = chk.checkType(typeTree.pos(),
+                          chk.checkClassType(typeTree.pos(), ctype),
+                          syms.throwableType);
+            multicatchTypes.append(ctype);
+        }
+        tree.type = result = check(tree, types.lub(multicatchTypes.toList()), TYP, pkind, pt);
     }
 
     public void visitTypeParameter(JCTypeParameter tree) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/multicatch/Neg06.java	Mon Dec 06 11:50:37 2010 +0000
@@ -0,0 +1,16 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 7002070
+ *
+ * @summary If catch clause has an incompatible type, error pointer points to first exception type in list
+ * @author mcimadamore
+ * @compile/fail/ref=Neg06.out -XDrawDiagnostics Neg06.java
+ *
+ */
+
+class Neg06 {
+    void test() {
+        try { }
+        catch (String | Integer s) {}
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/multicatch/Neg06.out	Mon Dec 06 11:50:37 2010 +0000
@@ -0,0 +1,3 @@
+Neg06.java:14:16: compiler.err.prob.found.req: (compiler.misc.incompatible.types), java.lang.String, java.lang.Throwable
+Neg06.java:14:25: compiler.err.prob.found.req: (compiler.misc.incompatible.types), java.lang.Integer, java.lang.Throwable
+2 errors