[records] SafeVarargs

Tagir Valeev amaembo at gmail.com
Sat Dec 28 05:39:08 UTC 2019


Hello!

Current spec draft says [1] says:
If the declared type of a variable arity record component has a
non-reifiable element type (4.7), then a compile-time unchecked
warning occurs for the declaration of the variable arity record
component, unless the record type is annotated with @SafeVarargs
(9.6.4.7) or the warning is suppressed by @SuppressWarnings (9.6.4.5).

However, currently SafeVarargs is applicable only to constructors and
methods [2]. This code is not compilable (14-ea+28-1366):

import java.util.List;

@SafeVarargs
record RecordTest(List<String>... list) {
}

>"C:\Program Files\Java\jdk-14\bin\javac.exe" --enable-preview --release=14 Test.java
Test.java:3: error: annotation type not applicable to this kind of declaration
@SafeVarargs
^
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Test.java uses preview language features.
Note: Recompile with -Xlint:preview for details.

SafeVarargs could be extended to be applicable to TYPE, though it will
allow to put it (meaninglessly) to non-records as well (or it should
be explicitly prohibited). Another possibility is to allow annotating
records with annotations applicable to CONSTRUCTOR and propagate these
annotations to the implicit canonical constructor.

Finally, spec chapter 9.6.4.7. @SafeVarargs need adjustments, as
currently it says:

> The annotation type SafeVarargs, when used to annotate a method or constructor declaration
(should be added "or record type")
> Since @SafeVarargs is only applicable to static methods, final and/or private instance methods, and constructors
(should be added "and record types")

I personally think that record varargness should be dropped. It adds
too many adjustments in spec and tools code while useful outcome is
very little. Let's consider dropping it in Java 15!

With best regards,
Tagir Valeev.

[1] http://cr.openjdk.java.net/~gbierman/jep359/jep359-20191125/specs/records-jls.html#jls-8.10.1
[2] http://hg.openjdk.java.net/jdk/jdk/file/7c2236ea739e/src/java.base/share/classes/java/lang/SafeVarargs.java#l92
[3] https://docs.oracle.com/javase/specs/jls/se13/html/jls-9.html#jls-9.6.4.7


More information about the amber-spec-experts mailing list