Commit abf7f6b6

abf7f6b62737Merged

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

Description

Fix each_with_object block param shadowing + double-splat kwargs

analyze: bind_call_params now handles **h (AssocSplatNode) double-splat calls, propagating hash value types to keyword params. Also adds fallback for Ruby's kwarg-collapse-to-positional idiom (def f(opts={})) so the backstop doesn't kill methods with untyped hash-default params. infer_default_param_types treats empty {} default as TY_SYM_POLY_HASH.

codegen: emit_args_filled pre-evaluates **h hash to a temp and extracts per-param values by sym key. emit_each_with_object_expr opens a C scope block when block param names collide with outer variables of different types, shadowing correctly.

Removes temporary debug envvar hooks added during investigation.

Fixes: kwargs_double_splat, kwargs_merge_default_hash, bundle_misc_c_37

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
abf7f6b62737
Parent
86fc7098f329

src/analyze.cmodifiedView file

@@ -3951,18 +3951,57 @@
39513951 if (kwh >= 0) {
39523952 int en = 0;
39533953 const int *elems = nt_arr(nt, kwh, "elements", &en);
3954+ /* Check for a double-splat (**h) covering all keyword params. */
3955+ TyKind ds_val = TY_UNKNOWN;
39543956 for (int e = 0; e < en; e++) {
3955- int key = nt_ref(nt, elems[e], "key");
3956- int val = nt_ref(nt, elems[e], "value");
3957- if (key < 0 || val < 0) continue;
3958- const char *kty = nt_type(nt, key);
3959- const char *kname = (kty && !strcmp(kty, "SymbolNode")) ? nt_str(nt, key, "value") : NULL;
3960- if (!kname) continue;
3961- LocalVar *p = scope_local(m, kname);
3962- if (!p) continue;
3963- TyKind at = infer_type(c, val);
3964- TyKind merged = ty_unify(p->type, at);
3965- if (merged != p->type) { p->type = merged; changed = 1; }
3957+ const char *ety = nt_type(nt, elems[e]);
3958+ if (ety && !strcmp(ety, "AssocSplatNode")) {
3959+ int inner = nt_ref(nt, elems[e], "value");
3960+ if (inner >= 0) {
3961+ TyKind ht = infer_type(c, inner);
3962+ if (ty_is_hash(ht)) ds_val = ty_hash_val(ht);
3963+ }
3964+ break;
3965+ }
3966+ }
3967+ if (ds_val != TY_UNKNOWN) {
3968+ /* Bind all keyword params of the callee from the splat hash value type. */
3969+ TyKind at = (ds_val == TY_POLY) ? TY_POLY : ds_val;
3970+ if (at == TY_NIL) at = TY_POLY;
3971+ for (int i = 0; i < m->nparams; i++) {
3972+ if (!m->pnames[i]) continue;
3973+ LocalVar *p = scope_local(m, m->pnames[i]);
3974+ if (!p) continue;
3975+ TyKind merged = ty_unify(p->type, at);
3976+ if (merged != p->type) { p->type = merged; changed = 1; }
3977+ }
3978+ } else {
3979+ int any_kw_bound = 0;
3980+ for (int e = 0; e < en; e++) {
3981+ int key = nt_ref(nt, elems[e], "key");
3982+ int val = nt_ref(nt, elems[e], "value");
3983+ if (key < 0 || val < 0) continue;
3984+ const char *kty = nt_type(nt, key);
3985+ const char *kname = (kty && !strcmp(kty, "SymbolNode")) ? nt_str(nt, key, "value") : NULL;
3986+ if (!kname) continue;
3987+ LocalVar *p = scope_local(m, kname);
3988+ if (!p) continue;
3989+ TyKind at = infer_type(c, val);
3990+ TyKind merged = ty_unify(p->type, at);
3991+ if (merged != p->type) { p->type = merged; changed = 1; }
3992+ any_kw_bound = 1;
3993+ }
3994+ /* Ruby collapses trailing kwargs into a positional hash parameter when
3995+ the callee has no named keyword params (e.g. `def f(opts = {})`
3996+ called as `f(key: val)`). Bind the next unbound positional param
3997+ to TY_SYM_POLY_HASH so the backstop doesn't kill the method. */
3998+ if (!any_kw_bound && pos_argc < max_bind && max_bind > 0) {
3999+ LocalVar *p = m->pnames[pos_argc] ? scope_local(m, m->pnames[pos_argc]) : NULL;
4000+ if (p) {
4001+ TyKind merged = ty_unify(p->type, TY_SYM_POLY_HASH);
4002+ if (merged != p->type) { p->type = merged; changed = 1; }
4003+ }
4004+ }
39664005 }
39674006 }
39684007 return changed;
@@ -4010,7 +4049,16 @@
40104049 for (int i = 0; i < sc->nparams; i++) {
40114050 if (sc->pdefault[i] < 0) continue;
40124051 TyKind dt = infer_type(c, sc->pdefault[i]);
4013- if (dt == TY_NIL) continue; /* nil default doesn't pin the type */
4052+ /* An empty hash `{}` default returns TY_UNKNOWN from infer_type; treat
4053+ it as TY_SYM_POLY_HASH since it is used as a kwargs receiver. */
4054+ if (dt == TY_UNKNOWN) {
4055+ const char *dty = nt_type(c->nt, sc->pdefault[i]);
4056+ if (dty && (!strcmp(dty, "HashNode") || !strcmp(dty, "KeywordHashNode"))) {
4057+ int dn = 0; nt_arr(c->nt, sc->pdefault[i], "elements", &dn);
4058+ if (dn == 0) dt = TY_SYM_POLY_HASH;
4059+ }
4060+ }
4061+ if (dt == TY_NIL || dt == TY_UNKNOWN) continue;
40144062 LocalVar *p = scope_local(sc, sc->pnames[i]);
40154063 if (!p) continue;
40164064 TyKind merged = ty_unify(p->type, dt);