Commit fea6561a
fea6561a6e40Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
analyze,codegen: str_array #max_by / #min_by with block
Both used to fall through to the unresolved-call warning ("cannot resolve call to 'max_by' on str_array") and emit 0. Now codegen loops the array, scores each element via the block, and tracks the best score / element. Analyzer returns the element type (string for str_array, int for int_array, ...).
Fixes #863
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- fea6561a6e40
- Parent
- 78a42f454edb
spinel_analyze.rbmodifiedView file
@@ -5217,10 +5217,25 @@
52175217
end52185218
return "int_array"52195219
end5220-
if mname == "min_by"5221-
return "int"5222-
end5223-
if mname == "max_by"5220+
if mname == "min_by" || mname == "max_by"5221+
# Result is an *element* of the receiver array (Ruby:5222+
# `[1,2,3].max_by{|x| -x} == 1`). Strip the `_array` suffix to5223+
# recover the element type when receiver is typed.5224+
if recv >= 05225+
rt_mb = infer_type(recv)5226+
if rt_mb == "int_array"5227+
return "int"5228+
end5229+
if rt_mb == "str_array"5230+
return "string"5231+
end5232+
if rt_mb == "float_array"5233+
return "float"5234+
end5235+
if rt_mb == "sym_array"5236+
return "symbol"5237+
end5238+
end52245239
return "int"52255240
end52265241
if mname == "unshift"spinel_codegen.rbmodifiedView file
Loading diff…
test/str_array_max_min_by.rbaddedView file
Loading diff…