Where do empty compilation units belong?

Jonathan Gibbons jonathan.gibbons at oracle.com
Wed Nov 21 00:44:56 UTC 2018



On 11/20/2018 12:26 PM, Alex Buckley wrote:
> 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

I think that would be useful but not necessary, since we are dealing 
with the way the host system finds/handles compilation units.

It is the case that javac accepts and effectively ignores empty files; 
that is nothing new or specific to the module system or 
module-source-path. javac doesn't go out of its way to ignore them; it's 
more that there is nothing in an empty file that needs to be checked.  
Yes, 7.4.2 says that an empty file is in an unnamed package in an 
unnamed module,  and yes you can say it is weird to allow them in a 
hierarchy on the module source path, but it would be equally weird to 
forbid empty files in subdirectories of directories on the source path 
or module source path. javac does reject non-empty compilation units 
without a package declaration that are associated with a named module.

Generally, in addition to the various suggestions in JLS 7 regarding 
restrictions that may be enforced when packages are stored in a file 
system, javac uses the following rules in being a "host system" (7.2)

  * Types that are looked up implicitly on the source path or module
    source path must be in an appropriately named directory.

  * Module membership is determined by examining the search path for
    each module on the module source path to determine which search path
    contains a directory that indirectly contains any given file. If
    there is only one source hierarchy for each module, that simplifies
    to finding the module declaration in the enclosing directory on the
    module source path.

-- Jon





More information about the jigsaw-dev mailing list