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 end
20502+ # Comparable#between? on a user type: `self >= min && self <= max`
20503+ # must dispatch through the class's `<=>`, not a raw struct compare
20504+ # (which gcc rejects with "invalid operands to binary >="). Mirrors
20505+ # the <=>-based lowering of direct </>/<=/>= comparisons. Issue #1245.
20506+ if is_obj_type(lt) == 1
20507+ cname_bt = lt[4, lt.length - 4]
20508+ ci_bt = find_class_idx(cname_bt)
20509+ if ci_bt >= 0
20510+ 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+ end
20514+ end
20515+ end
2050220516 return "(" + rc + " >= " + lo + " && " + rc + " <= " + hi + ")"
2050320517 end
2050420518 end