Where do empty compilation units belong?

Alex Buckley alex.buckley at oracle.com
Tue Nov 20 20:26:42 UTC 2018


On 11/20/2018 10:54 AM, Alex Buckley wrote:
> From the above, it's hard to understand for which file an error is
> reported by Eclipse. In any case, as Jon indicated, if the file D.java
> is empty, then there is no stream of tokens matching the JLS 7.3
> production `CompilationUnit` and thus there is no compilation unit to
> discuss.

Jon points out that `OrdinaryCompilationUnit` will match an empty stream 
of tokens (I dislike the syntax-driven optionality here, but it's 
longstanding) so the file D.java could be regarded as a compilation unit 
with no package declaration, no import declarations, and no type 
declarations.

Per JLS 7.4.2, such a compilation unit is in an unnamed package, and 
must be associated with an unnamed module. (The "must" somewhat 
conflicts with the "may" in 
https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.3-310)

I would prefer 7.4.2 to say only that a compilation unit with no package 
declarations _and at least one type declaration_ is in an unnamed 
package (and must be associated with an unnamed module; 7.3 should 
enumerate that possibility). A compilation unit with no package 
declarations _and no type declarations_ would be deemed unobservable by 
7.3, and all these questions about what to do with empty files would 
disappear.

What do compiler engineers think?

Alex


More information about the jigsaw-dev mailing list