Commit ce699566
ce6995665306Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +1 added · ~2 modified
Description
Implement String#upto
"a".upto("e") { |c| ... } failed to compile: codegen reused the
Integer#upto handler and emitted for (mrb_int i = "a"; ...), treating
the string receiver as an integer loop variable.
Route a string receiver through a succ-based walk instead, reusing
sp_StrArray_from_string_range (the same sequence a String range
produces) for both the block form (yield each string, return self) and
the block-less form (materialize a str_array). analyze types the block
param and result accordingly. Also add str_array#to_a (identity), which
the block-less form's .to_a needs and which was missing.
This covers C2 of the String method-gap issue (C1 succ already worked; C3 undump remains).
Refs #1230
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- ce6995665306
- Parent
- d6f84294de53
spinel_analyze.rbmodifiedView file
end return "int" end # String#upto: the block form yields successive strings and returns # the receiver; the block-less form materializes as a str_array (the # enumerator's elements). Integer#upto keeps the int default below. if mname == "upto" && recv >= 0 rt_up = base_type(infer_type(recv)) if rt_up == "string" || rt_up == "mutable_str" if @nd_block[nid] >= 0 return "string" end @needs_str_array = 1 return "str_array" end end if mname == "pred" return "int" end if recv >= 0 recv_t = infer_type(recv) 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") return "string" end # Struct#to_h { |member_name, value| ... } yields the member symbol # and its value. Members are heterogeneous, so the value param is # poly. Without this the params default to int and the block bodyspinel_codegen.rbmodifiedView file
Loading diff…
test/string_upto.rbaddedView file
Loading diff…