7039937: Improved catch analysis fails to handle a common idiom in the libraries
Summary: Disable generation of 'unreachable catch' warnings for catch statements catching Exception/Throwable
Reviewed-by: jjg
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java Fri Apr 29 16:05:02 2011 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java Fri Apr 29 16:05:29 2011 +0100
@@ -1153,8 +1153,7 @@
if (chk.subset(exc, caughtInTry)) {
log.error(pos, "except.already.caught", exc);
} else if (!chk.isUnchecked(pos, exc) &&
- exc.tsym != syms.throwableType.tsym &&
- exc.tsym != syms.exceptionType.tsym &&
+ !isExceptionOrThrowable(exc) &&
!chk.intersects(exc, thrownInTry)) {
log.error(pos, "except.never.thrown.in.try", exc);
} else if (allowImprovedCatchAnalysis) {
@@ -1163,7 +1162,8 @@
// unchecked exception, the result list would not be empty, as the augmented
// thrown set includes { RuntimeException, Error }; if 'exc' was a checked
// exception, that would have been covered in the branch above
- if (chk.diff(catchableThrownTypes, caughtInTry).isEmpty()) {
+ if (chk.diff(catchableThrownTypes, caughtInTry).isEmpty() &&
+ !isExceptionOrThrowable(exc)) {
String key = catchableThrownTypes.length() == 1 ?
"unreachable.catch" :
"unreachable.catch.1";
@@ -1171,6 +1171,12 @@
}
}
}
+ //where
+ private boolean isExceptionOrThrowable(Type exc) {
+ return exc.tsym == syms.throwableType.tsym ||
+ exc.tsym == syms.exceptionType.tsym;
+ }
+
public void visitConditional(JCConditional tree) {
scanCond(tree.cond);
--- a/langtools/test/tools/javac/6558548/T6558548.java Fri Apr 29 16:05:02 2011 +0100
+++ b/langtools/test/tools/javac/6558548/T6558548.java Fri Apr 29 16:05:29 2011 +0100
@@ -1,9 +1,9 @@
/*
* @test /nodynamiccopyright/
- * @bug 6558548
+ * @bug 6558548 7039937
* @summary The compiler needs to be aligned with clarified specification of throws
* @compile/fail/ref=T6558548_latest.out -XDrawDiagnostics T6558548.java
- * @compile/fail/ref=T6558548_6.out -source 6 -XDrawDiagnostics T6558548.java
+ * @compile/fail/ref=T6558548_6.out -source 6 -Xlint:-options -XDrawDiagnostics T6558548.java
*/
class T6558548 {
@@ -12,7 +12,7 @@
void checked() throws InterruptedException {}
void runtime() throws IllegalArgumentException {}
- void m1() {
+ void m1a() {
try {
throw new java.io.FileNotFoundException();
}
@@ -20,7 +20,7 @@
catch(java.io.IOException exc) { } // 6: ok; latest: unreachable
}
- void m1a() {
+ void m1b() {
try {
throw new java.io.IOException();
}
@@ -28,11 +28,20 @@
catch(java.io.IOException exc) { } //ok
}
- void m2() {
+ void m1c() {
try {
- nothing();
+ throw new java.io.FileNotFoundException();
}
- catch(Exception exc) { } // ok
+ catch(java.io.FileNotFoundException exc) { }
+ catch(Exception ex) { } //ok (Exception/Throwable always allowed)
+ }
+
+ void m1d() {
+ try {
+ throw new java.io.FileNotFoundException();
+ }
+ catch(java.io.FileNotFoundException exc) { }
+ catch(Throwable ex) { } //ok (Exception/Throwable always allowed)
}
void m3() {
@@ -131,7 +140,7 @@
runtime();
}
catch(RuntimeException exc) { }
- catch(Exception exc) { } //6: ok; latest: unreachable
+ catch(Exception exc) { } //6: ok; latest: ok (Exception/Throwable always allowed)
}
void m17() {
@@ -139,7 +148,7 @@
nothing();
}
catch(RuntimeException exc) { }
- catch(Exception exc) { } //6: ok; latest: unreachable
+ catch(Exception exc) { } //6: ok; latest: ok (Exception/Throwable always allowed)
}
void m18() {
@@ -148,7 +157,7 @@
}
catch(RuntimeException exc) { }
catch(InterruptedException exc) { }
- catch(Exception exc) { } //6: ok; latest: unreachable
+ catch(Exception exc) { } //6: ok; latest: ok (Exception/Throwable always allowed)
}
void m19() {
@@ -157,7 +166,7 @@
}
catch(RuntimeException exc) { }
catch(InterruptedException exc) { } //never thrown in try
- catch(Exception exc) { } //6: ok; latest: unreachable
+ catch(Exception exc) { } //6: ok; latest: ok (Exception/Throwable always allowed)
}
void m20() {
@@ -166,7 +175,7 @@
}
catch(RuntimeException exc) { }
catch(InterruptedException exc) { } //never thrown in try
- catch(Exception exc) { } //6: ok; latest: unreachable
+ catch(Exception exc) { } //6: ok; latest: ok (Exception/Throwable always allowed)
}
void m21() {
@@ -182,7 +191,7 @@
runtime();
}
catch(RuntimeException exc) { }
- catch(Exception exc) { } // 6: ok; latest: unreachable
+ catch(Exception exc) { } // 6: ok; latest: ok (Exception/Throwable always allowed)
}
void m23() {
@@ -190,7 +199,7 @@
nothing();
}
catch(RuntimeException exc) { }
- catch(Exception exc) { } // 6: ok; latest: unreachable
+ catch(Exception exc) { } // 6: ok; latest: ok (Exception/Throwable always allowed)
}
void m24() {
@@ -208,7 +217,7 @@
}
catch(RuntimeException exc) { }
catch(Error exc) { }
- catch(Throwable exc) { } //6: ok; latest: unreachable
+ catch(Throwable exc) { } //6: ok; latest: ok (Exception/Throwable always allowed)
}
void m26() {
@@ -217,7 +226,7 @@
}
catch(RuntimeException exc) { }
catch(Error exc) { }
- catch(Throwable exc) { } //6: ok; latest: unreachable
+ catch(Throwable exc) { } //6: ok; latest: ok (Exception/Throwable always allowed)
}
void m27() {
@@ -227,7 +236,7 @@
catch(RuntimeException exc) { }
catch(Error exc) { }
catch(InterruptedException exc) { }
- catch(Throwable exc) { } //6: ok; latest: unreachable
+ catch(Throwable exc) { } //6: ok; latest: ok (Exception/Throwable always allowed)
}
void m28() {
@@ -237,7 +246,7 @@
catch(RuntimeException exc) { }
catch(Error exc) { }
catch(InterruptedException exc) { } //never thrown in try
- catch(Throwable exc) { } //6: ok; latest: unreachable
+ catch(Throwable exc) { } //6: ok; latest: ok (Exception/Throwable always allowed)
}
void m29() {
@@ -247,7 +256,7 @@
catch(RuntimeException exc) { }
catch(Error exc) { }
catch(InterruptedException exc) { } //never thrown in try
- catch(Throwable exc) { } //6: ok; latest: unreachable
+ catch(Throwable exc) { } //6: ok; latest: ok (Exception/Throwable always allowed)
}
void m30() {
@@ -265,7 +274,7 @@
}
catch(RuntimeException exc) { }
catch(Error exc) { }
- catch(Throwable exc) { } //6: ok; latest: unreachable
+ catch(Throwable exc) { } //6: ok; latest: ok (Exception/Throwable always allowed)
}
void m32() {
@@ -274,7 +283,7 @@
}
catch(RuntimeException exc) { }
catch(Error exc) { }
- catch(Throwable exc) { } //6: ok; latest: unreachable
+ catch(Throwable exc) { } //6: ok; latest: ok (Exception/Throwable always allowed)
}
void m33() {
--- a/langtools/test/tools/javac/6558548/T6558548_6.out Fri Apr 29 16:05:02 2011 +0100
+++ b/langtools/test/tools/javac/6558548/T6558548_6.out Fri Apr 29 16:05:29 2011 +0100
@@ -1,9 +1,7 @@
-- compiler.warn.source.no.bootclasspath: 1.6
-T6558548.java:159:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
T6558548.java:168:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
-T6558548.java:239:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
-T6558548.java:249:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
-T6558548.java:291:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
-T6558548.java:298:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
+T6558548.java:177:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
+T6558548.java:248:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
+T6558548.java:258:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
+T6558548.java:300:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
+T6558548.java:307:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
6 errors
-1 warning
--- a/langtools/test/tools/javac/6558548/T6558548_latest.out Fri Apr 29 16:05:02 2011 +0100
+++ b/langtools/test/tools/javac/6558548/T6558548_latest.out Fri Apr 29 16:05:29 2011 +0100
@@ -1,23 +1,9 @@
T6558548.java:20:9: compiler.warn.unreachable.catch: java.io.FileNotFoundException
-T6558548.java:134:9: compiler.warn.unreachable.catch: java.lang.RuntimeException
-T6558548.java:142:9: compiler.warn.unreachable.catch: java.lang.RuntimeException
-T6558548.java:151:9: compiler.warn.unreachable.catch.1: java.lang.InterruptedException,java.lang.RuntimeException
-T6558548.java:159:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
-T6558548.java:160:9: compiler.warn.unreachable.catch: java.lang.RuntimeException
T6558548.java:168:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
-T6558548.java:169:9: compiler.warn.unreachable.catch: java.lang.RuntimeException
-T6558548.java:185:9: compiler.warn.unreachable.catch: java.lang.RuntimeException
-T6558548.java:193:9: compiler.warn.unreachable.catch: java.lang.RuntimeException
-T6558548.java:211:9: compiler.warn.unreachable.catch.1: java.lang.RuntimeException,java.lang.Error
-T6558548.java:220:9: compiler.warn.unreachable.catch.1: java.lang.RuntimeException,java.lang.Error
-T6558548.java:230:9: compiler.warn.unreachable.catch.1: java.lang.InterruptedException,java.lang.RuntimeException,java.lang.Error
-T6558548.java:239:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
-T6558548.java:240:9: compiler.warn.unreachable.catch.1: java.lang.RuntimeException,java.lang.Error
-T6558548.java:249:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
-T6558548.java:250:9: compiler.warn.unreachable.catch.1: java.lang.RuntimeException,java.lang.Error
-T6558548.java:268:9: compiler.warn.unreachable.catch.1: java.lang.RuntimeException,java.lang.Error
-T6558548.java:277:9: compiler.warn.unreachable.catch.1: java.lang.RuntimeException,java.lang.Error
-T6558548.java:291:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
-T6558548.java:298:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
+T6558548.java:177:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
+T6558548.java:248:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
+T6558548.java:258:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
+T6558548.java:300:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
+T6558548.java:307:9: compiler.err.except.never.thrown.in.try: java.lang.InterruptedException
6 errors
-15 warnings
+1 warning
--- a/langtools/test/tools/javac/diags/examples/UnreachableCatch1.java Fri Apr 29 16:05:02 2011 +0100
+++ b/langtools/test/tools/javac/diags/examples/UnreachableCatch1.java Fri Apr 29 16:05:29 2011 +0100
@@ -23,14 +23,21 @@
// key: compiler.warn.unreachable.catch.1
+import java.io.*;
+
class UnreachableCatch1 {
void test() {
try {
- throw new IllegalArgumentException();
+ if (true) {
+ throw new FileNotFoundException();
+ }
+ else {
+ throw new EOFException();
+ }
}
- catch(Error err) { }
- catch(RuntimeException rex) { }
- catch(Throwable t) { } //unreachable
+ catch(FileNotFoundException fnf) { }
+ catch(EOFException eof) { }
+ catch(IOException ex) { } //unreachable
}
}