Commit 25a2866f

25a2866f5602Merged

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

Description

fix(codegen): user-class def []= dispatched at call site (#413)

A class method def []=(key, value) that dispatched on key and assigned into ivars of mixed types compiled the [] = body correctly but silently dropped the call site f[:id] = 42 - compile_bracket_assign had arms for typed arrays / typed hashes / poly_array / poly slots, but no arm for a user-class receiver. The IndexAssignNode produced no output and the program ran with the ivars never written.

Fix: add an is_obj_type(rt) arm at the end of compile_bracket_assign. It walks the inheritance chain via find_method_owner(ci, "[]=") to locate the defining class, then dispatches to the typed _aset helper:

sp___aset(recv, key, value);

Arg boxing matches the method's declared param types via compile_typed_call_args. So a value param widened to sp_RbVal (because the case branches assign to ivars of mixed concrete types) takes a boxed rhs; a homogeneously- typed param takes it raw.

The accompanying lv_value parameter monomorphisation concern from the issue body — that the function signature committed lv_value to mrb_int — was already addressed by unrelated inference passes by the time this fix landed; the signature now correctly reads sp_RbVal lv_value for the multi-type-ivar case. The remaining gap was purely the missing call-site dispatch.

Test user_class_aset_dispatch.rb covers (a) the mixed-type shape (int + string ivars) from the issue's repro and (b) a homogeneous-type variant. Subclass-receiver coverage was attempted but tripped a separate attr_reader-inheritance bug that's out of scope for this PR; the test stays simple.

Closes #413

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
25a2866f5602
Parent
4044da9afd67

spinel_codegen.rbmodifiedView file

@@ -23637,6 +23637,30 @@
2363723637 emit(" sp_StrStrHash_set(" + rc + ", " + idx + ", " + val + ");")
2363823638 return
2363923639 end
23640+ # Issue #413: user-class `def []=(key, value)`. The recv is a
23641+ # typed instance (`obj_<C>`); dispatch to the class's `_aset`
23642+ # method, with arg boxing matching the method's declared
23643+ # param types (a `value` param widened to poly takes the rhs
23644+ # box, a typed param takes it raw).
23645+ if is_obj_type(rt) == 1
23646+ bt_aset = base_type(rt)
23647+ cname_aset = bt_aset[4, bt_aset.length - 4]
23648+ ci_aset = find_class_idx(cname_aset)
23649+ if ci_aset >= 0
23650+ owner_aset = find_method_owner(ci_aset, "[]=")
23651+ if owner_aset != ""
23652+ owner_ci_aset = find_class_idx(owner_aset)
23653+ midx_aset = cls_find_method_direct(owner_ci_aset, "[]=")
23654+ if midx_aset >= 0
23655+ ca_aset = compile_typed_call_args(nid, owner_ci_aset, midx_aset, 0)
23656+ recv_arg_aset = owner_aset == cname_aset ? rc : "(sp_" + owner_aset + " *)" + rc
23657+ tail_aset = ca_aset != "" ? ", " + ca_aset : ""
23658+ emit(" sp_" + owner_aset + "__aset(" + recv_arg_aset + tail_aset + ");")
23659+ return
23660+ end
23661+ end
23662+ end
23663+ end
2364023664 end
2364123665
2364223666 # `arr[start, len] = src` where arr is a poly-typed slot (sp_RbVal).