mirror of
https://github.com/openjdk/jdk.git
synced 2026-03-19 04:13:07 +00:00
6676362: Spurious forward reference error with final var + instance variable initializer
Some javac forward reference errors aren't compliant with the JLS Reviewed-by: jjg
This commit is contained in:
parent
fe12031643
commit
a7cdf34685
@ -923,14 +923,7 @@ public abstract class Symbol implements Element {
|
||||
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
|
||||
|
||||
@ -730,9 +730,8 @@ public class Attr extends JCTree.Visitor {
|
||||
// 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 @@ public class Attr extends JCTree.Visitor {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -66,6 +66,11 @@ public class AttrContext {
|
||||
*/
|
||||
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 @@ public class AttrContext {
|
||||
info.varArgs = varArgs;
|
||||
info.tvars = tvars;
|
||||
info.lint = lint;
|
||||
info.enclVar = enclVar;
|
||||
return info;
|
||||
}
|
||||
|
||||
|
||||
@ -627,8 +627,11 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
|
||||
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);
|
||||
|
||||
@ -200,6 +200,10 @@ compiler.err.illegal.forward.ref=\
|
||||
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=\
|
||||
|
||||
36
langtools/test/tools/javac/ForwardReference/T6676362a.java
Normal file
36
langtools/test/tools/javac/ForwardReference/T6676362a.java
Normal file
@ -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;
|
||||
}
|
||||
36
langtools/test/tools/javac/ForwardReference/T6676362b.java
Normal file
36
langtools/test/tools/javac/ForwardReference/T6676362b.java
Normal file
@ -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;
|
||||
}
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user