Commit c1cd009d

c1cd009db5b3Merged

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

Description

Bind array/hash patterns in rightward assignment

[1, 2, 3] => a, b, c bound nothing past the first element: a MatchRequiredNode carrying an array or hash pattern had no analyze or codegen handler, so it fell through to expression position and the destructured locals kept their default 0.

Add a codegen statement handler that reuses the case/in array/hash pattern arm builders with a single matching if plus a raising else (NoMatchingPatternError on a length/shape mismatch). On the analyze side, register the bound locals with the scrutinee's element/value type via the scan-time side channel shared with case/in, now extracted as scan_pred_channels_for. Single-target x => v is unaffected: Prism lowers it to a plain LocalVariableWriteNode.

Fixes #1225

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

Details

Commit
c1cd009db5b3
Parent
edb362fb5792

spinel_analyze.rbmodifiedView file

@@ -28081,6 +28081,35 @@
2808128081 0
2808228082 end
2808328083
28084+ # Set the scan-time side channels that ArrayPatternNode /
28085+ # HashPatternNode read to type their bound locals, given a scrutinee
28086+ # of inferred type `cmt`. Shared by case/in and rightward assignment.
28087+ def scan_pred_channels_for(cmt)
28088+ @scan_case_match_pred_type = cmt
28089+ if cmt == "int_array" || cmt == "sym_array"
28090+ @scan_case_match_pred_elem = "int"
28091+ elsif cmt == "str_array"
28092+ @scan_case_match_pred_elem = "string"
28093+ elsif cmt == "float_array"
28094+ @scan_case_match_pred_elem = "float"
28095+ elsif cmt == "poly_array"
28096+ @scan_case_match_pred_elem = "poly"
28097+ elsif is_ptr_array_type(cmt) == 1
28098+ @scan_case_match_pred_elem = ptr_array_elem_type(cmt)
28099+ else
28100+ @scan_case_match_pred_elem = ""
28101+ end
28102+ if cmt == "sym_int_hash" || cmt == "str_int_hash"
28103+ @scan_case_match_pred_val_t = "int"
28104+ elsif cmt == "sym_str_hash" || cmt == "str_str_hash" || cmt == "int_str_hash"
28105+ @scan_case_match_pred_val_t = "string"
28106+ elsif cmt == "sym_poly_hash" || cmt == "str_poly_hash" || cmt == "poly_poly_hash"
28107+ @scan_case_match_pred_val_t = "poly"
28108+ else
28109+ @scan_case_match_pred_val_t = ""
28110+ end
28111+ end
28112+
2808428113 def scan_locals(nid, names, types, params)
2808528114 if nid < 0
2808628115 return
@@ -28206,6 +28235,24 @@
2820628235 end
2820728236 # CaseMatchNode: stash the scrutinee's element type so the
2820828237 # ArrayPatternNode handler above can type the bound LVs.
28238+ # `value => pattern` (MatchRequiredNode). The scrutinee is the
28239+ # `value`; type the destructured locals from it exactly as case/in
28240+ # types them from its predicate.
28241+ if @nd_type[nid] == "MatchRequiredNode"
28242+ saved_mr_elem = @scan_case_match_pred_elem
28243+ saved_mr_val_t = @scan_case_match_pred_val_t
28244+ saved_mr_type = @scan_case_match_pred_type
28245+ # The AST loader routes the "value" ref to @nd_expression.
28246+ mr_val = @nd_expression[nid]
28247+ if mr_val >= 0
28248+ scan_pred_channels_for(infer_type(mr_val))
28249+ end
28250+ scan_locals_children(nid, names, types, params)
28251+ @scan_case_match_pred_elem = saved_mr_elem
28252+ @scan_case_match_pred_val_t = saved_mr_val_t
28253+ @scan_case_match_pred_type = saved_mr_type
28254+ return
28255+ end
2820928256 if @nd_type[nid] == "CaseMatchNode"
2821028257 saved_pred_elem = @scan_case_match_pred_elem
2821128258 saved_pred_val_t = @scan_case_match_pred_val_t
@@ -28216,32 +28263,9 @@
2821628263 # Issues #884 #905: stash the scrutinee's full type so a bare
2821728264 # `LocalVariableTargetNode` pattern (`case x in n`) or a
2821828265 # `CapturePatternNode` (`case x in [...] => arr`) can bind
28219- # its LV with the right slot type.
28220- @scan_case_match_pred_type = cmt
28221- if cmt == "int_array" || cmt == "sym_array"
28222- @scan_case_match_pred_elem = "int"
28223- elsif cmt == "str_array"
28224- @scan_case_match_pred_elem = "string"
28225- elsif cmt == "float_array"
28226- @scan_case_match_pred_elem = "float"
28227- elsif cmt == "poly_array"
28228- @scan_case_match_pred_elem = "poly"
28229- elsif is_ptr_array_type(cmt) == 1
28230- @scan_case_match_pred_elem = ptr_array_elem_type(cmt)
28231- else
28232- @scan_case_match_pred_elem = ""
28233- end
28234- # Issue #805: stash the hash value type so HashPatternNode's
28235- # LV binding above gets the right element type.
28236- if cmt == "sym_int_hash" || cmt == "str_int_hash"
28237- @scan_case_match_pred_val_t = "int"
28238- elsif cmt == "sym_str_hash" || cmt == "str_str_hash" || cmt == "int_str_hash"
28239- @scan_case_match_pred_val_t = "string"
28240- elsif cmt == "sym_poly_hash" || cmt == "str_poly_hash" || cmt == "poly_poly_hash"
28241- @scan_case_match_pred_val_t = "poly"
28242- else
28243- @scan_case_match_pred_val_t = ""
28244- end
28266+ # its LV with the right slot type. Issue #805: the hash value
28267+ # type feeds HashPatternNode's LV binding.
28268+ scan_pred_channels_for(cmt)
2824528269 end
2824628270 scan_locals_children(nid, names, types, params)
2824728271 @scan_case_match_pred_elem = saved_pred_elem