Commit 3bcdcf6a

3bcdcf6a4c5fMerged

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

Description

StringIO.open block form + putc(string)/fsync

StringIO.open(args) { |io| body } constructs a fresh StringIO, runs the block (io bound), returns the block's value, then closes; without a block it behaves like StringIO.new. putc accepts a string (writes the first byte); fsync/fileno/pid return 0.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
3bcdcf6a4c5f
Parent
cbeed6072c24

src/analyze.cmodifiedView file

@@ -92,6 +92,19 @@
9292 }
9393 }
9494
95+ /* StringIO.open(args) { |io| body } -> the block body's value */
96+ if (recv >= 0 && !strcmp(name, "open") && nt_type(nt, recv) &&
97+ !strcmp(nt_type(nt, recv), "ConstantReadNode") && nt_str(nt, recv, "name") &&
98+ !strcmp(nt_str(nt, recv, "name"), "StringIO")) {
99+ int block = nt_ref(nt, id, "block");
100+ int bbody = block >= 0 ? nt_ref(nt, block, "body") : -1;
101+ if (bbody >= 0) {
102+ int bn = 0; const int *bb = nt_arr(nt, bbody, "body", &bn);
103+ return bn > 0 ? infer_type(c, bb[bn - 1]) : TY_NIL;
104+ }
105+ return TY_STRINGIO;
106+ }
107+
95108 /* StringIO instance methods */
96109 if (recv >= 0 && rt == TY_STRINGIO) {
97110 if (!strcmp(name, "string") || !strcmp(name, "read")) return TY_STRING;
@@ -1338,6 +1351,16 @@
13381351 int recv = nt_ref(nt, id, "receiver");
13391352 if (!name) continue;
13401353
1354+ /* StringIO.open(args) { |io| ... }: io is a StringIO */
1355+ if (recv >= 0 && !strcmp(name, "open") && nt_type(nt, recv) &&
1356+ !strcmp(nt_type(nt, recv), "ConstantReadNode") && nt_str(nt, recv, "name") &&
1357+ !strcmp(nt_str(nt, recv, "name"), "StringIO")) {
1358+ const char *p0 = block_param_name(c, block, 0);
1359+ if (p0) { LocalVar *l = scope_local_intern(comp_scope_of(c, block), p0); l->is_block_param = 1;
1360+ if (l->type != TY_STRINGIO) { l->type = TY_STRINGIO; changed = 1; } }
1361+ continue;
1362+ }
1363+
13411364 /* struct.to_h { |k, v| ... }: k is a member symbol, v its (poly) value */
13421365 if (recv >= 0 && !strcmp(name, "to_h")) {
13431366 TyKind rt0 = infer_type(c, recv);