Commit e4842fdd

e4842fddb148Merged

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

Description

Support the StringIO.open block form

StringIO.open(str) { |io| ... } was fully unresolved ("cannot resolve call to 'open' on int") because no pass modeled it as a value-returning block method. Wire it like then/yield_self, but bind the block param to a freshly-constructed StringIO rather than the receiver.

Analyze gains three guarded arms (keyed on the syntactic receiver via constructor_class_name, so File.open/IO.open/user open are untouched): block_param_type_at and scan_locals type the block param as stringio, and infer_method_name_type returns the block's result type (or stringio for the no-block form). Codegen adds compile_stringio_open_expr (a clone of compile_then_expr whose param init is compile_stringio_construct) plus an expression dispatch routing the block form to it and the no-block form to the plain constructor.

The empty-block case returns int in both passes so the two stay consistent (no analyze-says-pointer / codegen-emits-NULL mismatch).

Fixes #1139

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
e4842fddb148
Parent
43dc1284130e

spinel_analyze.rbmodifiedView file

@@ -5136,6 +5136,35 @@
51365136 end
51375137 return "int"
51385138 end
5139+ # `StringIO.open(str) { |io| ... }` returns the block's value (and
5140+ # closes the io). Same value-returning-block shape as `then`, but the
5141+ # block param is a freshly-constructed StringIO, not the receiver.
5142+ if mname == "open" && constructor_class_name(recv) == "StringIO"
5143+ blk = @nd_block[nid]
5144+ if blk >= 0
5145+ # Block form: the result is the block's value (matching codegen's
5146+ # compile_stringio_open_expr). An empty block yields int 0, so leave
5147+ # the default-int return below for that case.
5148+ bbody = @nd_body[blk]
5149+ if bbody >= 0
5150+ bbs = get_stmts(bbody)
5151+ if bbs.length > 0
5152+ bp = get_block_param(nid, 0)
5153+ if bp == ""
5154+ bp = "_x"
5155+ end
5156+ push_scope
5157+ declare_var(bp, "stringio")
5158+ rt = infer_type(bbs.last)
5159+ pop_scope
5160+ return rt
5161+ end
5162+ end
5163+ return "int"
5164+ end
5165+ # No block: StringIO.open returns the constructed io.
5166+ return "stringio"
5167+ end
51395168 if mname == "succ" || mname == "next"
51405169 if recv >= 0
51415170 rt = infer_type(recv)
@@ -31737,6 +31766,9 @@
3173731766 elsif mname == "tap" || mname == "then" || mname == "yield_self"
3173831767 # Block param gets receiver type
3173931768 types.push(recv_type)
31769+ elsif mname == "open" && @nd_receiver[nid] >= 0 && constructor_class_name(@nd_receiver[nid]) == "StringIO"
31770+ # `StringIO.open(str) { |io| ... }` yields a freshly-built StringIO.
31771+ types.push("stringio")
3174031772 elsif mname == "each_with_object"
3174131773 if bk == 0
3174231774 # Element
@@ -34050,6 +34082,12 @@
3405034082 if recv >= 0
3405134083 recv_t = infer_type(recv)
3405234084 end
34085+ # `StringIO.open(str) { |io| ... }` yields a freshly-constructed
34086+ # StringIO. Keyed on the syntactic receiver (the StringIO constant
34087+ # has no useful inferred type) so it doesn't hijack File.open/IO.open.
34088+ if mname == "open" && pi == 0 && constructor_class_name(recv) == "StringIO"
34089+ return "stringio"
34090+ end
3405334091 # `"a".upto("e") { |c| ... }` yields successive strings; the block
3405434092 # param is a string (Integer#upto keeps the int default).
3405534093 if mname == "upto" && pi == 0 && (base_type(recv_t) == "string" || base_type(recv_t) == "mutable_str")