Commit 36794530

367945306092Merged

Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

feat(parse): support implicit it block parameter (Ruby 3.4+)

Numbered block params _1, _2 were already supported through Prism's NumberedParametersNode + a regular LocalVariableReadNode at the use site; the codegen has handled them since v1. Implicit it (Ruby 3.4) was emitted as PM_IT_PARAMETERS_NODE for the parameter declaration and PM_IT_LOCAL_VARIABLE_READ_NODE for each use, which the codegen treated as an unknown node and silently ignored.

Fix lowers both at the AST serializer boundary:

PM_IT_PARAMETERS_NODE → NumberedParametersNode { maximum: 1 } PM_IT_LOCAL_VARIABLE_READ_NODE → LocalVariableReadNode { name: "_1" }

This piggybacks on the existing _1 path in get_block_param at spinel_codegen.rb:23015 — it and _1 produce identical generated C, which matches Ruby's semantics (they're interchangeable references to the same implicit parameter).

Test exercises six shapes:

  • _1 in each and map+each chains
  • it in each and map+each chains
  • it with arithmetic, comparison, and select
  • it over a string array

Multi-arg numbered params (_2, _3) destructured from array-of-array or hash entries remain unsupported — the codegen's block-iteration path doesn't yet split a single int element into multiple block slots. Out of scope here.

Bootstrap: gen2.c == gen3.c. Tests: 192 pass.

Details

Commit
367945306092
Parent
02005f26deba

spinel_parse.cmodifiedView file

@@ -769,7 +769,20 @@
769769 break;
770770 }
771771 case PM_IT_PARAMETERS_NODE:
772- N("ItParametersNode");
772+ /* Ruby 3.4 implicit `it` is semantically `_1` — lower to a
773+ NumberedParametersNode so the codegen's existing
774+ NumberedParametersNode arity path (get_block_param) handles it
775+ transparently. The block body's `it` references separately
776+ become PM_IT_LOCAL_VARIABLE_READ_NODE, also lowered below. */
777+ N("NumberedParametersNode");
778+ I("maximum", 1);
779+ break;
780+ case PM_IT_LOCAL_VARIABLE_READ_NODE:
781+ /* `it` inside the block body. Lowered to a regular
782+ LocalVariableReadNode named "_1" so it pairs with the
783+ lowered NumberedParametersNode { maximum: 1 } above. */
784+ N("LocalVariableReadNode");
785+ S("name", escape_str((const uint8_t *)"_1", 2));
773786 break;
774787 default: {
775788 /* Fallback: emit unknown node type */