Commit 531e8355

531e8355ececMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): attr_writer setter expression detected as bigint when slot promoted

compile_expr_for_expected_type's post-promotion override looked at the rhs's static type via cached at = infer_type(nid). For an attr_writer setter call (obj.attr = v) whose ivar slot was promoted to bigint, the cache was stale at "int" and the override fell through to wrap the already-bigint emit with sp_bigint_new _int — producing lv_local = sp_bigint_new_int((b->iv_n = sp_bigint_new_int(42))) double-wrap.

Extend the override to:

  • Unwrap a ParenthesesNode wrapper before checking @nd_type so local = (b.n = 42) peeks through to the CallNode.
  • Detect the attr_writer setter case (mname ends with =, the recv class has the attr_writer) and infer the call's value type from the slot's now-promoted bigint type.

Also extend compile_call_stmt's short-circuited attr_writer arm with the same detection so b1.n = b2.n = 7 chains pass the inner-setter's bigint value through without re-wrapping.

Promote-mode test count: 509 pass / 47 fail / 62 error -> 510 pass / 47 fail / 61 error. Default raise mode unchanged at 618 / 0 / 0.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
531e8355ecec
Parent
057759034cbd

spinel_codegen.rbmodifiedView file

@@ -5544,14 +5544,26 @@
55445544 # cached `at` here still says "int". Mirror compile_operator_
55455545 # expr's recv-type check so we don't wrap the already-bigint
55465546 # emit with another sp_bigint_new_int.
5547- if at != "bigint" && @nd_type[nid] == "CallNode"
5548- cm_recv_p = @nd_receiver[nid]
5549- cm_mn_p = @nd_name[nid]
5547+ # `(b.n = 42)` parses as ParenthesesNode wrapping a CallNode;
5548+ # peek through so the override below sees the inner call.
5549+ eff_nid_pft = nid
5550+ if nid >= 0 && @nd_type[nid] == "ParenthesesNode"
5551+ pbody_pft = @nd_body[nid]
5552+ if pbody_pft >= 0
5553+ pstmts_pft = get_stmts(pbody_pft)
5554+ if pstmts_pft.length == 1
5555+ eff_nid_pft = pstmts_pft[0]
5556+ end
5557+ end
5558+ end
5559+ if at != "bigint" && @nd_type[eff_nid_pft] == "CallNode"
5560+ cm_recv_p = @nd_receiver[eff_nid_pft]
5561+ cm_mn_p = @nd_name[eff_nid_pft]
55505562 if cm_recv_p >= 0 && (cm_mn_p == "+" || cm_mn_p == "-" || cm_mn_p == "*" || cm_mn_p == "/" || cm_mn_p == "%" || cm_mn_p == "**" || cm_mn_p == "<<" || cm_mn_p == ">>" || cm_mn_p == "&" || cm_mn_p == "|" || cm_mn_p == "^")
55515563 if base_type(infer_type(cm_recv_p)) == "bigint"
55525564 at = "bigint"
55535565 else
5554- cm_args_p = @nd_arguments[nid]
5566+ cm_args_p = @nd_arguments[eff_nid_pft]
55555567 if cm_args_p >= 0
55565568 cm_aargs_p = get_args(cm_args_p)
55575569 if cm_aargs_p.length > 0 && base_type(infer_type(cm_aargs_p[0])) == "bigint"
@@ -5570,6 +5582,19 @@
55705582 rcname_p = recv_t_p[4, recv_t_p.length - 4]
55715583 rci_p = find_class_idx(rcname_p)
55725584 if rci_p >= 0
5585+ # attr_writer setter call (`obj.attr = v`) returns the rhs.
5586+ # The codegen emits as the boxed expr form, so the call's
5587+ # value carries the slot's type — bigint when the ivar slot
5588+ # was promoted.
5589+ if cm_mn_p.length > 1 && cm_mn_p[cm_mn_p.length - 1] == "=" && cm_mn_p != "==" && cm_mn_p != "!=" && cm_mn_p != "<=" && cm_mn_p != ">="
5590+ bname_aw = cm_mn_p[0, cm_mn_p.length - 1]
5591+ if cls_has_attr_writer(rci_p, bname_aw) == 1
5592+ slot_aw = cls_ivar_type(rci_p, "@" + bname_aw)
5593+ if base_type(slot_aw) == "bigint"
5594+ at = "bigint"
5595+ end
5596+ end
5597+ end
55735598 mr_p = cls_method_return(rci_p, cm_mn_p)
55745599 if base_type(mr_p) == "bigint"
55755600 at = "bigint"
@@ -28537,6 +28562,41 @@
2853728562 arg_ids_aw = get_args(args_id_aw)
2853828563 if arg_ids_aw.length > 0
2853928564 arg_t_aw = infer_type(arg_ids_aw[0])
28565+ # Check if the rhs is itself a promote-mode setter or
28566+ # bigint-returning method call whose cached type is stale.
28567+ # Mirror the compile_expr_for_expected_type override so a
28568+ # `b1.n = b2.n = 7` chain doesn't double-wrap.
28569+ inner_aw = arg_ids_aw[0]
28570+ if inner_aw >= 0 && @nd_type[inner_aw] == "ParenthesesNode"
28571+ pbody_in = @nd_body[inner_aw]
28572+ if pbody_in >= 0
28573+ pstmts_in = get_stmts(pbody_in)
28574+ if pstmts_in.length == 1
28575+ inner_aw = pstmts_in[0]
28576+ end
28577+ end
28578+ end
28579+ if arg_t_aw != "bigint" && inner_aw >= 0 && @nd_type[inner_aw] == "CallNode"
28580+ rcv_in_aw = @nd_receiver[inner_aw]
28581+ mn_in_aw = @nd_name[inner_aw]
28582+ if rcv_in_aw >= 0
28583+ rt_in_aw = base_type(infer_type(rcv_in_aw))
28584+ if is_obj_type(rt_in_aw) == 1
28585+ rcn_in_aw = rt_in_aw[4, rt_in_aw.length - 4]
28586+ rci_in_aw = find_class_idx(rcn_in_aw)
28587+ if rci_in_aw >= 0
28588+ if mn_in_aw.length > 1 && mn_in_aw[mn_in_aw.length - 1] == "=" && mn_in_aw != "==" && mn_in_aw != "!=" && mn_in_aw != "<=" && mn_in_aw != ">="
28589+ bn_in_aw = mn_in_aw[0, mn_in_aw.length - 1]
28590+ if cls_has_attr_writer(rci_in_aw, bn_in_aw) == 1
28591+ if base_type(cls_ivar_type(rci_in_aw, "@" + bn_in_aw)) == "bigint"
28592+ arg_t_aw = "bigint"
28593+ end
28594+ end
28595+ end
28596+ end
28597+ end
28598+ end
28599+ end
2854028600 end
2854128601 end
2854228602 if slot_t == "poly"