Static methods of generic classes are treated differently
Archie Cobbs
archie.cobbs at gmail.com
Sat Jun 10 22:06:56 UTC 2023
Looks like a legitimate bug to me...
You'd think how the compiler treats a static method reference shouldn't be
at all determined by whether the class containing that static method is
generic or not...
I've created https://bugs.openjdk.org/browse/JDK-8309767 to track this.
-Archie
On Fri, Jun 9, 2023 at 11:59 AM Ethan McCue <ethan at mccue.dev> wrote:
> Following up on this just to see if there is an explanation of some sort I
> am missing.
>
> On Sun, Apr 30, 2023 at 8:40 AM Ethan McCue <ethan at mccue.dev> wrote:
>
>> I've run into either a corner case in type resolution or a compiler bug
>> and I'm not sure which.
>>
>> In an expression context, if a static method is referenced from a generic
>> class then it won't properly target overloads that require primitive
>> wrapper unboxing.
>>
>> package org.example;
>>
>> import java.time.Month;
>> import java.util.function.Supplier;
>>
>>
>> interface A {
>> static int f() {
>> return 1;
>> }
>> }
>>
>> interface B<T> {
>> static int f() {
>> return 2;
>> }
>> }
>>
>> final class C {
>> static int f() {
>> return 3;
>> }
>> }
>>
>> final class D<T> {
>> static int f() {
>> return 4;
>> }
>> }
>>
>>
>> public class Main {
>> static void h(Month b) {}
>> static void h(int b) {}
>>
>> static <T> T g(Supplier<? extends T> d) {
>> return d.get();
>> }
>>
>>
>> public static void main(String[] args) {
>> // Compiles
>> h(
>> g(A::f)
>> );
>>
>> // Doesn't compile
>> h(
>> g(B::f)
>> );
>>
>> // Compiles
>> var r1 = g(B::f);
>> h(r1);
>>
>> // Compiles
>> h(
>> g(C::f)
>> );
>>
>> // Doesn't compile
>> h(
>> g(D::f)
>> );
>>
>> // Compiles
>> var r2 = g(D::f);
>> h(r2);
>> }
>> }
>>
>
--
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20230610/405c1db4/attachment.htm>
More information about the compiler-dev
mailing list