RFR: 8343286: Missing unchecked cast warning in polymorphic method call [v6]

Maurizio Cimadamore mcimadamore at openjdk.org
Wed Nov 13 15:22:20 UTC 2024


On Tue, 12 Nov 2024 16:15:31 GMT, Vicente Romero <vromero at openjdk.org> wrote:

>> Javac is not issuing a mandated unchecked cast warning for code like:
>> 
>> 
>> import java.lang.invoke.VarHandle;
>> 
>> class VarHandleCast<V> {
>>      VarHandle vh;
>>      V method(Object obj) {
>>          return (V)vh.getAndSet(this, obj);
>>      }
>> }
>> 
>> 
>> according to the spec the return type inferred for this method polymorphic method should be Object see:
>> 
>> -  If the compile-time declaration for the method invocation is a signature polymorphic method, then:
>> ...
>>     – The compile-time result is determined as follows:
>>         Otherwise, if the method invocation expression is the operand of a cast
>>     expression (§15.16), the compile-time result is the erasure of the type of the
>>     cast expression (§4.6).
>> 
>> javac is not doing the type erasure mandated by the spec which is what this PR is fixing
>> 
>> TIA
>> 
>> Note: I had to add some annotations to suppress warnings in java.base that were not being issued before due to the javac bug
>
> Vicente Romero has updated the pull request incrementally with one additional commit since the last revision:
> 
>   adding regression test

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java line 266:

> 264:         MODULE_IMPORTS(JDK23, Fragments.FeatureModuleImports, DiagKind.PLURAL),
> 265:         PRIVATE_MEMBERS_IN_PERMITS_CLAUSE(JDK19),
> 266:         COMPILE_TIME_TYPE_SIG_POLYMORPHIC_MTH(JDK24),

Nit: I'd pick a name that is more representative of what the behavior is. E.g. `erase_poly_sig_return_type`. Same for the field in `Infer` storing the boolean flag.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/21914#discussion_r1840566249


More information about the core-libs-dev mailing list