JDK-8150729 : j.l.i.MethodHandles.loop(...) permits null 'pred' + non-null 'fini'
  • Type: Sub-task
  • Component: core-libs
  • Sub-Component: java.lang.invoke
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2016-02-26
  • Updated: 2016-09-06
  • Resolved: 2016-09-06
Description
According to spec (http://download.java.net/jdk9/docs/api/java/lang/invoke/MethodHandles.html#loop-java.lang.invoke.MethodHandle:A...-),
Step3.c: 
If a pred function is omitted, the corresponding fini function must also be omitted.

Considering factorial example from specification, let's add null 'pred' and MH_fin 'fini' function to counterClause.

Expected result: IllegalArgumentException.
Actual result: no exceptions, code works as before


Was found on jdk9 b106, b107. Tests are under development,  tck_red label will be added after development completion.
Example attached
Comments
This has been addressed in a rework of JDK-8151179; the fix will be pushed with the change for that issue.
06-09-2016

What would an easy detection look like? Even for the case where the pred handle is a MHs.constant(boolean.class, true) this looks complicated, as such constant handles are created through identity / bindTo / dropArguments.
17-08-2016

This was intended to be an error check to diagnose an unreached exit point. If the pred function is always true, then the fini function can never be executed. If we can detect this easily we should. But maybe the irregularity will harm some use cases, like dynamic languages which exit loops using throws. So, we should note the error condition that this rule was designed to help avoid. (I'm ambivalent about omitting the rule itself.) So the following rule should be amplified as follows: * <li>If a pred function is omitted, use a constant {@code true} function. (This will keep the loop going, as far - * as this clause is concerned.) + * as this clause is concerned. Note that in such cases the corresponding fini function is unreachable.) Maybe also add this somewhere: "If there are no pred functions, or if the pred functions always return true, the loop cannot exit except by throwing an exception."
16-08-2016

Addressed internally; fix will be pushed with JDK-8151179.
21-03-2016

Documentation should be changed: the requirement is too strict, the existing logic is capable of substituting for the missing finaliser in case the predicate is missing.
21-03-2016