RFR: 8254784: javac should reject records with @SafeVarargs applied to varargs record component [v2]

Maurizio Cimadamore mcimadamore at openjdk.java.net
Fri Nov 13 12:47:06 UTC 2020


On Thu, 5 Nov 2020 17:13:12 GMT, Vicente Romero <vromero at openjdk.org> wrote:

>> Please review this fix for issue [1]. This is the background, for a record defined like:
>> 
>> record R(@SafeVarargs String... s) {}
>> 
>> an the following accessor will be generated:
>> 
>>     `@SafeVararags public String[] s() { return s; }`
>> 
>> this code is incorrect as the accessor is not a varargs method. But still the compiler is not generating a compiler error. The reason for this is that the method defining the accessor is not generated until the compiler get to its backend phase (at Lower to be more specific) and the check for incorrect @SafeVarargs use in the compiler happen before that, in Attr.
>> 
>> We decided to do not generate record related code until late phases in order to avoid generating code in the compiler frontend and because if code is generated in early stages then APs could see ASTs not created from user defined code. 
>> 
>> The proposed fix is to issue an error as soon as the compiler finds out that the @SafeVarargs annotation is applied to a compiler generated accessor because in other case the compiler would be forced to generate incorrect code.
>> 
>> TIA for the reviews,
>> Vicente
>> 
>> [1] [https://bugs.openjdk.java.net/browse/JDK-8254784](https://bugs.openjdk.java.net/browse/JDK-8254784)
>
> Vicente Romero has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains two commits:
> 
>  - manual merge with master
>  - 8254784: javac should reject records with @SafeVarargs applied to varargs record component

Seems sensible. A possible alternative would be to err immediately when we see (e.g. in TypeEnter?) that a record header contains a SafeVarargs annotation, in which case we know we can't support it.

-------------

Marked as reviewed by mcimadamore (Reviewer).

PR: https://git.openjdk.java.net/jdk/pull/690


More information about the compiler-dev mailing list