Commit b9193545
b9193545b820Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): obj_op_dispatch coerces rhs to user-method's promoted param
obj.op rhs and obj.op= rhs user-defined operator dispatch built
sp_<Klass>_<op>(recv, rhs) raw. With promote mode widening the
op method's first param to bigint, an int rhs literal/local
tripped -Wint-conversion (makes pointer from integer).
Extend obj_op_dispatch_expr with an optional rhs_t parameter;
when provided, consult the user op-method's recorded ptypes and
wrap rhs through sp_bigint_new_int / sp_bigint_to_int as needed.
Threading the rhs's inferred type from compile_stmt's three
op-write arms (ivar / LV op write — both compile_expr and
compile_stmt forms) — the legacy 4-arg shape still works for
non-promote-aware callers.
Promote-mode test count: 520 pass / 49 fail / 49 error -> 521 pass / 49 fail / 48 error. Default raise mode unchanged at 618 / 0 / 0.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- b9193545b820
- Parent
- 87bc59b8d6b5
spinel_codegen.rbmodifiedView file
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) disp_iow = obj_op_dispatch_expr(ivar_t_iow, lhs_iow, op_iow, val_iow, infer_type(@nd_expression[nid])) if disp_iow != "" return "(" + lhs_iow + " = " + disp_iow + ")" end # 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) def obj_op_dispatch_expr(recv_t, recv_c, op, rhs_c, rhs_t = "") if is_obj_type(recv_t) == 0 return "" end if owner != cname cast = "(sp_" + owner + " *)" end "sp_" + owner + "_" + sanitize_name(op) + "(" + cast + recv_c + ", " + rhs_c + ")" # When the user method's first param has been promoted to bigint # by `--int-overflow=promote`, an int rhs needs the bigint wrap # to match the C-level signature. Same shape for the reverse. rhs_coerced = rhs_c owner_ci = find_class_idx(owner) if owner_ci >= 0 && rhs_t != "" && rhs_t != "" midx_oo = cls_find_method_direct(owner_ci, op) if midx_oo >= 0 pts_oo = cls_meth_ptypes_get(owner_ci, midx_oo) if pts_oo.length > 0 first_pt_oo = base_type(pts_oo[0]) if first_pt_oo == "bigint" && rhs_t == "int" @needs_bigint = 1 rhs_coerced = "sp_bigint_new_int(" + rhs_c + ")" elsif first_pt_oo == "int" && rhs_t == "bigint" @needs_bigint = 1 rhs_coerced = "sp_bigint_to_int((sp_Bigint *)" + rhs_c + ")" end end end end "sp_" + owner + "_" + sanitize_name(op) + "(" + cast + recv_c + ", " + rhs_coerced + ")" end def if_branch_has_multi_stmt(body) # 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) disp = obj_op_dispatch_expr(vt, vref, op, val, infer_type(@nd_expression[nid])) if disp != "" emit(" " + vref + " = " + disp + ";") return # 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) disp = obj_op_dispatch_expr(ivar_t, lhs, op, val, infer_type(@nd_expression[nid])) if disp != "" emit(" " + lhs + " = " + disp + ";") return