Commit 219cf9dd
219cf9dd9018Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
catch/throw, post-test loops, Class#=== nil/bool, Fiber value-passing
Unblocks bundle_misc_a. Four independent gaps that the bundle exercises:
- catch(:tag){...}/throw lowered to the runtime setjmp tag stack, with catch as an expression yielding the block value or thrown value.
- begin..end while/until honors PM_LOOP_FLAGS_BEGIN_MODIFIER as a post-test do/while so the body runs at least once.
- Class#=== handles nil/true/false args and universal ancestors (Object/Kernel/BasicObject) over nil and bool receivers.
- Fiber.new { |x| } block param typed poly; resume/transfer args boxed.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 219cf9dd9018
- Parent
- 8e46c5aa574e
src/analyze.cmodifiedView file
@@ -190,6 +190,40 @@
190190
return result;191191
}192192
193+
/* Unify the value types of every `throw <tag>, <val>` inside a catch block.194+
A nested inner BlockNode is still scanned because `throw` is dynamic. */195+
static TyKind scan_throw_type(Compiler *c, int id, int depth) {196+
if (id < 0 || depth > 32) return TY_UNKNOWN;197+
const NodeTable *nt = c->nt;198+
const char *ty = nt_type(nt, id);199+
if (!ty) return TY_UNKNOWN;200+
if (!strcmp(ty, "DefNode")) return TY_UNKNOWN;201+
if (!strcmp(ty, "CallNode")) {202+
const char *nm = nt_str(nt, id, "name");203+
if (nm && !strcmp(nm, "throw") && nt_ref(nt, id, "receiver") < 0) {204+
int v = nt_ref(nt, id, "arguments");205+
int vargc = 0; const int *vargs = v >= 0 ? nt_arr(nt, v, "arguments", &vargc) : NULL;206+
if (vargc >= 2) return infer_type(c, vargs[1]);207+
return TY_NIL;208+
}209+
}210+
TyKind result = TY_UNKNOWN;211+
int nr = nt_num_refs(nt, id);212+
for (int i = 0; i < nr; i++) {213+
TyKind t = scan_throw_type(c, nt_ref_at(nt, id, i), depth + 1);214+
if (t != TY_UNKNOWN) result = ty_unify(result, t);215+
}216+
int na = nt_num_arrs(nt, id);217+
for (int i = 0; i < na; i++) {218+
int n = 0; const int *ids = nt_arr_at(nt, id, i, &n);219+
for (int k = 0; k < n; k++) {220+
TyKind t = scan_throw_type(c, ids[k], depth + 1);221+
if (t != TY_UNKNOWN) result = ty_unify(result, t);222+
}223+
}224+
return result;225+
}226+
193227
/* The value type of `yield` / a `<&block-param>.call` inside method mi: the194228
block-body value type at a (any) call site of mi. Polymorphic, resolved from195229
the first matching caller -- matches how the rewrite inlines per call site. */@@ -415,6 +449,23 @@
415449
return TY_NIL;416450
}417451
452+
/* catch(:tag) { ... [throw :tag, val] ... } -> unify the block's last453+
value with every throw value targeting the tag */454+
if (recv < 0 && !strcmp(name, "catch")) {455+
int blk = nt_ref(nt, id, "block");456+
TyKind result = TY_UNKNOWN;457+
if (blk >= 0) {458+
int body = nt_ref(nt, blk, "body");459+
if (body >= 0) {460+
int bn = 0; const int *bb = nt_arr(nt, body, "body", &bn);461+
if (bn > 0) result = infer_type(c, bb[bn - 1]);462+
TyKind tt = scan_throw_type(c, body, 0);463+
if (tt != TY_UNKNOWN) result = ty_unify(result, tt);464+
}465+
}466+
return result == TY_UNKNOWN ? TY_NIL : result;467+
}468+
418469
/* proc {} / lambda {} / Proc.new {} -> a first-class Proc value */419470
if (is_proc_literal(c, id)) return TY_PROC;420471
@@ -5319,6 +5370,33 @@
53195370
}53205371
}53215372
5373+
/* Fiber.new { |first| ... }: the block param receives the resume value,5374+
which is always a poly (boxed) value at the runtime ABI boundary. */5375+
for (int id = 0; id < nt->count; id++) {5376+
const char *ty = nt_type(nt, id);5377+
if (!ty || strcmp(ty, "CallNode")) continue;5378+
const char *cname = nt_str(nt, id, "name");5379+
if (!cname || strcmp(cname, "new")) continue;5380+
int recv = nt_ref(nt, id, "receiver");5381+
if (recv < 0 || !nt_type(nt, recv) || strcmp(nt_type(nt, recv), "ConstantReadNode")) continue;5382+
const char *rn = nt_str(nt, recv, "name");5383+
if (!rn || strcmp(rn, "Fiber")) continue;5384+
int blk = nt_ref(nt, id, "block");5385+
if (blk < 0) continue;5386+
int pn = nt_ref(nt, blk, "parameters");5387+
if (pn < 0) continue;5388+
int inner = nt_ref(nt, pn, "parameters");5389+
int pnode = inner >= 0 ? inner : pn;5390+
int rnp = 0; const int *reqs = nt_arr(nt, pnode, "requireds", &rnp);5391+
Scope *bs = comp_scope_of(c, blk);5392+
for (int k = 0; k < rnp; k++) {5393+
const char *p = nt_str(nt, reqs[k], "name");5394+
if (!p) continue;5395+
LocalVar *lv = scope_local_intern(bs, p); lv->is_block_param = 1;5396+
if (lv->type == TY_UNKNOWN) { lv->type = TY_POLY; changed = 1; }5397+
}5398+
}5399+
53225400
/* Proc/lambda call-site param inference: `f.call(:a)` propagates arg types53235401
to the proc's params (e.g. `t` gets TY_SYMBOL instead of the default TY_INT). */53245402
for (int id = 0; id < nt->count; id++) {src/codegen.cmodifiedView file
Loading diff…