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 step out of the times/upto/downto arm in both block_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-_i branch 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 step
27583+ # arg is float. Without the float slot, lv_x += 0.5 truncates
27584+ # to += 0 and loops forever.
27585+ elsif mname == "step"
27586+ is_float_step_sl = 0
27587+ if @nd_receiver[nid] >= 0 && infer_type(@nd_receiver[nid]) == "float"
27588+ is_float_step_sl = 1
27589+ end
27590+ args_step_sl = @nd_arguments[nid]
27591+ if args_step_sl >= 0
27592+ aargs_step_sl = get_args(args_step_sl)
27593+ ai_sl = 0
27594+ while ai_sl < aargs_step_sl.length
27595+ if infer_type(aargs_step_sl[ai_sl]) == "float"
27596+ is_float_step_sl = 1
27597+ end
27598+ ai_sl = ai_sl + 1
27599+ end
27600+ end
27601+ if is_float_step_sl == 1
27602+ types.push("float")
27603+ else
27604+ types.push("int")
27605+ end
2758227606 # `<source>.with_index(off).<consumer> { |elem, idx| }`:
2758327607 # block param at bk=1 is the idx counter (int), not another
2758427608 # element of the source. Without this, the same elem_type-
@@ -29960,7 +29984,28 @@
2996029984 end
2996129985 return "int"
2996229986 end
29963- if mname == "times" || mname == "upto" || mname == "downto" || mname == "step"
29987+ # Issue #870: Float#step yields floats. If recv or any step
29988+ # arg is float, the block param is float (without this the
29989+ # 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+ end
29995+ args_step_bp = @nd_arguments[call_nid]
29996+ if args_step_bp >= 0
29997+ aargs_step_bp = get_args(args_step_bp)
29998+ ai_step = 0
29999+ while ai_step < aargs_step_bp.length
30000+ if infer_type(aargs_step_bp[ai_step]) == "float"
30001+ return "float"
30002+ end
30003+ ai_step = ai_step + 1
30004+ end
30005+ end
30006+ return "int"
30007+ end
30008+ if mname == "times" || mname == "upto" || mname == "downto"
2996430009 return "int"
2996530010 end
2996630011 if mname == "reduce" || mname == "inject"