Commit a976fcfe
a976fcfe53b5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · ~3 modified
Description
codegen: HashPatternNode + rest-form ArrayPatternNode
HashPatternNode used to surface as UnsupportedNode at parse time, so `case h in {a:}` failed to compile (lv_a never declared). The "FindPatternNode" repro from #805 actually parses as ArrayPatternNode with a SplatNode rest, which compile_array_pattern_arm rejected via its strict `len == requireds` guard.
Three coordinated pieces:
- spinel_parse.c: emit HashPatternNode (constant + elements + rest) and FindPatternNode (constant + left + requireds + right) instead of dropping to UnsupportedNode.
- spinel_analyze.rb: scan_locals registers HashPatternNode-bound LVs with the hash's value type, plumbed through a new @scan_case_match_pred_val_t side channel set in the CaseMatchNode handler (sibling of the existing _pred_elem channel).
- spinel_codegen.rb: compile_hash_pattern_arm emits a guarded has_key && has_key && ... if-arm and binds each LocalVariableTargetNode value via the variant's _get. The ArrayPatternNode arm widens its length check to `>=` when rest is present and binds `*name` to a slice for the four scalar array variants.
Fixes #805
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- a976fcfe53b5
- Parent
- 79836f033a1c
spinel_analyze.rbmodifiedView file
@@ -110,6 +110,9 @@
110110
# type each bound LV with the enclosing CaseMatchNode's scrutinee111111
# element type. Issue #669.112112
@scan_case_match_pred_elem = ""113+
# Sibling side-channel for HashPatternNode: the hash's value type.114+
# Issue #805.115+
@scan_case_match_pred_val_t = ""113116
# 1 once analysis has converged and freeze_analysis has filled114117
# @nd_inferred_type. Switches infer_type to consult the cache115118
# first; analysis iterations themselves keep recomputing because@@ -25803,10 +25806,35 @@
2580325806
ap_ri = ap_ri + 12580425807
end2580525808
end25809+
# `case x in {a:, b:}` -- HashPatternNode, the named hash keys25810+
# bind to LVs whose type is the hash's value type. Issue #805.25811+
if @nd_type[nid] == "HashPatternNode"25812+
hp_elems = parse_id_list(@nd_elements[nid])25813+
hp_val_t = @scan_case_match_pred_val_t != "" ? @scan_case_match_pred_val_t : "int"25814+
hp_ei = 025815+
while hp_ei < hp_elems.length25816+
hp_asoc = hp_elems[hp_ei]25817+
if @nd_type[hp_asoc] == "AssocNode"25818+
hp_v = @nd_expression[hp_asoc]25819+
if hp_v >= 0 && @nd_type[hp_v] == "LocalVariableTargetNode"25820+
hp_lname = @nd_name[hp_v]25821+
if not_in(hp_lname, names) == 1 && not_in(hp_lname, params) == 125822+
names.push(hp_lname)25823+
types.push(hp_val_t)25824+
@scan_literal_flags.push("")25825+
@scan_empty_flags.push("")25826+
@scan_empty_hash_flags.push("")25827+
end25828+
end25829+
end25830+
hp_ei = hp_ei + 125831+
end25832+
end2580625833
# CaseMatchNode: stash the scrutinee's element type so the2580725834
# ArrayPatternNode handler above can type the bound LVs.2580825835
if @nd_type[nid] == "CaseMatchNode"2580925836
saved_pred_elem = @scan_case_match_pred_elem25837+
saved_pred_val_t = @scan_case_match_pred_val_t2581025838
pred_nid = @nd_predicate[nid]2581125839
if pred_nid >= 02581225840
cmt = infer_type(pred_nid)@@ -25823,9 +25851,21 @@
2582325851
else2582425852
@scan_case_match_pred_elem = ""2582525853
end25854+
# Issue #805: stash the hash value type so HashPatternNode's25855+
# LV binding above gets the right element type.25856+
if cmt == "sym_int_hash" || cmt == "str_int_hash"25857+
@scan_case_match_pred_val_t = "int"25858+
elsif cmt == "sym_str_hash" || cmt == "str_str_hash" || cmt == "int_str_hash"25859+
@scan_case_match_pred_val_t = "string"25860+
elsif cmt == "sym_poly_hash" || cmt == "str_poly_hash" || cmt == "poly_poly_hash"25861+
@scan_case_match_pred_val_t = "poly"25862+
else25863+
@scan_case_match_pred_val_t = ""25864+
end2582625865
end2582725866
scan_locals_children(nid, names, types, params)2582825867
@scan_case_match_pred_elem = saved_pred_elem25868+
@scan_case_match_pred_val_t = saved_pred_val_t2582925869
return2583025870
end2583125871
if @nd_type[nid] == "MultiWriteNode"spinel_codegen.rbmodifiedView file
Loading diff…
spinel_parse.cmodifiedView file
Loading diff…