Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 415439 - [1.8][compiler] Needlessly divergent error messages under similar contexts
Summary: [1.8][compiler] Needlessly divergent error messages under similar contexts
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.4   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: BETA J8   Edit
Assignee: shankha banerjee CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 287648
  Show dependency tree
 
Reported: 2013-08-20 05:16 EDT by Srikanth Sankaran CLA
Modified: 2013-09-19 23:33 EDT (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 Srikanth Sankaran CLA 2013-08-20 05:16:11 EDT
BETA_JAVA8:

The following program elicits two different diagnostics: this is needlessly
divergent. Also as one of the messages is more explicit we should opt for it.

// ---
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*; 

@Target(TYPE_USE)
@interface IllegalSyntax {}
@Target(TYPE_USE)
@interface Legal {}

class X {
	static int staticField;
	static class StaticNestedClass {}
	void foo() {
 // Syntax error, type annotations are illegal here
		int x = @IllegalSyntax X.staticField;
 // Type annotations are not allowed on type names used to access static members
		StaticNestedClass snc = (@IllegalSyntax X.StaticNestedClass) null;
	}
}
Comment 1 Srikanth Sankaran CLA 2013-08-20 05:16:40 EDT
Shankha, Thanks for following up.
Comment 2 shankha banerjee CLA 2013-09-15 03:39:55 EDT
Working on it.
Comment 3 shankha banerjee CLA 2013-09-19 06:34:29 EDT
The two errors received are:

1) Syntax error, type annotations are illegal here
2) Type annotations are not allowed on type names used to access static members

I assume (2) is more concise and therefore the chosen one.

The issue is the (1) error happens at the parser stage and (2) happens at the annotation stage. 

There is no information as per as I see to recognize a variable as static at parser stage.

Thanks
Comment 4 shankha banerjee CLA 2013-09-19 12:46:23 EDT
I have no idea how to go about it.

One way could be to report (1) at the annotation processing stage.

Consider the test case:

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(ElementType.TYPE_USE)
  @interface IllegalSyntax {}
@Target(ElementType.TYPE_USE)
  @interface Legal {}

class Y {
	public static int staticField;
	public static class StaticNestedClass {}
	public int nonStatic = 10;
}
  class X {
	void foo() {
	  // Syntax error, type annotations are illegal here
	  int x = @IllegalSyntax Y.staticField;
	  // Type annotations are not allowed on type names used to access static members
	  Y.StaticNestedClass snc = (@IllegalSyntax Y.StaticNestedClass) null;
	  Y y = new Y();
	  int z = @Legal y.nonStatic;
	}
  }     

At the parsing stage while you are processing:
int x = @IllegalSyntax Y.staticField;

How will you know If Y.staticField is access to a static field?

Thanks
Comment 5 Srikanth Sankaran CLA 2013-09-19 23:33:09 EDT
(In reply to shankha banerjee from comment #4)

> At the parsing stage while you are processing:
> int x = @IllegalSyntax Y.staticField;
> 
> How will you know If Y.staticField is access to a static field?

I agree, this is not knowable at parsing stage. We should also not defer this
to resolve stage because there could be clients that don't request bindings and
the erroneous program should continue to elicit the error message already.

Thanks for the clear and correct analysis Shankha, I think we can live with
this behavior.