Commit 8123a5af

8123a5af9ad1Merged

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

Description

fix(codegen,analyze): Array#* with String separator routes through join

[1, 2, 3] * "," was treated as int-repeat — codegen took the is_array_type arm and emitted for (_mi = 0; _mi < _t3; _mi++) where _t3 was the string pointer cast to mrb_int. That loops up to a huge address-as-integer and either blows out memory or emits garbage.

Fix:

  • codegen: in the mname == "*" arm, when the arg's inferred type is "string" and the recv is an int_array / str_array, dispatch to sp_IntArray_join / sp_StrArray_join instead of the repeat template. Other array kinds fall through (no *_join helper).
  • analyze: infer_call_type returns "string" for <arr> * <string> so callers like (arr * sep).inspect route through string dispatch.

Adds test/array_mul_string.rb covering int_array * sep, the .inspect chain from the issue repro, empty separator, and str_array * sep.

Tests: default 450/0/0, promote 450/0/0.

Closes #680

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

Details

Commit
8123a5af9ad1
Parent
037c73788699

spinel_analyze.rbmodifiedView file

@@ -3411,7 +3411,16 @@
34113411 return "poly"
34123412 end
34133413 if is_array_type(lt) == 1
3414- # Array#* (repeat) yields another array of the same element type.
3414+ # Array#* has two shapes: `arr * n` (Integer) repeats and yields
3415+ # another array of the same element type; `arr * sep` (String)
3416+ # joins and yields a string.
3417+ args_id_am = @nd_arguments[nid]
3418+ if args_id_am >= 0
3419+ aargs_am = get_args(args_id_am)
3420+ if aargs_am.length > 0 && infer_type(aargs_am.first) == "string"
3421+ return "string"
3422+ end
3423+ end
34153424 return lt
34163425 end
34173426 if lt == "complex"