cfr icon indicating copy to clipboard operation
cfr copied to clipboard

[Inlining] Generated try-with-resource creates null throwable.

Open Col-E opened this issue 4 years ago • 1 comments

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

Test.zip

Col-E avatar May 04 '20 06:05 Col-E

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. :)

leibnitz27 avatar May 04 '20 09:05 leibnitz27