Commit 274ef03f
274ef03f228cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Fix top-level cvars, exit/abort, backtick strings, poly IO dispatch
- Add Toplevel pseudo-class to handle @@cvar writes/reads at top level; infer_cvar_types Pass 3 collects top-level cvar types into it
- Add exit/abort dispatch in both stmt and expr contexts
- Add XStringNode/InterpolatedXStringNode (backtick) emit via sp_backtick()
- Infer XStringNode/InterpolatedXStringNode as TY_STRING
- Add loop{break val} return type inference via scan_break_type
- Add Fiber/Thread/IO/File class-method and instance-method type inference
as fallback AFTER user-defined poly dispatch (fixes poly return type
regression where
.readon user-defined poly objects was mistyped) - Fix ClassVariableReadNode inference to fall back to Toplevel class
Fixes hash_temp_gc_root and module_cvars tests (ERR → PASS).
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 274ef03f228c
- Parent
- 22a0c773783c
src/analyze.cmodifiedView file
@@ -133,6 +133,39 @@
133133
ClassNode/ModuleNode body traversal). -1 outside any class body. */134134
static int g_cbody_class_id = -1;135135
136+
/* Scan a subtree for BreakNode values and return their unified type.137+
Stops at DefNode/BlockNode boundaries (inner blocks have their own break scope). */138+
static TyKind scan_break_type(Compiler *c, int id, int depth) {139+
if (id < 0 || depth > 32) return TY_UNKNOWN;140+
const NodeTable *nt = c->nt;141+
const char *ty = nt_type(nt, id);142+
if (!ty) return TY_UNKNOWN;143+
if (!strcmp(ty, "DefNode")) return TY_UNKNOWN;144+
if (!strcmp(ty, "BlockNode") && depth > 0) return TY_UNKNOWN; /* inner block's breaks don't escape */145+
if (!strcmp(ty, "BreakNode")) {146+
int v = nt_ref(nt, id, "arguments");147+
if (v < 0) return TY_NIL;148+
int vargc = 0; const int *vargs = nt_arr(nt, v, "arguments", &vargc);149+
if (vargc > 0) return infer_type(c, vargs[0]);150+
return TY_NIL;151+
}152+
TyKind result = TY_UNKNOWN;153+
int nr = nt_num_refs(nt, id);154+
for (int i = 0; i < nr; i++) {155+
TyKind t = scan_break_type(c, nt_ref_at(nt, id, i), depth + 1);156+
if (t != TY_UNKNOWN) result = ty_unify(result, t);157+
}158+
int na = nt_num_arrs(nt, id);159+
for (int i = 0; i < na; i++) {160+
int n = 0; const int *ids = nt_arr_at(nt, id, i, &n);161+
for (int k = 0; k < n; k++) {162+
TyKind t = scan_break_type(c, ids[k], depth + 1);163+
if (t != TY_UNKNOWN) result = ty_unify(result, t);164+
}165+
}166+
return result;167+
}168+
136169
/* The value type of `yield` / a `<&block-param>.call` inside method mi: the137170
block-body value type at a (any) call site of mi. Polymorphic, resolved from138171
the first matching caller -- matches how the rewrite inlines per call site. */@@ -325,6 +358,19 @@
325358
if (self && self->is_cmethod && self->class_id >= 0) return TY_STRING;326359
}327360
361+
/* loop { break val } -> the type of the break value */362+
if (recv < 0 && !strcmp(name, "loop")) {363+
int blk = nt_ref(nt, id, "block");364+
if (blk >= 0) {365+
int body = nt_ref(nt, blk, "body");366+
if (body >= 0) {367+
TyKind bt = scan_break_type(c, body, 0);368+
if (bt != TY_UNKNOWN) return bt;369+
}370+
}371+
return TY_NIL;372+
}373+
328374
/* proc {} / lambda {} / Proc.new {} -> a first-class Proc value */329375
if (is_proc_literal(c, id)) return TY_PROC;330376
@@ -475,6 +521,10 @@
475521
if (cn && !strcmp(cn, "StringScanner")) return TY_STRINGSCANNER;476522
if (cn && !strcmp(cn, "Hash")) return TY_UNKNOWN; /* hash type determined by key usage */477523
if (cn && !strcmp(cn, "Regexp")) return TY_REGEX;524+
/* Builtin object types: return TY_POLY (they're boxed sp_RbVal at runtime) */525+
if (cn && (!strcmp(cn, "Fiber") || !strcmp(cn, "Thread") || !strcmp(cn, "Mutex") ||526+
!strcmp(cn, "Random") || !strcmp(cn, "IO") || !strcmp(cn, "File") ||527+
!strcmp(cn, "GzipReader") || !strcmp(cn, "GzipWriter"))) return TY_POLY;478528
}479529
}480530
@@ -605,6 +655,21 @@
605655
return TY_BOOL;606656
if (!strcmp(name, "mtime"))607657
return TY_TIME;658+
/* File.open / File.new without a block -> a File object (TY_POLY) */659+
if (!strcmp(name, "open") || !strcmp(name, "new")) return TY_POLY;660+
}661+
if (rty && !strcmp(rty, "ConstantReadNode") &&662+
nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "IO")) {663+
/* IO.pipe -> [r, w] pair; each is TY_POLY; the pair is a str_array */664+
if (!strcmp(name, "pipe")) return TY_STR_ARRAY;665+
}666+
/* Fiber.new { } / Thread.new { } -> TY_POLY (builtin object) */667+
if (rty && !strcmp(rty, "ConstantReadNode")) {668+
const char *cn2 = nt_str(nt, recv, "name");669+
if (cn2 && !strcmp(name, "new") &&670+
(!strcmp(cn2, "Fiber") || !strcmp(cn2, "Thread") || !strcmp(cn2, "Mutex") ||671+
!strcmp(cn2, "Random")))672+
return TY_POLY;608673
}609674
}610675
@@ -1256,6 +1321,15 @@
12561321
}12571322
}12581323
if (found) return r;1324+
/* Fiber/Thread/IO/File instance methods: fallback when no user class defines `name`. */1325+
if (!strcmp(name, "resume") || !strcmp(name, "value") || !strcmp(name, "join"))1326+
return TY_POLY;1327+
if (!strcmp(name, "alive?") || !strcmp(name, "dead?") || !strcmp(name, "closed?") ||1328+
!strcmp(name, "eof?")) return TY_BOOL;1329+
if (!strcmp(name, "write") || !strcmp(name, "read") || !strcmp(name, "gets") ||1330+
!strcmp(name, "readline")) return TY_STRING;1331+
if (!strcmp(name, "close") || !strcmp(name, "flush")) return TY_NIL;1332+
if (!strcmp(name, "fileno")) return TY_INT;12591333
}12601334
}12611335
@@ -1726,6 +1800,7 @@
17261800
if (!strcmp(ty, "RegularExpressionNode") ||17271801
!strcmp(ty, "InterpolatedRegularExpressionNode")) return TY_REGEX;17281802
if (!strcmp(ty, "InterpolatedStringNode")) return TY_STRING;1803+
if (!strcmp(ty, "XStringNode") || !strcmp(ty, "InterpolatedXStringNode")) return TY_STRING;17291804
if (!strcmp(ty, "InterpolatedSymbolNode")) return TY_SYMBOL;17301805
if (!strcmp(ty, "SymbolNode")) return TY_SYMBOL;17311806
if (!strcmp(ty, "TrueNode")) return TY_BOOL;@@ -1860,9 +1935,11 @@
18601935
if (!strcmp(ty, "ClassVariableReadNode")) {18611936
const char *nm = nt_str(nt, id, "name");18621937
Scope *s = comp_scope_of(c, id);1863-
if (s->class_id < 0) return TY_UNKNOWN;1864-
int idx = nm ? comp_cvar_index(&c->classes[s->class_id], nm) : -1;1865-
return idx >= 0 ? c->classes[s->class_id].cvar_types[idx] : TY_UNKNOWN;1938+
int cid = s->class_id;1939+
if (cid < 0) cid = comp_class_index(c, "Toplevel");1940+
if (cid < 0) return TY_UNKNOWN;1941+
int idx = nm ? comp_cvar_index(&c->classes[cid], nm) : -1;1942+
return idx >= 0 ? c->classes[cid].cvar_types[idx] : TY_UNKNOWN;18661943
}18671944
if (!strcmp(ty, "ClassVariableWriteNode") ||18681945
!strcmp(ty, "ClassVariableOperatorWriteNode") ||@@ -3021,6 +3098,22 @@
30213098
TyKind merged = ty_unify(ci->cvar_types[idx], vt);30223099
if (merged != ci->cvar_types[idx]) { ci->cvar_types[idx] = merged; changed = 1; }30233100
}3101+
/* Pass 3: top-level writes (class_id == -1 in scope 0) -- use Toplevel pseudo-class. */3102+
for (int id = 0; id < nt->count; id++) {3103+
const char *ty = nt_type(nt, id);3104+
if (!ty || strcmp(ty, "ClassVariableWriteNode")) continue;3105+
const char *nm = nt_str(nt, id, "name");3106+
Scope *s = comp_scope_of(c, id);3107+
if (!nm || s->class_id >= 0) continue;3108+
int tl_idx = comp_class_index(c, "Toplevel");3109+
if (tl_idx < 0) { comp_class_new(c, "Toplevel", -1); tl_idx = c->nclasses - 1; }3110+
ClassInfo *ci = &c->classes[tl_idx];3111+
int idx = comp_cvar_intern(ci, nm);3112+
TyKind vt = infer_type(c, nt_ref(nt, id, "value"));3113+
if (vt == TY_NIL) continue;3114+
TyKind merged = ty_unify(ci->cvar_types[idx], vt);3115+
if (merged != ci->cvar_types[idx]) { ci->cvar_types[idx] = merged; changed = 1; }3116+
}30243117
return changed;30253118
}30263119
@@ -4448,7 +4541,7 @@
44484541
!strcmp(name, "tally_by") || !strcmp(name, "min_by_all") ||44494542
!strcmp(name, "filter_map") || !strcmp(name, "count_by") ||44504543
!strcmp(name, "partition") || !strcmp(name, "each_slice") ||4451-
!strcmp(name, "each_cons")) &&4544+
!strcmp(name, "each_cons") || !strcmp(name, "cycle")) &&44524545
ty_is_array(rt))44534546
pt = ty_array_elem(rt);44544547
/* TY_POLY receiver with iteration methods: element type is TY_POLY */src/codegen.cmodifiedView file
Loading diff…