Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8322810

Lambda expressions can implement classes

    XMLWordPrintable

Details

    • generic
    • generic

    Description

      ADDITIONAL SYSTEM INFORMATION :
      Tested on JDK 17, 21 and 22

      A DESCRIPTION OF THE PROBLEM :
      javac can compile lambda expressions assigned to intersection types, even if one of the types is a class, not an interface. This frequently leads to bytecode being generated that raises a LambdaConversionException when executed. It is also possible to generate bytecode that fails verification and throws a VerifyError. I'm not fully sure why the latter case occurs, but I assume that a lambda implementing a class breaks some assumptions in other components in the compiler which results in bytecode that fails verification.

      This appears to be a regression, as javac refuses to compile this code in Java 8, but by Java 11 it compiles.

      REGRESSION : Last worked in version 8u391

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Download the test case
      2. Compile it with javac
      3. Observe it compiles successfully with no errors or warnings.
      4. Run Main for the LambdaConversionException, and run MainVerifyError for the VerifyError.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Lambdas cannot implement classes and a compile error is raised.
      ACTUAL -
      Code compiles successfully and fails at runtime with a LambdaConversionException, or sometimes a VerifyError.

      ---------- BEGIN SOURCE ----------
      public class Main {
        public static void main(String[] args) {
          var r = (Main & Runnable) () -> System.out.println("Hello, World!");
        }
      }

      class MainVerifyError {
        public static void main(String[] args) {
          run(() -> System.out.println("Hello, World!"));
        }

        static <T extends MainVerifyError & Runnable> void run(T t) {
          t.run();
        }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Activity

          People

            vromero Vicente Arturo Romero Zaldivar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: