Commit f98130e1
f98130e117bbMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Dispatch Comparable#between? through <=> for user types
Comparable#between? lowered to a raw self >= min && self <= max
struct comparison, which gcc rejects on a user type ("invalid operands
to binary >="). Direct </>/<=/>= on a Comparable user type already
route through the class's <=>; between? now does the same, emitting
(self <=> min) >= 0 && (self <=> max) <= 0. Built-in receivers
(int/float/string) keep their existing lowering.
Fixes #1245
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- f98130e117bb
- Parent
- a90aa739c354
spinel_codegen.rbmodifiedView file
@@ -20499,6 +20499,20 @@
2049920499
if lt == "string"2050020500
return "(strcmp(" + rc + ", " + lo + ") >= 0 && strcmp(" + rc + ", " + hi + ") <= 0)"2050120501
end20502+
# Comparable#between? on a user type: `self >= min && self <= max`20503+
# must dispatch through the class's `<=>`, not a raw struct compare20504+
# (which gcc rejects with "invalid operands to binary >="). Mirrors20505+
# the <=>-based lowering of direct </>/<=/>= comparisons. Issue #1245.20506+
if is_obj_type(lt) == 120507+
cname_bt = lt[4, lt.length - 4]20508+
ci_bt = find_class_idx(cname_bt)20509+
if ci_bt >= 020510+
cmp_owner_bt = find_method_owner(ci_bt, "<=>")20511+
if cmp_owner_bt != ""20512+
return "(sp_" + cmp_owner_bt + "__cmp(" + rc + ", " + lo + ") >= 0 && sp_" + cmp_owner_bt + "__cmp(" + rc + ", " + hi + ") <= 0)"20513+
end20514+
end20515+
end2050220516
return "(" + rc + " >= " + lo + " && " + rc + " <= " + hi + ")"2050320517
end2050420518
endtest/comparable_between_user_type.rbaddedView file
Loading diff…
test/comparable_between_user_type.rb.expectedaddedView file
Loading diff…