Commit 1d2c2905

1d2c2905b8d2Merged

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

Description

fix(codegen): poly .length / .size dispatch covers SP_TAG_STR

A poly receiver carrying a string at runtime previously had no .length / .size arm. The per-tag emit in compile_poly_method_call covered SP_TAG_OBJ (typed Array variants via sp_*Array_length) but not SP_TAG_STR, so a string-tagged poly's .length always read 0. The bug bit user-level guarded shapes like if v.is_a?(String) && v.length > 0; ... end where the && half that depends on the length silently false-d out and the then-arm never fired.

Fix: emit an SP_TAG_STR arm parallel to the existing gsub / sub SP_TAG_STR arm at the top of compile_poly_method_call, routing to sp_str_length(recv.v.s) and boxing for the poly-return case.

Validation: tests 458 pass (regression: is_a?(String) && length

0 path on a poly_array element), bench 57/57, bootstrap 4-way fixpoint OK.

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

Details

Commit
1d2c2905b8d2
Parent
b6f25665a42d

spinel_codegen.rbmodifiedView file

@@ -18580,6 +18580,20 @@
1858018580 end
1858118581 end
1858218582 end
18583+ # SP_TAG_STR arm for length / size. `v.length` where v is poly
18584+ # (e.g. v came from `poly_array[i]`) carrying a string at
18585+ # runtime needs a tag-dispatch arm — without this the only
18586+ # length emits live inside the SP_TAG_OBJ block below (typed
18587+ # array variants), so a string-tagged poly always read 0.
18588+ # Surfaces in `if v.is_a?(String) && v.length > 0 ; ... end`
18589+ # against a poly_array[i] receiver: the && condition's second
18590+ # half stays 0, the then-arm never fires, and the user-level
18591+ # guarded code path is silently skipped.
18592+ if mname == "length" || mname == "size"
18593+ slen_c = "sp_str_length(" + recv_tmp + ".v.s)"
18594+ slen_rhs = is_poly_ret == 1 ? "sp_box_int(" + slen_c + ")" : slen_c
18595+ emit(" if (" + recv_tmp + ".tag == SP_TAG_STR) " + tmp + " = " + slen_rhs + ";")
18596+ end
1858318597 emit(" if (" + recv_tmp + ".tag == SP_TAG_OBJ) {")
1858418598 # User-class dispatch
1858518599 i = 0