Commit 97a359a7
97a359a737f4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #409 from ryanseys/rs-primitive-eq-user-class
fix(codegen): cross-type ==/!= between user class and primitive
Details
- Commit
- 97a359a737f4
- Parent
- a7275ef4e912
- Merge parent
- db41d59ed6c8
spinel_codegen.rbmodifiedView file
@@ -11650,45 +11650,50 @@
1165011650
cname = lt[4, lt.length - 4]1165111651
ci = find_class_idx(cname)1165211652
if ci >= 011653-
owner = find_method_owner(ci, mname)11653+
# MRI's default `!=` is `!(self == other)`. Synthesize when11654+
# the class defines `==` but not `!=` -- otherwise this call11655+
# falls into compile_eq's raw fallback and breaks under11656+
# -Werror.11657+
synth_neq = mname == "!=" &&11658+
find_method_owner(ci, "!=") == "" &&11659+
find_method_owner(ci, "==") != ""11660+
target_mname = synth_neq ? "==" : mname11661+
11662+
owner = find_method_owner(ci, target_mname)1165411663
if owner != ""11655-
ca = compile_call_args(nid)11664+
# Box args to match the dispatched method's param types. A11665+
# widened `==(sp_RbVal other)` called with raw `mrb_int` /11666+
# `const char *` is the original -Werror failure.11667+
ca = compile_typed_call_args_by_name(nid, owner, target_mname)11668+
rc = compile_expr_gc_rooted(recv)11669+
recv_arg = owner == cname ? rc : "(sp_" + owner + " *)" + rc11670+
tail = ca != "" ? ", " + ca : ""11671+
call = "sp_" + owner + "_" + sanitize_name(target_mname) + "(" + recv_arg + tail + ")"11672+
return synth_neq ? "(!" + call + ")" : call11673+
end11674+
11675+
cmp_owner = find_method_owner(ci, "<=>")11676+
if cmp_owner != ""11677+
ca = compile_typed_call_args_by_name(nid, cmp_owner, "<=>")1165611678
rc = compile_expr_gc_rooted(recv)11657-
if owner == cname11658-
if ca != ""11659-
return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ", " + ca + ")"11660-
else11661-
return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ")"11662-
end11663-
else11664-
if ca != ""11665-
return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ", " + ca + ")"11666-
else11667-
return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ")"11668-
end11679+
cmp_call = "sp_" + cmp_owner + "__cmp(" + rc + ", " + ca + ")"11680+
if mname == "<"11681+
return "(" + cmp_call + " < 0)"11682+
end11683+
if mname == ">"11684+
return "(" + cmp_call + " > 0)"11685+
end11686+
if mname == "<="11687+
return "(" + cmp_call + " <= 0)"11688+
end11689+
if mname == ">="11690+
return "(" + cmp_call + " >= 0)"11691+
end11692+
if mname == "=="11693+
return "(" + cmp_call + " == 0)"1166911694
end11670-
else11671-
# Check if class has <=> (Comparable) for comparison operators11672-
cmp_owner = find_method_owner(ci, "<=>")11673-
if cmp_owner != ""11674-
ca = compile_call_args(nid)11675-
rc = compile_expr_gc_rooted(recv)11676-
cmp_call = "sp_" + cmp_owner + "__cmp(" + rc + ", " + ca + ")"11677-
if mname == "<"11678-
return "(" + cmp_call + " < 0)"11679-
end11680-
if mname == ">"11681-
return "(" + cmp_call + " > 0)"11682-
end11683-
if mname == "<="11684-
return "(" + cmp_call + " <= 0)"11685-
end11686-
if mname == ">="11687-
return "(" + cmp_call + " >= 0)"11688-
end11689-
if mname == "=="11690-
return "(" + cmp_call + " == 0)"11691-
end11695+
if mname == "!="11696+
return "(" + cmp_call + " != 0)"1169211697
end1169311698
end1169411699
end@@ -16777,6 +16782,39 @@
1677716782
if arg_id >= 01677816783
at = infer_type(arg_id)1677916784
end16785+
# Cross-type prim-vs-obj. Integer#== / Float#== fall back to16786+
# `other == self` via num_equal (numeric.c); String, Symbol,16787+
# Nil, True, False are type-strict with no fallback. obj-LHS16788+
# is handled in compile_obj_operator_expr -- this path is16789+
# `prim == obj` plus the obj-LHS no-`==`-no-`<=>` fall-through.16790+
lhs_obj = is_obj_type(lt)16791+
rhs_obj = is_obj_type(at)16792+
if lhs_obj != rhs_obj16793+
other_t = lhs_obj == 1 ? at : lt16794+
if lhs_obj == 0 && (other_t == "int" || other_t == "float")16795+
cname = at[4, at.length - 4]16796+
ci = find_class_idx(cname)16797+
if ci >= 016798+
owner = find_method_owner(ci, "==")16799+
if owner != ""16800+
owner_ci = find_class_idx(owner)16801+
midx = cls_find_method_direct(owner_ci, "==")16802+
ptypes = cls_meth_ptypes_get(owner_ci, midx)16803+
boxed = ptypes.length > 0 ? compile_expr_for_expected_type(recv, ptypes[0]) : compile_expr(recv)16804+
obj_expr = compile_expr_gc_rooted(arg_id)16805+
recv_arg = owner == cname ? obj_expr : "(sp_" + owner + " *)" + obj_expr16806+
call = "sp_" + owner + "_" + sanitize_name("==") + "(" + recv_arg + ", " + boxed + ")"16807+
return op == "==" ? call : "(!" + call + ")"16808+
end16809+
end16810+
end16811+
# Type-strict primitive, or Numeric vs an obj with no user `==`16812+
# (BasicObject#== identity -> false in MRI).16813+
if other_t == "int" || other_t == "float" || other_t == "bool" || other_t == "nil" ||16814+
other_t == "string" || other_t == "mutable_str" || other_t == "symbol"16815+
return op == "==" ? "FALSE" : "TRUE"16816+
end16817+
end1678016818
lc = compile_expr(recv)1678116819
rc = "0"1678216820
if arg_id >= 0@@ -18004,6 +18042,16 @@
1800418042
tmp1800518043
end1800618044
18045+
# compile_typed_call_args for callers that hold the owner / method18046+
# names rather than pre-resolved indices. Untyped fallback on miss.18047+
def compile_typed_call_args_by_name(nid, owner, mname, omit_trailing = 0)18048+
owner_ci = find_class_idx(owner)18049+
return compile_call_args(nid) if owner_ci < 018050+
midx = cls_find_method_direct(owner_ci, mname)18051+
return compile_call_args(nid) if midx < 018052+
compile_typed_call_args(nid, owner_ci, midx, omit_trailing)18053+
end18054+
1800718055
def compile_typed_call_args(nid, target_ci, target_midx, omit_trailing, root_constructor_args = 0)1800818056
# Like compile_call_args but casts arguments to match target method param1800918057
# 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…