On 2/26/2016 8:37 AM, Georgiy Rakov wrote:
current spec [1] now contains following assertions related to grammar:
A compilation unit (JLS 7.3) may contain a module declaration, in
which case the filename of the compilation unit is typically
|module-info.java|.
CompilationUnit:
[PackageDeclaration] {ImportDeclaration} {TypeDeclaration}
ModuleDeclaration
These assertions allows to specify any of import, package or type
declarations in any compilation unit, for instance module-info.java is
allowed to contain any of the mentioned declarations. However currently
javac in the latest jigsaw build [2] reports an error on such cases
provided they are compiled in module mode. For example if we have
following directory structure:
mod\module-info.java:
module mod {
exports pkg;
}
mod\pkg\module-info.java:
package pkg;
class C {
}
then compiling it by following command line with javac from [2]:
javac -modulesourcepath . mod\module-info.java mod\pkg\module-info.java
causes following output:
mod\pkg\module-info.java:1: error: expected 'module'
package pkg;
^
1 error
javac is merely choosing to implement the rule at the end of JLS 7.6
that a type declaration (optionally preceded by package/import
declarations) must be provided in a suitably named file.
Perhaps I should say "a variant of the rule" because 7.6 as written
concerns a public type and your example has a package-access type.
Still, bottom line, javac is free to require that a compilation unit
which starts with a package declaration _must not_ be in a file called
foo-bar.java -- the hyphen indicates a name that can't possibly align
with the type declared in the compilation unit.
The error message for mod\pkg\module-info.java could be a bit more
helpful, but that's a quality-of-implementation detail.
Conversely, a compilation unit that contains a module declaration _may_
be in a file called module-info.java, or in a file called foo-bar.java,
or in a file called mod_decl.JAV. The "typically" in [1] is meant to
indicate that the sub-clause on filename is non-normative. This is akin
to how a compilation unit that contains a package-access type
declaration for class C _may_ be in a file D.java.
Alex