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

Definitely unassigned field can be accessed

    Details

    • Subcomponent:
    • Resolved In Build:
      b13
    • Verification:
      Verified

      Backports

        Description

        The following erroneous code compiles on JDK8:

        public class Test {
          final int y;
          final int z = this.y;
          { y = 1; }
        }

        If z is instead initialized to 'y', then the appropriate error is given ("variable y might not have been initialized").

        This may be related to a javac change for 7004835, whereby 'this.y' (not just 'y') is an "access" to a variable. Of course, since 'this.y' is an access, and the y variable is definitely unassigned at that point, an error is due.

          Issue Links

            Activity

            Hide
            pgovereau Paul Govereau (Inactive) added a comment - - edited
            Here are two more examples which compile.
            How should they behave?

            public class Test {
                final Test t = new Test();
                final int y;
                final int x = (t.y == 1 ? this : t).y;
                { y = 1; }
            }

            public class Test {
                final int y;
                { y = 1; }
                final int x = y;
            }
            Show
            pgovereau Paul Govereau (Inactive) added a comment - - edited Here are two more examples which compile. How should they behave? public class Test {     final Test t = new Test();     final int y;     final int x = (t.y == 1 ? this : t).y;     { y = 1; } } public class Test {     final int y;     { y = 1; }     final int x = y; }
            Hide
            abuckley Alex Buckley added a comment - - edited
            The point to remember is that JLS 16 traditionally prohibited access to a blank final field via a simple name, but said nothing about [the equally dangerous] access via a primary expression or qualified name. Those formulations are "too hard" to analyze, so they slide by. So it is with the first example, where (t.y == 1 ? this : t).y is "too hard" despite evaluating to this.y which should be an error if written explicitly.

            The second one should compile. y is definitely assigned after the instance initializer, and hence before the assignment to x.
            Show
            abuckley Alex Buckley added a comment - - edited The point to remember is that JLS 16 traditionally prohibited access to a blank final field via a simple name, but said nothing about [the equally dangerous] access via a primary expression or qualified name. Those formulations are "too hard" to analyze, so they slide by. So it is with the first example, where (t.y == 1 ? this : t).y is "too hard" despite evaluating to this.y which should be an error if written explicitly. The second one should compile. y is definitely assigned after the instance initializer, and hence before the assignment to x.
            Hide
            jlahoda Jan Lahoda added a comment -
            Just to double check, it is my reading of the JLS that this should be rejected as well:

            public class DU {
                 private final int y;
                 {
                     System.out.println(this.y);
                     y = 0;
                 }
            }

            Is that correct?
            Show
            jlahoda Jan Lahoda added a comment - Just to double check, it is my reading of the JLS that this should be rejected as well: public class DU {      private final int y;      {          System.out.println(this.y);          y = 0;      } } Is that correct?
            Hide
            abuckley Alex Buckley added a comment -
            Yes. Within a constructor, instance initializer, or instance variable initializer, the DU/DA analysis for 'this.y' should be the same as for 'y'.
            Show
            abuckley Alex Buckley added a comment - Yes. Within a constructor, instance initializer, or instance variable initializer, the DU/DA analysis for 'this.y' should be the same as for 'y'.
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk8u/jdk8u-dev/langtools/rev/12f99d1f23d9
            User: vromero
            Date: 2014-04-30 22:31:25 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk8u/jdk8u-dev/langtools/rev/12f99d1f23d9 User: vromero Date: 2014-04-30 22:31:25 +0000
            Hide
            larbouzo Leonid Arbuzov added a comment - - edited
            Very old JCK tests starts failing now under jdk9 nightly:

               lang/ICLS/icls112/icls11291m1/icls11291m1
               lang/ICLS/icls112/icls11291m2/icls11291m2

            ....
                final String i1, i2, i3;
            ...
                {
            // Definite assignment restrictions do not apply to "this.i2".
            itrace += "/i2=" + this.i2 + "/i2init=" + this.i2init;

            The JCK tests are not changed since 1998.
            What spec version does the change apply to? Covered by any CCC requests?
            Show
            larbouzo Leonid Arbuzov added a comment - - edited Very old JCK tests starts failing now under jdk9 nightly:    lang/ICLS/icls112/icls11291m1/icls11291m1    lang/ICLS/icls112/icls11291m2/icls11291m2 ....     final String i1, i2, i3; ...     { // Definite assignment restrictions do not apply to "this.i2". itrace += "/i2=" + this.i2 + "/i2init=" + this.i2init; The JCK tests are not changed since 1998. What spec version does the change apply to? Covered by any CCC requests?
            Hide
            abuckley Alex Buckley added a comment - - edited
            This bug corresponds to spec issue JDK-7004835, which was actioned in JLS7 (see https://jcp.org/aboutJava/communityprocess/maintenance/JLS/JLS-JavaSE7-ChangeLog.html). A CCC request to change the visible behavior of javac is expected.
            Show
            abuckley Alex Buckley added a comment - - edited This bug corresponds to spec issue JDK-7004835 , which was actioned in JLS7 (see https://jcp.org/aboutJava/communityprocess/maintenance/JLS/JLS-JavaSE7-ChangeLog.html) . A CCC request to change the visible behavior of javac is expected.
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/rev/12f99d1f23d9
            User: lana
            Date: 2014-05-05 17:28:40 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/rev/12f99d1f23d9 User: lana Date: 2014-05-05 17:28:40 +0000
            Hide
            mkhramov Maksim Khramov (Inactive) added a comment -
            Verified
            Show
            mkhramov Maksim Khramov (Inactive) added a comment - Verified

              People

              • Assignee:
                pgovereau Paul Govereau (Inactive)
                Reporter:
                abuckley Alex Buckley
              • Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: