cfr
cfr copied to clipboard
[Inlining] Generated try-with-resource creates null throwable.
CFR version
0.150-SNAPSHOT (5ff6fdb5b330f14a6625f5713c341c881fa7d4ba)
Compiler
javac 1.8.0_181
Description
Was debugging a project of mine and noticed that with the following source code, the generated Throwable
variable that acts as the context for addSuppressed
is always null.
The local variable value is assigned once with aconst_null
and from then on is read-only.
Source:
private static void bar(ByteArrayInputStream is) throws IOException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {}
finally {
is.close();
}
}
CFR
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private static void bar(ByteArrayInputStream is) throws IOException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Throwable throwable = null;
if (baos != null) {
if (throwable != null) {
try {
baos.close();
}
catch (Throwable throwable2) {
throwable.addSuppressed(throwable2);
}
} else {
baos.close();
}
}
}
finally {
is.close();
}
}
If the throwable
usage is ignored (It's an opaque predicate after all) we can get something much cleaner.
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (baos != null) {
baos.close();
}
}
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {}
Demo
Thanks - there's a few try-with-resources issues already raised, will see if this clashes.
Fwiw - try-with in javac 8 is a complete ass. It generates utter spaghetti.
Interestingly, javac 9 is much cleaner. But of course I do try (hah.) to get it, so yay testcases. :)