Bug 365117 - Switching on literal can produce dead code (but compiler doesn't warn)
Summary: Switching on literal can produce dead code (but compiler doesn't warn)
Status: VERIFIED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.8   Edit
Hardware: PC Windows XP
: P3 minor (vote)
Target Milestone: 3.8 M4   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-29 13:35 EST by Paul Benedict CLA
Modified: 2011-12-06 05:18 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Benedict CLA 2011-11-29 13:35:08 EST
Build Identifier: 20110916-0149

switch (1) {
case 1:
    System.out.println(1);
    break;
case 2:
    System.out.println(2);
    break;
default:
    System.out.println(3);
    break;
}

All cases except "case 1" will clearly never be executed. I expected the superfluous case/default statements to be flagged by the compiler but they were not.

Reproducible: Always
Comment 1 Paul Benedict CLA 2011-11-29 13:37:13 EST
I am referring to the Dead Code compiler problem option.
Comment 2 Ayushman Jain CLA 2011-11-29 13:49:19 EST
Whats the use case for this? Why would one switch on a literal as shown?
Comment 3 Paul Benedict CLA 2011-11-29 14:25:12 EST
Ayushman, I have no idea what the use case would be. However, I verified that javac also accepts this goofy code. I discovered it while testing switch semantics using 1.7 to see what syntax is allowed and disallowed.
Comment 4 Ayushman Jain CLA 2011-11-29 14:28:35 EST
(In reply to comment #3)
> Ayushman, I have no idea what the use case would be. However, I verified that
> javac also accepts this goofy code. I discovered it while testing switch
> semantics using 1.7 to see what syntax is allowed and disallowed.

Yes the code is legal. I'm sure no1 spent time in implementing the dead code analysis to such cases just because nobody uses a switch statement like that. So I guess we'll let this one be. Planning to close as WONTFIX.
Comment 5 Paul Benedict CLA 2011-11-29 19:51:33 EST
True, but Eclipse does implement dead code checking for "if(false)" -- I don't think that's very common either. In any event, I wanted to raise this issue since what was expected didn't occur.
Comment 6 Srikanth Sankaran CLA 2011-11-29 21:16:31 EST
JLS3 section 14.21 spell out the conditions of unreachableness.
For a switch statement it reads:

A statement in a switch block is reachable iff its switch statement is reachable
and at least one of the following is true:
Ÿ It bears a case or default label.
Ÿ There is a statement preceding it in the switch block and that preceding
statement can complete normally.

So there is no dead code here from the spec p.o.v.

From the same section:

Except for the special treatment of while, do, and for statements whose
condition expression has the constant value true, the values of expressions are
not taken into account in the flow analysis.
For example, a Java compiler will accept the code:
{
int n = 5;
while (n > 7) k = 2;
}
even though the value of n is known at compile time and in principle it can be
known at compile time that the assignment to k can never be executed.
A Java compiler must operate according to the rules laid out in this section.

-----------------

This is not fixable in eclipse without going against Java language specification.
Comment 7 Ayushman Jain CLA 2011-11-30 01:06:03 EST
(In reply to comment #6)
> JLS3 section 14.21 spell out the conditions of unreachableness.
> For a switch statement it reads:
> 
> A statement in a switch block is reachable iff its switch statement is
> reachable
> and at least one of the following is true:
> Ÿ It bears a case or default label.
> Ÿ There is a statement preceding it in the switch block and that preceding
> statement can complete normally.
> 
> So there is no dead code here from the spec p.o.v.

Actually this is a part of reachability analysis, for which we have a different warning - "Unreachable code". So this analysis doesn't really apply to the "dead code" warnings - which are eclipse specific. javac doesnt elicit "dead code" warnings at all, so there's no question of complying with JLS or javac for the dead code diagnostic.
Dead code warnings are generated by our own null analysis. Eg:

Object p = null;
if (p!= null) {
// do something
}

the whole if block above is marked as dead code.

Hence changing resolution to WONTFIX