Commit 43fcd7c7

43fcd7c7d9d5Merged

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

Description

fix(codegen): box procs into sp_RbVal slots; promote empty {} ivar to *_poly_hash

Issue #65: an ivar initialized as {} and later assigned a captured &block proc dropped into str_int_hash, then fed sp_Proc * into sp_StrIntHash_set (which takes mrb_int). Two pieces:

  • box_expr_to_poly / box_value_to_poly had no proc / lambda case, so the []= site fell through to sp_box_int(lv_block). Add a new SP_BUILTIN_PROC (-9) cls_id and sp_box_proc(p) runtime helper, and route both boxers through it.

  • The empty-hash promotion from issue #64 already widens str_int_hash → str_poly_hash for non-int/string/sym values (procs land here), but the inline empty-hash constructor switches in compile_stmt and emit_constructor only knew about the monomorphic *_*_hash types. Add str_poly_hash / sym_poly_hash arms so @h = {} against a poly-promoted slot emits sp_StrPolyHash_new() / sp_SymPolyHash_new() instead of the str_int_hash default. The corresponding @needs_* flags are set in scan_writer_calls at promotion time so emit_features picks up the runtime helpers.

test/proc_hash_value.rb covers the issue's exact reproducer (@h[name] = block against an empty-hash ivar inside a method that captures &block).

Closes #65

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
43fcd7c7d9d5
Parent
c734bbad3ac0

lib/sp_runtime.hmodifiedView file

@@ -565,6 +565,7 @@
565565 #define SP_BUILTIN_FLT_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_FLT) /* -3 */
566566 #define SP_BUILTIN_PTR_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_OBJ) /* -6 */
567567 #define SP_BUILTIN_SYM_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_SYM) /* -7 */
568+#define SP_BUILTIN_PROC (-9) /* sp_Proc *, distinct from any tag-based id */
568569 typedef struct { int tag; int cls_id; union { mrb_int i; const char *s; mrb_float f; mrb_bool b; void *p; } v; } sp_RbVal;
569570 static sp_RbVal sp_box_int(mrb_int v) { sp_RbVal r; r.tag = SP_TAG_INT; r.cls_id = 0; r.v.i = v; return r; }
570571 static sp_RbVal sp_box_str(const char *v) { sp_RbVal r; r.tag = SP_TAG_STR; r.cls_id = 0; r.v.s = v; return r; }
@@ -580,6 +581,7 @@
580581 static sp_RbVal sp_box_str_array(void *p) { return sp_box_obj(p, SP_BUILTIN_STR_ARRAY); }
581582 static sp_RbVal sp_box_sym_array(void *p) { return sp_box_obj(p, SP_BUILTIN_SYM_ARRAY); }
582583 static sp_RbVal sp_box_ptr_array(void *p) { return sp_box_obj(p, SP_BUILTIN_PTR_ARRAY); }
584+static sp_RbVal sp_box_proc(void *p) { return sp_box_obj(p, SP_BUILTIN_PROC); }
583585 static void sp_poly_puts(sp_RbVal v) {
584586 switch (v.tag) {
585587 case SP_TAG_INT: printf("%lld\n", (long long)v.v.i); break;