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, so5 == 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
==readsotherso 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 >= 024949-
owner = find_method_owner(ci, mname)24949+
# MRI's default `!=` is `!(self == other)`. Synthesize when24950+
# the class defines `==` but not `!=` -- otherwise this call24951+
# falls into compile_eq's raw fallback and breaks under24952+
# -Werror.24953+
synth_neq = mname == "!=" &&24954+
find_method_owner(ci, "!=") == "" &&24955+
find_method_owner(ci, "==") != ""24956+
target_mname = synth_neq ? "==" : mname24957+
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. A24961+
# 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 + " *)" + rc24966+
tail = ca != "" ? ", " + ca : ""24967+
call = "sp_" + owner + "_" + sanitize_name(target_mname) + "(" + recv_arg + tail + ")"24968+
return synth_neq ? "(!" + call + ")" : call24969+
end24970+
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 == cname24954-
if ca != ""24955-
return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ", " + ca + ")"24956-
else24957-
return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ")"24958-
end24959-
else24960-
if ca != ""24961-
return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ", " + ca + ")"24962-
else24963-
return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ")"24964-
end24975+
cmp_call = "sp_" + cmp_owner + "__cmp(" + rc + ", " + ca + ")"24976+
if mname == "<"24977+
return "(" + cmp_call + " < 0)"24978+
end24979+
if mname == ">"24980+
return "(" + cmp_call + " > 0)"24981+
end24982+
if mname == "<="24983+
return "(" + cmp_call + " <= 0)"24984+
end24985+
if mname == ">="24986+
return "(" + cmp_call + " >= 0)"24987+
end24988+
if mname == "=="24989+
return "(" + cmp_call + " == 0)"2496524990
end24966-
else24967-
# Check if class has <=> (Comparable) for comparison operators24968-
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-
end24976-
if mname == ">"24977-
return "(" + cmp_call + " > 0)"24978-
end24979-
if mname == "<="24980-
return "(" + cmp_call + " <= 0)"24981-
end24982-
if mname == ">="24983-
return "(" + cmp_call + " >= 0)"24984-
end24985-
if mname == "=="24986-
return "(" + cmp_call + " == 0)"24987-
end24991+
if mname == "!="24992+
return "(" + cmp_call + " != 0)"2498824993
end2498924994
end2499024995
end@@ -30060,6 +30065,39 @@
3006030065
if arg_id >= 03006130066
at = infer_type(arg_id)3006230067
end30068+
# Cross-type prim-vs-obj. Integer#== / Float#== fall back to30069+
# `other == self` via num_equal (numeric.c); String, Symbol,30070+
# Nil, True, False are type-strict with no fallback. obj-LHS30071+
# is handled in compile_obj_operator_expr -- this path is30072+
# `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_obj30076+
other_t = lhs_obj == 1 ? at : lt30077+
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 >= 030081+
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_expr30089+
call = "sp_" + owner + "_" + sanitize_name("==") + "(" + recv_arg + ", " + boxed + ")"30090+
return op == "==" ? call : "(!" + call + ")"30091+
end30092+
end30093+
end30094+
# 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+
end30100+
end3006330101
lc = compile_expr(recv)3006430102
rc = "0"3006530103
if arg_id >= 0@@ -31287,6 +31325,16 @@
3128731325
tmp3128831326
end3128931327
31328+
# compile_typed_call_args for callers that hold the owner / method31329+
# 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 < 031333+
midx = cls_find_method_direct(owner_ci, mname)31334+
return compile_call_args(nid) if midx < 031335+
compile_typed_call_args(nid, owner_ci, midx, omit_trailing)31336+
end31337+
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 param3129231340
# types AND fills in defaults from @cls_meth_defaults for trailingtest/primitive_eq_user_class.rbaddedView file
Loading diff…
test/primitive_eq_user_class.rb.expectedaddedView file
Loading diff…