Commit 62191310
6219131053d8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Expand string-range splat in array literals
[*"a".."z"] produced invalid C: the array literal mis-typed to
int_array (infer_type on a SplatNode defaults to int), and the splat
branch emitted an integer expansion loop with the string bounds as
mrb_int -- a char* used in integer context.
- analyze/codegen infer_array_elem_type_from_ids: a leading SplatNode whose inner is a string RangeNode types the literal as str_array.
- codegen str_array path: a SplatNode element expands its source into the str_array -- a string range via the existing sp_StrArray_from_string_range, or another str_array elementwise.
["a".."e"] # => ["a","b","c","d","e"] (was: compile error) ["a"..."d"] # => ["a","b","c"] [*"x".."z", "!"] # => ["x","y","z","!"]
Fixes #1131
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 6219131053d8
- Parent
- 04052c60b425
spinel_analyze.rbmodifiedView file
@@ -2766,6 +2766,15 @@
27662766
# without needing an actual ArrayNode to host them.27672767
def infer_array_elem_type_from_ids(elems)27682768
if elems.length > 02769+
# `[*"a".."z"]` -- a splat of a string range expands to string2770+
# elements, so the literal is a str_array. infer_type(SplatNode)2771+
# defaults to int, which would otherwise mis-route this to int_array.2772+
if @nd_type[elems[0]] == "SplatNode"2773+
inner0 = @nd_expression[elems[0]]2774+
if inner0 >= 0 && @nd_type[inner0] == "RangeNode" && infer_type(@nd_left[inner0]) == "string"2775+
return "str_array"2776+
end2777+
end27692778
et = infer_type(elems[0])27702779
if et == "symbol"27712780
# Check if ALL elements are symbolsspinel_codegen.rbmodifiedView file
Loading diff…
test/string_range_splat.rbaddedView file
Loading diff…
test/string_range_splat.rb.expectedaddedView file
Loading diff…