JDK-8236622 : Records: Clarify interaction between records and @SafeVarargs
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 14
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2020-01-02
  • Updated: 2020-04-10
  • Resolved: 2020-04-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 15
15Fixed
Related Reports
Relates :  
Relates :  
Description
Tagir Valeev has proposed the following sensible treatment:

1. Do not allow annotating record type with SafeVarargs
2. If the record type has explicit canonical/compact constructor, the
heap pollution warning should be issued on the constructor, rather
than on record header declaration
3. If explicit canonical/compact constructor is annotated with
SafeVarargs, no warning should be issued.

So if one has a record declaration with potential heap pollution, and
want to declare that varargs are safe, they must explicitly add an
empty compact constructor and annotate it with SafeVarargs. Given that
this case should be extremely rare in practice, such an amount of
boilerplate doesn't look too big to me. On the other hand, such
solution requires less changes in Java (e.g. no need to allow
SafeVarargs on types).

Comments
Spec has been updated with the following text: > 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 canonical constructor ([8.10.4]) is annotated with `@SafeVarargs` ([9.6.4.7]) or the warning is suppressed by `@SuppressWarnings` ([9.6.4.5]).
10-04-2020