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

Expression lambda erroneously compatible with void-returning descriptor

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • None
    • tools
    • b120
    • Verified

      This program compiles even when it should not:

      class Test {
        void test() {
         Runnable r = () -> (foo());
      }
      private void foo() {}
      }

      The body of the lambda is not an expression statement - therefor it should not be compatible with a void-returning functional interface (see JLS 15.27.3. Type of a Lambda Expression):

      "A lambda expression is congruent with a function type if all of the following are true:

          The function type has no type parameters.

          The number of lambda parameters is the same as the number of parameter types of the function type.

          If the lambda expression is explicitly typed, its formal parameter types are the same as the parameter types of the function type.

          If the lambda parameters are assumed to have the same types as the function type's parameter types, then:

              If the function type's result is void, the lambda body is either a statement expression (§14.8) or a void-compatible block.

              If the function type's result is a (non-void) type R, then either i) the lambda body is an expression that is compatible with R in an assignment context, or ii) the lambda body is a value-compatible block, and each result expression (§15.27.2) is compatible with R in an assignment context."

            mcimadamore Maurizio Cimadamore
            mcimadamore Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: