Skip to content

Commit 7d92e74

Browse files
committed
Merge branch 'gq-l5/stack-a-1907' into gq-l5/stack-a-1913
Brings in the base branch with main 658639e merged (DeusData#2685, Rust generic-impl call scope), so this branch merges cleanly into main again. No conflicts. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2 parents aa4775f + 730a874 commit 7d92e74

9 files changed

Lines changed: 547 additions & 102 deletions

File tree

‎internal/cbm/extract_unified.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,20 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const WalkS
11991199
if (!name || !name[0]) {
12001200
return NULL;
12011201
}
1202+
/* Rust: the impl's type text carries its generic arguments (`impl<T> Gen<T>`,
1203+
* `impl<'a> Parser<'a>`), which the def side strips before naming the type
1204+
* (extract_defs.c extract_rust_impl: `Buffer<T>` -> `Buffer`). Strip them the
1205+
* same way, or every method of a generic impl gets a scope QN no node carries
1206+
* and all its calls fall back to the File node. */
1207+
if (ctx->language == CBM_LANG_RUST && strcmp(ts_node_type(node), "impl_item") == 0) {
1208+
char *lt = strchr(name, '<');
1209+
if (lt == name) {
1210+
return NULL; /* `<T as Tr>`-style self type: no name to scope by */
1211+
}
1212+
if (lt) {
1213+
*lt = '\0';
1214+
}
1215+
}
12021216

12031217
/* Nested class: prefix with the enclosing class QN (Outer.Inner) so this
12041218
* scope QN matches the def-side class QN (extract_defs.c compute_class_qn /

‎internal/cbm/lsp/rust_lsp.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,22 @@ static void rust_strip_turbofish(char *path) {
306306
*write = '\0';
307307
}
308308

309+
/* In place: `Gen<T>` -> `Gen`, `foo::Bar<'a, T>` -> `foo::Bar` (#763). An
310+
* impl's self type keeps its generic arguments in the source text, but every
311+
* def-side QN (a method's receiver, the extractor's call scope) strips them.
312+
* Unstripped, the LSP's caller QN, `self`/`Self` dispatch and the return-type
313+
* harvest never matched a generic impl's methods. Text starting with `<` (a
314+
* qualified path) is left alone. Safe on NULL. */
315+
static void rust_strip_impl_type_args(char *type_text) {
316+
if (!type_text) {
317+
return;
318+
}
319+
char *lt = strchr(type_text, '<');
320+
if (lt && lt > type_text) {
321+
*lt = '\0';
322+
}
323+
}
324+
309325
/* Look up a `use` alias and return its fully-qualified module path,
310326
* or NULL if absent. The returned pointer aliases into the use map. */
311327
static const char *rust_resolve_use(RustLSPContext *ctx, const char *local_name) {
@@ -5274,6 +5290,7 @@ static void rust_process_impl(RustLSPContext *ctx, TSNode impl_node) {
52745290
char *type_text = rust_node_text(ctx, type_node);
52755291
if (!type_text)
52765292
return;
5293+
rust_strip_impl_type_args(type_text);
52775294

52785295
/* Detect blanket impl: `impl<T: Trait> ForeignTrait for T { ... }`
52795296
* where type_text is a name that appears in the impl's type
@@ -6028,6 +6045,7 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
60286045
if (ts_node_is_null(type_node) || ts_node_is_null(body))
60296046
continue;
60306047
char *type_name = cbm_node_text(arena, type_node, source);
6048+
rust_strip_impl_type_args(type_name);
60316049
if (!type_name || !type_name[0])
60326050
continue;
60336051
const char *type_qn = cbm_arena_sprintf(arena, "%s.%s", module_qn, type_name);

‎src/pipeline/pass_definitions.c‎

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
309309
}
310310
size_t pos = (size_t)n;
311311
append_json_string(buf, bufsize, &pos, "docstring", def->docstring);
312-
/* Right after the docstring: the buffer is sized for exactly these three
313-
* uncapped fields (pd_props_buf), so none can be squeezed out. */
312+
/* The buffer is sized for every field below (pd_props_buf), so none can be
313+
* squeezed out. */
314314
append_json_raw(buf, bufsize, &pos, "variants", def->variants);
315315
append_json_string(buf, bufsize, &pos, "variant_key", def->variant_key);
316316
append_json_string(buf, bufsize, &pos, "signature", def->signature);
@@ -353,24 +353,71 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
353353
}
354354
}
355355

356-
/* A def's properties buffer: CBM_SZ_2K for every other field plus the whole
357-
* serialized docstring, variants and variant_key fields, which have no length
358-
* cap (a field that does not fit is dropped whole). Returns `stack` for a def
359-
* with none of them, or when the larger buffer cannot be allocated. Twin of
360-
* pass_parallel.c -- keep both in sync. */
361-
static char *pd_props_buf(const CBMDefinition *def, char *stack, size_t *size) {
362-
size_t need = *size;
363-
if (def->docstring && def->docstring[0]) {
364-
need += strlen("docstring") + def_json_escaped_len(def->docstring) + PD_JSON_FIELD_OVERHEAD;
356+
/* Upper bound of build_def_props' fixed prefix: the Function/Method metrics
357+
* literal (about 330 B) with nine ints and seven true/false/test_role strings at
358+
* their widest stays under 600 B. */
359+
enum { PD_PROPS_PREFIX_MAX = CBM_SZ_1K };
360+
361+
/* Serialized size of one optional field as append_json_string emits it
362+
* (0 when the field is absent). */
363+
static size_t pd_str_field_len(const char *key, const char *val) {
364+
return val && val[0] ? strlen(key) + def_json_escaped_len(val) + PD_JSON_FIELD_OVERHEAD : 0;
365+
}
366+
367+
/* Serialized size of a string array as append_json_str_array emits it. */
368+
static size_t pd_arr_field_len(const char *key, const char **arr) {
369+
if (!arr || !arr[0]) {
370+
return 0;
371+
}
372+
size_t n = strlen(key) + PD_JSON_FIELD_OVERHEAD;
373+
for (int i = 0; arr[i]; i++) {
374+
n += def_json_escaped_len(arr[i]) + PD_ESC_SPACE + (i > 0 ? SKIP_ONE : 0);
365375
}
376+
return n;
377+
}
378+
379+
/* A def's properties buffer, sized so EVERY field build_def_props appends fits:
380+
* the fixed metrics prefix (PD_PROPS_PREFIX_MAX) plus each optional field's
381+
* whole serialized form. A field that does not fit is dropped whole, so a buffer
382+
* sized for only some of them let a long signature, decorator list or parameter
383+
* list squeeze out the MinHash fingerprint ("fp"), structural profile ("sp") and
384+
* body tokens ("bt") behind them: no SIMILAR_TO edge and fewer
385+
* SEMANTICALLY_RELATED edges for exactly the widest definitions (jellyfin's
386+
* ImageController actions, 2026-10-18). fp/sp/bt are appended behind their own
387+
* room checks, so both the check's reserve and the field are counted. Returns
388+
* `stack` when everything fits it, or when the larger buffer cannot be
389+
* allocated. Twin of pass_parallel.c -- keep both in sync. */
390+
static char *pd_props_buf(const CBMDefinition *def, char *stack, size_t *size) {
391+
size_t need = PD_PROPS_PREFIX_MAX + pd_str_field_len("docstring", def->docstring);
366392
if (def->variants && def->variants[0]) {
367393
need += strlen("variants") + strlen(def->variants) + PD_JSON_FIELD_OVERHEAD;
368394
}
369-
if (def->variant_key && def->variant_key[0]) {
370-
need +=
371-
strlen("variant_key") + def_json_escaped_len(def->variant_key) + PD_JSON_FIELD_OVERHEAD;
395+
need += pd_str_field_len("variant_key", def->variant_key) +
396+
pd_str_field_len("signature", def->signature) +
397+
pd_str_field_len("return_type", def->return_type) +
398+
pd_str_field_len("parent_class", def->parent_class) +
399+
pd_arr_field_len("decorators", def->decorators) +
400+
pd_arr_field_len("base_classes", def->base_classes) +
401+
pd_arr_field_len("param_names", def->param_names) +
402+
pd_arr_field_len("param_types", def->param_types) +
403+
pd_str_field_len("route_path", def->route_path) +
404+
pd_str_field_len("route_method", def->route_method) +
405+
pd_str_field_len("http_client", def->http_client) +
406+
pd_str_field_len("http_base_url", def->http_base_url);
407+
for (const char **kv = def->extra_props; kv && kv[0] && kv[SKIP_ONE]; kv += PAIR_LEN) {
408+
need += pd_str_field_len(kv[0], kv[SKIP_ONE]);
409+
}
410+
if (def->fingerprint && def->fingerprint_k > 0) {
411+
need += CBM_MINHASH_HEX_LEN + CBM_MINHASH_JSON_OVERHEAD + SKIP_ONE;
412+
}
413+
if (def->structural_profile) {
414+
need += CBM_AST_PROFILE_BUF + pd_str_field_len("sp", def->structural_profile) + SKIP_ONE;
415+
}
416+
if (def->body_tokens) {
417+
need += CBM_SZ_512 + pd_str_field_len("bt", def->body_tokens) + SKIP_ONE;
372418
}
373-
if (need == *size) {
419+
need += PD_ESC_SPACE; /* the closing '}' + NUL */
420+
if (need <= *size) {
374421
return stack;
375422
}
376423
char *buf = cbm_alloc(CBM_MEM_CLASS_GBUF_STRING, need);

‎src/pipeline/pass_parallel.c‎

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
539539
}
540540
size_t pos = (size_t)n;
541541
append_json_string(buf, bufsize, &pos, "docstring", def->docstring);
542-
/* Right after the docstring: the buffer is sized for exactly these three
543-
* uncapped fields (pp_props_buf), so none can be squeezed out. */
542+
/* The buffer is sized for every field below (pp_props_buf), so none can be
543+
* squeezed out. */
544544
append_json_raw(buf, bufsize, &pos, "variants", def->variants);
545545
append_json_string(buf, bufsize, &pos, "variant_key", def->variant_key);
546546
append_json_string(buf, bufsize, &pos, "signature", def->signature);
@@ -731,24 +731,71 @@ typedef struct {
731731
* to the response/logfile — this only throttles the stderr noise). */
732732
enum { PP_OVERSIZED_WARN_MAX = 32 };
733733

734-
/* A def's properties buffer: CBM_SZ_2K for every other field plus the whole
735-
* serialized docstring, variants and variant_key fields, which have no length
736-
* cap (a field that does not fit is dropped whole). Returns `stack` for a def
737-
* with none of them, or when the larger buffer cannot be allocated. Twin of
738-
* pass_definitions.c -- keep both in sync. */
739-
static char *pp_props_buf(const CBMDefinition *def, char *stack, size_t *size) {
740-
size_t need = *size;
741-
if (def->docstring && def->docstring[0]) {
742-
need += strlen("docstring") + pp_json_escaped_len(def->docstring) + PP_JSON_FIELD_OVERHEAD;
734+
/* Upper bound of build_def_props' fixed prefix: the Function/Method metrics
735+
* literal (about 330 B) with nine ints and seven true/false/test_role strings at
736+
* their widest stays under 600 B. */
737+
enum { PP_PROPS_PREFIX_MAX = CBM_SZ_1K };
738+
739+
/* Serialized size of one optional field as append_json_string emits it
740+
* (0 when the field is absent). */
741+
static size_t pp_str_field_len(const char *key, const char *val) {
742+
return val && val[0] ? strlen(key) + pp_json_escaped_len(val) + PP_JSON_FIELD_OVERHEAD : 0;
743+
}
744+
745+
/* Serialized size of a string array as append_json_str_array emits it. */
746+
static size_t pp_arr_field_len(const char *key, const char **arr) {
747+
if (!arr || !arr[0]) {
748+
return 0;
749+
}
750+
size_t n = strlen(key) + PP_JSON_FIELD_OVERHEAD;
751+
for (int i = 0; arr[i]; i++) {
752+
n += pp_json_escaped_len(arr[i]) + PP_ESC_SPACE + (i > 0 ? SKIP_ONE : 0);
743753
}
754+
return n;
755+
}
756+
757+
/* A def's properties buffer, sized so EVERY field build_def_props appends fits:
758+
* the fixed metrics prefix (PP_PROPS_PREFIX_MAX) plus each optional field's
759+
* whole serialized form. A field that does not fit is dropped whole, so a buffer
760+
* sized for only some of them let a long signature, decorator list or parameter
761+
* list squeeze out the MinHash fingerprint ("fp"), structural profile ("sp") and
762+
* body tokens ("bt") behind them: no SIMILAR_TO edge and fewer
763+
* SEMANTICALLY_RELATED edges for exactly the widest definitions (jellyfin's
764+
* ImageController actions, 2026-10-18). fp/sp/bt are appended behind their own
765+
* room checks, so both the check's reserve and the field are counted. Returns
766+
* `stack` when everything fits it, or when the larger buffer cannot be
767+
* allocated. Twin of pass_definitions.c -- keep both in sync. */
768+
static char *pp_props_buf(const CBMDefinition *def, char *stack, size_t *size) {
769+
size_t need = PP_PROPS_PREFIX_MAX + pp_str_field_len("docstring", def->docstring);
744770
if (def->variants && def->variants[0]) {
745771
need += strlen("variants") + strlen(def->variants) + PP_JSON_FIELD_OVERHEAD;
746772
}
747-
if (def->variant_key && def->variant_key[0]) {
748-
need +=
749-
strlen("variant_key") + pp_json_escaped_len(def->variant_key) + PP_JSON_FIELD_OVERHEAD;
773+
need += pp_str_field_len("variant_key", def->variant_key) +
774+
pp_str_field_len("signature", def->signature) +
775+
pp_str_field_len("return_type", def->return_type) +
776+
pp_str_field_len("parent_class", def->parent_class) +
777+
pp_arr_field_len("decorators", def->decorators) +
778+
pp_arr_field_len("base_classes", def->base_classes) +
779+
pp_arr_field_len("param_names", def->param_names) +
780+
pp_arr_field_len("param_types", def->param_types) +
781+
pp_str_field_len("route_path", def->route_path) +
782+
pp_str_field_len("route_method", def->route_method) +
783+
pp_str_field_len("http_client", def->http_client) +
784+
pp_str_field_len("http_base_url", def->http_base_url);
785+
for (const char **kv = def->extra_props; kv && kv[0] && kv[SKIP_ONE]; kv += PAIR_LEN) {
786+
need += pp_str_field_len(kv[0], kv[SKIP_ONE]);
787+
}
788+
if (def->fingerprint && def->fingerprint_k > 0) {
789+
need += CBM_MINHASH_HEX_LEN + CBM_MINHASH_JSON_OVERHEAD + SKIP_ONE;
790+
}
791+
if (def->structural_profile) {
792+
need += CBM_AST_PROFILE_BUF + pp_str_field_len("sp", def->structural_profile) + SKIP_ONE;
793+
}
794+
if (def->body_tokens) {
795+
need += CBM_SZ_512 + pp_str_field_len("bt", def->body_tokens) + SKIP_ONE;
750796
}
751-
if (need == *size) {
797+
need += PP_ESC_SPACE; /* the closing '}' + NUL */
798+
if (need <= *size) {
752799
return stack;
753800
}
754801
char *buf = cbm_alloc(CBM_MEM_CLASS_GBUF_STRING, need);

0 commit comments

Comments
 (0)