Commit f5b2abe6
f5b2abe6953cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
spinel-c: GC.start, array methods, module-body ivars, defined? fixes
- GC.start / GC.compact: emit no-op (0LL); rooting bugs prevent live collection without output corruption
- GC module analyze: return TY_NIL for start/compact/stat
- Array bsearch/find_index/map!/select!/filter!/reject!/keep_if/ delete_if/each_index: codegen block handlers in typed-array section
- Fix
blockundeclared at codegen typed-array insertion point - analyze: infer return types for the same array methods; extend infer_block_params to cover them; empty-array backstop → TY_POLY_ARRAY
- yield_value_type re-entrancy guard: prevent infinite recursion for recursive block-forwarding methods
- Module-body ivar writes: track g_class_body_id in ClassNode/ModuleNode
handler so
@x = {}insidemodule Memitsciv_M_x = ...rather than crashing with undeclaredself - Top-level ivar writes (truly outside any class/def): skip silently
instead of generating
self->iv_Xin main() - defined?(nil/true/false): return "nil"/"true"/"false" per Ruby 3.2 (not "expression"); SelfNode → "self"; ivar check against write sites; built-in constants (Object, Array, etc.) recognized as "constant"
- Update test/defined_op.rb.expected to CRuby 3.2 output
Refs #spinel-c
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- f5b2abe6953c
- Parent
- 28e43f9924e9
src/analyze.cmodifiedView file
@@ -78,16 +78,31 @@
7878
return rn && bp && bp[0] && !strcmp(rn, bp);7979
}8080
81+
/* Re-entrancy guard for yield_value_type: prevents infinite recursion when a82+
recursive method forwards its block to itself (e.g. countdown { blk.call }). */83+
#define MAX_YVT_DEPTH 3284+
static int g_yvt_mi[MAX_YVT_DEPTH];85+
static int g_yvt_depth = 0;86+
8187
/* The value type of `yield` / a `<&block-param>.call` inside method mi: the8288
block-body value type at a (any) call site of mi. Polymorphic, resolved from8389
the first matching caller -- matches how the rewrite inlines per call site. */8490
static TyKind yield_value_type(Compiler *c, int mi) {91+
for (int i = 0; i < g_yvt_depth; i++)92+
if (g_yvt_mi[i] == mi) return TY_UNKNOWN;93+
if (g_yvt_depth >= MAX_YVT_DEPTH) return TY_UNKNOWN;94+
g_yvt_mi[g_yvt_depth++] = mi;95+
8596
const NodeTable *nt = c->nt;97+
TyKind result = TY_UNKNOWN;8698
for (int cid = 0; cid < nt->count; cid++) {8799
const char *cty = nt_type(nt, cid);88100
if (!cty || strcmp(cty, "CallNode")) continue;89101
int blk = nt_ref(nt, cid, "block");90102
if (blk < 0) continue;103+
/* skip calls that live inside method mi itself (recursive self-calls);104+
only external call sites provide a concrete block value type */105+
if ((int)(comp_scope_of(c, cid) - c->scopes) == mi) continue;91106
const char *cn = nt_str(nt, cid, "name");92107
int crecv = nt_ref(nt, cid, "receiver");93108
int rmi = -1;@@ -101,11 +116,13 @@
101116
if (rmi != mi) continue;102117
int bb = nt_ref(nt, blk, "body");103118
int bn = 0; const int *bd = bb >= 0 ? nt_arr(nt, bb, "body", &bn) : NULL;104-
if (bn == 0) return TY_NIL;119+
if (bn == 0) { result = TY_NIL; break; }105120
TyKind bt = infer_type(c, bd[bn - 1]);106-
return bt == TY_VOID ? TY_NIL : bt; /* a void last-expr's block value is nil */121+
result = bt == TY_VOID ? TY_NIL : bt; /* a void last-expr's block value is nil */122+
break;107123
}108-
return TY_UNKNOWN;124+
g_yvt_depth--;125+
return result;109126
}110127
111128
static TyKind method_call_ret(Compiler *c, int mi, int call_id) {@@ -357,6 +374,10 @@
357374
!strcmp(name, "mktime") || !strcmp(name, "utc") || !strcmp(name, "gm")))358375
return TY_TIME;359376
if (rty && !strcmp(rty, "ConstantReadNode") &&377+
nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "GC") &&378+
(!strcmp(name, "start") || !strcmp(name, "compact") || !strcmp(name, "stat")))379+
return TY_NIL;380+
if (rty && !strcmp(rty, "ConstantReadNode") &&360381
nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "Process") &&361382
(!strcmp(name, "pid") || !strcmp(name, "ppid")))362383
return TY_INT;@@ -574,6 +595,18 @@
574595
if (!strcmp(name, "empty?") || !strcmp(name, "include?")) return TY_BOOL;575596
if ((!strcmp(name, "all?") || !strcmp(name, "any?") ||576597
!strcmp(name, "none?") || !strcmp(name, "one?")) && argc == 0) return TY_BOOL;598+
if ((!strcmp(name, "bsearch") || !strcmp(name, "find") || !strcmp(name, "detect")) && block >= 0)599+
return ty_array_elem(rt); /* element or nil */600+
if ((!strcmp(name, "map!") || !strcmp(name, "collect!")) && block >= 0) {601+
int body = nt_ref(nt, block, "body");602+
int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;603+
TyKind bt = bn > 0 ? infer_type(c, bb[bn - 1]) : TY_UNKNOWN;604+
return bt != TY_UNKNOWN ? ty_array_of(bt) : rt;605+
}606+
if ((!strcmp(name, "select!") || !strcmp(name, "filter!") || !strcmp(name, "reject!") ||607+
!strcmp(name, "keep_if") || !strcmp(name, "delete_if")) && block >= 0) return rt;608+
if (!strcmp(name, "find_index") || !strcmp(name, "index")) return TY_INT; /* int or nil */609+
if (!strcmp(name, "each_index")) return rt;577610
if (!strcmp(name, "push") || !strcmp(name, "<<") || !strcmp(name, "append") ||578611
!strcmp(name, "reverse") || !strcmp(name, "sort") || !strcmp(name, "uniq") ||579612
!strcmp(name, "to_a") || !strcmp(name, "dup") || !strcmp(name, "clone") ||@@ -2209,7 +2242,12 @@
22092242
!strcmp(name, "reverse_each") || !strcmp(name, "each_entry") ||22102243
!strcmp(name, "sum") || !strcmp(name, "count") ||22112244
!strcmp(name, "any?") || !strcmp(name, "all?") || !strcmp(name, "none?") ||2212-
!strcmp(name, "one?") || !strcmp(name, "each_with_index")) &&2245+
!strcmp(name, "one?") || !strcmp(name, "each_with_index") ||2246+
!strcmp(name, "bsearch") || !strcmp(name, "find_index") ||2247+
!strcmp(name, "map!") || !strcmp(name, "collect!") ||2248+
!strcmp(name, "select!") || !strcmp(name, "filter!") || !strcmp(name, "reject!") ||2249+
!strcmp(name, "keep_if") || !strcmp(name, "delete_if") || !strcmp(name, "each_index") ||2250+
!strcmp(name, "flat_map") || !strcmp(name, "each_with_object")) &&22132251
ty_is_array(rt))22142252
pt = ty_array_elem(rt);22152253
@@ -2724,6 +2762,22 @@
27242762
int iv = comp_ivar_index(ci, nt_str(c->nt, id, "name"));27252763
if (iv >= 0 && ci->ivar_types[iv] == TY_UNKNOWN) ci->ivar_types[iv] = TY_INT_ARRAY;27262764
}2765+
/* Backstop: a local variable assigned only empty array literals with no2766+
push evidence stays TY_UNKNOWN. Default it to TY_POLY_ARRAY so array2767+
operations (map!, p, etc.) can dispatch. */2768+
for (int id = 0; id < c->nt->count; id++) {2769+
const char *ty = nt_type(c->nt, id);2770+
if (!ty || strcmp(ty, "LocalVariableWriteNode")) continue;2771+
int v = nt_ref(c->nt, id, "value");2772+
const char *vty = v >= 0 ? nt_type(c->nt, v) : NULL;2773+
if (!vty || strcmp(vty, "ArrayNode")) continue;2774+
int en = 0; nt_arr(c->nt, v, "elements", &en);2775+
if (en != 0) continue;2776+
const char *nm = nt_str(c->nt, id, "name");2777+
Scope *s = comp_scope_of(c, id);2778+
LocalVar *lv = nm ? scope_local(s, nm) : NULL;2779+
if (lv && lv->type == TY_UNKNOWN) lv->type = TY_POLY_ARRAY;2780+
}27272781
/* A read-only ivar (referenced but never assigned a typed value) stays27282782
TY_UNKNOWN -> it has no C type. Such a slot always reads nil at runtime;27292783
give it a boxed-nil poly field so `.nil?`/`.inspect` behave (#712). */src/codegen.cmodifiedView file
Loading diff…
test/defined_op.rb.expectedmodifiedView file
Loading diff…