RFR(14): JDK-8227010: Error recovery after local variable redeclaration can be improved
Jan Lahoda
jan.lahoda at oracle.com
Mon Jul 1 15:24:09 UTC 2019
Hi,
Consider code like this:
---
public class T {
{
String v = "";
Integer v = 0;
System.err.println(v.byteValue());
}
}
---
This produces errors like this:
---
$ javac T.java
T.java:4: error: variable v is already defined in instance initializer
of class T
Integer v = 0;
^
T.java:5: error: cannot find symbol
System.err.println(v.byteValue());
^
symbol: method byteValue()
location: variable v of type String
2 errors
---
The second error feels superfluous, as the last declaration for "v"
declares it as "Integer". The reason is that non-unique (local)
variables are not added into the Scope, and hence will never be looked
up. The proposed patch is to add non-unique local variables (but not
other Symbols) to the Scope, which should hide the previous declaration.
The first error will remain untouched.
JBS: https://bugs.openjdk.java.net/browse/JDK-8227010
Webrev: http://cr.openjdk.java.net/~jlahoda/8227010/webrev.00/
How does this look?
Thanks,
Jan
More information about the compiler-dev
mailing list