Bug 547805 - ECJ cannot compile annotation on inner class when attribute value is defined as private constant in said inner class
Summary: ECJ cannot compile annotation on inner class when attribute value is defined ...
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.12   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2019-05-31 07:07 EDT by Yoann Rodiere CLA
Modified: 2023-08-31 03:28 EDT (History)
5 users (show)

See Also:


Attachments
Reproducer (4.42 KB, application/zip)
2019-05-31 07:07 EDT, Yoann Rodiere CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Yoann Rodiere CLA 2019-05-31 07:07:45 EDT
Created attachment 278787 [details]
Reproducer

See attachement, compare output of `mvn clean install` (javac) and `mvn clean install -Decj.version=3.17` (ECJ).

You can also see the logs of both commands on Travis: https://travis-ci.com/yrodiere/eclipse-bugs/builds/113604107
Comment 1 Andrey Loskutov CLA 2019-05-31 07:09:57 EDT
Could you please try with ecj 3.18 (4.12 release M3 or RC1).
Comment 2 Yoann Rodiere CLA 2019-05-31 07:12:35 EDT
(In reply to Yoann  Rodiere from comment #0)
> See attachement, compare output of `mvn clean install` (javac) and `mvn
> clean install -Decj.version=3.17` (ECJ).

My apologies, the actual command to compile using ECJ is: `mvn clean install -Decj.version=3.17.0`.
Comment 3 Yoann Rodiere CLA 2019-05-31 07:15:41 EDT
org.eclipse.jdt:ecj:3.18.0 is not available on Maven central.
Comment 4 Yoann Rodiere CLA 2019-09-04 03:36:49 EDT
(In reply to Yoann  Rodiere from comment #3)
> org.eclipse.jdt:ecj:3.18.0 is not available on Maven central.

It is now, but the result is the same with 3.18.0: https://travis-ci.com/yrodiere/eclipse-bugs/jobs/230982845
Comment 5 Stephan Herrmann CLA 2019-09-04 16:06:28 EDT
The problem occurs since ecj version 3.6.0.

JLS §6.6.1 is very clear on this:

"Otherwise, the member or constructor is declared private, and access is permitted if and only if it occurs within the body of the top level type (§7.6) that encloses the declaration of the member or constructor."

In fact we allow the following definition of a member of PrivateConstantBug:

	String myConst = InnerClass.CONSTANT;

Thus the bug is specific to field references used inside an annotation.
Comment 6 Eclipse Genie CLA 2021-08-25 16:57:07 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 7 Yoann Rodiere CLA 2021-09-01 10:09:29 EDT
The problem is still present in ECJ 3.26.0.

With the attached reproducer:

> mvn clean install -Decj.version=3.26.0


> [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:testCompile (default-testCompile) on project jdk-bugs: Compilation failure
> [ERROR] /home/yrodiere/workspaces/testcases/eclipse-playground/src/test/java/org/hibernate/eclipse/bugs/privateconstant/PrivateConstantBug.java:[10,292] The field PrivateConstantBug.InnerClass.CONSTANT is not visible
Comment 8 Jay Arthanareeswaran CLA 2021-09-08 07:28:36 EDT
The code that causes trouble is in Scope#findField() which for some reason considers "insideTypeAnnotations" in determining whether or not the invocation site can see/access the field.

This code was introduced by bug 316456 first.

If you are wondering why insideTypeAnnotations is true, then that seems to be the effect of SourceTypeBinding.initAnnotationTagBits(). This was more recently added for bug 551426. 

Stephan, probably you have some idea about this?
Comment 9 Stephan Herrmann CLA 2021-09-09 05:14:57 EDT
Clarification regarding the name insideTypeAnnotations: this is not about TYPE_USE annotations, but about an annotation on a type declaration.

In bug 316456 we had that problem that a field reference inside a member-value pair of such an annotation could illegally see private members of the type being annotated. Illegal, because the annotation is not "within" the type, where private members are visible.

Perhaps, all this could have been resolved by resolving the annotation in the enclosing scope not the current class scope - but then I'm not sure if that would break legal access to non-private inherited members.

So, it was decided to introduce this flag, which in a way signals, we're in this type, but not within its body.

Considering insideTypeAnnotations within Scope.findField() is the original business of bug 316456, but I see us jumping to a wrong conclusion: if the flag is true, we completely ignore currentType and directly jump to the package assuming that the field must be visible within the package. 

This is wrong in the case of a nested type. Before consulting visibility to the package we must check visibility to the enclosing type.

One question remains: is the enclosing type relevant for static and non-static nested types alike? 


I don't see bug 551426 being relevant here, it just "re-structured the code a bit". The general logic we present before.


As a matter of style: I really would have to dig into the spec to be sure how this pattern (in Scope.findField) is evaluated:
  if (x || y ? s : t)

I suggest to add a pair of parens for readability :)
Comment 10 Eclipse Genie CLA 2023-08-31 03:28:07 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.