Commit 6047c90e

6047c90ee1edMerged

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

Description

Type Class values in collections as poly, not int

A Class object stored in a Hash value or Array element was inferred as int, so codegen emitted an int-typed container (str_int_hash / int_array) and stored the sp_Class struct through the integer path -- a C type mismatch (incompatible type for sp_StrIntHash_set / sp_IntArray_push). A Class in a scalar variable was already handled; only collections mis-typed it.

Add a class arm to the array-element and hash-value type inference, in both the analyze and codegen passes (which mirror each other), so a class-valued collection routes to poly storage (poly_array / *_poly_hash). Add a class case to box_non_nullable_value_to_poly so the element boxes via sp_box_class rather than the sp_box_int default, and the missing SP_TAG_CLASS arm to sp_poly_puts so a class read back out of poly storage prints its name instead of its cls_id.

Closes #1337

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

Details

Commit
6047c90ee1ed
Parent
2874f4170de9

lib/sp_runtime.hmodifiedView file

@@ -2649,6 +2649,7 @@
26492649 return buf;
26502650 }
26512651 static sp_RbVal sp_box_poly_array(void *p) { return sp_box_obj(p, SP_BUILTIN_POLY_ARRAY); }
2652+static const char *sp_class_to_s(sp_Class c); /* fwd decl: sp_poly_puts' SP_TAG_CLASS arm */
26522653 static inline void sp_poly_puts(sp_RbVal v) {
26532654 switch (v.tag) {
26542655 case SP_TAG_INT: printf("%lld\n", (long long)v.v.i); break;
@@ -2658,6 +2659,7 @@
26582659 case SP_TAG_NIL: putchar('\n'); break;
26592660 case SP_TAG_SYM: { const char *_ss = sp_sym_to_s((sp_sym)v.v.i); fputs(_ss, stdout); putchar('\n'); break; }
26602661 case SP_TAG_ENCODING: { const char *_es = v.v.s ? v.v.s : sp_str_empty; fputs(_es, stdout); putchar('\n'); break; }
2662+ case SP_TAG_CLASS: { sp_Class _c = {v.v.i}; fputs(sp_class_to_s(_c), stdout); putchar('\n'); break; }
26612663 case SP_TAG_OBJ: {
26622664 /* MRI's `puts arr` iterates an Array, printing one element per
26632665 line (using to_s on each); a non-Array OBJ falls back to