Regression: accessing a private method through a type variable

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Oct 16 12:15:08 UTC 2023


I recall fixing this a long time ago:


https://bugs.openjdk.org/browse/JDK-6711619


Now, the test case mentioned in that bug is still correctly rejected by 
javac. Furhermore, a similar test:


```
class X<E extends X<E>> {
              private static int m() { return 1; }
              int f() {
                      return E.m();
              }
     }
```


Also fails to compile with 21/22.


```
Test.java:5: error: m() has private access in X
                      return E.m();
                              ^
1 error

```


Trying to manipulate Remi's example, it seems the regression only has to 
do with method refreences whose receiver is a type variable.


Here's a minimal reproducer:


```
import java.util.function.*;

class Test {
   private String asString() {
     return "bar";
   }

   static <T extends Test> Function<T, String> foo() {
     return T::asString;
   }
}
```

I filed a bug:


https://bugs.openjdk.org/browse/JDK-6711619


Thanks
Maurizio


On 16/10/2023 10:40, Sundararajan Athijegannathan wrote:
> fwiw, this compile from at least JDK 11 onwards:
>
> import java.util.*;
> import java.util.stream.*;
>
> public class TDotToString {
>   class Bar {
>     private String asString() {
>       return "bar";
>     }
>   }
>
>   static <T extends Bar> String foo(List<T> list) {
>     return list.stream().map(T::asString).collect(Collectors.joining());
>   }
> }
>
> javac from 1.8.0 does issue error as mentioned by Remi.
>
>
> -Sundar
> ------------------------------------------------------------------------
> *From:* compiler-dev <compiler-dev-retn at openjdk.org> on behalf of Remi 
> Forax <forax at univ-mlv.fr>
> *Sent:* 16 October 2023 13:57
> *To:* compiler-dev <compiler-dev at openjdk.org>
> *Subject:* Regression: accessing a private method through a type variable
> Hello,
> There is a regression in recent versions of javac.
> javac 21 allows to access a private method through a type variable, 
> here T::asString.
>
> public class TDotToString {
>   class Bar() {
>     private String asString() {
>       return "bar";
>     }
>   }
>
>   static <T extends Bar> String foo(List<T> list) {
>     return list.stream().map(T::asString).collect(Collectors.joining());
>   }
> }
>
> Both IntelliJ and Eclipse emit an error in this case.
>
> And javac 8 emits
>   TDotToString.java:12: error: invalid method reference
>     return list.stream().map(T::asString).collect(Collectors.joining());
>                              ^
>   cannot find symbol
>     symbol:   method asString()
>     location: bound of type variable T
>   where T is a type-variable:
>     T extends TDotToString.Bar declared in method <T>foo(List<T>)
> 1 error
>
> regards,
> Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20231016/1d56ef2a/attachment.htm>


More information about the compiler-dev mailing list