Commit d3558d52
d3558d524e45Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
analyze,codegen: Float#step block param typed as float
1.0.step(3.0, 0.5) { |x| ... } looped forever because the
block param x slot was declared as mrb_int. lv_x += 0.5
truncated the increment to += 0, never reaching the limit.
- analyze: split
stepout of the times/upto/downto arm in bothblock_param_type_at(walk-and-cache path) and the parallel scan_locals arm. When the receiver OR any explicit step arg is float, the block param is float. - codegen: detect float step and emit the loop with mrb_float
induction variable + .0 init. The synth-
_ibranch picks the right C type.
Refs #870
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- d3558d524e45
- Parent
- d2a5bfa81580
spinel_analyze.rbmodifiedView file
@@ -27579,6 +27579,30 @@
2757927579
# Already pushed via the no-recv yield branch above.2758027580
elsif mname == "times" || mname == "upto" || mname == "downto"2758127581
types.push("int")27582+
# Issue #870: Float#step yields floats when recv or any step27583+
# arg is float. Without the float slot, lv_x += 0.5 truncates27584+
# to += 0 and loops forever.27585+
elsif mname == "step"27586+
is_float_step_sl = 027587+
if @nd_receiver[nid] >= 0 && infer_type(@nd_receiver[nid]) == "float"27588+
is_float_step_sl = 127589+
end27590+
args_step_sl = @nd_arguments[nid]27591+
if args_step_sl >= 027592+
aargs_step_sl = get_args(args_step_sl)27593+
ai_sl = 027594+
while ai_sl < aargs_step_sl.length27595+
if infer_type(aargs_step_sl[ai_sl]) == "float"27596+
is_float_step_sl = 127597+
end27598+
ai_sl = ai_sl + 127599+
end27600+
end27601+
if is_float_step_sl == 127602+
types.push("float")27603+
else27604+
types.push("int")27605+
end2758227606
# `<source>.with_index(off).<consumer> { |elem, idx| }`:2758327607
# block param at bk=1 is the idx counter (int), not another2758427608
# element of the source. Without this, the same elem_type-@@ -29960,7 +29984,28 @@
2996029984
end2996129985
return "int"2996229986
end29963-
if mname == "times" || mname == "upto" || mname == "downto" || mname == "step"29987+
# Issue #870: Float#step yields floats. If recv or any step29988+
# arg is float, the block param is float (without this the29989+
# int slot truncates `+= 0.5` to `+= 0` and loops forever).29990+
if mname == "step"29991+
recv_step_bp = @nd_receiver[call_nid]29992+
if recv_step_bp >= 0 && infer_type(recv_step_bp) == "float"29993+
return "float"29994+
end29995+
args_step_bp = @nd_arguments[call_nid]29996+
if args_step_bp >= 029997+
aargs_step_bp = get_args(args_step_bp)29998+
ai_step = 029999+
while ai_step < aargs_step_bp.length30000+
if infer_type(aargs_step_bp[ai_step]) == "float"30001+
return "float"30002+
end30003+
ai_step = ai_step + 130004+
end30005+
end30006+
return "int"30007+
end30008+
if mname == "times" || mname == "upto" || mname == "downto"2996430009
return "int"2996530010
end2996630011
if mname == "reduce" || mname == "inject"spinel_codegen.rbmodifiedView file
Loading diff…
test/float_step_block.rbaddedView file
Loading diff…
test/float_step_block.rb.expectedaddedView file
Loading diff…