Bug 210419 - [compiler] Invalid field initializer not flagged as error
Summary: [compiler] Invalid field initializer not flagged as error
Status: VERIFIED WORKSFORME
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.7 M1   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-11-20 12:21 EST by Frederic Fusier CLA
Modified: 2010-08-04 10:15 EDT (History)
3 users (show)

See Also:


Attachments
Proposed regression test (1.87 KB, patch)
2010-07-07 02:26 EDT, Srikanth Sankaran CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Frederic Fusier CLA 2007-11-20 12:21:13 EST
Build I20071120-0800.

Eclipse does not flag any error on following test case:

package bug;
public class Test {
  static int X;
  String field = {
      String str;
      switch (X) {
        case 0:
          str = "zero";
          break;
        default:
          str = "other";
          break;
      }
      this.field = str;
  };
  public static void main(String[] args) {
    System.out.println(new Test().field);
  }
}

You can even run it and get a "zero" in the console.

javac 1.4.2 report following errors:
java version "1.4.2_16"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_16-b05)
Java HotSpot(TM) Client VM (build 1.4.2_16-b05, mixed mode)
test005\Test.java:5: '}' expected
                        String str;
                               ^
test005\Test.java:6: illegal start of type
                        switch (X) {
                        ^
test005\Test.java:14: <identifier> expected
                        this.field = str;
                                        ^
test005\Test.java:16: 'class' or 'interface' expected
        public static void main(String[] args) {
                      ^
test005\Test.java:19: 'class' or 'interface' expected
}
^
test005\Test.java:20: 'class' or 'interface' expected
^
6 errors
Comment 1 Frederic Fusier CLA 2007-11-20 12:22:14 EST
Not a regression as this happened at least since 3.1.2...
Comment 2 Philipe Mulet CLA 2007-11-20 12:38:36 EST
If error diagnosis cannot infer anything, then as a fall-back approach, the original (ugly) syntax error should be reinjected back.

Separately from that, the diagnosis shouldn't fail to produce a diagnosis.
Comment 3 Srikanth Sankaran CLA 2010-07-06 05:14:04 EDT
This problem is not reproducible on HEAD: Here are the errors I get:


Description	Resource	Path	Location	Type
Syntax error on token ";", { expected after this token	Test.java	/_pasted_code_/src	line 4	Java Problem
Syntax error on token "(", ; expected	Test.java	/_pasted_code_/src	line 15	Java Problem
Syntax error on token ")", ; expected	Test.java	/_pasted_code_/src	line 15	Java Problem
Syntax error on token "}", delete this token	Test.java	/_pasted_code_/src	line 14	Java Problem
Syntax error on token "default", invalid Label	Test.java	/_pasted_code_/src	line 9	Java Problem
Syntax error on token(s), misplaced construct(s)	Test.java	/_pasted_code_/src	line 3	Java Problem
Syntax error on tokens, Label expected instead	Test.java	/_pasted_code_/src	line 6	Java Problem
Syntax error, insert "}" to complete ClassBody	Test.java	/_pasted_code_/src	line 18	Java Problem

I'll add a regression test and close this as WORKSFORME.
Comment 4 Srikanth Sankaran CLA 2010-07-07 02:26:47 EDT
Created attachment 173618 [details]
Proposed regression test
Comment 5 Srikanth Sankaran CLA 2010-07-07 02:33:56 EDT
Released regression tests on HEAD for 3.7 M1.
Comment 6 Ayushman Jain CLA 2010-08-04 10:15:02 EDT
Verified for 3.7M1 using build I20100802-1800.