Commit da7fb97d

da7fb97d0cbfMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): op-assign on obj slot dispatches user-defined operator

slot OP= rhs semantically desugars to slot = slot OP rhs. The previous codegen emitted a raw C slot OP= rhs for every slot type — on an obj-typed slot the C compiler interpreted it as pointer arithmetic, silently miscompiling cases like

class Box
  def +(other); Box.new(@n + other); end
end
@box = Box.new(5)
@box += 1            # was: pointer + 1 (sizeof(Box) bytes!)

Add obj_op_dispatch_expr: when the receiver type is obj_<X> and X (or an ancestor) defines the operator, return sp_<owner>_<op>(<recv>, <rhs>) so the assignment goes through the user method. When the class doesn't define the operator, emit the standard cannot resolve call to '<op>' on obj_<X> warning, mirroring the same fallback applied at every other unresolved obj method site.

Wired into the InstanceVariableOperatorWriteNode and LocalVariableOperatorWriteNode paths (both statement and expression forms). Existing int / numeric / string / bigint / hash op-assign behavior is unchanged.

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

Details

Commit
da7fb97d0cbf
Parent
1b82c20f5c96

spinel_codegen.rbmodifiedView file

@@ -17509,7 +17509,15 @@
1750917509 # outer write zeroes the ivar.
1751017510 op = @nd_binop[nid]
1751117511 val = compile_expr(@nd_expression[nid])
17512- return "(" + fiber_var_ref(@nd_name[nid]) + " " + op + "= " + val + ")"
17512+ vref = fiber_var_ref(@nd_name[nid])
17513+ vt = find_var_type(@nd_name[nid])
17514+ # Obj-typed local: dispatch via user-defined operator instead
17515+ # of emitting raw C `OP=` (pointer arithmetic on an obj slot).
17516+ disp = obj_op_dispatch_expr(vt, vref, op, val)
17517+ if disp != ""
17518+ return "(" + vref + " = " + disp + ")"
17519+ end
17520+ return "(" + vref + " " + op + "= " + val + ")"
1751317521 end
1751417522 if t == "LocalVariableOrWriteNode"
1751517523 # `local ||= expr` in expression context. Lower as
@@ -24542,6 +24550,32 @@
2454224550 ""
2454324551 end
2454424552
24553+ # `recv OP rhs` lowering for an obj-typed receiver. When the
24554+ # receiver's class (or an ancestor) defines `op` as a user method,
24555+ # returns the C expression `sp_<owner>_<op>(<recv_c>, <rhs_c>)`.
24556+ # Returns "" when no dispatch applies (caller falls back to its
24557+ # inline path, or to `warn_unresolved_call`).
24558+ def obj_op_dispatch_expr(recv_t, recv_c, op, rhs_c)
24559+ if is_obj_type(recv_t) == 0
24560+ return ""
24561+ end
24562+ cname = recv_t[4, recv_t.length - 4]
24563+ ci = find_class_idx(cname)
24564+ if ci < 0
24565+ return ""
24566+ end
24567+ owner = find_method_owner(ci, op)
24568+ if owner == ""
24569+ warn_unresolved_call(op, "obj_" + cname)
24570+ return ""
24571+ end
24572+ cast = ""
24573+ if owner != cname
24574+ cast = "(sp_" + owner + " *)"
24575+ end
24576+ "sp_" + owner + "_" + sanitize_name(op) + "(" + cast + recv_c + ", " + rhs_c + ")"
24577+ end
24578+
2454524579 def compile_if_expr(nid)
2454624580 cond = compile_cond_expr(@nd_predicate[nid])
2454724581 then_val = "0"
@@ -25049,6 +25083,14 @@
2504925083 val = compile_expr(@nd_expression[nid])
2505025084 vref = fiber_var_ref(@nd_name[nid])
2505125085 vt = find_var_type(@nd_name[nid])
25086+ # Same desugaring as the ivar form: `local OP= rhs` is
25087+ # `local = local OP rhs`. Dispatch through the user-defined
25088+ # operator when the local is obj-typed.
25089+ disp = obj_op_dispatch_expr(vt, vref, op, val)
25090+ if disp != ""
25091+ emit(" " + vref + " = " + disp + ";")
25092+ return
25093+ end
2505225094 if vt == "bigint"
2505325095 at = infer_type(@nd_expression[nid])
2505425096 barg = at == "bigint" ? val : "sp_bigint_new_int(" + val + ")"
@@ -25252,6 +25294,19 @@
2525225294 op = @nd_binop[nid]
2525325295 val = compile_expr(@nd_expression[nid])
2525425296 lhs = ivar_lhs(@nd_name[nid])
25297+ ivar_t = ""
25298+ if @current_class_idx >= 0
25299+ ivar_t = cls_ivar_type(@current_class_idx, @nd_name[nid])
25300+ end
25301+ # `@x OP= v` desugars to `@x = @x OP v`. When @x is obj-typed
25302+ # and the class defines OP, dispatch to the user method
25303+ # instead of emitting a raw C `+=` (which on a pointer is
25304+ # silently miscompiled as pointer arithmetic).
25305+ disp = obj_op_dispatch_expr(ivar_t, lhs, op, val)
25306+ if disp != ""
25307+ emit(" " + lhs + " = " + disp + ";")
25308+ return
25309+ end
2525525310 if op == "+"
2525625311 emit(" " + lhs + " += " + val + ";")
2525725312 end