Commit 507158f6

507158f61403Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · +1 added · ~1 modified

Description

Type StringScanner#scan to a local as a String, not an Array

m = scanner.scan(/re/) declared m as sp_StrArray * -- the generic String#scan arm (which returns an array of matches) fired for a StringScanner receiver too, because infer_type's scan rule had no receiver-type check. During scan_locals the receiver can be unresolved when that rule runs, so the earlier StringScanner arm hasn't typed it yet.

Gate the String#scan str_array/poly_array arm on a string-like receiver, so StringScanner#scan (a single matched string) types the local correctly while String#scan on a real String still returns the array.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
507158f61403
Parent
6d94883c4383

spinel_analyze.rbmodifiedView file

@@ -5341,15 +5341,23 @@
53415341 end
53425342 end
53435343 if mname == "scan"
5344- args_id_scan = @nd_arguments[nid]
5345- if args_id_scan >= 0
5346- arg_ids_scan = get_args(args_id_scan)
5347- if arg_ids_scan.length > 0 && regexp_capture_status(arg_ids_scan[0]) != 0
5348- @needs_rb_value = 1
5349- return "poly_array"
5344+ # String#scan returns an array of matches. Gate on a string-like
5345+ # receiver so StringScanner#scan (which returns a single matched
5346+ # string, handled by the strscan dispatch above) isn't mis-typed as
5347+ # str_array -- the receiver can still be unresolved during scan_locals,
5348+ # when the strscan arm hasn't fired yet.
5349+ rt_scan_recv = recv >= 0 ? base_type(infer_type(recv)) : ""
5350+ if recv < 0 || rt_scan_recv == "string" || rt_scan_recv == "mutable_str"
5351+ args_id_scan = @nd_arguments[nid]
5352+ if args_id_scan >= 0
5353+ arg_ids_scan = get_args(args_id_scan)
5354+ if arg_ids_scan.length > 0 && regexp_capture_status(arg_ids_scan[0]) != 0
5355+ @needs_rb_value = 1
5356+ return "poly_array"
5357+ end
53505358 end
5359+ return "str_array"
53515360 end
5352- return "str_array"
53535361 end
53545362 if mname == "match"
53555363 if recv >= 0