JDK 10/11 RFR of JDK-8189146: Have use of "var" in 9 and earlier source versions issue a warning
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Tue Jan 9 18:49:19 UTC 2018
Hi Joe,
I'm a bit unsure about having a retroactive warning in JDK 10 - e.g. I'm
not sure how useful that would be, but I get the spirit of what you're
getting at.
In terms of code, I think your warning should go inside the
isRestrictedLocalVarTypeName(Name), which is called in a few places (not
just the one you have touched). For instance, I don't think your patch
will cover this case:
List<var> l = null;
So, the code for isRestrictedLocalVarTypeName(Name) is currently:
return allowLocalVariableTypeInference && name == names.var;
I think it should be updated to:
if (name == names.var) {
if (allowLocalVariableTypeInference) {
return true;
} else {
warning(pos, "var.not.allowed", name);
}
}
return false;
Of course you need to enhance the method to take an extra position
argument (pos).
Maurizio
On 09/01/18 18:28, joe darcy wrote:
> Hello,
>
> When the possibility of "_" being removed from the future set of valid
> identifiers was determined, javac was updated to issue a warning to
> that effect in JDK 8. Underscore was removed from the set of valid
> identifiers as of JDK 9.
>
> With local variable type inference, the name "var" should be given
> similar treatment in source versions 9 and earlier. Please review the
> webrev which implements this:
>
> http://cr.openjdk.java.net/~darcy/8189146.0/
>
> As written, the patch warns for a type or type variable named "var";
> it would be possible to expand the patch to warn about other uses of
> "var" that would conflict with the JDK 10 feature. As a aid to
> reviewing, the non-raw diagnostic output from
> test/langtools/tools/javac/lvti/ParserTest.java under --release 9 is
> listed below.
>
> IMO It would be preferable to issue this kind of warning in the
> context of an -Xlint:future category (JDK-8189145), but until that
> feature is developed, it is still better to issue the warning than to
> not issue it.
>
> The webrev as presented is based off of JDK 11. Upon a successful code
> review, I will explore getting the change into JDK 10 as an RFE and
> make any needed adjustments to rebase the patch. If that is not
> fruitful, I'll push the change to JDK 11.
>
> Thanks,
>
> -Joe
>
>
> ParserTest.java:14: warning: as of release 10, 'var' is a restricted
> local variable type and cannot be used for type declarations
> class ParserTest<var> {
> ^
> ParserTest.java:16: warning: as of release 10, 'var' is a restricted
> local variable type and cannot be used for type declarations
> static class var { } //illegal
> ^
> ParserTest.java:20: warning: as of release 10, 'var' is a restricted
> local variable type and cannot be used for type declarations
> interface var { } //illegal
> ^
> ParserTest.java:24: warning: as of release 10, 'var' is a restricted
> local variable type and cannot be used for type declarations
> enum var { } //illegal
> ^
> ParserTest.java:28: warning: as of release 10, 'var' is a restricted
> local variable type and cannot be used for type declarations
> @interface var { } //illegal
> ^
> ParserTest.java:36: warning: as of release 10, 'var' is a restricted
> local variable type and cannot be used for type declarations
> static class var extends RuntimeException { } //illegal
> ^
> 6 warnings
>
More information about the compiler-dev
mailing list