Commit b6110d8a
b6110d8a8703Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Accept Range/Float arguments in rand, Range, and Array methods
A cluster of "sp_Range struct fed to an mrb_int operand" C errors where a Range or Float argument reached a path that only handled integers:
- rand(range) / rand(float): int range -> Integer, float range/arg -> Float, via a shared [0,1) uniform deviate; integer arg unchanged.
- Range#first(n) / #last(n): materialize an int_array slice (clamped to the range) instead of returning the struct field; analyze types the result int_array.
- Range#cover?(other_range): true when self spans the other range's inclusive bounds, instead of comparing an sp_Range to mrb_int.
- Array#values_at(range): expand the range into element gets (int and str arrays).
- Array#fill(value, range): fill the inclusive/exclusive index range, with negative endpoints counted from the length.
Fixes #1247 Fixes #1234 Fixes #1228 Refs #1240 Refs #1224
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- b6110d8a8703
- Parent
- 490093f5c137
spinel_analyze.rbmodifiedView file
@@ -5208,6 +5208,22 @@
52085208
if @nd_arguments[nid] < 0 || get_args(@nd_arguments[nid]).length == 052095209
return "float"52105210
end5211+
# `rand(range)` yields an Integer for an int range, a Float for a5212+
# float range; `rand(float)` yields a Float. An int arg falls5213+
# through to the int default. The early return above guarantees a5214+
# non-empty arg list here.5215+
a0_rand = get_args(@nd_arguments[nid])[0]5216+
if @nd_type[a0_rand] == "RangeNode"5217+
lr = @nd_left[a0_rand]5218+
rr = @nd_right[a0_rand]5219+
if (lr >= 0 && infer_type(lr) == "float") || (rr >= 0 && infer_type(rr) == "float")5220+
return "float"5221+
end5222+
return "int"5223+
end5224+
if infer_type(a0_rand) == "float"5225+
return "float"5226+
end52115227
end52125228
if mname == "scan"52135229
args_id_scan = @nd_arguments[nid]@@ -5720,10 +5736,15 @@
57205736
end57215737
end57225738
end5723-
# With arg → returns array of same type5739+
# With arg → returns array of same type. A Range receiver5740+
# materializes to an int_array (`(1..10).first(3)` => [1,2,3]),5741+
# not another Range.57245742
if @nd_arguments[nid] >= 057255743
aargs = get_args(@nd_arguments[nid])57265744
if aargs.length > 05745+
if rt == "range"5746+
return "int_array"5747+
end57275748
return rt57285749
end57295750
endspinel_codegen.rbmodifiedView file
Loading diff…
test/range_and_array_range_args.rbaddedView file
Loading diff…
test/range_and_array_range_args.rb.expectedaddedView file
Loading diff…