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
end return "int" end # `StringIO.open(str) { |io| ... }` returns the block's value (and # closes the io). Same value-returning-block shape as `then`, but the # block param is a freshly-constructed StringIO, not the receiver. if mname == "open" && constructor_class_name(recv) == "StringIO" blk = @nd_block[nid] if blk >= 0 # Block form: the result is the block's value (matching codegen's # compile_stringio_open_expr). An empty block yields int 0, so leave # the default-int return below for that case. bbody = @nd_body[blk] if bbody >= 0 bbs = get_stmts(bbody) if bbs.length > 0 bp = get_block_param(nid, 0) if bp == "" bp = "_x" end push_scope declare_var(bp, "stringio") rt = infer_type(bbs.last) pop_scope return rt end end return "int" end # No block: StringIO.open returns the constructed io. return "stringio" end if mname == "succ" || mname == "next" if recv >= 0 rt = infer_type(recv) elsif mname == "tap" || mname == "then" || mname == "yield_self" # Block param gets receiver type types.push(recv_type) elsif mname == "open" && @nd_receiver[nid] >= 0 && constructor_class_name(@nd_receiver[nid]) == "StringIO" # `StringIO.open(str) { |io| ... }` yields a freshly-built StringIO. types.push("stringio") elsif mname == "each_with_object" if bk == 0 # Element if recv >= 0 recv_t = infer_type(recv) end # `StringIO.open(str) { |io| ... }` yields a freshly-constructed # StringIO. Keyed on the syntactic receiver (the StringIO constant # has no useful inferred type) so it doesn't hijack File.open/IO.open. if mname == "open" && pi == 0 && constructor_class_name(recv) == "StringIO" return "stringio" end # `"a".upto("e") { |c| ... }` yields successive strings; the block # param is a string (Integer#upto keeps the int default). if mname == "upto" && pi == 0 && (base_type(recv_t) == "string" || base_type(recv_t) == "mutable_str")spinel_codegen.rbmodifiedView file
Loading diff…
test/stringio_open.rbaddedView file
Loading diff…
test/stringio_open.rb.expectedaddedView file
Loading diff…