question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

assert statement containing switch expression is not decompiled correctly

See original GitHub issue

CFR version

CFR 0.151-SNAPSHOT (7b97993)

Compiler

javac 14.0.1

Description

assert statements containing switch expressions (and maybe other complicated constructs as well) are not decompiled correctly. Instead of emitting an assert, CFR emits a read of a variable called $assertionsDisabled, which does not exist.

Example

Source:

class AssertTest {
    void test(Integer i) {
        assert switch (i + 1) {
            case 1 -> true;
            default -> false;
        } : switch (i + 2) {
            case 1 -> "1";
            default -> "2";
        };
    }
    
    void test2(int i) {
        assert i != 2 : "Test";
    }
}

Decompiled output:

class AssertTest {
    AssertTest() {
    }

    void test(Integer n) {
        if (!$assertionsDisabled) {
            switch (n + 1) {
                case 1: {
                    break;
                }
                default: {
                    throw new AssertionError((Object)(switch (n + 2) {
                        case 1 -> "1";
                        default -> "2";
                    }));
                }
            }
        }
    }

    void test2(int n) {
        assert (n != 2) : "Test";
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
leibnitz27commented, Jun 27, 2020

BTW - I used to get this one a little wrong, also fixed - but this produces amusing results in FF 😉

class AssertTest21 {

    private static boolean absolutelyVitalFunction(int x) {
        System.out.println("Disable the reactor");
        return true;
    }

    private static boolean checkCheeseSupply() {
        System.out.println("We've got cheese");
        return true;
    }

    public static void stopTheMeltdown(Integer x) {
        if (absolutelyVitalFunction(x)) {
            assert (checkCheeseSupply());
        }
    }
}
1reaction
leibnitz27commented, Jun 27, 2020

Oh, how gross is it that you can now write this:

class SwitchExpressionAssert1f {
    void test(Integer i) {
        assert switch (i + 1) {
            default -> { System.out.println("Uh oh");
            if (i == 3) {
                assert(false); // <--- look ma nested assert.
            }
            if (i < 10) yield true;
            yield true;
            }
        };
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

switch statement not properly decompile · Issue #567 - GitHub
Original Code: namespace ILSpyBug { internal class Program { private static void Main(string[] args) { List list1 = new List (); List list2 ......
Read more >
Why does an incomplete switch expression compile successfully
This is a known bug. See JDK-8212982 for details on its status. This code: public class SwitchBug { static String hold(String item) {...
Read more >
CFR - yet another java decompiler. - benf.org
Fix for switch incorrectly marking some sources of break targets as breaks, leading to unstructured output. Fix for missing assert pattern (some cases...
Read more >
Static Single Assignment for Decompilation
Copy statements inserted before the loop are not shown.111 ... 6.1 High level expressions for switch expressions in the Boomerang decompiler. . 199....
Read more >
Switch Statements - RecStudio Decompiler Design
Expression propagation cannot remove the R1 register because it is used multiple times, but statement structuring can check if the expression has no...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found