Code is moved out of default case of switch
See original GitHub issueCFR version
0.150-SNAPSHOT (commit cf242d2)
Compiler
- javac 11.0.5
- javac 14
Description
Relates to #128
Code in the default case of a switch statement or expression with no other cases (or fall-through to default) is moved outside the switch. This is sometimes not possible, e.g. when the default case defines a local class with the same name.
Source:
class SwitchExpressionLocalClass {
void test() {
switch(0) {
default: {
class Test {
Test(int i) { }
}
new Test(1);
}
};
class Test { }
new Test();
}
}
Decompiled output:
class SwitchExpressionLocalClass {
SwitchExpressionLocalClass() {
}
/*
* Inner class renamed, behaviour may change
* Enabled force condition propagation
* Lifted jumps to return sites
*/
void test() {
switch (0) {
default:
}
class Test_1 {
Test_1(int n) {
}
}
new Test_1(1);
class Test {
Test() {
}
}
new Test();
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Should switch statements always contain a default clause?
Leaving out the default case enables the compiler to optionally warn or fail when it sees an unhandled case. Static verifiability is after...
Read more >The Great Debate: Always Include a Default Case in Switch?
As stated above, it is not mandatory to declare the default clause in a switch statement. It is entirely optional. Without it, the...
Read more >Switch statements: `default` doesn't have to be the last case
Non-last `default` cases are confusing but perfectly valid. Sometimes it makes sense to place them at the beginning or in the middle.
Read more >[java] MissingBreakInSwitch - last default case does not ...
Description: A switch statement does not contain a break, even if it is the default case. Code Sample demonstrating the issue: https://www.
Read more >Is it necessary to add the default case while using switch cases?
If this is the case, I think you are asked to add a default case , because it is an accepted coding style,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, I briefly looked at the byte code and noticed that it does not provide the necessary information to easily tell where the local class is declared. So I am perfectly fine with you considering this a “never fix” (and closing this issue).
Thanks for letting me know that the process will be reduced. Apparently you have had a pretty long commute based on the amount of work you have put into this 😄 Stay well 😃
As discussed - this isn’t going to be fixed, apart from the cases (like #128) where it’s possible to know which form must be required. (and the fix for #128 is fairly heavy handed, so I’m keen not to use it generally 😉 )