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

@@ -20854,6 +20854,27 @@
2085420854 end
2085520855 return "(" + vref + " " + op + "= " + val + ")"
2085620856 end
20857+ if t == "InstanceVariableOperatorWriteNode"
20858+ # `@x OP= expr` used as an expression — typically the RHS of a
20859+ # chained ivar write like `@b = @a &= 0x80`. Without this arm
20860+ # compile_expr falls through to the catch-all `0`, so the inner
20861+ # `@a &=` write never emits and the outer `@b = ...` zeros the
20862+ # slot. Mirror the LocalVariableOperatorWriteNode shape — return
20863+ # a parenthesized C-style compound assignment so the side effect
20864+ # fires AND the value is the result.
20865+ op_iow = @nd_binop[nid]
20866+ val_iow = compile_expr(@nd_expression[nid])
20867+ lhs_iow = ivar_lhs(@nd_name[nid])
20868+ ivar_t_iow = ""
20869+ if @current_class_idx >= 0
20870+ ivar_t_iow = cls_ivar_type(@current_class_idx, @nd_name[nid])
20871+ end
20872+ disp_iow = obj_op_dispatch_expr(ivar_t_iow, lhs_iow, op_iow, val_iow)
20873+ if disp_iow != ""
20874+ return "(" + lhs_iow + " = " + disp_iow + ")"
20875+ end
20876+ return "(" + lhs_iow + " " + op_iow + "= " + val_iow + ")"
20877+ end
2085720878 if t == "LocalVariableOrWriteNode"
2085820879 # `local ||= expr` in expression context. Lower as
2085920880 # `(local = local ? local : expr)` so the side effect runs
@@ -29992,11 +30013,38 @@
2999230013 emit(" " + lhs + " = " + disp + ";")
2999330014 return
2999430015 end
30016+ # Bitwise / shift / arithmetic op-assigns map directly onto C
30017+ # `OP=`. Without these arms `@a &= v`, `@a |= v`, etc. were
30018+ # silently dropped — the function emit fell through with no
30019+ # `iv_<name>` write, so every read after kept the pre-call
30020+ # value. Mirror the LocalVariableOperatorWriteNode arm.
2999530021 if op == "+"
29996- emit(" " + lhs + " += " + val + ";")
30022+ if ivar_t == "string" && infer_type(@nd_expression[nid]) == "string"
30023+ emit(" " + lhs + " = sp_str_concat(" + lhs + ", " + val + ");")
30024+ else
30025+ emit(" " + lhs + " += " + val + ";")
30026+ end
30027+ return
2999730028 end
2999830029 if op == "-"
2999930030 emit(" " + lhs + " -= " + val + ";")
30031+ return
30032+ end
30033+ if op == "*"
30034+ emit(" " + lhs + " *= " + val + ";")
30035+ return
30036+ end
30037+ if op == "/"
30038+ emit(" " + lhs + " = sp_idiv(" + lhs + ", " + val + ");")
30039+ return
30040+ end
30041+ if op == "%"
30042+ emit(" " + lhs + " = sp_imod(" + lhs + ", " + val + ");")
30043+ return
30044+ end
30045+ if op == "<<" || op == ">>" || op == "&" || op == "|" || op == "^"
30046+ emit(" " + lhs + " " + op + "= " + val + ";")
30047+ return
3000030048 end
3000130049 return
3000230050 end