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
end end end # SP_TAG_STR arm for length / size. `v.length` where v is poly # (e.g. v came from `poly_array[i]`) carrying a string at # runtime needs a tag-dispatch arm — without this the only # length emits live inside the SP_TAG_OBJ block below (typed # array variants), so a string-tagged poly always read 0. # Surfaces in `if v.is_a?(String) && v.length > 0 ; ... end` # against a poly_array[i] receiver: the && condition's second # half stays 0, the then-arm never fires, and the user-level # guarded code path is silently skipped. if mname == "length" || mname == "size" slen_c = "sp_str_length(" + recv_tmp + ".v.s)" slen_rhs = is_poly_ret == 1 ? "sp_box_int(" + slen_c + ")" : slen_c emit(" if (" + recv_tmp + ".tag == SP_TAG_STR) " + tmp + " = " + slen_rhs + ";") end emit(" if (" + recv_tmp + ".tag == SP_TAG_OBJ) {") # User-class dispatch i = 0test/poly_length_string_tag.rbaddedView file
Loading diff…
test/poly_length_string_tag.rb.expectedaddedView file
Loading diff…