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

@@ -5041,6 +5041,19 @@
50415041 end
50425042 return "int"
50435043 end
5044+ # String#upto: the block form yields successive strings and returns
5045+ # the receiver; the block-less form materializes as a str_array (the
5046+ # enumerator's elements). Integer#upto keeps the int default below.
5047+ if mname == "upto" && recv >= 0
5048+ rt_up = base_type(infer_type(recv))
5049+ if rt_up == "string" || rt_up == "mutable_str"
5050+ if @nd_block[nid] >= 0
5051+ return "string"
5052+ end
5053+ @needs_str_array = 1
5054+ return "str_array"
5055+ end
5056+ end
50445057 if mname == "pred"
50455058 return "int"
50465059 end
@@ -33011,6 +33024,11 @@
3301133024 if recv >= 0
3301233025 recv_t = infer_type(recv)
3301333026 end
33027+ # `"a".upto("e") { |c| ... }` yields successive strings; the block
33028+ # param is a string (Integer#upto keeps the int default).
33029+ if mname == "upto" && pi == 0 && (base_type(recv_t) == "string" || base_type(recv_t) == "mutable_str")
33030+ return "string"
33031+ end
3301433032 # Struct#to_h { |member_name, value| ... } yields the member symbol
3301533033 # and its value. Members are heterogeneous, so the value param is
3301633034 # poly. Without this the params default to int and the block body