Commit 9a958544
9a958544a9bfMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
analyze,codegen: Kernel#rand without args returns Float in [0.0, 1.0)
CRuby's rand() with no arg returns a Float; spinel was returning `(mrb_int)rand()` and analyze typed it as int. Add the no-arg arm in both layers: analyze returns "float", codegen emits `(mrb_float)rand() / (RAND_MAX + 1.0)` to stay in [0.0, 1.0). The int-arg form is unchanged.
Fixes #959
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 9a958544a9bf
- Parent
- d54542978b35
spinel_analyze.rbmodifiedView file
@@ -4754,6 +4754,14 @@
47544754
if mname == "lines"47554755
return "str_array"47564756
end4757+
if mname == "rand" && recv < 04758+
# `rand()` (no arg) returns Float in [0.0, 1.0). `rand(int)`4759+
# returns Integer; the int-arg case falls through to the int4760+
# default below.4761+
if @nd_arguments[nid] < 0 || get_args(@nd_arguments[nid]).length == 04762+
return "float"4763+
end4764+
end47574765
if mname == "scan"47584766
return "str_array"47594767
endspinel_codegen.rbmodifiedView file
Loading diff…
test/kernel_rand_no_arg_float.rbaddedView file
Loading diff…
test/kernel_rand_no_arg_float.rb.expectedaddedView file
Loading diff…