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 + 191909190
next91919191
end9192-
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 type9196+
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 == 09195-
types.push("int")9202+
types.push("string")91969203
else91979204
types.push("int")91989205
end9206+
elsif recv_type == "str_str_hash"9207+
types.push("string")9208+
elsif recv_type == "poly_array"9209+
types.push("poly")9210+
@needs_rb_value = 19211+
elsif is_ptr_array_type(recv_type) == 19212+
types.push(ptr_array_elem_type(recv_type))91999213
else9214+
types.push("int")9215+
end9216+
elsif mname == "each_with_index"9217+
if bk == 09218+
# Element92009219
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) == 19224+
types.push(ptr_array_elem_type(recv_type))92029225
else9203-
if recv_type == "str_int_hash"9204-
if bk == 09205-
types.push("string")9206-
else9207-
types.push("int")9208-
end9209-
else9210-
if recv_type == "str_str_hash"9211-
types.push("string")9212-
else9213-
if recv_type == "poly_array"9214-
types.push("poly")9215-
@needs_rb_value = 19216-
else9217-
types.push("int")9218-
end9219-
end9226+
types.push("int")9227+
end9228+
else9229+
# Index9230+
types.push("int")9231+
end9232+
elsif mname == "reduce" || mname == "inject"9233+
if bk == 09234+
# Accumulator: infer from initial value argument9235+
args_id = @nd_arguments[nid]9236+
if args_id >= 09237+
aargs = get_args(args_id)9238+
if aargs.length > 09239+
types.push(infer_type(aargs[0]))9240+
bk = bk + 19241+
next92209242
end92219243
end9222-
end9223-
else9224-
if mname == "times"92259244
types.push("int")92269245
else9227-
if mname == "upto"9228-
types.push("int")9246+
# Element9247+
if recv_type == "str_array"9248+
types.push("string")9249+
elsif recv_type == "float_array"9250+
types.push("float")92299251
else9230-
if mname == "downto"9231-
types.push("int")9232-
else9233-
if mname == "reduce"9234-
types.push("int")9235-
else9236-
if mname == "inject"9237-
types.push("int")9238-
else9239-
if mname == "reject"9240-
types.push("int")9241-
else9242-
types.push("int")9243-
end9244-
end9245-
end9246-
end9252+
types.push("int")92479253
end92489254
end9255+
else9256+
types.push("int")92499257
end92509258
end92519259
end