Commit 05b8746d

05b8746dad82Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

codegen: String#<=> dispatches strcmp clamped to -1/0/1

"abc" <=> "abd" returned 0 (the unresolved-call placeholder). Add a String arm in compile_string_method_expr that emits strcmp(...) clamped to {-1, 0, 1} per Ruby's <=> contract. Mirrors the int / sym arms.

Refs #900

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
05b8746dad82
Parent
193a8d856d21

spinel_codegen.rbmodifiedView file

@@ -18817,6 +18817,25 @@
1881718817 if mname == "chomp!"
1881818818 return compile_string_method_expr(nid, "chomp", rc)
1881918819 end
18820+ # String#<=> dispatches to strcmp clamped to -1/0/1.
18821+ # Issue #900. Symmetric to the int / sym arms.
18822+ if mname == "<=>"
18823+ args_id_scmp = @nd_arguments[nid]
18824+ if args_id_scmp >= 0
18825+ a_scmp = get_args(args_id_scmp)
18826+ if a_scmp.length >= 1
18827+ arg_t_scmp = infer_type(a_scmp[0])
18828+ if arg_t_scmp == "string" || arg_t_scmp == "mutable_str"
18829+ arg_e_scmp = compile_expr(a_scmp[0])
18830+ if arg_t_scmp == "mutable_str"
18831+ arg_e_scmp = "(" + arg_e_scmp + ")->data"
18832+ end
18833+ cmp_scmp = "strcmp(" + rc + ", " + arg_e_scmp + ")"
18834+ return "((" + cmp_scmp + ") < 0 ? (mrb_int)-1 : ((" + cmp_scmp + ") > 0 ? (mrb_int)1 : (mrb_int)0))"
18835+ end
18836+ end
18837+ end
18838+ end
1882018839 # is_a? / kind_of? / instance_of? for primitive String. Decide at
1882118840 # compile time based on Ruby's class hierarchy (String < Comparable
1882218841 # < Object). Anything outside that chain is FALSE.