This commit is contained in:
Tim Bell 2008-08-10 18:36:19 -07:00
commit 52da428ead
22 changed files with 387 additions and 23 deletions

View File

@ -87,7 +87,7 @@ public class Bark extends Log {
context.put(barkKey, this);
// register additional resource bundle for APT messages.
Messages aptMessages = new Messages(Messages.getDefaultBundle());
Messages aptMessages = Messages.instance(context);
aptMessages.add("com.sun.tools.apt.resources.apt");
aptDiags = new JCDiagnostic.Factory(aptMessages, "apt");

View File

@ -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

View File

@ -305,6 +305,11 @@ public class Types {
else if (t.tag == TYPEVAR) {
return isSubtypeUnchecked(t.getUpperBound(), s, warn);
}
else if (s.tag == UNDETVAR) {
UndetVar uv = (UndetVar)s;
if (uv.inst != null)
return isSubtypeUnchecked(t, uv.inst, warn);
}
else if (!s.isRaw()) {
Type t2 = asSuper(t, s.tsym);
if (t2 != null && t2.isRaw()) {

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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);
@ -828,6 +831,9 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
// Save class environment for later member enter (2) processing.
halfcompleted.append(env);
// Mark class as not yet attributed.
c.flags_field |= UNATTRIBUTED;
// If this is a toplevel-class, make sure any preceding import
// clauses have been seen.
if (c.owner.kind == PCK) {
@ -835,9 +841,6 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
todo.append(env);
}
// Mark class as not yet attributed.
c.flags_field |= UNATTRIBUTED;
if (c.owner.kind == TYP)
c.owner.complete();

View File

@ -27,6 +27,7 @@ package com.sun.tools.javac.main;
import java.io.*;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.MissingResourceException;
@ -1185,14 +1186,16 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
* are processed through attribute and flow before subtypes are translated.
*/
class ScanNested extends TreeScanner {
Set<Env<AttrContext>> dependencies = new HashSet<Env<AttrContext>>();
Set<Env<AttrContext>> dependencies = new LinkedHashSet<Env<AttrContext>>();
public void visitClassDef(JCClassDecl node) {
Type st = types.supertype(node.sym.type);
if (st.tag == TypeTags.CLASS) {
ClassSymbol c = st.tsym.outermostClass();
Env<AttrContext> stEnv = enter.getEnv(c);
if (stEnv != null && env != stEnv)
dependencies.add(stEnv);
if (stEnv != null && env != stEnv) {
if (dependencies.add(stEnv))
scan(stEnv.tree);
}
}
super.visitClassDef(node);
}
@ -1204,6 +1207,11 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
flow(attribute(dep));
}
//We need to check for error another time as more classes might
//have been attributed and analyzed at this stage
if (errorCount() > 0)
return;
if (verboseCompilePolicy)
log.printLines(log.noticeWriter, "[desugar " + env.enclClass.sym + "]");

View File

@ -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=\

View File

@ -0,0 +1,39 @@
/*
* 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 6734819
* @summary Javac performs flows analysis on already translated classes
* @author Maurizio Cimadamore
*
* @compile/ref=T6734819a.out -XDrawDiagnostics -Xlint:all -XDverboseCompilePolicy T6734819a.java
*/
class Y extends W {}
class W extends Z {}
class Z {
void m(Z z) {
W w = (W)z;
}
}

View File

@ -0,0 +1,12 @@
[attribute Y]
[flow Y]
[attribute W]
[flow W]
[attribute Z]
[flow Z]
[desugar Y]
[generate code Y]
[desugar W]
[generate code W]
[desugar Z]
[generate code Z]

View File

@ -0,0 +1,35 @@
/*
* 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 6734819
* @summary Javac performs flows analysis on already translated classes
* @author Maurizio Cimadamore
*
* @compile/ref=T6734819b.out -XDrawDiagnostics -Xlint:all -XDverboseCompilePolicy T6734819b.java
*/
class A extends B {}
class B {
class C extends B {}
}

View File

@ -0,0 +1,9 @@
[attribute A]
[flow A]
[attribute B]
[flow B]
[desugar A]
[generate code A]
[desugar B]
[generate code B]
[generate code B]

View File

@ -0,0 +1,40 @@
/*
* 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 6734819
* @summary Javac performs flows analysis on already translated classes
* @author Maurizio Cimadamore
*
* @compile/fail/ref=T6734819c.out -XDrawDiagnostics -Xlint:all -XDverboseCompilePolicy T6734819c.java
*/
class Y extends W {}
class W extends Z {}
class Z {
void m(Z z) {
return;
W w = (W)z;
}
}

View File

@ -0,0 +1,8 @@
[attribute Y]
[flow Y]
[attribute W]
[flow W]
[attribute Z]
[flow Z]
T6734819c.java:38:11: compiler.err.unreachable.stmt
1 error

View 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;
}

View 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;
}

View File

@ -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

View File

@ -0,0 +1,38 @@
/*
* 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 6718364
* @summary inference fails when a generic method is invoked with raw arguments
* @compile/ref=T6718364.out -XDstdout -XDrawDiagnostics -Xlint:unchecked T6718364.java
*/
class T6718364 {
class X<T> {}
public <T> void m(X<T> x, T t) {}
public void test() {
m(new X<X<Integer>>(), new X());
}
}

View File

@ -0,0 +1,3 @@
T6718364.java:36:32: compiler.warn.prob.found.req: (- compiler.misc.unchecked.assign), T6718364.X, T6718364.X<java.lang.Integer>
T6718364.java:36:10: compiler.warn.unchecked.meth.invocation.applied: <T>m(T6718364.X<T>,T), T6718364, , T6718364.X<T6718364.X<java.lang.Integer>>,T6718364.X
2 warnings

View File

@ -0,0 +1,31 @@
/*
* 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 6695838
* @summary javac does not detect cyclic inheritance involving static inner classes after import clause
* @author Maurizio Cimadamore
*
* @compile/fail a/FooInterface.java
*/

View File

@ -0,0 +1,29 @@
/*
* 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.
*/
package a;
class Foo implements FooInterface {
public static interface InnerInterface {}
}

View File

@ -0,0 +1,29 @@
/*
* 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.
*/
package a;
import a.Foo.InnerInterface;
public interface FooInterface extends Foo.InnerInterface {}