Commit a4015e0b

a4015e0b6a4dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

codegen: dispatch a poly-operand <=> through sp_poly_cmp

A user def <=>(other) whose body compares int ivars (@n <=> other.n) sees both operands as poly once the ivars widen in promote mode. The <=> emitter had no poly arm, so it fell through to the object-receiver dispatch, which switched on the boxed value's cls_id and recursed into the same <=> casting the boxed int's payload to a user-class pointer -- a segfault. Add a poly arm emitting sp_poly_cmp, mirroring the numeric/string/symbol arms.

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

Details

Commit
a4015e0b6a4d
Parent
73dfad9fd98d

src/codegen_call.cmodifiedView file

@@ -7481,6 +7481,17 @@
74817481 buf_printf(b, "; (mrb_int)sp_time_cmp(_t%d, _t%d); })", ta, tb);
74827482 return;
74837483 }
7484+ /* Poly operands (e.g. `@n <=> other.n` with int ivars widened to poly in
7485+ promote mode): tag-dispatch via sp_poly_cmp rather than falling through
7486+ to the object-receiver path, which would misread a boxed int's payload
7487+ as a user-class pointer and recurse into this same `<=>`. */
7488+ if (lrt == TY_POLY || lat == TY_POLY) {
7489+ int ta = ++g_tmp, tb = ++g_tmp, tk = ++g_tmp;
7490+ buf_printf(b, "({ sp_RbVal _t%d = ", ta); emit_boxed(c, recv, b);
7491+ buf_printf(b, "; sp_RbVal _t%d = ", tb); emit_boxed(c, argv[0], b);
7492+ buf_printf(b, "; mrb_bool _t%d; sp_poly_cmp(_t%d, _t%d, &_t%d); })", tk, ta, tb, tk);
7493+ return;
7494+ }
74847495 }
74857496
74867497 if (recv >= 0 && argc == 1 &&