Bug 105592 - Enum switch statement compile error
Summary: Enum switch statement compile error
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2 M4   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 108675 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-07-29 13:33 EDT by Adrian Grealish CLA
Modified: 2005-12-12 09:57 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Adrian Grealish CLA 2005-07-29 13:33:05 EDT
The following code compiles in Eclipse but not under javac. The problem is the 
parenthesis around NORMAL, under javac (java version "1.5.0_04") this causes a 
compile time error. According to the JLS 3rd edition 
(http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.11) 
it's either a constant expression or an enum constant name. I think javac has 
the correct behavior (it fails to compile), but a lousy error message.

public class EnumError {
	
	public enum State {
		NORMAL
	}
	
	public void foo() {
		State state = State.NORMAL;
		switch (state) {
		case (NORMAL) :
			System.out.println(State.NORMAL);
			break;
		}
	}
}
Comment 1 Olivier Thomann CLA 2005-07-30 15:31:31 EDT
I would say this is a bug in javac if I refer to jls 15.28 about the constant
expression specifications:
15.28 Constant Expression


ConstantExpression:
        Expression

A compile-time constant expression is an expression denoting a value of
primitive type or a String that does not complete abruptly and is composed using
only the following:
...
    * Parenthesized expressions whose contained expression is a constant expression.
...

So because NORMAL is a constant expression, (NORMAL) should also be a constant
expression.
If you remove the surrounding parenthesis, it works fine.

Philippe, I would close as INVALID.
Comment 2 Philipe Mulet CLA 2005-07-31 17:02:49 EDT
I would agree with you Olivier. Please add a regression test, and close as invalid.
Comment 3 Olivier Thomann CLA 2005-08-02 12:00:35 EDT
Added org.eclipse.jdt.core.tests.compiler.regression.EnumTest.test122.
Closing as INVALID.
Comment 4 Olivier Thomann CLA 2005-09-06 09:44:44 EDT
*** Bug 108675 has been marked as a duplicate of this bug. ***
Comment 5 Philipe Mulet CLA 2005-12-02 10:23:34 EST
Reopening, pls see last comment Javac matching entryhttp://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6356542

We should reject parenthesized enum constants in switch.
Comment 6 Philipe Mulet CLA 2005-12-02 10:23:57 EST
URL is http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6356542
Comment 7 Olivier Thomann CLA 2005-12-02 10:48:07 EST
Fixed and released in HEAD.
Regression test added in org.eclipse.jdt.core.tests.compiler.regression.EnumTest.test122.
Comment 8 Frederic Fusier CLA 2005-12-12 09:57:56 EST
Verified for 3.2 M4 using build I20051212-0010