On Thu, 27 Nov 2025 18:47:15 GMT, Alan Bateman <[email protected]> wrote:

>> Currently, the hotspot compiler (as in ciField) trusts final fields in 
>> hidden classes, record classes, and selected jdk packages. Some classes in 
>> the JDK wish to be trusted, but they cannot apply package-wide opt-in due to 
>> other legacy classes in the package, such as java.util.
>> 
>> They currently can use `@Stable` as a workaround, but this is fragile 
>> because a stable final field may hold a trusted null, zero, or false value, 
>> which is currently treated as non-constant by ciField.
>> 
>> We should add an annotation to opt-in for a whole class, mainly for legacy 
>> packages. This would benefit greatly some of our classes already using a lot 
>> of Stable, such as java.util.Optional, whose empty instance is now 
>> constant-foldable, as demonstrated in a new IR test.
>> 
>> Paging @minborg who requested Optional folding for review.
>> 
>> I think we can remove redundant Stable in a few other java.util classes 
>> after this patch is integrated. I plan to do that in subsequent patches.
>
> src/hotspot/share/ci/ciField.cpp line 220:
> 
>> 218:     return false;
>> 219:   // Explicit opt-in from system classes
>> 220:   if (holder->trust_final_fields())
> 
> This is definitely nicer than listing specific classes. It would be nicer 
> again once we can make this exceptions go away.

True, this occupies one of the 16 precious instance klass bits in runtime. I 
wish we can derive this from our final means final restrictions, but their 
setup is to permit use-sites to migrate more easily, and is harder for 
declaration sites to deduce if a declaration is easier to be permitted. We can 
consider blanket-trust when the JVM uses `--illegal-final-field-mutation=deny` 
without additional `--enable-final-field-mutation`.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/28540#discussion_r2569800720

Reply via email to