<div dir="ltr">In the upgrade of code from JDK 21 to JDK 22, I've run into a problem with VarHandle instances built using PathElement. Something must have changed, which I'm not aware of, or I must have been using them incorrectly all along, or there is a bug.<div><br></div><div>The VarHandle instances can be created but when they are used to access data an exception like this one is thrown:</div><div><br></div><div>Exception in thread "main" java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle(VarHandle,MemorySegment,long)int to (VarHandle,MemorySegment)int<br>at java.base/java.lang.invoke.MethodHandle.asTypeUncached(MethodHandle.java:903)<br>at java.base/java.lang.invoke.MethodHandle.asType(MethodHandle.java:870)<br>at Main.main(Main.java:16)<br><br>The code to reproduce the error can be found below. It crashes on "errnoHandle.get(...)".</div><div><br></div><div>The code is mostly form the documentation of Linker.Option.captureCallState() (see <a href="https://cr.openjdk.org/~mcimadamore/jdk/FFM_22_PR/javadoc/java.base/java/lang/foreign/Linker.Option.html#captureCallState(java.lang.String...)">https://cr.openjdk.org/~mcimadamore/jdk/FFM_22_PR/javadoc/java.base/java/lang/foreign/Linker.Option.html#captureCallState(java.lang.String...)</a>) But it doesn't just affect call state. It affects all kinds of VarHandle instances built with PathElement.</div><div><br></div><div>The bug occurred with the OpenJDK build 29, both on macOS and Linux. </div><div><br></div><div>Any help is appreciated.</div><div><br></div><div>Regards</div><div>Manuel</div><div><br></div><div><br></div><div>import java.lang.foreign.*;<br>import static java.lang.foreign.ValueLayout.JAVA_INT;<br><br>public class Main {<br>    public static void main(String[] args) throws Throwable {<br>        var linker = Linker.nativeLinker();<br>        var descriptor = FunctionDescriptor.of(JAVA_INT, JAVA_INT);<br>        var ccs = Linker.Option.captureCallState("errno");<br>        var handle = linker.downcallHandle(linker.defaultLookup().find("close").get(), descriptor, ccs);<br>        var capturedStateLayout = Linker.Option.captureStateLayout();<br>        var errnoHandle = capturedStateLayout.varHandle(MemoryLayout.PathElement.groupElement("errno"));<br><br>        try (Arena arena = Arena.ofConfined()) {<br>            var capturedState = arena.allocate(capturedStateLayout);<br>            var res = (int) handle.invokeExact(capturedState, 100);<br>            int errno = (int) errnoHandle.get(capturedState);<br>            System.out.println("errno = " + errno);<br>        }<br>    }<br>}<br></div><div><br></div><div><div class="gmail-gs" style="margin:0px;min-width:0px;padding:0px 0px 20px;width:initial;font-family:"Google Sans",Roboto,RobotoDraft,Helvetica,Arial,sans-serif"><div class="gmail-"><div id="gmail-:27w" class="gmail-ii gmail-gt" style="direction:ltr;margin:8px 0px 0px;padding:0px;font-size:0.875rem;overflow-x:hidden"><div id="gmail-:27v" class="gmail-a3s gmail-aiL" style="direction:initial;font-stretch:normal;font-size:small;line-height:1.5;font-family:Arial,Helvetica,sans-serif;font-size-adjust:none;font-kerning:auto;font-variant-alternates:normal;font-variant-ligatures:normal;font-variant-numeric:normal;font-variant-east-asian:normal;font-feature-settings:normal;overflow:auto hidden"><div class="gmail-yj6qo"></div><div class="gmail-adL"></div></div></div><div class="gmail-hi" style="padding:0px;width:auto;background:repeat rgb(242,242,242);margin:0px;border-bottom-left-radius:1px;border-bottom-right-radius:1px"></div><div class="gmail-WhmR8e" style="clear:both"></div></div></div><br class="gmail-Apple-interchange-newline"></div></div>