Commit c9cbc19c

c9cbc19cec90Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

fix: infer block parameter types from receiver collection type

Block parameters for map, select, reject, find, detect, any?, all?, none?, count, min_by, max_by, sort_by, flat_map, each_with_index, reduce, and inject now correctly infer element types from the receiver collection (str_array→string, float_array→float, ptr_array→element type). reduce/inject accumulator type is inferred from the initial value argument. Previously all defaulted to int.

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

Details

Commit
c9cbc19cec90
Parent
0fd2b126507d

spinel_codegen.rbmodifiedView file

@@ -9189,63 +9189,71 @@
91899189 bk = bk + 1
91909190 next
91919191 end
9192- if mname == "each"
9193- if recv_type == "int_array"
9192+ if mname == "times" || mname == "upto" || mname == "downto"
9193+ types.push("int")
9194+ elsif mname == "each" || mname == "map" || mname == "select" || mname == "reject" || mname == "find" || mname == "detect" || mname == "any?" || mname == "all?" || mname == "none?" || mname == "count" || mname == "min_by" || mname == "max_by" || mname == "sort_by" || mname == "flat_map"
9195+ # Element iteration: infer block param from collection type
9196+ if recv_type == "str_array"
9197+ types.push("string")
9198+ elsif recv_type == "float_array"
9199+ types.push("float")
9200+ elsif recv_type == "str_int_hash"
91949201 if bk == 0
9195- types.push("int")
9202+ types.push("string")
91969203 else
91979204 types.push("int")
91989205 end
9206+ elsif recv_type == "str_str_hash"
9207+ types.push("string")
9208+ elsif recv_type == "poly_array"
9209+ types.push("poly")
9210+ @needs_rb_value = 1
9211+ elsif is_ptr_array_type(recv_type) == 1
9212+ types.push(ptr_array_elem_type(recv_type))
91999213 else
9214+ types.push("int")
9215+ end
9216+ elsif mname == "each_with_index"
9217+ if bk == 0
9218+ # Element
92009219 if recv_type == "str_array"
92019220 types.push("string")
9221+ elsif recv_type == "float_array"
9222+ types.push("float")
9223+ elsif is_ptr_array_type(recv_type) == 1
9224+ types.push(ptr_array_elem_type(recv_type))
92029225 else
9203- if recv_type == "str_int_hash"
9204- if bk == 0
9205- types.push("string")
9206- else
9207- types.push("int")
9208- end
9209- else
9210- if recv_type == "str_str_hash"
9211- types.push("string")
9212- else
9213- if recv_type == "poly_array"
9214- types.push("poly")
9215- @needs_rb_value = 1
9216- else
9217- types.push("int")
9218- end
9219- end
9226+ types.push("int")
9227+ end
9228+ else
9229+ # Index
9230+ types.push("int")
9231+ end
9232+ elsif mname == "reduce" || mname == "inject"
9233+ if bk == 0
9234+ # Accumulator: infer from initial value argument
9235+ args_id = @nd_arguments[nid]
9236+ if args_id >= 0
9237+ aargs = get_args(args_id)
9238+ if aargs.length > 0
9239+ types.push(infer_type(aargs[0]))
9240+ bk = bk + 1
9241+ next
92209242 end
92219243 end
9222- end
9223- else
9224- if mname == "times"
92259244 types.push("int")
92269245 else
9227- if mname == "upto"
9228- types.push("int")
9246+ # Element
9247+ if recv_type == "str_array"
9248+ types.push("string")
9249+ elsif recv_type == "float_array"
9250+ types.push("float")
92299251 else
9230- if mname == "downto"
9231- types.push("int")
9232- else
9233- if mname == "reduce"
9234- types.push("int")
9235- else
9236- if mname == "inject"
9237- types.push("int")
9238- else
9239- if mname == "reject"
9240- types.push("int")
9241- else
9242- types.push("int")
9243- end
9244- end
9245- end
9246- end
9252+ types.push("int")
92479253 end
92489254 end
9255+ else
9256+ types.push("int")
92499257 end
92509258 end
92519259 end