Commit 763c2cd1
763c2cd12d01Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(codegen,analyze): Array#at, Array#append, String#chomp! aliases
Three of the ten #619 puzzles all reduced to missing method aliases:
-
Array#at(n):[1,2,3].at(1)previously hit warn_unresolved_call and returned 0. Aliased to[]inside compile_array_method_expr (the is_array_type guard at the top scopes the alias to built-in arrays only -- a user class withdef atkeeps its own dispatch). Mirrored on the analyzer's[]return-type block with an is_array_type-on-recv guard so non-arrayFoo.at(...)calls (Time.at and friends) don't accidentally pick the array element return type. -
Array#append(x):%w[a].append("b")previously misdispatched. Aliased topushin compile_array_method_expr; same array-only scoping. The analyzer's mname == "push" return-type rule also matches "append" (both return the recv array type). -
String#chomp!:"ab\n".chomp!aliased to chomp on the codegen and analyzer sides. The Rubynil if no changemutator semantic isn't preserved (a documented quirk; spinel strings are immutable so there's nothing to mutate in place).
Codegen aliases recurse with the canonical mname rather than reassigning the parameter. Writing back to a Ruby parameter inside the body triggers a body-usage widening pass that lands warn_unresolved_call's mname as poly in the bootstrap build, so the recursion form keeps the param's tracked type stable.
compile_array_method_expr's str_array push arm now returns the
receiver (matching CRuby's arr.push(x) -> arr) instead of the
literal 0 it emitted before. Without this, an expression-form push
(arr.push("b") == other, the .append alias above) compared int 0
against the other array and surfaced false. The << operator's
str_array branch already returns the receiver; this lines push up
with that.
Refs #619 (puzzles 8 / 9 / 10). Puzzles 2-7 (split("", n), out-of-bounds String#[], heterogeneous arrays, Array.new(N) -> [nil; N], poly-element dig, mixed-element arrays) require deeper type-system work and stay open.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 763c2cd12d01
- Parent
- 10c6e42f9c0d
spinel_analyze.rbmodifiedView file
if mname == "strip" return "string" end if mname == "chomp" || mname == "chop" if mname == "chomp" || mname == "chop" || mname == "chomp!" return "string" end # `force_encoding` / `encode` / `b` lower to a receiver if mname == "invert" return "str_str_hash" end if mname == "push" if mname == "push" || mname == "append" if recv >= 0 return infer_type(recv) end end return "int" end if mname == "[]" if mname == "[]" || (mname == "at" && recv >= 0 && is_array_type(infer_type(recv)) == 1) if recv >= 0 # ENV["X"] returns `const char *` (sp_str_dup_external of # getenv). The plain receiver-type dispatch below wouldspinel_codegen.rbmodifiedView file
Loading diff…
test/array_at_append_string_chomp_bang.rbaddedView file
Loading diff…
test/array_at_append_string_chomp_bang.rb.expectedaddedView file
Loading diff…