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 end
4757+ if mname == "rand" && recv < 0
4758+ # `rand()` (no arg) returns Float in [0.0, 1.0). `rand(int)`
4759+ # returns Integer; the int-arg case falls through to the int
4760+ # default below.
4761+ if @nd_arguments[nid] < 0 || get_args(@nd_arguments[nid]).length == 0
4762+ return "float"
4763+ end
4764+ end
47574765 if mname == "scan"
47584766 return "str_array"
47594767 end