RFR: 8322810: Lambda expressions can implement classes
Vicente Romero
vromero at openjdk.org
Wed Apr 9 19:59:30 UTC 2025
On Wed, 9 Apr 2025 17:15:52 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> Currently javac is accepting code like:
>>
>>
>> class Test {
>> void m() {
>> var r = (Test & Runnable) () -> System.out.println("Hello, World!");
>> }
>> }
>>
>>
>> according to: `4.9 Intersection Types` a notional interface can't be induced here as one of the components is a class different from `j.l.Object`
>>
>> With this fix this code pattern will be rejected with a compiler error
>>
>> TIA
>
> test/langtools/tools/javac/diags/examples/NotAnInterfaceComponent.java line 29:
>
>> 27:
>> 28: class NotAnInterfaceComponent {
>> 29: Object o = (String & Runnable) ()-> { };
>
> How was this rejected before? The generated compiler keys look strange...
it was rejected because javac was trying to determine the function descriptor associated with the supposed functional interface that could be derived from the intersection of `String & Runnable` a this is impossible then it was failing. This check is at `DescriptorCache::findDescriptorInternal`. But if we have an empty class as in the test case that shows the bug that only inherits from `j.l.Object` but doesn't defines any other method, then javac was only seeing the method defined in the interface and thus a function descriptor was "found". See that the filter `DescriptorFilter` used by `DescriptorCache::findDescriptorInternal` all methods already defined in `j.l.Object`
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/24548#discussion_r2036052458
More information about the compiler-dev
mailing list