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
-
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. -
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.
-
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
if (iv >= 0) return c->classes[self->class_id].ivar_types[iv]; } int mi = comp_method_in_chain(c, self->class_id, name, NULL); if (mi < 0 && self->is_cmethod) mi = comp_cmethod_in_chain(c, self->class_id, name, NULL); if (mi >= 0) return method_call_ret(c, mi, id); } } int dv = nt_ref(c->nt, opts[i], "value"); if (pname) scope_add_param(s, pname, dv); } /* keyword parameters: `tag:` (required) or `tag: default` (optional) */ int kn = 0; const int *kws = nt_arr(c->nt, pn, "keywords", &kn); for (int i = 0; i < kn; i++) { const char *pty = nt_type(c->nt, kws[i]); if (!pty) continue; const char *pname = nt_str(c->nt, kws[i], "name"); int dv = !strcmp(pty, "OptionalKeywordParameterNode") ? nt_ref(c->nt, kws[i], "value") : -1; if (pname) scope_add_param(s, pname, dv); } /* `&block` parameter: tracked for yield-style inlining, no typed slot. Anonymous `&` has no name; record "" so forwarding still works. */ int bp = nt_ref(c->nt, pn, "block"); int argc = 0; const int *argv = NULL; if (args >= 0) argv = nt_arr(nt, args, "arguments", &argc); int n = argc < m->nparams ? argc : m->nparams; int changed = 0; /* Separate positional args from the trailing keyword-hash arg (if any). */ int kwh = -1; int pos_argc = argc; if (argc > 0 && nt_type(nt, argv[argc - 1]) && !strcmp(nt_type(nt, argv[argc - 1]), "KeywordHashNode")) { kwh = argv[argc - 1]; pos_argc = argc - 1; } int n = pos_argc < m->nparams ? pos_argc : m->nparams; for (int k = 0; k < n; k++) { TyKind at = infer_type(c, argv[k]); LocalVar *p = scope_local(m, m->pnames[k]); if (!p) continue; TyKind merged = ty_unify(p->type, at); if (merged != p->type) { p->type = merged; changed = 1; } /* a proc passed as an argument carries its body return type to the param, so `f.call(...)` inside the method knows its result type */ if (merged == TY_PROC) { TyKind pr = proc_ret_of(c, argv[k]); if (pr != TY_UNKNOWN && p->proc_ret != (int)pr) { p->proc_ret = (int)pr; changed = 1; } } } /* Keyword arguments: match KeywordHashNode elements to named params. */ if (kwh >= 0) { int en = 0; const int *elems = nt_arr(nt, kwh, "elements", &en); for (int e = 0; e < en; e++) { int key = nt_ref(nt, elems[e], "key"); int val = nt_ref(nt, elems[e], "value"); if (key < 0 || val < 0) continue; const char *kty = nt_type(nt, key); const char *kname = (kty && !strcmp(kty, "SymbolNode")) ? nt_str(nt, key, "value") : NULL; if (!kname) continue; LocalVar *p = scope_local(m, kname); if (!p) continue; TyKind at = infer_type(c, val); TyKind merged = ty_unify(p->type, at); if (merged != p->type) { p->type = merged; changed = 1; } } } return changed;} int mi = comp_method_index(c, name); if (mi < 0) { Scope *self = comp_scope_of(c, id); if (self->class_id >= 0) mi = comp_method_in_chain(c, self->class_id, name, NULL); if (self->class_id >= 0) { mi = comp_method_in_chain(c, self->class_id, name, NULL); /* inside a class method: also check sibling class methods */ if (mi < 0 && self->is_cmethod) mi = comp_cmethod_in_chain(c, self->class_id, name, NULL); } } if (mi < 0) mi = comp_included_method_index(c, name); changed |= bind_call_params(c, id, mi);src/codegen.cmodifiedView file
Loading diff…