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
emit(" sp_StrStrHash_set(" + rc + ", " + idx + ", " + val + ");") return end # Issue #413: user-class `def []=(key, value)`. The recv is a # typed instance (`obj_<C>`); dispatch to the class's `_aset` # method, with arg boxing matching the method's declared # param types (a `value` param widened to poly takes the rhs # box, a typed param takes it raw). if is_obj_type(rt) == 1 bt_aset = base_type(rt) cname_aset = bt_aset[4, bt_aset.length - 4] ci_aset = find_class_idx(cname_aset) if ci_aset >= 0 owner_aset = find_method_owner(ci_aset, "[]=") if owner_aset != "" owner_ci_aset = find_class_idx(owner_aset) midx_aset = cls_find_method_direct(owner_ci_aset, "[]=") if midx_aset >= 0 ca_aset = compile_typed_call_args(nid, owner_ci_aset, midx_aset, 0) recv_arg_aset = owner_aset == cname_aset ? rc : "(sp_" + owner_aset + " *)" + rc tail_aset = ca_aset != "" ? ", " + ca_aset : "" emit(" sp_" + owner_aset + "__aset(" + recv_arg_aset + tail_aset + ");") return end end end end end # `arr[start, len] = src` where arr is a poly-typed slot (sp_RbVal).test/user_class_aset_dispatch.rbaddedView file
Loading diff…
test/user_class_aset_dispatch.rb.expectedaddedView file
Loading diff…