Repository: apple/pkl
File: pkl-core/src/main/java/org/pkl/core/ast/expression/literal/AmendFunctionNode.java (Java)

Code before:
    if (hostDescriptor != null) {
      parameterSlots = new int[hostDescriptor.getNumberOfSlots()];
      for (int i = 0; i < hostDescriptor.getNumberOfSlots(); i++) {
        var slotKind = hostDescriptor.getSlotKind(i);
        var slot = builder.addSlot(slotKind, hostDescriptor.getSlotName(i), null);
        parameterSlots[i] = slot;
      }
    } else {
      parameterSlots = new int[0];
    }
    var hasForGenVars = false;
    for (var i = 0; i < hostFrameDesecriptor.getNumberOfSlots(); i++) {
      var slotInfo = hostFrameDesecriptor.getSlotInfo(i);
      // Copy for-generator variables from the outer frame descriptor into inner lambda.
      //
      // We need to do this because at parse time within AstBuilder, we inject for-generator
      // variables into the frame descriptor of the containing root node.
      // The expectation is that when GeneratorForNode executes, it writes for-generator variables
      // into these slots.
      //
      // In the case of an amend function node, AstBuilder can't determine out that there is another
      // frame (e.g. with `new Mixin { ... }` syntax), so it injects for-generator vars into the
      // wrong frame.
      //
      // As a remedy, we simply copy outer variables into this frame if there are any for generator
      // variables.
      if (slotInfo != null && slotInfo.equals(SymbolTable.FOR_GENERATOR_VARIABLE)) {
        if (!hasForGenVars) {
          hasForGenVars = true;
          for (var j = 0; j < i; j++) {
            builder.addSlot(FrameSlotKind.Illegal, Identifier.DUMMY, null);
          }
        }
        builder.addSlot(
            hostFrameDesecriptor.getSlotKind(i), hostFrameDesecriptor.getSlotName(i), null);
      } else if (hasForGenVars) {
        builder.addSlot(FrameSlotKind.Illegal, Identifier.DUMMY, null);
      }
    }
    var objectToAmendSlot = builder.addSlot(FrameSlotKind.Object, new Object(), null);
    var frameDescriptor = builder.build();

    var subsequentFunctionRootNode =
        new SimpleRootNode(
            hostNode.language,
            frameDescriptor,
            sourceSection,
            hostNode.qualifiedScopeName + ".<function>",
            new AmendFunctionBodyNode(
                sourceSection,
                hostNode.copy(

Review comment (suggestion):
```suggestion
      // variables.
      //
      // We need to preserve the frame slot index, so we insert dummy identifiers
      // for other slots that aren't for generator variables.
```

Code after:
    if (hostDescriptor != null) {
      parameterSlots = new int[hostDescriptor.getNumberOfSlots()];
      for (int i = 0; i < hostDescriptor.getNumberOfSlots(); i++) {
        var slotKind = hostDescriptor.getSlotKind(i);
        var slot = builder.addSlot(slotKind, hostDescriptor.getSlotName(i), null);
        parameterSlots[i] = slot;
      }
    } else {
      parameterSlots = new int[0];
    }
    var hasForGenVars = false;
    for (var i = 0; i < hostFrameDesecriptor.getNumberOfSlots(); i++) {
      var slotInfo = hostFrameDesecriptor.getSlotInfo(i);
      // Copy for-generator variables from the outer frame descriptor into inner lambda.
      //
      // We need to do this because at parse time within AstBuilder, we inject for-generator
      // variables into the frame descriptor of the containing root node.
      // The expectation is that when GeneratorForNode executes, it writes for-generator variables
      // into these slots.
      //
      // In the case of an amend function node, AstBuilder can't determine out that there is another
      // frame (e.g. with `new Mixin { ... }` syntax), so it injects for-generator vars into the
      // wrong frame.
      //
      // As a remedy, we simply copy outer variables into this frame if there are any for generator
      // variables.
      //
      // We need to preserve the frame slot index, so we insert dummy identifiers
      // for other slots that aren't for generator variables.
      if (slotInfo != null && slotInfo.equals(SymbolTable.FOR_GENERATOR_VARIABLE)) {
        if (!hasForGenVars) {
          hasForGenVars = true;
          for (var j = 0; j < i; j++) {
            builder.addSlot(FrameSlotKind.Illegal, Identifier.DUMMY, null);
          }
        }
        builder.addSlot(
            hostFrameDesecriptor.getSlotKind(i), hostFrameDesecriptor.getSlotName(i), null);
      } else if (hasForGenVars) {
        builder.addSlot(FrameSlotKind.Illegal, Identifier.DUMMY, null);
      }
    }
    var objectToAmendSlot = builder.addSlot(FrameSlotKind.Object, new Object(), null);
    var frameDescriptor = builder.build();

    var subsequentFunctionRootNode =
        new SimpleRootNode(
            hostNode.language,
            frameDescriptor,
            sourceSection,
            hostNode.qualifiedScopeName + ".<function>",