Commit 0a9dceef
0a9dceefe1bcMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): poly attr-writer evaluates rhs only once
The expression-form obj.attr = rhs lowered to a comma expression
(slot = box(arg0_w), arg0_w) that textually substituted the rhs
twice, so a side-effecting rhs (e.g. a method call) ran twice in C.
Spill the rhs to a typed temp via a GCC statement expression so the boxed assignment reads from the temp and the chain returns the temp's value:
({TYPE _t = rhs; slot = box(_t); _t;})
Add a regression that calls a Counter#step rhs three times and verifies the counter advances 1/2/3 (would advance 2/4/6 with the double-eval bug).
Refs #253
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 0a9dceefe1bc
- Parent
- cecd16e7960a
spinel_codegen.rbmodifiedView file
@@ -22299,28 +22299,27 @@
2229922299
while j < writers.length2230022300
if writers[j] == bname2230122301
# Box the rhs when the slot is poly so the assignment22302-
# type-matches at C level. Without this, an obj-typed22303-
# rhs flowing into a poly slot emits22304-
# `slot = ptr_value` with a struct LHS — rejected by22305-
# C compile. Use a comma expression so the value of22306-
# the chain is the *original* rhs (typed), preserving22307-
# Ruby's `obj.attr = v` semantics for downstream22308-
# consumers like `local = obj.attr = v` or22309-
# `puts(obj.attr = v)` where `v`'s static type drives22310-
# the formatter / cast.22302+
# type-matches at C level. Spill the rhs to a typed22303+
# temp first via a statement-expression so its side22304+
# effects run exactly once: the chain's value must be22305+
# the *original* rhs (Ruby's `obj.attr = v` returns22306+
# `v`), so without the temp the comma form would22307+
# textually substitute `arg0_w` twice and re-execute22308+
# any function call inside it.2231122309
slot_t = cls_ivar_type(ci, "@" + bname)22312-
args_id_w = @nd_arguments[nid]2231322310
arg0_w = compile_arg0(nid)2231422311
if slot_t == "poly"2231522312
arg_t = "int"22313+
args_id_w = @nd_arguments[nid]2231622314
if args_id_w >= 02231722315
arg_ids_w = get_args(args_id_w)2231822316
if arg_ids_w.length > 02231922317
arg_t = infer_type(arg_ids_w[0])2232022318
end2232122319
end22322-
boxed = box_value_to_poly(arg_t, arg0_w)22323-
return "(" + rc + arrow + sanitize_ivar(bname) + " = " + boxed + ", " + arg0_w + ")"22320+
tmp_w = new_temp22321+
boxed = box_value_to_poly(arg_t, tmp_w)22322+
return "({" + c_type(arg_t) + " " + tmp_w + " = " + arg0_w + "; " + rc + arrow + sanitize_ivar(bname) + " = " + boxed + "; " + tmp_w + ";})"2232422323
end2232522324
return "(" + rc + arrow + sanitize_ivar(bname) + " = " + arg0_w + ")"2232622325
endtest/attr_writer_poly_no_double_eval.rbaddedView file
Loading diff…
test/attr_writer_poly_no_double_eval.rb.expectedaddedView file
Loading diff…