RFR: 8322810: Lambda expressions can implement classes

Maurizio Cimadamore mcimadamore at openjdk.org
Wed Apr 9 17:20:32 UTC 2025


On Wed, 9 Apr 2025 14:03:03 GMT, Vicente Romero <vromero 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

I'm surprised this slipped through the cracks for so long! It seems like that other cases were already failing (e.g. `String & I`) so I wonder what's the difference between those and the new example in the test case...

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...

test/langtools/tools/javac/lambda/ClassInIntersectionTypeTest.java line 8:

> 6:  */
> 7: 
> 8: public class ClassInIntersectionTypeTest {

Maybe add a test with an annotation target?

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

PR Review: https://git.openjdk.org/jdk/pull/24548#pullrequestreview-2754202633
PR Review Comment: https://git.openjdk.org/jdk/pull/24548#discussion_r2035802436
PR Review Comment: https://git.openjdk.org/jdk/pull/24548#discussion_r2035804466


More information about the compiler-dev mailing list