Commit 05e95ac2

05e95ac2addbMerged

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

Description

Keyword params, sibling class method calls, module_function improvements

  1. Keyword parameters (RequiredKeywordParameterNode/OptionalKeywordParameterNode) are now registered in walk_scope so methods with tag: style params get typed correctly. bind_call_params separates trailing KeywordHashNode from positional args and matches by name. emit_args_filled / emit_dispatch do the same so keyword args are passed by name rather than positionally.

  2. Bare calls inside class methods (def self.foo calling bar()) now resolve sibling class methods via comp_cmethod_in_chain in both the analyze fixpoint seeding and infer_call, plus the emit_call path emits the correct sp_Cls_s_method() function name.

  3. module_function: improved comp_included_method_index to check cmethod chain first (module_function defs are is_cmethod=1 after the new register_module_functions pass).

Fixes bare_return_in_initialize, bare_sibling_cls_method_call, and several other tests that use keyword params.

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

Details

Commit
05e95ac2addb
Parent
36a47707f320

src/analyze.cmodifiedView file

@@ -498,6 +498,8 @@
498498 if (iv >= 0) return c->classes[self->class_id].ivar_types[iv];
499499 }
500500 int mi = comp_method_in_chain(c, self->class_id, name, NULL);
501+ if (mi < 0 && self->is_cmethod)
502+ mi = comp_cmethod_in_chain(c, self->class_id, name, NULL);
501503 if (mi >= 0) return method_call_ret(c, mi, id);
502504 }
503505 }
@@ -1130,6 +1132,16 @@
11301132 int dv = nt_ref(c->nt, opts[i], "value");
11311133 if (pname) scope_add_param(s, pname, dv);
11321134 }
1135+ /* keyword parameters: `tag:` (required) or `tag: default` (optional) */
1136+ int kn = 0;
1137+ const int *kws = nt_arr(c->nt, pn, "keywords", &kn);
1138+ for (int i = 0; i < kn; i++) {
1139+ const char *pty = nt_type(c->nt, kws[i]);
1140+ if (!pty) continue;
1141+ const char *pname = nt_str(c->nt, kws[i], "name");
1142+ int dv = !strcmp(pty, "OptionalKeywordParameterNode") ? nt_ref(c->nt, kws[i], "value") : -1;
1143+ if (pname) scope_add_param(s, pname, dv);
1144+ }
11331145 /* `&block` parameter: tracked for yield-style inlining, no typed slot.
11341146 Anonymous `&` has no name; record "" so forwarding still works. */
11351147 int bp = nt_ref(c->nt, pn, "block");
@@ -1785,21 +1797,45 @@
17851797 int argc = 0;
17861798 const int *argv = NULL;
17871799 if (args >= 0) argv = nt_arr(nt, args, "arguments", &argc);
1788- int n = argc < m->nparams ? argc : m->nparams;
17891800 int changed = 0;
1801+ /* Separate positional args from the trailing keyword-hash arg (if any). */
1802+ int kwh = -1;
1803+ int pos_argc = argc;
1804+ if (argc > 0 && nt_type(nt, argv[argc - 1]) &&
1805+ !strcmp(nt_type(nt, argv[argc - 1]), "KeywordHashNode")) {
1806+ kwh = argv[argc - 1];
1807+ pos_argc = argc - 1;
1808+ }
1809+ int n = pos_argc < m->nparams ? pos_argc : m->nparams;
17901810 for (int k = 0; k < n; k++) {
17911811 TyKind at = infer_type(c, argv[k]);
17921812 LocalVar *p = scope_local(m, m->pnames[k]);
17931813 if (!p) continue;
17941814 TyKind merged = ty_unify(p->type, at);
17951815 if (merged != p->type) { p->type = merged; changed = 1; }
1796- /* a proc passed as an argument carries its body return type to the param,
1797- so `f.call(...)` inside the method knows its result type */
17981816 if (merged == TY_PROC) {
17991817 TyKind pr = proc_ret_of(c, argv[k]);
18001818 if (pr != TY_UNKNOWN && p->proc_ret != (int)pr) { p->proc_ret = (int)pr; changed = 1; }
18011819 }
18021820 }
1821+ /* Keyword arguments: match KeywordHashNode elements to named params. */
1822+ if (kwh >= 0) {
1823+ int en = 0;
1824+ const int *elems = nt_arr(nt, kwh, "elements", &en);
1825+ for (int e = 0; e < en; e++) {
1826+ int key = nt_ref(nt, elems[e], "key");
1827+ int val = nt_ref(nt, elems[e], "value");
1828+ if (key < 0 || val < 0) continue;
1829+ const char *kty = nt_type(nt, key);
1830+ const char *kname = (kty && !strcmp(kty, "SymbolNode")) ? nt_str(nt, key, "value") : NULL;
1831+ if (!kname) continue;
1832+ LocalVar *p = scope_local(m, kname);
1833+ if (!p) continue;
1834+ TyKind at = infer_type(c, val);
1835+ TyKind merged = ty_unify(p->type, at);
1836+ if (merged != p->type) { p->type = merged; changed = 1; }
1837+ }
1838+ }
18031839 return changed;
18041840 }
18051841
@@ -1879,7 +1915,12 @@
18791915 int mi = comp_method_index(c, name);
18801916 if (mi < 0) {
18811917 Scope *self = comp_scope_of(c, id);
1882- if (self->class_id >= 0) mi = comp_method_in_chain(c, self->class_id, name, NULL);
1918+ if (self->class_id >= 0) {
1919+ mi = comp_method_in_chain(c, self->class_id, name, NULL);
1920+ /* inside a class method: also check sibling class methods */
1921+ if (mi < 0 && self->is_cmethod)
1922+ mi = comp_cmethod_in_chain(c, self->class_id, name, NULL);
1923+ }
18831924 }
18841925 if (mi < 0) mi = comp_included_method_index(c, name);
18851926 changed |= bind_call_params(c, id, mi);