6676362: Spurious forward reference error with final var + instance variable initializer
authormcimadamore
Fri, 08 Aug 2008 17:43:24 +0100
changeset 1045 56f6e84f7825
parent 1044 7016e624ec3a
child 1046 e395ce284cbf
6676362: Spurious forward reference error with final var + instance variable initializer Summary: Some javac forward reference errors aren't compliant with the JLS Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java
langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
langtools/src/share/classes/com/sun/tools/javac/comp/AttrContext.java
langtools/src/share/classes/com/sun/tools/javac/comp/MemberEnter.java
langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/test/tools/javac/ForwardReference/T6676362a.java
langtools/test/tools/javac/ForwardReference/T6676362b.java
langtools/test/tools/javac/enum/forwardRef/T6425594.out
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java	Fri Aug 08 17:38:20 2008 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java	Fri Aug 08 17:43:24 2008 +0100
@@ -923,14 +923,7 @@
                 public Object call() {
                     JavaFileObject source = log.useSource(env.toplevel.sourcefile);
                     try {
-                        // In order to catch self-references, we set
-                        // the variable's declaration position to
-                        // maximal possible value, effectively marking
-                        // the variable as undefined.
-                        int pos = VarSymbol.this.pos;
-                        VarSymbol.this.pos = Position.MAXPOS;
                         Type itype = attr.attribExpr(initializer, env, type);
-                        VarSymbol.this.pos = pos;
                         if (itype.constValue() != null)
                             return attr.coerce(itype, type).constValue();
                         else
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Aug 08 17:38:20 2008 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Aug 08 17:43:24 2008 +0100
@@ -730,9 +730,8 @@
                     // In order to catch self-references, we set the variable's
                     // declaration position to maximal possible value, effectively
                     // marking the variable as undefined.
-                    v.pos = Position.MAXPOS;
+                    initEnv.info.enclVar = v;
                     attribExpr(tree.init, initEnv, v.type);
-                    v.pos = tree.pos;
                 }
             }
             result = tree.type = v.type;
@@ -2198,18 +2197,19 @@
             // This check applies only to class and instance
             // variables.  Local variables follow different scope rules,
             // and are subject to definite assignment checking.
-            if (v.pos > tree.pos &&
+            if ((env.info.enclVar == v || v.pos > tree.pos) &&
                 v.owner.kind == TYP &&
                 canOwnInitializer(env.info.scope.owner) &&
                 v.owner == env.info.scope.owner.enclClass() &&
                 ((v.flags() & STATIC) != 0) == Resolve.isStatic(env) &&
                 (env.tree.getTag() != JCTree.ASSIGN ||
                  TreeInfo.skipParens(((JCAssign) env.tree).lhs) != tree)) {
-
+                String suffix = (env.info.enclVar == v) ?
+                                "self.ref" : "forward.ref";
                 if (!onlyWarning || isStaticEnumField(v)) {
-                    log.error(tree.pos(), "illegal.forward.ref");
+                    log.error(tree.pos(), "illegal." + suffix);
                 } else if (useBeforeDeclarationWarning) {
-                    log.warning(tree.pos(), "forward.ref", v);
+                    log.warning(tree.pos(), suffix, v);
                 }
             }
 
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/AttrContext.java	Fri Aug 08 17:38:20 2008 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/AttrContext.java	Fri Aug 08 17:43:24 2008 +0100
@@ -66,6 +66,11 @@
      */
     Lint lint;
 
+    /** The variable whose initializer is being attributed
+     * useful for detecting self-references in variable initializers
+     */
+    Symbol enclVar = null;
+
     /** Duplicate this context, replacing scope field and copying all others.
      */
     AttrContext dup(Scope scope) {
@@ -77,6 +82,7 @@
         info.varArgs = varArgs;
         info.tvars = tvars;
         info.lint = lint;
+        info.enclVar = enclVar;
         return info;
     }
 
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/MemberEnter.java	Fri Aug 08 17:38:20 2008 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/MemberEnter.java	Fri Aug 08 17:43:24 2008 +0100
@@ -627,8 +627,11 @@
         tree.sym = v;
         if (tree.init != null) {
             v.flags_field |= HASINIT;
-            if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS)
-                v.setLazyConstValue(initEnv(tree, env), log, attr, tree.init);
+            if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS) {
+                Env<AttrContext> initEnv = getInitEnv(tree, env);
+                initEnv.info.enclVar = v;
+                v.setLazyConstValue(initEnv(tree, initEnv), log, attr, tree.init);
+            }
         }
         if (chk.checkUnique(tree.pos(), v, enclScope)) {
             chk.checkTransparentVar(tree.pos(), v, enclScope);
--- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Aug 08 17:38:20 2008 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Aug 08 17:43:24 2008 +0100
@@ -200,6 +200,10 @@
     illegal forward reference
 compiler.warn.forward.ref=\
     reference to variable ''{0}'' before it has been initialized
+compiler.err.illegal.self.ref=\
+    self-reference in initializer
+compiler.warn.self.ref=\
+    self-reference in initializer of variable ''{0}''
 compiler.err.illegal.generic.type.for.instof=\
     illegal generic type for instanceof
 compiler.err.illegal.initializer.for.type=\
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/ForwardReference/T6676362a.java	Fri Aug 08 17:43:24 2008 +0100
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6676362
+ * @summary Spurious forward reference error with final var + instance variable initializer
+ * @author Maurizio Cimadamore
+ *
+ * @compile T6676362a.java
+ */
+
+public class T6676362a {
+    Object o = new Object() {Object m() {return o2;}};
+    final Object o2 = o;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/ForwardReference/T6676362b.java	Fri Aug 08 17:43:24 2008 +0100
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6676362
+ * @summary Spurious forward reference error with final var + instance variable initializer
+ * @author Maurizio Cimadamore
+ *
+ * @compile T6676362b.java
+ */
+
+public class T6676362b {
+    static final int i1 = T6676362b.i2; //legal - usage is not via simple name
+    static final int i2 = i1;
+}
--- a/langtools/test/tools/javac/enum/forwardRef/T6425594.out	Fri Aug 08 17:38:20 2008 +0100
+++ b/langtools/test/tools/javac/enum/forwardRef/T6425594.out	Fri Aug 08 17:43:24 2008 +0100
@@ -1,4 +1,4 @@
-T6425594.java:10:28: compiler.warn.forward.ref: x
+T6425594.java:10:28: compiler.warn.self.ref: x
 T6425594.java:11:26: compiler.err.illegal.forward.ref
 1 error
 1 warning