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 to110+
# type each bound LV with the enclosing CaseMatchNode's scrutinee111+
# element type. Issue #669.112+
@scan_case_match_pred_elem = ""109113
# 1 once analysis has converged and freeze_analysis has filled110114
# @nd_inferred_type. Switches infer_type to consult the cache111115
# 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
end24563+
# `case x in [a, b, c]` -- the bound LVs in the array pattern's24564+
# requireds list need to live in the surrounding scope so the24565+
# arm's body can read them and a sibling arm can re-bind the same24566+
# 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 we24570+
# can see it via @scan_case_match_pred_type (a side channel set24571+
# 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 = 024574+
while ap_ri < ap_reqs.length24575+
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) == 124579+
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+
else24585+
# Same name re-used across sibling arms with different element24586+
# type (e.g. `case x in [a, b]` then `case y in [a]` where x24587+
# and y are different array families): widen the existing slot24588+
# to poly so both string and int bindings fit.24589+
existing_idx = -124590+
ni = 024591+
while ni < names.length24592+
if names[ni] == ap_lname24593+
existing_idx = ni24594+
ni = names.length24595+
else24596+
ni = ni + 124597+
end24598+
end24599+
if existing_idx >= 0 && types[existing_idx] != ap_elem_t24600+
types[existing_idx] = "poly"24601+
@needs_rb_value = 124602+
end24603+
end24604+
end24605+
ap_ri = ap_ri + 124606+
end24607+
end24608+
# CaseMatchNode: stash the scrutinee's element type so the24609+
# ArrayPatternNode handler above can type the bound LVs.24610+
if @nd_type[nid] == "CaseMatchNode"24611+
saved_pred_elem = @scan_case_match_pred_elem24612+
pred_nid = @nd_predicate[nid]24613+
if pred_nid >= 024614+
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) == 124624+
@scan_case_match_pred_elem = ptr_array_elem_type(cmt)24625+
else24626+
@scan_case_match_pred_elem = ""24627+
end24628+
end24629+
scan_locals_children(nid, names, types, params)24630+
@scan_case_match_pred_elem = saved_pred_elem24631+
return24632+
end2455924633
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] >= 02580625880
scan_locals(@nd_receiver[nid], names, types, params)2580725881
end25882+
# `in <pattern>` arm: recurse into the pattern so ArrayPatternNode's25883+
# bound LVs (LocalVariableTargetNode in requireds) reach the25884+
# scan_locals' pattern handler. Issue #669.25885+
if @nd_pattern[nid] >= 025886+
scan_locals(@nd_pattern[nid], names, types, params)25887+
end2580825888
if @nd_collection[nid] >= 02580925889
scan_locals(@nd_collection[nid], names, types, params)2581025890
endspinel_codegen.rbmodifiedView file
Loading diff…
spinel_parse.cmodifiedView file
Loading diff…
test/array_pattern_match_basic.rbaddedView file
Loading diff…