Commit 5aa54558

5aa545581f8bMerged

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

Description

Add between? for user Comparable types, fix constant namespace and NameError

  • Comparable#between? now dispatches through the class's <=> method for user-defined types (previously emitted unsupported call)
  • Fix module_body_string_const_interp: remove class-name guard in register_globals_consts so M::V value constants are registered even when a module V also exists
  • uninit_const_raises_nameerror: unregistered/untyped constants now emit a runtime NameError raise instead of failing at compile time
  • Skip ConstantWriteNode codegen when type is TY_UNKNOWN to avoid undeclared C global references

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
5aa545581f8b
Parent
763c88376e4e

src/codegen.cmodifiedView file

@@ -5285,6 +5285,32 @@
52855285 buf_printf(b, " && _t%d <= ", tv); emit_expr(c, argv[1], b); buf_puts(b, "); })");
52865286 return;
52875287 }
5288+ /* Comparable: user type with <=> method */
5289+ if (ty_is_object(rt)) {
5290+ int cid_b = ty_object_class(rt);
5291+ int defcls_b = -1;
5292+ int mi_b = comp_method_in_chain(c, cid_b, "<=>", &defcls_b);
5293+ if (mi_b >= 0) {
5294+ int ts = ++g_tmp, tlo = ++g_tmp, thi = ++g_tmp;
5295+ const char *cname = c->classes[defcls_b].name;
5296+ emit_indent(g_pre, g_indent);
5297+ emit_ctype(c, rt, g_pre); buf_printf(g_pre, " _t%d = ", ts);
5298+ Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);
5299+ buf_puts(g_pre, rb.p ? rb.p : ""); buf_puts(g_pre, ";\n"); free(rb.p);
5300+ emit_indent(g_pre, g_indent);
5301+ emit_ctype(c, rt, g_pre); buf_printf(g_pre, " _t%d = ", tlo);
5302+ Buf lb; memset(&lb, 0, sizeof lb); emit_expr(c, argv[0], &lb);
5303+ buf_puts(g_pre, lb.p ? lb.p : ""); buf_puts(g_pre, ";\n"); free(lb.p);
5304+ emit_indent(g_pre, g_indent);
5305+ emit_ctype(c, rt, g_pre); buf_printf(g_pre, " _t%d = ", thi);
5306+ Buf hb; memset(&hb, 0, sizeof hb); emit_expr(c, argv[1], &hb);
5307+ buf_puts(g_pre, hb.p ? hb.p : ""); buf_puts(g_pre, ";\n"); free(hb.p);
5308+ buf_printf(b, "(sp_%s_%s((sp_%s *)_t%d, _t%d) >= 0 && sp_%s_%s((sp_%s *)_t%d, _t%d) <= 0)",
5309+ cname, mc("<=>"), cname, ts, tlo,
5310+ cname, mc("<=>"), cname, ts, thi);
5311+ return;
5312+ }
5313+ }
52885314 }
52895315
52905316 /* object_id: a stable integer id. Int uses MRI's 2n+1; pointer-backed