JDK 10/11 RFR of JDK-8189146: Have use of "var" in 9 and earlier source versions issue a warning
joe darcy
joe.darcy at oracle.com
Tue Jan 9 22:29:31 UTC 2018
Hi Maurizio,
On 1/9/2018 10:49 AM, Maurizio Cimadamore wrote:
> 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.
I think this kind of warning allows a smoother cross-release transition.
>
> 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).
>
>
Webrev updated as suggested:
http://cr.openjdk.java.net/~darcy/8189146.1/
That does emit more warnings than the earlier iteration in ParserTest,
17 rather than 6. As a review aid, the 17 expanded non-raw warnings are
listed below. Under release 10, there are 24 errors generated for the
file, including different error messages.
Thanks,
-Joe
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/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> {
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/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
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/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
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/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
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/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
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/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
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/ParserTest.java:38:
warning: as of release 10, 'var' is a restricted local variable type and
cannot be used for type declarations
var x = null; //illegal
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/ParserTest.java:54:
warning: as of release 10, 'var' is a restricted local variable type and
cannot be used for type declarations
var m() { //illegal
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/ParserTest.java:58:
warning: as of release 10, 'var' is a restricted local variable type and
cannot be used for type declarations
void test2(var x) { //error
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/ParserTest.java:59:
warning: as of release 10, 'var' is a restricted local variable type and
cannot be used for type declarations
List<var> l1; //error
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/ParserTest.java:60:
warning: as of release 10, 'var' is a restricted local variable type and
cannot be used for type declarations
List<? extends var> l2; //error
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/ParserTest.java:61:
warning: as of release 10, 'var' is a restricted local variable type and
cannot be used for type declarations
List<? super var> l3; //error
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/ParserTest.java:63:
warning: as of release 10, 'var' is a restricted local variable type and
cannot be used for type declarations
Function<var, String> f = (var x2) -> ""; //error
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/ParserTest.java:63:
warning: as of release 10, 'var' is a restricted local variable type and
cannot be used for type declarations
Function<var, String> f = (var x2) -> ""; //error
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/ParserTest.java:64:
warning: as of release 10, 'var' is a restricted local variable type and
cannot be used for type declarations
} catch (var ex) { } //error
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/ParserTest.java:68:
warning: as of release 10, 'var' is a restricted local variable type and
cannot be used for type declarations
boolean b1 = o instanceof var; //error
^
/home/darcy/JDK/10/hg/test/langtools/tools/javac/lvti/ParserTest.java:69:
warning: as of release 10, 'var' is a restricted local variable type and
cannot be used for type declarations
Object o2 = (var)o; //error
^
17 warnings
More information about the compiler-dev
mailing list