Bug 42960

Summary: Difference seeing local class in switch with javac 1.4.2
Product: [Eclipse Project] JDT Reporter: Jerome Lanneluc <jerome_lanneluc>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: RESOLVED WONTFIX QA Contact:
Severity: normal    
Priority: P3    
Version: 2.1   
Target Milestone: 3.0 M4   
Hardware: PC   
OS: Windows 2000   
Whiteboard:

Description Jerome Lanneluc CLA 2003-09-11 11:55:58 EDT
Build 20030910

The following class compiles fine with javac 1.4.1 and our compiler, but it 
fails with javac 1.4.2:

public class TestCaseSwitch {
	int i = 0;
	public TestCaseSwitch() {
		switch (i) {
			case 0 :
				class InCaseClass {
					public void print() {
						System.out.println("i=" + i);
					}
				}
				InCaseClass icc = new InCaseClass();
				icc.print();
				break;
			default :
				icc = new InCaseClass();
				icc.print();
				break;
		}
	}
	public static void main(String[] args) {
		TestCaseSwitch t = new TestCaseSwitch();
	}
}

The failure with javac 1.4.2 is:
TestCaseSwitch.java:15: cannot resolve symbol
symbol  : class InCaseClass
location: class TestCaseSwitch
                                icc = new InCaseClass();
                                          ^
1 error

Who is right? Need to check the spec.
Comment 1 Olivier Thomann CLA 2003-09-11 14:42:25 EDT
jikes 1.18 also accepts this code.
Comment 2 Olivier Thomann CLA 2003-09-11 14:46:03 EDT
javac 1.4.2 looks inconsistent.
If you change the code to:
public class TestCaseSwitch {
	int i = 0;
	public TestCaseSwitch() {
		switch (i) {
			case 0 :
				class InCaseClass {
					public void print() {
						System.out.println("i=" + i);
					}
				}
				Object icc = new InCaseClass();
				icc.toString();
				break;
			default :
				icc = new Object();
				icc.toString();
				break;
		}
	}
	public static void main(String[] args) {
		TestCaseSwitch t = new TestCaseSwitch();
	}
}
It compiles fine with javac 1.4.2.
If the inner class is not visible (first version of the code), why the local
variable icc would be visible (second version)?
Comment 4 Philipe Mulet CLA 2003-09-11 15:07:20 EDT
I remember javac 1.4.2 build notes mentionning something about solving a defect 
in this area. I had the impression they had solved a defect in flow analysis 
where the local type could be used in subsequent catch but captured variable 
wasn't initialized there. It seems they took a too drastic measure to disallow 
this.

Bug http://developer.java.sun.com/developer/bugParade/bugs/4725650.html
clearly shows they solved the flow analysis problem by reducing the scope, and 
introducing an inconsistency with local variables, in an area where the spec is 
vague. 

Jikes and us both agree, Javac is wrong.
Closing.