RFR(14): JDK-8227010: Error recovery after local variable redeclaration can be improved

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Tue Jul 2 13:19:01 UTC 2019


Looks good!

Maurizio

On 01/07/2019 16:24, Jan Lahoda wrote:
> 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