Commit d8a6db34
d8a6db349ae3Merged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
analyze,codegen: typed-hash #clear empties the receiver
{a:1,b:2}.clear previously fell through compile_hash_method_expr
and emitted the unresolved-call warning + runtime
undefined method 'clear' for sym_int_hash. Hash#clear is the
mutating zero-arg form that resets the hash to empty and returns
self.
Add a shared mname == "clear" && is_hash_type(recv_type) == 1
arm at the top of compile_hash_method_expr (alongside dig
and to_h), routing through a hash_clear_c_expr helper that
picks the right empty-slot marker per variant. The open-
addressing probe chain relies on each slot's empty marker, so
the loop has to wipe every slot (not just len):
- sym-keyed variants (sym_int, sym_str, sym_poly): keys[i] = -1
- str-keyed variants (str_int, str_str, str_poly): keys[i] = NULL
- int-keyed variants (int_str, int_poly): memset used[] = 0
- poly_poly_hash: memset occ[] = 0
Then h->len = 0 and yield the receiver via a GCC compound
expression so h.clear is a valid r-value (matches Hash#clear
returning self).
Analyzer side: extend the existing mname == "clear" arm
(spinel_analyze.rb:4964, previously array/string only) to
preserve any is_hash_type receiver so chained / ||=-style
calls don't fall back to int.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- d8a6db349ae3
- Parent
- 24ec1cf637a4
spinel_analyze.rbmodifiedView file
if rt_clr == "int_array" || rt_clr == "sym_array" || rt_clr == "str_array" || rt_clr == "float_array" || rt_clr == "poly_array" || is_ptr_array_type(rt_clr) == 1 || rt_clr == "string" || rt_clr == "mutable_str" rt_clr == "string" || rt_clr == "mutable_str" || is_hash_type(rt_clr) == 1 return rt_clr end endspinel_codegen.rbmodifiedView file
Loading diff…
test/hash_clear.rbaddedView file
Loading diff…
test/hash_clear.rb.expectedaddedView file
Loading diff…