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
# outer write zeroes the ivar. op = @nd_binop[nid] val = compile_expr(@nd_expression[nid]) return "(" + fiber_var_ref(@nd_name[nid]) + " " + op + "= " + val + ")" vref = fiber_var_ref(@nd_name[nid]) vt = find_var_type(@nd_name[nid]) # Obj-typed local: dispatch via user-defined operator instead # of emitting raw C `OP=` (pointer arithmetic on an obj slot). disp = obj_op_dispatch_expr(vt, vref, op, val) if disp != "" return "(" + vref + " = " + disp + ")" end return "(" + vref + " " + op + "= " + val + ")" end if t == "LocalVariableOrWriteNode" # `local ||= expr` in expression context. Lower as "" end # `recv OP rhs` lowering for an obj-typed receiver. When the # receiver's class (or an ancestor) defines `op` as a user method, # returns the C expression `sp_<owner>_<op>(<recv_c>, <rhs_c>)`. # Returns "" when no dispatch applies (caller falls back to its # inline path, or to `warn_unresolved_call`). def obj_op_dispatch_expr(recv_t, recv_c, op, rhs_c) if is_obj_type(recv_t) == 0 return "" end cname = recv_t[4, recv_t.length - 4] ci = find_class_idx(cname) if ci < 0 return "" end owner = find_method_owner(ci, op) if owner == "" warn_unresolved_call(op, "obj_" + cname) return "" end cast = "" if owner != cname cast = "(sp_" + owner + " *)" end "sp_" + owner + "_" + sanitize_name(op) + "(" + cast + recv_c + ", " + rhs_c + ")" end def compile_if_expr(nid) cond = compile_cond_expr(@nd_predicate[nid]) then_val = "0" val = compile_expr(@nd_expression[nid]) vref = fiber_var_ref(@nd_name[nid]) vt = find_var_type(@nd_name[nid]) # Same desugaring as the ivar form: `local OP= rhs` is # `local = local OP rhs`. Dispatch through the user-defined # operator when the local is obj-typed. disp = obj_op_dispatch_expr(vt, vref, op, val) if disp != "" emit(" " + vref + " = " + disp + ";") return end if vt == "bigint" at = infer_type(@nd_expression[nid]) barg = at == "bigint" ? val : "sp_bigint_new_int(" + val + ")" op = @nd_binop[nid] val = compile_expr(@nd_expression[nid]) lhs = ivar_lhs(@nd_name[nid]) ivar_t = "" if @current_class_idx >= 0 ivar_t = cls_ivar_type(@current_class_idx, @nd_name[nid]) end # `@x OP= v` desugars to `@x = @x OP v`. When @x is obj-typed # and the class defines OP, dispatch to the user method # instead of emitting a raw C `+=` (which on a pointer is # silently miscompiled as pointer arithmetic). disp = obj_op_dispatch_expr(ivar_t, lhs, op, val) if disp != "" emit(" " + lhs + " = " + disp + ";") return end if op == "+" emit(" " + lhs + " += " + val + ";") endtest/op_assign_user_operator.rbaddedView file
Loading diff…