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

@@ -11534,7 +11534,7 @@
1153411534 if @current_class_idx >= 0
1153511535 ivar_t_iow = cls_ivar_type(@current_class_idx, @nd_name[nid])
1153611536 end
11537- disp_iow = obj_op_dispatch_expr(ivar_t_iow, lhs_iow, op_iow, val_iow)
11537+ disp_iow = obj_op_dispatch_expr(ivar_t_iow, lhs_iow, op_iow, val_iow, infer_type(@nd_expression[nid]))
1153811538 if disp_iow != ""
1153911539 return "(" + lhs_iow + " = " + disp_iow + ")"
1154011540 end
@@ -24143,7 +24143,7 @@
2414324143 # returns the C expression `sp_<owner>_<op>(<recv_c>, <rhs_c>)`.
2414424144 # Returns "" when no dispatch applies (caller falls back to its
2414524145 # inline path, or to `warn_unresolved_call`).
24146- def obj_op_dispatch_expr(recv_t, recv_c, op, rhs_c)
24146+ def obj_op_dispatch_expr(recv_t, recv_c, op, rhs_c, rhs_t = "")
2414724147 if is_obj_type(recv_t) == 0
2414824148 return ""
2414924149 end
@@ -24161,7 +24161,28 @@
2416124161 if owner != cname
2416224162 cast = "(sp_" + owner + " *)"
2416324163 end
24164- "sp_" + owner + "_" + sanitize_name(op) + "(" + cast + recv_c + ", " + rhs_c + ")"
24164+ # When the user method's first param has been promoted to bigint
24165+ # by `--int-overflow=promote`, an int rhs needs the bigint wrap
24166+ # to match the C-level signature. Same shape for the reverse.
24167+ rhs_coerced = rhs_c
24168+ owner_ci = find_class_idx(owner)
24169+ if owner_ci >= 0 && rhs_t != "" && rhs_t != ""
24170+ midx_oo = cls_find_method_direct(owner_ci, op)
24171+ if midx_oo >= 0
24172+ pts_oo = cls_meth_ptypes_get(owner_ci, midx_oo)
24173+ if pts_oo.length > 0
24174+ first_pt_oo = base_type(pts_oo[0])
24175+ if first_pt_oo == "bigint" && rhs_t == "int"
24176+ @needs_bigint = 1
24177+ rhs_coerced = "sp_bigint_new_int(" + rhs_c + ")"
24178+ elsif first_pt_oo == "int" && rhs_t == "bigint"
24179+ @needs_bigint = 1
24180+ rhs_coerced = "sp_bigint_to_int((sp_Bigint *)" + rhs_c + ")"
24181+ end
24182+ end
24183+ end
24184+ end
24185+ "sp_" + owner + "_" + sanitize_name(op) + "(" + cast + recv_c + ", " + rhs_coerced + ")"
2416524186 end
2416624187
2416724188 def if_branch_has_multi_stmt(body)
@@ -25219,7 +25240,7 @@
2521925240 # Same desugaring as the ivar form: `local OP= rhs` is
2522025241 # `local = local OP rhs`. Dispatch through the user-defined
2522125242 # operator when the local is obj-typed.
25222- disp = obj_op_dispatch_expr(vt, vref, op, val)
25243+ disp = obj_op_dispatch_expr(vt, vref, op, val, infer_type(@nd_expression[nid]))
2522325244 if disp != ""
2522425245 emit(" " + vref + " = " + disp + ";")
2522525246 return
@@ -25609,7 +25630,7 @@
2560925630 # and the class defines OP, dispatch to the user method
2561025631 # instead of emitting a raw C `+=` (which on a pointer is
2561125632 # silently miscompiled as pointer arithmetic).
25612- disp = obj_op_dispatch_expr(ivar_t, lhs, op, val)
25633+ disp = obj_op_dispatch_expr(ivar_t, lhs, op, val, infer_type(@nd_expression[nid]))
2561325634 if disp != ""
2561425635 emit(" " + lhs + " = " + disp + ";")
2561525636 return