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 (was meth render nil string).
  • meth required_kw_only bool int,string for (Integer count, name: String) -> bool (was meth required_kw_only bool int).
  • Positional-only signatures unchanged: string,int.
  • *args / **opts still trigger the early-return drop.
  • Full make test: 538 pass, 0 fail, 0 error.

Details

Commit
ec1622a9f193
Parent
b1604e893600

spinel_rbs_extract.cmodifiedView file

@@ -388,13 +388,26 @@
388388 }
389389 rbs_types_function_t *fn = (rbs_types_function_t *) mt->type;
390390
391- /* Out-of-subset shapes: optional / rest / keyword params. Skip
392- * the whole signature rather than emit a partial one. */
393- if ((fn->optional_positionals != NULL && fn->optional_positionals->length > 0)
391+ /* Out-of-subset shapes: rest-positional / rest-keyword / optional
392+ * or trailing positionals. These break the fixed-arity contract
393+ * the seed format expects (one ptype slot per Ruby def param).
394+ * Required + optional keywords ARE handled — we walk them in
395+ * insertion order (head/next linked list; rbs_hash_set appends
396+ * to tail, matching RBS source order) and append the value types
397+ * to the ptype list after the required positionals. The arity
398+ * lines up with the Ruby def order spinel's collect_all sees.
399+ *
400+ * NOTE: rbs_hash_t.length is not incremented by rbs_hash_set
401+ * (upstream rbs/ast.c#rbs_hash_set inserts into head/tail without
402+ * updating length). Detect emptiness via `head == NULL`, not via
403+ * `length`. Same applies in the walking code below.
404+ *
405+ * Positional rbs_node_list_t.length IS maintained by rbs_node_list_
406+ * append — so the optional/trailing positional checks below use
407+ * `head == NULL` as the safer cross-cutting "is empty" predicate. */
408+ if ((fn->optional_positionals != NULL && fn->optional_positionals->head != NULL)
394409 || fn->rest_positionals != NULL
395- || (fn->trailing_positionals != NULL && fn->trailing_positionals->length > 0)
396- || (fn->required_keywords != NULL && fn->required_keywords->length > 0)
397- || (fn->optional_keywords != NULL && fn->optional_keywords->length > 0)
410+ || (fn->trailing_positionals != NULL && fn->trailing_positionals->head != NULL)
398411 || fn->rest_keywords != NULL) {
399412 return;
400413 }
@@ -421,6 +434,51 @@
421434 cur = cur->next;
422435 }
423436 }
437+
438+ /* Required keywords, then optional keywords. Both are stored as
439+ * rbs_hash mapping name-Symbol → function_param. The hash is
440+ * insertion-ordered (head/tail linked list); RBS's parser inserts
441+ * kwargs in source order. We walk in that order, mapping each
442+ * param's value type. The arity adds up to (positionals +
443+ * required_kwargs + optional_kwargs), matching Ruby's def in
444+ * source-order — same shape collect_all sees. Optionality is
445+ * an arity-affecting source property and doesn't change the
446+ * type slot the seed targets. */
447+ if (fn->required_keywords != NULL) {
448+ rbs_hash_node_t *cur = fn->required_keywords->head;
449+ while (cur != NULL) {
450+ if (cur->value == NULL || cur->value->type != RBS_TYPES_FUNCTION_PARAM) {
451+ sbuf_free(&ptypes); return;
452+ }
453+ rbs_types_function_param_t *pp = (rbs_types_function_param_t *) cur->value;
454+ sbuf_t t;
455+ sbuf_init(&t);
456+ if (!map_type(p, pp->type, enclosing_scope, &t)) { sbuf_free(&t); sbuf_free(&ptypes); return; }
457+ if (!first_p) sbuf_append_cstr(&ptypes, ",");
458+ sbuf_append(&ptypes, t.buf, t.len);
459+ first_p = false;
460+ sbuf_free(&t);
461+ cur = cur->next;
462+ }
463+ }
464+ if (fn->optional_keywords != NULL) {
465+ rbs_hash_node_t *cur = fn->optional_keywords->head;
466+ while (cur != NULL) {
467+ if (cur->value == NULL || cur->value->type != RBS_TYPES_FUNCTION_PARAM) {
468+ sbuf_free(&ptypes); return;
469+ }
470+ rbs_types_function_param_t *pp = (rbs_types_function_param_t *) cur->value;
471+ sbuf_t t;
472+ sbuf_init(&t);
473+ if (!map_type(p, pp->type, enclosing_scope, &t)) { sbuf_free(&t); sbuf_free(&ptypes); return; }
474+ if (!first_p) sbuf_append_cstr(&ptypes, ",");
475+ sbuf_append(&ptypes, t.buf, t.len);
476+ first_p = false;
477+ sbuf_free(&t);
478+ cur = cur->next;
479+ }
480+ }
481+
424482 if (ptypes.len == 0) sbuf_append_cstr(&ptypes, "-");
425483
426484 sbuf_t ret;