Commit b16098e3
b16098e3994dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
codegen: multi-value return emits poly array
return a, b now builds a sp_PolyArray on the heap (GC-rooted),
pushes each element boxed, and returns it. Callers that assign the
result to a TY_POLY slot receive a sp_box_poly_array()-wrapped value,
so poly array subscript dispatch (pair[0], pair[1]) works correctly.
On the analyze side, return_node_type() returns TY_POLY_ARRAY for n > 1 arguments, which propagates to the function's sc->ret and drives the correct C return-type declaration.
Fixes value_type_tuple_gc_scan (455 pass, gate FAIL set unchanged).
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- b16098e3994d
- Parent
- dcb0590ca076
src/analyze.cmodifiedView file
@@ -553,6 +553,7 @@
553553
if (!strcmp(name, "max_by") || !strcmp(name, "min_by") ||554554
!strcmp(name, "find") || !strcmp(name, "detect"))555555
return ty_array_elem(rt); /* returns an element */556+
if (!strcmp(name, "partition")) return TY_POLY_ARRAY; /* [[truthy...],[falsy...]] */556557
}557558
/* grep/grep_v without a block filter by `pattern === e`, preserving the558559
receiver's array type. */@@ -648,6 +649,7 @@
648649
if ((!strcmp(name, "gsub") || !strcmp(name, "sub")) && argc == 2) return TY_STRING;649650
if (!strcmp(name, "to_i") || !strcmp(name, "length") || !strcmp(name, "size")) return TY_INT;650651
if (!strcmp(name, "to_f")) return TY_FLOAT;652+
if (!strcmp(name, "[]") && argc == 1) return TY_POLY; /* boxed array element access */651653
/* poly method dispatch: unify the return type over every class that652654
defines `name` (the runtime cls_id picks the impl). */653655
TyKind r = TY_UNKNOWN; int found = 0;@@ -923,6 +925,27 @@
923925
924926
if (is_void_call(name) && recv < 0) return TY_VOID;925927
928+
/* tap: run block, return self */929+
if (!strcmp(name, "tap") && recv >= 0) return rt;930+
/* then / yield_self: run block, return block result */931+
if (!strcmp(name, "then") || !strcmp(name, "yield_self")) {932+
int blk_id = nt_ref(nt, id, "block");933+
if (blk_id >= 0) {934+
int bdy = nt_ref(nt, blk_id, "body");935+
int bbn = 0; const int *bbb = bdy >= 0 ? nt_arr(nt, bdy, "body", &bbn) : NULL;936+
if (bbn <= 0) return TY_NIL;937+
/* Pin block param to receiver type so body inference uses the right type */938+
const char *bp0 = block_param_name(c, blk_id, 0);939+
Scope *bs = bp0 ? comp_scope_of(c, blk_id) : NULL;940+
LocalVar *blv = (bs && bp0) ? scope_local(bs, bp0) : NULL;941+
TyKind saved_blv = blv ? blv->type : TY_UNKNOWN;942+
if (blv && rt != TY_UNKNOWN) blv->type = rt;943+
TyKind result = infer_type(c, bbb[bbn - 1]);944+
if (blv) blv->type = saved_blv;945+
return result;946+
}947+
}948+
926949
return TY_UNKNOWN;927950
}928951
@@ -2205,6 +2228,15 @@
22052228
const char *p0 = block_param_name(c, block, 0);22062229
if (!p0) continue;22072230
2231+
/* then / yield_self: block param receives the receiver value */2232+
if (!strcmp(name, "then") || !strcmp(name, "yield_self")) {2233+
Scope *bs = comp_scope_of(c, block);2234+
LocalVar *lv = scope_local_intern(bs, p0); lv->is_block_param = 1;2235+
TyKind m = ty_unify(lv->type, rt);2236+
if (m != lv->type) { lv->type = m; changed = 1; }2237+
continue;2238+
}2239+
22082240
TyKind pt = TY_UNKNOWN;22092241
if (!strcmp(name, "step") && (rt == TY_INT || rt == TY_FLOAT)) {22102242
/* a float receiver or float limit/step yields floats */@@ -2382,6 +2414,7 @@
23822414
if (args < 0) return TY_NIL;23832415
int n = 0;23842416
const int *a = nt_arr(c->nt, args, "arguments", &n);2417+
if (n > 1) return TY_POLY_ARRAY;23852418
return n > 0 ? infer_type(c, a[0]) : TY_NIL;23862419
}23872420
src/codegen.cmodifiedView file
Loading diff…