Commit ec1622a9
ec1622a9f193Merged
Sam Ruby <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
rbs_extract: emit kwarg types instead of dropping signatures
def render: (String body, ?status: Symbol, ?content_type: String?, ?location: String?) -> void previously extracted as
meth render nil string — just the positional type, kwargs silently
dropped. The seed apply (spinel_analyze.rb#apply_rbs_seeds) then
overwrote @cls_meth_ptypes[render] with ["string"], truncating
the slots collect_all had populated for the three kwargs. Those
slots defaulted to int, and callers passing Symbol/String? hit
type mismatches at the C boundary.
Surfaces in roundhouse's spinel-toolchain whenever --rbs sig is
passed against any RBS file declaring kwarg-bearing methods (the
framework runtime's RBS sidecars do this heavily — render,
redirect_to, head, form_with, etc.).
Fix: walk fn->required_keywords and fn->optional_keywords as a
linked list via head/next, mapping each param's value type, and
append to the ptype list after the required positionals. The
resulting arity matches what collect_all populates from the Ruby
def (positionals + kwargs in source order). Optionality (?name:)
is an arity-affecting source-syntax feature and doesn't change the
type slot the seed targets; we emit the value type either way.
Drop the early-return for required_keywords->length > 0 /
optional_keywords->length > 0. Also switch the optional/trailing-
positional drop checks from length > 0 to head != NULL.
NOTE: rbs_hash_t.length is not incremented by rbs_hash_set (upstream rbs ast.c#rbs_hash_set inserts into head/tail without updating length). The old length-based check thus always saw 0 and never fired — kwargs fell through to "emit positionals only," producing the truncated seed line that broke downstream. The new code uses head-based emptiness throughout to be robust regardless of upstream's length-maintenance contract.
Tested:
meth render nil string,symbol,string?,string?for the example above (wasmeth render nil string).meth required_kw_only bool int,stringfor(Integer count, name: String) -> bool(wasmeth required_kw_only bool int).- Positional-only signatures unchanged:
string,int. *args/**optsstill trigger the early-return drop.- Full
make test: 538 pass, 0 fail, 0 error.
Details
- Commit
- ec1622a9f193
- Parent
- b1604e893600
spinel_rbs_extract.cmodifiedView file
} rbs_types_function_t *fn = (rbs_types_function_t *) mt->type; /* Out-of-subset shapes: optional / rest / keyword params. Skip * the whole signature rather than emit a partial one. */ if ((fn->optional_positionals != NULL && fn->optional_positionals->length > 0) /* Out-of-subset shapes: rest-positional / rest-keyword / optional * or trailing positionals. These break the fixed-arity contract * the seed format expects (one ptype slot per Ruby def param). * Required + optional keywords ARE handled — we walk them in * insertion order (head/next linked list; rbs_hash_set appends * to tail, matching RBS source order) and append the value types * to the ptype list after the required positionals. The arity * lines up with the Ruby def order spinel's collect_all sees. * * NOTE: rbs_hash_t.length is not incremented by rbs_hash_set * (upstream rbs/ast.c#rbs_hash_set inserts into head/tail without * updating length). Detect emptiness via `head == NULL`, not via * `length`. Same applies in the walking code below. * * Positional rbs_node_list_t.length IS maintained by rbs_node_list_ * append — so the optional/trailing positional checks below use * `head == NULL` as the safer cross-cutting "is empty" predicate. */ if ((fn->optional_positionals != NULL && fn->optional_positionals->head != NULL) || fn->rest_positionals != NULL || (fn->trailing_positionals != NULL && fn->trailing_positionals->length > 0) || (fn->required_keywords != NULL && fn->required_keywords->length > 0) || (fn->optional_keywords != NULL && fn->optional_keywords->length > 0) || (fn->trailing_positionals != NULL && fn->trailing_positionals->head != NULL) || fn->rest_keywords != NULL) { return; } cur = cur->next; } } /* Required keywords, then optional keywords. Both are stored as * rbs_hash mapping name-Symbol → function_param. The hash is * insertion-ordered (head/tail linked list); RBS's parser inserts * kwargs in source order. We walk in that order, mapping each * param's value type. The arity adds up to (positionals + * required_kwargs + optional_kwargs), matching Ruby's def in * source-order — same shape collect_all sees. Optionality is * an arity-affecting source property and doesn't change the * type slot the seed targets. */ if (fn->required_keywords != NULL) { rbs_hash_node_t *cur = fn->required_keywords->head; while (cur != NULL) { if (cur->value == NULL || cur->value->type != RBS_TYPES_FUNCTION_PARAM) { sbuf_free(&ptypes); return; } rbs_types_function_param_t *pp = (rbs_types_function_param_t *) cur->value; sbuf_t t; sbuf_init(&t); if (!map_type(p, pp->type, enclosing_scope, &t)) { sbuf_free(&t); sbuf_free(&ptypes); return; } if (!first_p) sbuf_append_cstr(&ptypes, ","); sbuf_append(&ptypes, t.buf, t.len); first_p = false; sbuf_free(&t); cur = cur->next; } } if (fn->optional_keywords != NULL) { rbs_hash_node_t *cur = fn->optional_keywords->head; while (cur != NULL) { if (cur->value == NULL || cur->value->type != RBS_TYPES_FUNCTION_PARAM) { sbuf_free(&ptypes); return; } rbs_types_function_param_t *pp = (rbs_types_function_param_t *) cur->value; sbuf_t t; sbuf_init(&t); if (!map_type(p, pp->type, enclosing_scope, &t)) { sbuf_free(&t); sbuf_free(&ptypes); return; } if (!first_p) sbuf_append_cstr(&ptypes, ","); sbuf_append(&ptypes, t.buf, t.len); first_p = false; sbuf_free(&t); cur = cur->next; } } if (ptypes.len == 0) sbuf_append_cstr(&ptypes, "-"); sbuf_t ret;