leibnitz27 / cfr

This is the public repository for the CFR Java decompiler
https://www.benf.org/other/cfr
MIT License
2k stars 258 forks source link

try-finally in switch expression can cause subsequent code to be omitted #291

Open Marcono1234 opened 2 years ago

Marcono1234 commented 2 years ago

CFR version

0.153-SNAPSHOT (1a7dfa3)

Compiler

javac 17

Description

It appears when a switch expression contains a try-finally statement where the finally block always uses yield causes CFR to omit subsequent code after the switch expression.

This might be a bit contrived, but maybe it highlights a more generic issue?

Example

Source:

class FinallyAbnormalExit {
    static void finallyYield(int i) {
        String s = switch(i) {
            default -> {
                try {
                    yield "try";
                } finally {
                    yield "finally";
                }
            }
        };

        System.out.println("test");
    }
}

Decompiled output:

class FinallyAbnormalExit {
    FinallyAbnormalExit() {
    }

    static void finallyYield(int n) {
        String string;
        switch (n) {
            default: 
        }
        try {
            string = "try";
        }
        finally {
            String string2 = string = "finally";
        }

        // NOTE: `System.out.println` call is missing
    }
}

Maybe this is related to the dead code javac generates (see first goto 30 below):

 0: iload_0
 1: lookupswitch  { // 0
         default: 12
    }
12: ldc           #7                  // String try
14: astore_2
15: ldc           #9                  // String finally
17: astore_2
18: aload_2
19: goto          30
22: astore_3
23: ldc           #9                  // String finally
25: astore_2
26: aload_2
27: goto          30
30: astore_1
31: getstatic     #11                 // Field java/lang/System.out:Ljava/io/PrintStream;
34: ldc           #17                 // String test
36: invokevirtual #19                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
39: return