Commit f0b499e8
f0b499e86711Merged
Yusuke Endoh <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified
Description
fix(codegen): emit ivar op-assign for all binary ops + as expression value
The InstanceVariableOperatorWriteNode statement-form arm only handled
+ and -, so @a &= v, @a |= v, @a *= v, @a <<= v, etc. were
silently dropped — the function body emitted no iv_<name> write, so
every read after kept the pre-call value. Extend the arm to mirror the
LocalVariableOperatorWriteNode coverage (*, /, %, <<, >>,
&, |, ^, plus the +/string-concat special case).
The same node had no compile_expr arm at all, so when used as the RHS
of a chained write (@b = @a &= 0x80), it fell through to the
generic-expression default 0. The inner @a &= never emitted and
the outer @b = … saw the literal 0 — both ivars ended up wrong.
Add an expression-form arm that returns the parenthesized
compound-assign expression ((self->iv_a &= v)) so the side effect
fires AND the C-comma value is the new ivar value, with the same
obj_op_dispatch_expr hook the local-var arm uses for user-class
operators.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- f0b499e86711
- Parent
- 59f48e594a9d
spinel_codegen.rbmodifiedView file
end return "(" + vref + " " + op + "= " + val + ")" end if t == "InstanceVariableOperatorWriteNode" # `@x OP= expr` used as an expression — typically the RHS of a # chained ivar write like `@b = @a &= 0x80`. Without this arm # compile_expr falls through to the catch-all `0`, so the inner # `@a &=` write never emits and the outer `@b = ...` zeros the # slot. Mirror the LocalVariableOperatorWriteNode shape — return # a parenthesized C-style compound assignment so the side effect # fires AND the value is the result. op_iow = @nd_binop[nid] val_iow = compile_expr(@nd_expression[nid]) lhs_iow = ivar_lhs(@nd_name[nid]) ivar_t_iow = "" if @current_class_idx >= 0 ivar_t_iow = cls_ivar_type(@current_class_idx, @nd_name[nid]) end disp_iow = obj_op_dispatch_expr(ivar_t_iow, lhs_iow, op_iow, val_iow) if disp_iow != "" return "(" + lhs_iow + " = " + disp_iow + ")" end return "(" + lhs_iow + " " + op_iow + "= " + val_iow + ")" end if t == "LocalVariableOrWriteNode" # `local ||= expr` in expression context. Lower as # `(local = local ? local : expr)` so the side effect runs emit(" " + lhs + " = " + disp + ";") return end # Bitwise / shift / arithmetic op-assigns map directly onto C # `OP=`. Without these arms `@a &= v`, `@a |= v`, etc. were # silently dropped — the function emit fell through with no # `iv_<name>` write, so every read after kept the pre-call # value. Mirror the LocalVariableOperatorWriteNode arm. if op == "+" emit(" " + lhs + " += " + val + ";") if ivar_t == "string" && infer_type(@nd_expression[nid]) == "string" emit(" " + lhs + " = sp_str_concat(" + lhs + ", " + val + ");") else emit(" " + lhs + " += " + val + ";") end return end if op == "-" emit(" " + lhs + " -= " + val + ";") return end if op == "*" emit(" " + lhs + " *= " + val + ";") return end if op == "/" emit(" " + lhs + " = sp_idiv(" + lhs + ", " + val + ");") return end if op == "%" emit(" " + lhs + " = sp_imod(" + lhs + ", " + val + ");") return end if op == "<<" || op == ">>" || op == "&" || op == "|" || op == "^" emit(" " + lhs + " " + op + "= " + val + ";") return end return endtest/chained_ivar_op_assign_emits_inner_write.rbaddedView file
Loading diff…
test/chained_ivar_op_assign_emits_inner_write.rb.expectedaddedView file
Loading diff…
test/ivar_op_assign_bitwise_and_shift.rbaddedView file
Loading diff…
test/ivar_op_assign_bitwise_and_shift.rb.expectedaddedView file
Loading diff…