Commit f87de047

f87de0478ee6Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified

Description

parse,analyze,codegen: pattern-match as + guard variable bind

case x in pat => var (CapturePatternNode) and case x in n if guard (IfNode-wrapped pattern) used to leave the matched LV undeclared in the generated C.

  • parse: PM_CAPTURE_PATTERN_NODE -> CapturePatternNode with value (inner pattern) + target (LV target)
  • analyze: scan_locals registers the as-var with the scrutinee's type via a new @scan_case_match_pred_type side channel; IfNode-wrapped LocalVariableTargetNode is also unwrapped so the guard form registers its LV
  • codegen: compile_in_pattern handles LocalVariableTargetNode (always-match + bind via comma-expr), IfNode (recurse for inner pattern, AND-fold the guard predicate), CapturePatternNode (recurse + bind). The InNode dispatch peels CapturePatternNode off ArrayPattern/HashPattern arms and injects the binding at body entry.

Refs #884 #905

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
f87de0478ee6
Parent
f6640684b3f3

spinel_analyze.rbmodifiedView file

@@ -110,6 +110,11 @@
110110 # type each bound LV with the enclosing CaseMatchNode's scrutinee
111111 # element type. Issue #669.
112112 @scan_case_match_pred_elem = ""
113+ # Sibling channel for CapturePatternNode (`pat => var`) and
114+ # bare LocalVariableTargetNode in InNode (`case x in n`). Holds
115+ # the scrutinee's full type so the target LV gets the right slot.
116+ # Issues #884 #905.
117+ @scan_case_match_pred_type = ""
113118 # Sibling side-channel for HashPatternNode: the hash's value type.
114119 # Issue #805.
115120 @scan_case_match_pred_val_t = ""
@@ -26313,9 +26318,15 @@
2631326318 if @nd_type[nid] == "CaseMatchNode"
2631426319 saved_pred_elem = @scan_case_match_pred_elem
2631526320 saved_pred_val_t = @scan_case_match_pred_val_t
26321+ saved_pred_type_full = @scan_case_match_pred_type
2631626322 pred_nid = @nd_predicate[nid]
2631726323 if pred_nid >= 0
2631826324 cmt = infer_type(pred_nid)
26325+ # Issues #884 #905: stash the scrutinee's full type so a bare
26326+ # `LocalVariableTargetNode` pattern (`case x in n`) or a
26327+ # `CapturePatternNode` (`case x in [...] => arr`) can bind
26328+ # its LV with the right slot type.
26329+ @scan_case_match_pred_type = cmt
2631926330 if cmt == "int_array" || cmt == "sym_array"
2632026331 @scan_case_match_pred_elem = "int"
2632126332 elsif cmt == "str_array"
@@ -26344,8 +26355,55 @@
2634426355 scan_locals_children(nid, names, types, params)
2634526356 @scan_case_match_pred_elem = saved_pred_elem
2634626357 @scan_case_match_pred_val_t = saved_pred_val_t
26358+ @scan_case_match_pred_type = saved_pred_type_full
26359+ return
26360+ end
26361+ # `case x in pat => var` (CapturePatternNode) -- register the
26362+ # target LV with the scrutinee's type. Issue #884.
26363+ if @nd_type[nid] == "CapturePatternNode"
26364+ cp_target = @nd_target[nid]
26365+ if cp_target >= 0 && @nd_type[cp_target] == "LocalVariableTargetNode"
26366+ cp_lname = @nd_name[cp_target]
26367+ if not_in(cp_lname, names) == 1 && not_in(cp_lname, params) == 1
26368+ names.push(cp_lname)
26369+ types.push(@scan_case_match_pred_type != "" ? @scan_case_match_pred_type : "int")
26370+ @scan_literal_flags.push("")
26371+ @scan_empty_flags.push("")
26372+ @scan_empty_hash_flags.push("")
26373+ end
26374+ end
26375+ scan_locals_children(nid, names, types, params)
2634726376 return
2634826377 end
26378+ # `case x in n` (bare LocalVariableTargetNode pattern at the
26379+ # InNode root) -- register n with the scrutinee's type. Also
26380+ # `in n if guard` (IfNode wrapping a LocalVariableTargetNode
26381+ # in its statements). Issues #905 #884.
26382+ if @nd_type[nid] == "InNode"
26383+ pat_in = @nd_pattern[nid]
26384+ in_target_pat = pat_in
26385+ # Unwrap IfNode (guard clause) to find the underlying pattern.
26386+ # "statements" parser field maps onto @nd_body.
26387+ if in_target_pat >= 0 && @nd_type[in_target_pat] == "IfNode"
26388+ in_if_stmts = @nd_body[in_target_pat]
26389+ if in_if_stmts >= 0
26390+ in_if_body = get_stmts(in_if_stmts)
26391+ if in_if_body.length > 0
26392+ in_target_pat = in_if_body[0]
26393+ end
26394+ end
26395+ end
26396+ if in_target_pat >= 0 && @nd_type[in_target_pat] == "LocalVariableTargetNode"
26397+ in_lname = @nd_name[in_target_pat]
26398+ if not_in(in_lname, names) == 1 && not_in(in_lname, params) == 1
26399+ names.push(in_lname)
26400+ types.push(@scan_case_match_pred_type != "" ? @scan_case_match_pred_type : "int")
26401+ @scan_literal_flags.push("")
26402+ @scan_empty_flags.push("")
26403+ @scan_empty_hash_flags.push("")
26404+ end
26405+ end
26406+ end
2634926407 if @nd_type[nid] == "MultiWriteNode"
2635026408 targets = parse_id_list(@nd_targets[nid])
2635126409 val_id2 = @nd_expression[nid]