Commit 4da7f778

4da7f778cdd2Merged

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

Description

feat(codegen,analyze,runtime): bind **kwargs to sym_poly_hash

def f(a, **kwargs) was missing the call-site dispatch that packs unmatched keyword args into the kwrest slot. Call sites like f(1, b: 2, c: 3) warned "call to 'f' is missing required arg #2 (kwargs)" and emitted 0 for the slot, then kwargs.inspect itself warned "cannot resolve call to 'inspect' on sym_poly_hash" and emitted 0 again.

  • analyze: track @meth_kwrest_index alongside @meth_rest_index so call-site arg-packing can locate the kwrest slot. New helper collect_kwrest_index walks reqs + opts + kws + (rest?) + posts to find the position. Plumbed through every meth-table append / update site + the IR emit.
  • codegen: load @meth_kwrest_index from IR; new method_kwrest_index reader. compile_call_args_with_defaults, when filling the kwrest slot, packs every kw_name/kw_val pair that doesn't bind to an explicit keyword param into a fresh sp_SymPolyHash and passes that.
  • codegen: sym_poly_hash dispatch gains .inspect / .to_s routing through the new runtime helper.
  • runtime: sp_SymPolyHash_inspect renders {:k=>v, ...} (CRuby's pre-3.4 form, parens-eq-syntax-friendly).

Tests: default 455/0/0, promote 455/0/0, optcarrot checksum 59662.

Closes #676

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
4da7f778cdd2
Parent
1d5adcad1a59

lib/sp_runtime.hmodifiedView file

@@ -2108,6 +2108,11 @@
21082108 static void sp_SymPolyHash_delete(sp_SymPolyHash*h,sp_sym k){mrb_int idx=(mrb_int)(((mrb_int)k)&h->mask);while(h->keys[idx]>=0){if(h->keys[idx]==k){h->keys[idx]=-1;h->vals[idx]=sp_box_nil();h->len--;mrb_int j=(idx+1)&h->mask;while(h->keys[j]>=0){mrb_int nj=(mrb_int)(((mrb_int)h->keys[j])&h->mask);if((j>idx&&(nj<=idx||nj>j))||(j<idx&&nj<=idx&&nj>j)){h->keys[idx]=h->keys[j];h->vals[idx]=h->vals[j];h->keys[j]=-1;h->vals[j]=sp_box_nil();idx=j;}j=(j+1)&h->mask;}{mrb_int oi=0;while(oi<=h->len){if(h->order[oi]==k){while(oi<h->len){h->order[oi]=h->order[oi+1];oi++;}break;}oi++;}}return;}idx=(idx+1)&h->mask;}}
21092109 static sp_SymPolyHash*sp_SymPolyHash_dup(sp_SymPolyHash*h){sp_SymPolyHash*r=sp_SymPolyHash_new();r->default_v=h->default_v;for(mrb_int i=0;i<h->len;i++)sp_SymPolyHash_set(r,h->order[i],sp_SymPolyHash_get(h,h->order[i]));return r;}
21102110 static mrb_bool sp_SymPolyHash_eq(sp_SymPolyHash*a,sp_SymPolyHash*b){if(!a||!b)return a==b;if(a->len!=b->len)return FALSE;for(mrb_int i=0;i<a->len;i++){sp_sym k=a->order[i];if(!sp_SymPolyHash_has_key(b,k))return FALSE;if(!sp_poly_eq(sp_SymPolyHash_get(a,k),sp_SymPolyHash_get(b,k)))return FALSE;}return TRUE;}
2111+/* Hash#inspect for sym_poly_hash. CRuby 3.4+ renders symbol keys
2112+ in shorthand: `{b: 2, c: 3}` rather than `{:b=>2, :c=>3}`. We
2113+ match the older `=>` form to keep round-tripping simple (callers
2114+ that compare against `eval(str)` survive either way). */
2115+static const char*sp_SymPolyHash_inspect(sp_SymPolyHash*h){if(!h){char*r=sp_str_alloc_raw(3);r[0]='{';r[1]='}';r[2]=0;sp_str_set_len(r,2);return r;}sp_String*s=sp_String_new("{");for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,":");sp_String_append(s,sp_sym_to_s(h->order[i]));sp_String_append(s,"=>");sp_String_append(s,sp_poly_inspect(sp_SymPolyHash_get(h,h->order[i])));}sp_String_append(s,"}");return s->data;}
21112116
21122117 /* PolyPolyHash: heterogeneous keys + values (both sp_RbVal). For
21132118 primitives the hash/eql is tag-based (value equality); for OBJ tag