Commit db41d59e

db41d59ed6c8Merged

Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): cross-type ==/!= between user class and primitive

obj == primitive passed raw primitives to sp_RbVal-widened params (-Werror rejected); primitive == obj fell into a raw (int op ptr) compare; obj != primitive had no !(b == x) synthesis.

  • compile_obj_operator_expr boxes args through the regular typed-dispatch path; != synthesis merges into that dispatch via target_mname (no recursion).
  • compile_eq splits cross-type prim-vs-obj: Numeric LHS with a user == on the obj dispatches symmetrically (mirrors MRI's num_equal in numeric.c, so 5 == Wrapper.new(5) returns true when the user == does); the rest fold to FALSE/TRUE.
  • New compile_typed_call_args_by_name slots into the existing dispatch-helper family.
  • Test covers all 7 primitives in both directions, plus a Wrapper whose == reads other so the boxing round-trip is verified.

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

Details

Commit
db41d59ed6c8
Parent
c465f83ed5c2

spinel_codegen.rbmodifiedView file

@@ -24946,45 +24946,50 @@
2494624946 cname = lt[4, lt.length - 4]
2494724947 ci = find_class_idx(cname)
2494824948 if ci >= 0
24949- owner = find_method_owner(ci, mname)
24949+ # MRI's default `!=` is `!(self == other)`. Synthesize when
24950+ # the class defines `==` but not `!=` -- otherwise this call
24951+ # falls into compile_eq's raw fallback and breaks under
24952+ # -Werror.
24953+ synth_neq = mname == "!=" &&
24954+ find_method_owner(ci, "!=") == "" &&
24955+ find_method_owner(ci, "==") != ""
24956+ target_mname = synth_neq ? "==" : mname
24957+
24958+ owner = find_method_owner(ci, target_mname)
2495024959 if owner != ""
24951- ca = compile_call_args(nid)
24960+ # Box args to match the dispatched method's param types. A
24961+ # widened `==(sp_RbVal other)` called with raw `mrb_int` /
24962+ # `const char *` is the original -Werror failure.
24963+ ca = compile_typed_call_args_by_name(nid, owner, target_mname)
24964+ rc = compile_expr_gc_rooted(recv)
24965+ recv_arg = owner == cname ? rc : "(sp_" + owner + " *)" + rc
24966+ tail = ca != "" ? ", " + ca : ""
24967+ call = "sp_" + owner + "_" + sanitize_name(target_mname) + "(" + recv_arg + tail + ")"
24968+ return synth_neq ? "(!" + call + ")" : call
24969+ end
24970+
24971+ cmp_owner = find_method_owner(ci, "<=>")
24972+ if cmp_owner != ""
24973+ ca = compile_typed_call_args_by_name(nid, cmp_owner, "<=>")
2495224974 rc = compile_expr_gc_rooted(recv)
24953- if owner == cname
24954- if ca != ""
24955- return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ", " + ca + ")"
24956- else
24957- return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ")"
24958- end
24959- else
24960- if ca != ""
24961- return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ", " + ca + ")"
24962- else
24963- return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ")"
24964- end
24975+ cmp_call = "sp_" + cmp_owner + "__cmp(" + rc + ", " + ca + ")"
24976+ if mname == "<"
24977+ return "(" + cmp_call + " < 0)"
24978+ end
24979+ if mname == ">"
24980+ return "(" + cmp_call + " > 0)"
24981+ end
24982+ if mname == "<="
24983+ return "(" + cmp_call + " <= 0)"
24984+ end
24985+ if mname == ">="
24986+ return "(" + cmp_call + " >= 0)"
24987+ end
24988+ if mname == "=="
24989+ return "(" + cmp_call + " == 0)"
2496524990 end
24966- else
24967- # Check if class has <=> (Comparable) for comparison operators
24968- cmp_owner = find_method_owner(ci, "<=>")
24969- if cmp_owner != ""
24970- ca = compile_call_args(nid)
24971- rc = compile_expr_gc_rooted(recv)
24972- cmp_call = "sp_" + cmp_owner + "__cmp(" + rc + ", " + ca + ")"
24973- if mname == "<"
24974- return "(" + cmp_call + " < 0)"
24975- end
24976- if mname == ">"
24977- return "(" + cmp_call + " > 0)"
24978- end
24979- if mname == "<="
24980- return "(" + cmp_call + " <= 0)"
24981- end
24982- if mname == ">="
24983- return "(" + cmp_call + " >= 0)"
24984- end
24985- if mname == "=="
24986- return "(" + cmp_call + " == 0)"
24987- end
24991+ if mname == "!="
24992+ return "(" + cmp_call + " != 0)"
2498824993 end
2498924994 end
2499024995 end
@@ -30060,6 +30065,39 @@
3006030065 if arg_id >= 0
3006130066 at = infer_type(arg_id)
3006230067 end
30068+ # Cross-type prim-vs-obj. Integer#== / Float#== fall back to
30069+ # `other == self` via num_equal (numeric.c); String, Symbol,
30070+ # Nil, True, False are type-strict with no fallback. obj-LHS
30071+ # is handled in compile_obj_operator_expr -- this path is
30072+ # `prim == obj` plus the obj-LHS no-`==`-no-`<=>` fall-through.
30073+ lhs_obj = is_obj_type(lt)
30074+ rhs_obj = is_obj_type(at)
30075+ if lhs_obj != rhs_obj
30076+ other_t = lhs_obj == 1 ? at : lt
30077+ if lhs_obj == 0 && (other_t == "int" || other_t == "float")
30078+ cname = at[4, at.length - 4]
30079+ ci = find_class_idx(cname)
30080+ if ci >= 0
30081+ owner = find_method_owner(ci, "==")
30082+ if owner != ""
30083+ owner_ci = find_class_idx(owner)
30084+ midx = cls_find_method_direct(owner_ci, "==")
30085+ ptypes = cls_meth_ptypes_get(owner_ci, midx)
30086+ boxed = ptypes.length > 0 ? compile_expr_for_expected_type(recv, ptypes[0]) : compile_expr(recv)
30087+ obj_expr = compile_expr_gc_rooted(arg_id)
30088+ recv_arg = owner == cname ? obj_expr : "(sp_" + owner + " *)" + obj_expr
30089+ call = "sp_" + owner + "_" + sanitize_name("==") + "(" + recv_arg + ", " + boxed + ")"
30090+ return op == "==" ? call : "(!" + call + ")"
30091+ end
30092+ end
30093+ end
30094+ # Type-strict primitive, or Numeric vs an obj with no user `==`
30095+ # (BasicObject#== identity -> false in MRI).
30096+ if other_t == "int" || other_t == "float" || other_t == "bool" || other_t == "nil" ||
30097+ other_t == "string" || other_t == "mutable_str" || other_t == "symbol"
30098+ return op == "==" ? "FALSE" : "TRUE"
30099+ end
30100+ end
3006330101 lc = compile_expr(recv)
3006430102 rc = "0"
3006530103 if arg_id >= 0
@@ -31287,6 +31325,16 @@
3128731325 tmp
3128831326 end
3128931327
31328+ # compile_typed_call_args for callers that hold the owner / method
31329+ # names rather than pre-resolved indices. Untyped fallback on miss.
31330+ def compile_typed_call_args_by_name(nid, owner, mname, omit_trailing = 0)
31331+ owner_ci = find_class_idx(owner)
31332+ return compile_call_args(nid) if owner_ci < 0
31333+ midx = cls_find_method_direct(owner_ci, mname)
31334+ return compile_call_args(nid) if midx < 0
31335+ compile_typed_call_args(nid, owner_ci, midx, omit_trailing)
31336+ end
31337+
3129031338 def compile_typed_call_args(nid, target_ci, target_midx, omit_trailing, root_constructor_args = 0)
3129131339 # Like compile_call_args but casts arguments to match target method param
3129231340 # types AND fills in defaults from @cls_meth_defaults for trailing