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 with def at keeps its own dispatch). Mirrored on the analyzer's [] return-type block with an is_array_type-on-recv guard so non-array Foo.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 to push in 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 Ruby nil if no change mutator 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

@@ -4163,7 +4163,7 @@
41634163 if mname == "strip"
41644164 return "string"
41654165 end
4166- if mname == "chomp" || mname == "chop"
4166+ if mname == "chomp" || mname == "chop" || mname == "chomp!"
41674167 return "string"
41684168 end
41694169 # `force_encoding` / `encode` / `b` lower to a receiver
@@ -4803,7 +4803,7 @@
48034803 if mname == "invert"
48044804 return "str_str_hash"
48054805 end
4806- if mname == "push"
4806+ if mname == "push" || mname == "append"
48074807 if recv >= 0
48084808 return infer_type(recv)
48094809 end
@@ -5283,7 +5283,7 @@
52835283 end
52845284 return "int"
52855285 end
5286- if mname == "[]"
5286+ if mname == "[]" || (mname == "at" && recv >= 0 && is_array_type(infer_type(recv)) == 1)
52875287 if recv >= 0
52885288 # ENV["X"] returns `const char *` (sp_str_dup_external of
52895289 # getenv). The plain receiver-type dispatch below would