Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8204534

javac should reject packages whose names are prefixes of names of uniquely visible packages

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 11
    • tools
    • None
    • source
    • low
    • Hide
      javac will reject some programs that are currently accepted. The compiler won't accept source code with imports that name not-uniquely visible packages whose names are prefixes of uniquely visible packages. To the extent of the current knowledge, such imports must be unused in programs for which the classfiles were produced, and so these imports shouldn't be very common and it is easy to avoid using them. Only programs compiled with -source >= 9 will be affected
      Show
      javac will reject some programs that are currently accepted. The compiler won't accept source code with imports that name not-uniquely visible packages whose names are prefixes of uniquely visible packages. To the extent of the current knowledge, such imports must be unused in programs for which the classfiles were produced, and so these imports shouldn't be very common and it is easy to avoid using them. Only programs compiled with -source >= 9 will be affected
    • Language construct

      Summary

      For type-import-on-demand, javac accepts not-uniquely visible packages whose names are prefixes of names of uniquely visible packages. This contradicts JLS 9 §7.5.2, which requires the package named in type-import-on-demand to be uniquely visible.

      Problem

      javac is not in sync with JLS 9 §7.5.2. In particular, javac accepts imports like:

      import java.*;

      even though package "java" is not uniquely visible.

      Solution

      For programs compiled with -source >= 9, javac will issue an error if the package named in the type-import-on-demand is not uniquely visible, as per JLS 9 §7.5.2. Javac will continue accepting those programs if compiled with -source < 9

      Specification

      From JLS 9 §7.5.2:

      It is a compile-time error if the named package is not uniquely visible to the current module (§7.4.3), or if the named type is not accessible (§6.6).

      From JLS 9 §7.4.3: A package is uniquely visible to a module M if and only if one of the following holds:

      • An ordinary compilation unit associated with M contains a declaration of the package, and M does not read any other module that exports the package to M .
      • No ordinary compilation unit associated with M contains a declaration of the package, and M reads exactly one other module that exports the package to M .

            jlahoda Jan Lahoda
            webbuggrp Webbug Group
            Vicente Arturo Romero Zaldivar
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: