Commit acf4c470

acf4c4704ffbMerged

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

Description

Type global variables from multi-assignment targets

A global on the left of a multi-assignment ($a, $b = ...) was only discovered when later read, which defaults its type to int. A non-int right-hand side (string, float, array) or a splat-rest target then declared the C global as mrb_int and the destructuring store failed to compile (gv_x undeclared / assignment from incompatible type).

Register each global multi-assign target in scan_features with the type it destructures to (multi_write_target_type / splat_rest_type), mirroring the local-target typing in scan_locals. Covers left, rest, and right targets, and upgrades an int placeholder left by an earlier read.

Fixes #1286

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

Details

Commit
acf4c4704ffb
Parent
07c2faa78c2e

spinel_analyze.rbmodifiedView file

@@ -22481,6 +22481,37 @@
2248122481 pop_scope
2248222482 end
2248322483
22484+ # Register a global that appears as a multi-assignment target
22485+ # (`$a, $b = ...`, `$x, *$rest = ...`). Without this the slot is only
22486+ # discovered via a later read, which defaults it to `int`, so a
22487+ # non-int RHS (string / array / float) declares the C global as
22488+ # mrb_int and the masgn store fails to compile. Mirrors the
22489+ # GlobalVariableWriteNode path but keyed off the destructured type.
22490+ def register_masgn_global(gname0, gt)
22491+ gname = resolve_gvar_alias(gname0)
22492+ if is_special_gvar(gname) == 1
22493+ return
22494+ end
22495+ if not_in(gname, @gvar_names) == 1
22496+ @gvar_names.push(gname)
22497+ @gvar_types.push(gt)
22498+ @gvar_written.push(1)
22499+ return
22500+ end
22501+ gi = 0
22502+ while gi < @gvar_names.length
22503+ if @gvar_names[gi] == gname
22504+ # Upgrade an `int` placeholder left by an earlier read to the
22505+ # real destructured type; keep an existing concrete type.
22506+ if @gvar_types[gi] == "int" && gt != "int" && gt != "nil"
22507+ @gvar_types[gi] = gt
22508+ end
22509+ @gvar_written[gi] = 1
22510+ end
22511+ gi = gi + 1
22512+ end
22513+ end
22514+
2248422515 def scan_features(nid)
2248522516 if nid < 0
2248622517 return
@@ -22750,6 +22781,46 @@
2275022781 end
2275122782 end
2275222783 end
22784+ if t == "MultiWriteNode"
22785+ # Register global targets of a multi-assignment with the type each
22786+ # one destructures to, so the C global is declared with the right
22787+ # storage. Mirrors the local-target typing in scan_locals.
22788+ val_id_mg = @nd_expression[nid]
22789+ lefts_mg = parse_id_list(@nd_targets[nid])
22790+ ti_mg = 0
22791+ lefts_mg.each { |tid|
22792+ if @nd_type[tid] == "GlobalVariableTargetNode"
22793+ register_masgn_global(@nd_name[tid], multi_write_target_type(val_id_mg, ti_mg))
22794+ end
22795+ ti_mg = ti_mg + 1
22796+ }
22797+ rest_mg = @nd_rest[nid]
22798+ if is_splat_with_target(rest_mg) == 1
22799+ st_mg = @nd_expression[rest_mg]
22800+ if st_mg >= 0 && @nd_type[st_mg] == "GlobalVariableTargetNode"
22801+ register_masgn_global(@nd_name[st_mg], splat_rest_type(val_id_mg))
22802+ end
22803+ end
22804+ rights_mg = parse_id_list(@nd_rights[nid])
22805+ r_total_mg = 0
22806+ if val_id_mg >= 0 && @nd_type[val_id_mg] == "ArrayNode"
22807+ r_total_mg = parse_id_list(@nd_elements[val_id_mg]).length
22808+ end
22809+ r_idx_mg = 0
22810+ rights_mg.each { |tid|
22811+ if @nd_type[tid] == "GlobalVariableTargetNode"
22812+ t_idx_mg = 0
22813+ if r_total_mg > 0
22814+ t_idx_mg = r_total_mg - rights_mg.length + r_idx_mg
22815+ if t_idx_mg < 0
22816+ t_idx_mg = 0
22817+ end
22818+ end
22819+ register_masgn_global(@nd_name[tid], multi_write_target_type(val_id_mg, t_idx_mg))
22820+ end
22821+ r_idx_mg = r_idx_mg + 1
22822+ }
22823+ end
2275322824 if t == "CallNode"
2275422825 mname = @nd_name[nid]
2275522826 # String methods that always need string helpers