Commit 707e0553

707e0553ba9eMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

Merge pull request #574 from rubys/rbs-extract-kwargs

rbs_extract: emit kwarg types instead of dropping signatures

Details

Commit
707e0553ba9e
Parent
506a8b5ed648
Merge parent
69aff436d390

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,44 @@
421434 cur = cur->next;
422435 }
423436 }
437+
438+ /* Required + optional keywords. Both are stored as rbs_hash
439+ * mapping name-Symbol → function_param. The hash is insertion-
440+ * ordered (head/tail linked list); RBS's parser inserts kwargs in
441+ * source order. We walk required then optional, 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 an
445+ * arity-affecting source property and doesn't change the type
446+ * slot the seed targets. */
447+ const rbs_hash_t *kw_hashes[] = { fn->required_keywords, fn->optional_keywords };
448+ for (size_t ki = 0; ki < sizeof(kw_hashes)/sizeof(kw_hashes[0]); ki++) {
449+ const rbs_hash_t *hash = kw_hashes[ki];
450+ if (hash == NULL) continue;
451+ rbs_hash_node_t *cur = hash->head;
452+ while (cur != NULL) {
453+ /* Defensive: an upstream rbs change adding a different
454+ * value-node type would land here. Skipping the whole
455+ * signature (rather than producing a partial seed) keeps
456+ * the analyzer consistent — apply_rbs_seeds overwrites a
457+ * method's ptype list wholesale, so a half-populated entry
458+ * is worse than no entry. Same policy as the positional
459+ * loop above and the existing out-of-subset early-return. */
460+ if (cur->value == NULL || cur->value->type != RBS_TYPES_FUNCTION_PARAM) {
461+ sbuf_free(&ptypes); return;
462+ }
463+ rbs_types_function_param_t *pp = (rbs_types_function_param_t *) cur->value;
464+ sbuf_t t;
465+ sbuf_init(&t);
466+ if (!map_type(p, pp->type, enclosing_scope, &t)) { sbuf_free(&t); sbuf_free(&ptypes); return; }
467+ if (!first_p) sbuf_append_cstr(&ptypes, ",");
468+ sbuf_append(&ptypes, t.buf, t.len);
469+ first_p = false;
470+ sbuf_free(&t);
471+ cur = cur->next;
472+ }
473+ }
474+
424475 if (ptypes.len == 0) sbuf_append_cstr(&ptypes, "-");
425476
426477 sbuf_t ret;