Summary
The JLS disallows the use of type annotations for variables declared using 'var'. javac is unfortunately not enforcing this limitation in all cases, and this ticket strives to fix that.
Problem
More specifically, since introduction of var in JDK 10, the type annotations have been disallowed on local variables, enhanced for statements variables and try-with-resources variables. When bindinding patterns were introduced in JDK 16, the restriction was enhanced to binding patterns in the JLS. And the restriction has been enhanced to lambda parameters declared using 'var' in JDK/JLS 20 (JDK-8295807). javac is sadly not enforcing this restriction for:
- lambda var parameters
- enhanced for statements variables
- partly for binding patterns (compile-time error is reported, but a wrong kind of error)
Solution
javac will properly enforce the restriction on present of type variables, and use of type variables in combination with var will lead to a compile-time error.
Specification
No change in an existing specification. javac behavior will be adjusted to more closely match the JLS. The proposed behavior is:
- for lambda var parameters, type annotations in combination with
varwill produce a compile-time error when--source >= 20/--release >= 20. - for enhanced for statement variables, type annotations in combination with
varwill produce a compile-time error for all source versions - for binding patterns, the proper error will be added for type annotations in combination with
var
- csr of
-
JDK-8371683 TYPE_USE annotation on var lambda parameter should be rejected
-
- Open
-