7086595: Error message bug: name of initializer is 'null'

Implementation of MethodSymbol.location() should take into account static/instance initializers

Reviewed-by: jjg
This commit is contained in:
Maurizio Cimadamore 2011-09-13 14:14:57 +01:00
parent c1efbbc362
commit 8db2f8de59
17 changed files with 210 additions and 31 deletions

View File

@ -103,6 +103,8 @@ public class Kinds {
VAL("kindname.value"),
METHOD("kindname.method"),
CLASS("kindname.class"),
STATIC_INIT("kindname.static.init"),
INSTANCE_INIT("kindname.instance.init"),
PACKAGE("kindname.package");
private String name;
@ -170,9 +172,11 @@ public class Kinds {
return KindName.CONSTRUCTOR;
case METHOD:
case STATIC_INIT:
case INSTANCE_INIT:
return KindName.METHOD;
case STATIC_INIT:
return KindName.STATIC_INIT;
case INSTANCE_INIT:
return KindName.INSTANCE_INIT;
default:
if (sym.kind == VAL)

View File

@ -311,7 +311,7 @@ public abstract class Printer implements Type.Visitor<String, Locale>, Symbol.Vi
@Override
public String visitMethodSymbol(MethodSymbol s, Locale locale) {
if ((s.flags() & BLOCK) != 0) {
if (s.isStaticOrInstanceInit()) {
return s.owner.name.toString();
} else {
String ms = (s.name == s.name.table.names.init)

View File

@ -149,7 +149,8 @@ public abstract class Symbol implements Element {
* the default package; otherwise, the owner symbol is returned
*/
public Symbol location() {
if (owner.name == null || (owner.name.isEmpty() && owner.kind != PCK && owner.kind != TYP)) {
if (owner.name == null || (owner.name.isEmpty() &&
(owner.flags() & BLOCK) == 0 && owner.kind != PCK && owner.kind != TYP)) {
return null;
}
return owner;
@ -1299,10 +1300,17 @@ public abstract class Symbol implements Element {
return ElementKind.CONSTRUCTOR;
else if (name == name.table.names.clinit)
return ElementKind.STATIC_INIT;
else if ((flags() & BLOCK) != 0)
return isStatic() ? ElementKind.STATIC_INIT : ElementKind.INSTANCE_INIT;
else
return ElementKind.METHOD;
}
public boolean isStaticOrInstanceInit() {
return getKind() == ElementKind.STATIC_INIT ||
getKind() == ElementKind.INSTANCE_INIT;
}
public Attribute getDefaultValue() {
return defaultValue;
}

View File

@ -306,7 +306,16 @@ public class Check {
*/
void duplicateError(DiagnosticPosition pos, Symbol sym) {
if (!sym.type.isErroneous()) {
log.error(pos, "already.defined", sym, sym.location());
Symbol location = sym.location();
if (location.kind == MTH &&
((MethodSymbol)location).isStaticOrInstanceInit()) {
log.error(pos, "already.defined.in.clinit", kindName(sym), sym,
kindName(sym.location()), kindName(sym.location().enclClass()),
sym.location().enclClass());
} else {
log.error(pos, "already.defined", kindName(sym), sym,
kindName(sym.location()), sym.location());
}
}
}

View File

@ -68,9 +68,13 @@ compiler.err.abstract.meth.cant.have.body=\
compiler.err.already.annotated=\
{0} {1} has already been annotated
# 0: symbol, 1: symbol
# 0: symbol kind, 1: symbol, 2: symbol kind, 3: symbol
compiler.err.already.defined=\
{0} is already defined in {1}
{0} {1} is already defined in {2} {3}
# 0: symbol kind, 1: symbol, 2: symbol kind, 3: symbol kind, 4: symbol
compiler.err.already.defined.in.clinit=\
{0} {1} is already defined in {2} of {3} {4}
# 0: string
compiler.err.already.defined.single.import=\
@ -1753,6 +1757,12 @@ compiler.misc.kindname.class=\
compiler.misc.kindname.package=\
package
compiler.misc.kindname.static.init=\
static initializer
compiler.misc.kindname.instance.init=\
instance initializer
#####
compiler.misc.no.args=\

View File

@ -412,7 +412,7 @@ public class RichDiagnosticFormatter extends
@Override
public String visitMethodSymbol(MethodSymbol s, Locale locale) {
String ownerName = visit(s.owner, locale);
if ((s.flags() & BLOCK) != 0) {
if (s.isStaticOrInstanceInit()) {
return ownerName;
} else {
String ms = (s.name == s.name.table.names.init)

View File

@ -0,0 +1,32 @@
/*
* @test /nodynamiccopyright/
* @bug 7086595
* @summary Error message bug: name of initializer is 'null'
* @compile/fail/ref=T7086595.out -XDrawDiagnostics T7086595.java
*/
class T7086595 {
String s = "x";
String s = nonExistent;
int foo() {
String s = "x";
String s = nonExistent;
}
static int bar() {
String s = "x";
String s = nonExistent;
}
{
String s = "x";
String s = nonExistent;
}
static {
String s = "x";
String s = nonExistent;
}
}

View File

@ -0,0 +1,11 @@
T7086595.java:11:12: compiler.err.already.defined: kindname.variable, s, kindname.class, T7086595
T7086595.java:11:16: compiler.err.cant.resolve.location: kindname.variable, nonExistent, , , (compiler.misc.location: kindname.class, T7086595, null)
T7086595.java:15:16: compiler.err.already.defined: kindname.variable, s, kindname.method, foo()
T7086595.java:15:20: compiler.err.cant.resolve.location: kindname.variable, nonExistent, , , (compiler.misc.location: kindname.class, T7086595, null)
T7086595.java:20:16: compiler.err.already.defined: kindname.variable, s, kindname.method, bar()
T7086595.java:20:20: compiler.err.cant.resolve.location: kindname.variable, nonExistent, , , (compiler.misc.location: kindname.class, T7086595, null)
T7086595.java:25:16: compiler.err.already.defined.in.clinit: kindname.variable, s, kindname.instance.init, kindname.class, T7086595
T7086595.java:25:20: compiler.err.cant.resolve.location: kindname.variable, nonExistent, , , (compiler.misc.location: kindname.class, T7086595, null)
T7086595.java:30:16: compiler.err.already.defined.in.clinit: kindname.variable, s, kindname.static.init, kindname.class, T7086595
T7086595.java:30:20: compiler.err.cant.resolve.location: kindname.variable, nonExistent, , , (compiler.misc.location: kindname.class, T7086595, null)
10 errors

View File

@ -1,2 +1,2 @@
T6860795.java:10:27: compiler.err.already.defined: x, foo
T6860795.java:10:27: compiler.err.already.defined: kindname.variable, x, kindname.method, foo
1 error

View File

@ -1,2 +1,2 @@
LocalClasses_2.java:15:13: compiler.err.already.defined: Local, foo()
LocalClasses_2.java:15:13: compiler.err.already.defined: kindname.class, Local, kindname.method, foo()
1 error

View File

@ -1,18 +1,18 @@
NestedInnerClassNames.java:16:5: compiler.err.already.defined: NestedInnerClassNames, compiler.misc.unnamed.package
NestedInnerClassNames.java:23:9: compiler.err.already.defined: NestedInnerClassNames.foo, NestedInnerClassNames
NestedInnerClassNames.java:34:9: compiler.err.already.defined: NestedInnerClassNames, compiler.misc.unnamed.package
NestedInnerClassNames.java:45:9: compiler.err.already.defined: NestedInnerClassNames.baz, NestedInnerClassNames
NestedInnerClassNames.java:46:13: compiler.err.already.defined: NestedInnerClassNames.baz.baz, NestedInnerClassNames.baz
NestedInnerClassNames.java:59:9: compiler.err.already.defined: NestedInnerClassNames.foo$bar, NestedInnerClassNames
NestedInnerClassNames.java:76:13: compiler.err.already.defined: NestedInnerClassNames.$bar, NestedInnerClassNames
NestedInnerClassNames.java:90:13: compiler.err.already.defined: NestedInnerClassNames.bar$bar.bar, NestedInnerClassNames.bar$bar
NestedInnerClassNames.java:16:5: compiler.err.already.defined: kindname.class, NestedInnerClassNames, kindname.package, compiler.misc.unnamed.package
NestedInnerClassNames.java:23:9: compiler.err.already.defined: kindname.class, NestedInnerClassNames.foo, kindname.class, NestedInnerClassNames
NestedInnerClassNames.java:34:9: compiler.err.already.defined: kindname.class, NestedInnerClassNames, kindname.package, compiler.misc.unnamed.package
NestedInnerClassNames.java:45:9: compiler.err.already.defined: kindname.class, NestedInnerClassNames.baz, kindname.class, NestedInnerClassNames
NestedInnerClassNames.java:46:13: compiler.err.already.defined: kindname.class, NestedInnerClassNames.baz.baz, kindname.class, NestedInnerClassNames.baz
NestedInnerClassNames.java:59:9: compiler.err.already.defined: kindname.class, NestedInnerClassNames.foo$bar, kindname.class, NestedInnerClassNames
NestedInnerClassNames.java:76:13: compiler.err.already.defined: kindname.class, NestedInnerClassNames.$bar, kindname.class, NestedInnerClassNames
NestedInnerClassNames.java:90:13: compiler.err.already.defined: kindname.class, NestedInnerClassNames.bar$bar.bar, kindname.class, NestedInnerClassNames.bar$bar
NestedInnerClassNames.java:109:5: compiler.err.duplicate.class: NestedInnerClassNames.foo.foo
NestedInnerClassNames.java:19:9: compiler.err.already.defined: NestedInnerClassNames, compiler.misc.unnamed.package
NestedInnerClassNames.java:28:13: compiler.err.already.defined: foo, m2()
NestedInnerClassNames.java:40:13: compiler.err.already.defined: NestedInnerClassNames, compiler.misc.unnamed.package
NestedInnerClassNames.java:52:13: compiler.err.already.defined: baz, m4()
NestedInnerClassNames.java:53:17: compiler.err.already.defined: baz.baz, baz
NestedInnerClassNames.java:67:13: compiler.err.already.defined: foo$bar, m5()
NestedInnerClassNames.java:83:17: compiler.err.already.defined: $bar, m6()
NestedInnerClassNames.java:97:17: compiler.err.already.defined: bar$bar.bar, bar$bar
NestedInnerClassNames.java:19:9: compiler.err.already.defined: kindname.class, NestedInnerClassNames, kindname.package, compiler.misc.unnamed.package
NestedInnerClassNames.java:28:13: compiler.err.already.defined: kindname.class, foo, kindname.method, m2()
NestedInnerClassNames.java:40:13: compiler.err.already.defined: kindname.class, NestedInnerClassNames, kindname.package, compiler.misc.unnamed.package
NestedInnerClassNames.java:52:13: compiler.err.already.defined: kindname.class, baz, kindname.method, m4()
NestedInnerClassNames.java:53:17: compiler.err.already.defined: kindname.class, baz.baz, kindname.class, baz
NestedInnerClassNames.java:67:13: compiler.err.already.defined: kindname.class, foo$bar, kindname.method, m5()
NestedInnerClassNames.java:83:17: compiler.err.already.defined: kindname.class, $bar, kindname.method, m6()
NestedInnerClassNames.java:97:17: compiler.err.already.defined: kindname.class, bar$bar.bar, kindname.class, bar$bar
17 errors

View File

@ -1,5 +1,5 @@
BadTwr.java:13:46: compiler.err.already.defined: r1, main(java.lang.String...)
BadTwr.java:18:20: compiler.err.already.defined: args, main(java.lang.String...)
BadTwr.java:13:46: compiler.err.already.defined: kindname.variable, r1, kindname.method, main(java.lang.String...)
BadTwr.java:18:20: compiler.err.already.defined: kindname.variable, args, kindname.method, main(java.lang.String...)
BadTwr.java:21:13: compiler.err.cant.assign.val.to.final.var: thatsIt
BadTwr.java:26:24: compiler.err.already.defined: name, main(java.lang.String...)
BadTwr.java:26:24: compiler.err.already.defined: kindname.variable, name, kindname.method, main(java.lang.String...)
4 errors

View File

@ -1,2 +1,2 @@
DuplicateResourceDecl.java:12:56: compiler.err.already.defined: c, main(java.lang.String[])
DuplicateResourceDecl.java:12:56: compiler.err.already.defined: kindname.variable, c, kindname.method, main(java.lang.String[])
1 error

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// key: compiler.err.already.defined.in.clinit
class AlreadyDefinedClinit {
static {
int i;
int i;
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// key: compiler.err.already.defined.in.clinit
// key: compiler.misc.kindname.instance.init
// key: compiler.misc.kindname.class
// key: compiler.misc.kindname.variable
// key: compiler.misc.count.error
// key: compiler.err.error
// run: backdoor
class KindnameInstanceInit {
{
int i;
int i;
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// key: compiler.err.already.defined.in.clinit
// key: compiler.misc.kindname.static.init
// key: compiler.misc.kindname.class
// key: compiler.misc.kindname.variable
// key: compiler.misc.count.error
// key: compiler.err.error
// run: backdoor
class KindnameStaticInit {
static {
int i;
int i;
}
}

View File

@ -1,2 +1,2 @@
T6910550d.java:12:14: compiler.err.already.defined: <X>m(X), T6910550d
T6910550d.java:12:14: compiler.err.already.defined: kindname.method, <X>m(X), kindname.class, T6910550d
1 error