Commit 4f42827b

4f42827bc25cMerged

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

Description

fix(parse,analyze,codegen): array pattern matching binds LVs (#669)

case arr in [a, b, c] previously emitted UnsupportedNode for the ArrayPatternNode in spinel_parse, and the codegen ran the body with lv_a / lv_b / lv_c never declared (C compile failed with "undeclared identifier").

parse:

  • New PM_ARRAY_PATTERN_NODE handler emits the constant / requireds / rest / posts fields. Minimal initial coverage; rest + posts + constant pass through but the codegen only uses requireds today.

analyze:

  • scan_locals' ArrayPatternNode arm collects each LocalVariableTargetNode in requireds into the surrounding scope with the scrutinee's element type (int for int_array / sym_array, string for str_array, etc.). Re-used names across sibling arms widen to poly if their per-arm types disagree.
  • scan_locals' CaseMatchNode arm sets the side channel @scan_case_match_pred_elem before descending so the ArrayPatternNode handler can read the element type.
  • scan_locals_children now recurses into @nd_pattern so the pattern's sub-tree gets walked.

codegen:

  • compile_case_match_stmt detects ArrayPatternNode and routes to compile_array_pattern_arm, which generates a length check and per-position bind. Bound LVs were already declared at function entry via the scan_locals path; the arm just assigns. Under promote mode the bind boxes via sp_bigint_new_int when the slot was widened to bigint.

Closes #669.

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

Details

Commit
4f42827bc25c
Parent
a3cc0f4995ad

spinel_analyze.rbmodifiedView file

@@ -106,6 +106,10 @@
106106 @reinfer_lv_target = ""
107107 @reinfer_lv_cur = ""
108108 @reinfer_lv_found = ""
109+ # Side channel used by scan_locals' ArrayPatternNode handler to
110+ # type each bound LV with the enclosing CaseMatchNode's scrutinee
111+ # element type. Issue #669.
112+ @scan_case_match_pred_elem = ""
109113 # 1 once analysis has converged and freeze_analysis has filled
110114 # @nd_inferred_type. Switches infer_type to consult the cache
111115 # first; analysis iterations themselves keep recomputing because
@@ -24556,6 +24560,76 @@
2455624560 # to a more specific variant on first []= write.
2455724561 @scan_empty_hash_flags = "".split(",")
2455824562 end
24563+ # `case x in [a, b, c]` -- the bound LVs in the array pattern's
24564+ # requireds list need to live in the surrounding scope so the
24565+ # arm's body can read them and a sibling arm can re-bind the same
24566+ # name without redeclaring. Issue #669.
24567+ if @nd_type[nid] == "ArrayPatternNode"
24568+ ap_reqs = parse_id_list(@nd_requireds[nid])
24569+ # Element type from the enclosing case-match's scrutinee, if we
24570+ # can see it via @scan_case_match_pred_type (a side channel set
24571+ # by scan_locals' CaseMatchNode handler below). Default to int.
24572+ ap_elem_t = @scan_case_match_pred_elem != "" ? @scan_case_match_pred_elem : "int"
24573+ ap_ri = 0
24574+ while ap_ri < ap_reqs.length
24575+ ap_tid = ap_reqs[ap_ri]
24576+ if @nd_type[ap_tid] == "LocalVariableTargetNode"
24577+ ap_lname = @nd_name[ap_tid]
24578+ if not_in(ap_lname, names) == 1 && not_in(ap_lname, params) == 1
24579+ names.push(ap_lname)
24580+ types.push(ap_elem_t)
24581+ @scan_literal_flags.push("")
24582+ @scan_empty_flags.push("")
24583+ @scan_empty_hash_flags.push("")
24584+ else
24585+ # Same name re-used across sibling arms with different element
24586+ # type (e.g. `case x in [a, b]` then `case y in [a]` where x
24587+ # and y are different array families): widen the existing slot
24588+ # to poly so both string and int bindings fit.
24589+ existing_idx = -1
24590+ ni = 0
24591+ while ni < names.length
24592+ if names[ni] == ap_lname
24593+ existing_idx = ni
24594+ ni = names.length
24595+ else
24596+ ni = ni + 1
24597+ end
24598+ end
24599+ if existing_idx >= 0 && types[existing_idx] != ap_elem_t
24600+ types[existing_idx] = "poly"
24601+ @needs_rb_value = 1
24602+ end
24603+ end
24604+ end
24605+ ap_ri = ap_ri + 1
24606+ end
24607+ end
24608+ # CaseMatchNode: stash the scrutinee's element type so the
24609+ # ArrayPatternNode handler above can type the bound LVs.
24610+ if @nd_type[nid] == "CaseMatchNode"
24611+ saved_pred_elem = @scan_case_match_pred_elem
24612+ pred_nid = @nd_predicate[nid]
24613+ if pred_nid >= 0
24614+ cmt = infer_type(pred_nid)
24615+ if cmt == "int_array" || cmt == "sym_array"
24616+ @scan_case_match_pred_elem = "int"
24617+ elsif cmt == "str_array"
24618+ @scan_case_match_pred_elem = "string"
24619+ elsif cmt == "float_array"
24620+ @scan_case_match_pred_elem = "float"
24621+ elsif cmt == "poly_array"
24622+ @scan_case_match_pred_elem = "poly"
24623+ elsif is_ptr_array_type(cmt) == 1
24624+ @scan_case_match_pred_elem = ptr_array_elem_type(cmt)
24625+ else
24626+ @scan_case_match_pred_elem = ""
24627+ end
24628+ end
24629+ scan_locals_children(nid, names, types, params)
24630+ @scan_case_match_pred_elem = saved_pred_elem
24631+ return
24632+ end
2455924633 if @nd_type[nid] == "MultiWriteNode"
2456024634 targets = parse_id_list(@nd_targets[nid])
2456124635 val_id2 = @nd_expression[nid]
@@ -25805,6 +25879,12 @@
2580525879 if @nd_receiver[nid] >= 0
2580625880 scan_locals(@nd_receiver[nid], names, types, params)
2580725881 end
25882+ # `in <pattern>` arm: recurse into the pattern so ArrayPatternNode's
25883+ # bound LVs (LocalVariableTargetNode in requireds) reach the
25884+ # scan_locals' pattern handler. Issue #669.
25885+ if @nd_pattern[nid] >= 0
25886+ scan_locals(@nd_pattern[nid], names, types, params)
25887+ end
2580825888 if @nd_collection[nid] >= 0
2580925889 scan_locals(@nd_collection[nid], names, types, params)
2581025890 end