MethodHandles.foldArguments(MH,int,MH) is underspecified for invalid positions

XMLWordPrintable

    • Type: Sub-task
    • Resolution: Unresolved
    • Priority: P4
    • None
    • Affects Version/s: 11
    • Component/s: core-libs
    • None

      The MethodHandles.foldArguments method that accepts a position argument does not specify any behavior for positions out of bounds.

      For positive values, the method currently throws an IllegalArgumentException:

      jshell> MethodHandles.foldArguments(MethodHandles.zero(void.class), 1, MethodHandles.zero(void.class))
      | Exception java.lang.IllegalArgumentException: target and combiner types must match: ()void != ()void
      | at MethodHandleStatics.newIllegalArgumentException (MethodHandleStatics.java:177)
      | at MethodHandles.misMatchedTypes (MethodHandles.java:6053)
      | at MethodHandles.foldArgumentChecks (MethodHandles.java:5902)
      | at MethodHandles.foldArguments (MethodHandles.java:5873)
      | at (#3:1)

      The exception message is at least confusing.

      For negative values, an ArrayIndexOutOfBoundsException is thrown:

      jshell> MethodHandles.foldArguments(MethodHandles.zero(void.class), -1, MethodHandles.zero(int.class))
      | Exception java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 0
      | at MethodType.parameterType (MethodType.java:826)
      | at MethodHandles.foldArgumentChecks (MethodHandles.java:5899)
      | at MethodHandles.foldArguments (MethodHandles.java:5873)
      | at (#6:1)

      Alternatively, it silently accepts negative values in other scenarios:

      jshell> MethodHandles.foldArguments(MethodHandles.zero(void.class), -1, MethodHandles.zero(void.class))
      $7 ==> MethodHandle()void

      Or throws an ArrayIndexOutOfBoundsException with a different message:

      jshell> MethodHandles.foldArguments(MethodHandles.zero(void.class), -2, MethodHandles.zero(void.class))
      | Exception java.lang.ArrayIndexOutOfBoundsException: arraycopy: source index -1 out of bounds for object array[1]
      | at System.arraycopy (Native Method)
      | at LambdaFormEditor.makeArgumentCombinationForm (LambdaFormEditor.java:840)
      | at LambdaFormEditor.foldArgumentsForm (LambdaFormEditor.java:1049)
      | at MethodHandles.foldArguments (MethodHandles.java:5876)
      | at (#8:1)


            Assignee:
            Unassigned
            Reporter:
            Hannes Greule
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: