Commit edc01a68

edc01a685617Merged

dak2 <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Fix block-param shadowing for reduce and each_with_object

The accumulator (reduce) and seed (each_with_object) block params were not declared in the block's inner scope with the seed's type. When an outer local of the same name but different type existed, infer_type inside the block resolved against the outer binding and dispatched the wrong runtime call (e.g. string concat instead of mrb_int addition).

reduce additionally needs expression-form support: the final accumulator must survive past the inner C scope, so the seed is evaluated into an outer-scope temp that the caller consumes.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
edc01a685617
Parent
5168e358a9e1

spinel_codegen.rbmodifiedView file

@@ -23056,22 +23056,20 @@
2305623056 @in_loop = 1
2305723057 rt = infer_type(@nd_receiver[nid])
2305823058 rc = compile_expr_gc_rooted(@nd_receiver[nid])
23059+ # Bind the seed to an outer-scope temp so the expression form can
23060+ # surface the final accumulator. CallNode seeds get GC-rooted by
23061+ # compile_expr_gc_rooted; literal/local seeds rely on the caller
23062+ # already holding a reference for the duration of the loop.
2305923063 obj_ct = "mrb_int"
23064+ obj_t = "int"
23065+ obj_arg_nid = -1
2306023066 args_id = @nd_arguments[nid]
2306123067 if args_id >= 0
2306223068 aargs = get_args(args_id)
2306323069 if aargs.length > 0
23064- obj_ct = c_type(infer_type(aargs[0]))
23065- end
23066- end
23067- # Root the seed: it lives across every loop iteration, and any
23068- # allocation inside the block can trigger a GC that would otherwise
23069- # sweep the freshly constructed seed object before the loop ends.
23070- obj_arg_nid = -1
23071- if args_id >= 0
23072- aargs2 = get_args(args_id)
23073- if aargs2.length > 0
23074- obj_arg_nid = aargs2[0]
23070+ obj_arg_nid = aargs[0]
23071+ obj_t = infer_type(obj_arg_nid)
23072+ obj_ct = c_type(obj_t)
2307523073 end
2307623074 end
2307723075 obj_arg = compile_expr_gc_rooted(obj_arg_nid)
@@ -23098,6 +23096,7 @@
2309823096 @indent = @indent + 1
2309923097 push_scope
2310023098 declare_var(bp1, elem_type_of_array(rt))
23099+ declare_var(bp2, obj_t)
2310123100 compile_stmts_body(@nd_body[@nd_block[nid]])
2310223101 pop_scope
2310323102 @indent = @indent - 1
@@ -24391,18 +24390,25 @@
2439124390 end
2439224391
2439324392 def compile_reduce_expr(nid)
24394- # Emit the reduce loop as side effects, return accumulator
2439524393 compile_reduce_block(nid)
24396- bp1 = get_block_param(nid, 0)
24397- if bp1 == ""
24398- bp1 = "_acc"
24399- end
24400- "lv_" + bp1
2440124394 end
2440224395
2440324396 def compile_reduce_block(nid)
24397+ old = @in_loop
24398+ @in_loop = 1
24399+ @needs_gc = 1
2440424400 rc = compile_expr_gc_rooted(@nd_receiver[nid])
24405- init_val = compile_arg0(nid)
24401+ # Hold on to the seed AST nid so we can `infer_type` it later for
24402+ # the accumulator's inner-scope registration.
24403+ seed_nid = -1
24404+ args_id = @nd_arguments[nid]
24405+ if args_id >= 0
24406+ aargs = get_args(args_id)
24407+ if aargs.length > 0
24408+ seed_nid = aargs[0]
24409+ end
24410+ end
24411+ init_val = compile_expr_gc_rooted(seed_nid)
2440624412 bp1 = get_block_param(nid, 0)
2440724413 bp2 = get_block_param(nid, 1)
2440824414 if bp1 == ""
@@ -24411,16 +24417,51 @@
2441124417 if bp2 == ""
2441224418 bp2 = "_x"
2441324419 end
24414- emit(" lv_" + bp1 + " = " + init_val + ";")
2441524420 rt = infer_type(@nd_receiver[nid])
2441624421 pfx = array_c_prefix(rt)
2441724422 elem_t = elem_type_of_array(rt)
24423+ # bp1 takes the seed's type. Seed-less form is currently treated as
24424+ # 0-seeded (init_val resolves to "0" via compile_expr(-1)); seed_t
24425+ # falls back to elem_t to keep the type system consistent. This is
24426+ # not true Ruby first-element seeding — known limitation.
24427+ seed_t = elem_t
24428+ if seed_nid >= 0
24429+ seed_t = infer_type(seed_nid)
24430+ end
24431+ # See compile_each_slice_block for the typed-shadow + SP_GC_ROOT
24432+ # technique inside a C block scope. reduce additionally needs
24433+ # result_tmp because it's expression-form: the final value must
24434+ # survive past the inner scope's `}` for the caller to consume.
24435+ outer_t = find_var_type(bp1)
24436+ shadowed = 0
24437+ if outer_t != "" && outer_t != seed_t
24438+ shadowed = 1
24439+ end
24440+ acc_expr = "lv_" + bp1
24441+ if shadowed == 1
24442+ result_tmp = new_temp
24443+ seed_ct = c_type(seed_t)
24444+ # Evaluate init_val once and share it between result_tmp and the
24445+ # shadowed lv_<bp1>; some seed expressions (ArrayNode/HashNode
24446+ # literals) are not lifted to a temp by compile_expr_gc_rooted.
24447+ emit(" " + seed_ct + " " + result_tmp + " = " + init_val + ";")
24448+ emit(" {")
24449+ @indent = @indent + 1
24450+ emit(" SP_GC_SAVE();")
24451+ emit(" " + seed_ct + " lv_" + bp1 + " = " + result_tmp + ";")
24452+ if type_is_pointer(seed_t) == 1
24453+ emit(" SP_GC_ROOT(lv_" + bp1 + ");")
24454+ end
24455+ acc_expr = result_tmp
24456+ else
24457+ emit(" lv_" + bp1 + " = " + init_val + ";")
24458+ end
2441824459 tmp = new_temp
2441924460 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {")
2442024461 emit(" " + c_type(elem_t) + " lv_" + bp2 + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
2442124462 @indent = @indent + 1
2442224463 push_scope
24423- declare_var(bp1, elem_t)
24464+ declare_var(bp1, seed_t)
2442424465 declare_var(bp2, elem_t)
2442524466 blk = @nd_block[nid]
2442624467 if blk >= 0
@@ -24437,6 +24478,13 @@
2443724478 pop_scope
2443824479 @indent = @indent - 1
2443924480 emit(" }")
24481+ if shadowed == 1
24482+ emit(" " + acc_expr + " = lv_" + bp1 + ";")
24483+ @indent = @indent - 1
24484+ emit(" }")
24485+ end
24486+ @in_loop = old
24487+ acc_expr
2444024488 end
2444124489
2444224490 def compile_reject_expr(nid)