Skip to content

Commit b974adf

Browse files
fix: grow the ts_lsp AST walk stacks so wide files keep every declaration
Three walks in internal/cbm/lsp/ts_lsp.c held at most 256 AST nodes in a fixed array and stopped pushing when it filled. Nothing reported the stop, so the passes answered as if they had seen the whole file. In rebuild_signatures_from_ast and convert_signature_type_params the seeding loop alone can fill all 256 slots, because it pushes every top-level child of the file. A file with more than 256 top-level declarations therefore skipped every declaration past that point: each one kept whatever signature the first extraction pass had guessed, and a generic function past the cap kept NAMED instead of TYPE_PARAM, so calls through it stopped resolving. In infer_return_type_from_body the same cap made a wide function body answer "unknown return type" for a return the walk had never reached. The repo already carries the module that solves this: internal/cbm/extract_node_stack.h, written for GitHub issue DeusData#199 to replace fixed TSNode stack[] arrays that silently drop subtrees. Nine extract files use it; ts_lsp.c was never converted. This converts it. The header gains ts_nstack_init_arena, which takes an arena directly, because the LSP passes hold a TSLSPContext rather than a CBMExtractCtx. ts_nstack_init keeps its context argument, which is what makes handing over the wrong arena a type error, and now delegates to the new function. Traversal order changes from last-child-first to first-child-first, because ts_nstack_push_children reverses on push. That also drops an O(N^2) ts_node_child(n, i) loop for one cursor pass. For infer_return_type_from_body the new order matches the comment the function already carried: it finds the first return, where the old code found the last. New test tslsp_stress_declarations_past_stack_cap makes the same claim as tslsp_generic_identity_inference, 300 declarations deeper in the file. Without this change it fails with "MISSING resolved call: caller~.go -> callee~use"; with it the ts_lsp suite is 304 passed, 0 failed. Signed-off-by: Joshua Richter <jrichter5781@gmail.com>
1 parent aa44c28 commit b974adf

3 files changed

Lines changed: 79 additions & 45 deletions

File tree

‎internal/cbm/extract_node_stack.h‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,26 @@ typedef struct {
3434
CBMArena *scratch;
3535
} TSNodeStack;
3636

37+
/* Initialize a stack against an arena directly. The LSP passes hold a
38+
* TSLSPContext, not a CBMExtractCtx, and every allocation in those files
39+
* already comes from that one arena — see collect_children in ts_lsp.c. An
40+
* extraction caller should still use ts_nstack_init below, whose context
41+
* argument is what makes handing over the wrong arena a type error. */
42+
static inline void ts_nstack_init_arena(TSNodeStack *s, CBMArena *arena, int initial_cap) {
43+
s->scratch = arena;
44+
s->items = (TSNode *)cbm_arena_alloc(arena, (size_t)initial_cap * sizeof(TSNode));
45+
s->count = 0;
46+
s->cap = s->items ? initial_cap : 0;
47+
}
48+
3749
/* Initialize a stack with the given initial capacity, allocated from the
3850
* context's traversal scratch. Taking the context rather than an arena is
3951
* deliberate: it makes handing over ctx->arena, or a local alias of it, a type
4052
* error rather than a retention bug nobody notices. A context built without a
4153
* scratch falls back to ctx->arena, which is the behaviour that shipped before
4254
* #1997, so no caller ever gets a NULL arena and silently loses nodes. */
4355
static inline void ts_nstack_init(TSNodeStack *s, const CBMExtractCtx *ctx, int initial_cap) {
44-
CBMArena *arena = ctx->scratch ? ctx->scratch : ctx->arena;
45-
s->scratch = arena;
46-
s->items = (TSNode *)cbm_arena_alloc(arena, (size_t)initial_cap * sizeof(TSNode));
47-
s->count = 0;
48-
s->cap = s->items ? initial_cap : 0;
56+
ts_nstack_init_arena(s, ctx->scratch ? ctx->scratch : ctx->arena, initial_cap);
4957
}
5058

5159
/* Push a node onto the stack, growing 2x if needed. */

‎internal/cbm/lsp/ts_lsp.c‎

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828

2929
#include "ts_lsp.h"
30+
#include "extract_node_stack.h"
3031
#include <stdatomic.h>
3132
#include <stdio.h>
3233
#include <stdlib.h>
@@ -4634,13 +4635,15 @@ static const CBMType *infer_return_type_from_body(TSLSPContext *ctx, TSNode body
46344635
if (ts_node_is_null(body))
46354636
return cbm_type_unknown();
46364637

4637-
// Iterative DFS using a small fixed-size stack to avoid C-stack blowup on big bodies.
4638-
enum { STACK_CAP = 256 };
4639-
TSNode stack[STACK_CAP];
4640-
int top = 0;
4641-
stack[top++] = body;
4642-
while (top > 0) {
4643-
TSNode n = stack[--top];
4638+
// Iterative DFS to avoid C-stack blowup on big bodies. The stack grows, so a
4639+
// body wider than any fixed cap is still walked in full: a fixed 256 slots
4640+
// used to stop pushing without a word, and this function then answered
4641+
// "unknown return type" for a body whose return it had never reached.
4642+
TSNodeStack stack;
4643+
ts_nstack_init_arena(&stack, ctx->arena, 256);
4644+
ts_nstack_push(&stack, body);
4645+
while (stack.count > 0) {
4646+
TSNode n = ts_nstack_pop(&stack);
46444647
if (ts_node_is_null(n))
46454648
continue;
46464649
const char *k = ts_node_type(n);
@@ -4662,10 +4665,7 @@ static const CBMType *infer_return_type_from_body(TSLSPContext *ctx, TSNode body
46624665
strcmp(k, "arrow_function") == 0 || strcmp(k, "method_definition") == 0)
46634666
continue;
46644667

4665-
uint32_t cnt = ts_node_child_count(n);
4666-
for (uint32_t i = 0; i < cnt && top < STACK_CAP; i++) {
4667-
stack[top++] = ts_node_child(n, i);
4668-
}
4668+
ts_nstack_push_children(&stack, n);
46694669
}
46704670
return cbm_type_unknown();
46714671
}
@@ -4681,31 +4681,29 @@ static void rebuild_signatures_from_ast(TSLSPContext *ctx, TSNode root, CBMTypeR
46814681
if (ts_node_is_null(root) || !reg || !ctx->module_qn)
46824682
return;
46834683

4684-
enum { STACK_CAP = 256 };
4685-
TSNode stack[STACK_CAP];
4686-
int top = 0;
4687-
uint32_t nc = ts_node_child_count(root);
4688-
for (uint32_t i = 0; i < nc && top < STACK_CAP; i++)
4689-
stack[top++] = ts_node_child(root, i);
4684+
/* The stack grows. A fixed 256 slots used to be filled by the seeding loop
4685+
* alone on a file with that many top-level children, and every declaration
4686+
* past it was then skipped without a word — it kept whatever signature
4687+
* extract_defs had guessed. */
4688+
TSNodeStack stack;
4689+
ts_nstack_init_arena(&stack, ctx->arena, 256);
4690+
ts_nstack_push_children(&stack, root);
46904691

4691-
while (top > 0) {
4692-
TSNode n = stack[--top];
4692+
while (stack.count > 0) {
4693+
TSNode n = ts_nstack_pop(&stack);
46934694
if (ts_node_is_null(n))
46944695
continue;
46954696
const char *k = ts_node_type(n);
46964697

46974698
// Recurse into export_statement and class bodies.
46984699
if (strcmp(k, "export_statement") == 0 || strcmp(k, "class_body") == 0) {
4699-
uint32_t cnt = ts_node_child_count(n);
4700-
for (uint32_t i = 0; i < cnt && top < STACK_CAP; i++) {
4701-
stack[top++] = ts_node_child(n, i);
4702-
}
4700+
ts_nstack_push_children(&stack, n);
47034701
continue;
47044702
}
47054703
if (strcmp(k, "class_declaration") == 0) {
47064704
TSNode body = ts_node_child_by_field_name(n, "body", TS_LSP_FIELD_LEN("body"));
4707-
if (!ts_node_is_null(body) && top < STACK_CAP)
4708-
stack[top++] = body;
4705+
if (!ts_node_is_null(body))
4706+
ts_nstack_push(&stack, body);
47094707
continue;
47104708
}
47114709

@@ -4845,31 +4843,29 @@ static void convert_signature_type_params(TSLSPContext *ctx, TSNode root, CBMTyp
48454843
return;
48464844

48474845
// Walk: function_declaration, class_declaration { method_definition }, plus exported.
4848-
enum { STACK_CAP = 256 };
4849-
TSNode stack[STACK_CAP];
4850-
int top = 0;
4851-
uint32_t nc = ts_node_child_count(root);
4852-
for (uint32_t i = 0; i < nc && top < STACK_CAP; i++)
4853-
stack[top++] = ts_node_child(root, i);
4854-
4855-
while (top > 0) {
4856-
TSNode n = stack[--top];
4846+
/* The stack grows. A fixed 256 slots used to be filled by the seeding loop
4847+
* alone on a file with that many top-level children, and every declaration
4848+
* past it was then skipped without a word — it kept whatever signature
4849+
* extract_defs had guessed. */
4850+
TSNodeStack stack;
4851+
ts_nstack_init_arena(&stack, ctx->arena, 256);
4852+
ts_nstack_push_children(&stack, root);
4853+
4854+
while (stack.count > 0) {
4855+
TSNode n = ts_nstack_pop(&stack);
48574856
if (ts_node_is_null(n))
48584857
continue;
48594858
const char *k = ts_node_type(n);
48604859

48614860
// Recurse into export_statement and class_body.
48624861
if (strcmp(k, "export_statement") == 0 || strcmp(k, "class_body") == 0) {
4863-
uint32_t cnt = ts_node_child_count(n);
4864-
for (uint32_t i = 0; i < cnt && top < STACK_CAP; i++) {
4865-
stack[top++] = ts_node_child(n, i);
4866-
}
4862+
ts_nstack_push_children(&stack, n);
48674863
continue;
48684864
}
48694865
if (strcmp(k, "class_declaration") == 0) {
48704866
TSNode body = ts_node_child_by_field_name(n, "body", TS_LSP_FIELD_LEN("body"));
4871-
if (!ts_node_is_null(body) && top < STACK_CAP)
4872-
stack[top++] = body;
4867+
if (!ts_node_is_null(body))
4868+
ts_nstack_push(&stack, body);
48734869
continue;
48744870
}
48754871

‎tests/test_ts_lsp.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,6 +2817,35 @@ TEST(tslsp_stress_many_classes) {
28172817
PASS();
28182818
}
28192819

2820+
TEST(tslsp_stress_declarations_past_stack_cap) {
2821+
/* 300 padding functions, then the generic pair that needs the AST walk.
2822+
* The walk used to hold 256 nodes at most, so every top-level declaration
2823+
* after the 256th was dropped without a word and kept whatever signature
2824+
* the first extraction pass had guessed. */
2825+
enum { BUF_CAP = 64 * 1024 };
2826+
char *buf = (char *)malloc(BUF_CAP);
2827+
if (!buf)
2828+
PASS();
2829+
char *p = buf;
2830+
char *end = buf + BUF_CAP;
2831+
for (int i = 0; i < 300; i++) {
2832+
p += snprintf(p, (size_t)(end - p), "function pad%d(): void {}\n", i);
2833+
}
2834+
p += snprintf(p, (size_t)(end - p), "function identity<T>(x: T): T { return x; }\n");
2835+
p += snprintf(p, (size_t)(end - p), "class Box { use(): void {} }\n");
2836+
p += snprintf(p, (size_t)(end - p),
2837+
"function go() { const b = identity(new Box()); b.use(); }\n");
2838+
*p = '\0';
2839+
2840+
CBMFileResult *r = extract_ts(buf);
2841+
free(buf);
2842+
ASSERT_NOT_NULL(r);
2843+
/* Same claim as tslsp_generic_identity_inference, 300 declarations deeper. */
2844+
ASSERT_GTE(require_resolved(r, ".go", "use"), 0);
2845+
cbm_free_result(r);
2846+
PASS();
2847+
}
2848+
28202849
TEST(tslsp_stress_deep_inheritance) {
28212850
/* Chain of 30 classes via extends, leaf method on root. */
28222851
char buf[16 * 1024];
@@ -4565,6 +4594,7 @@ SUITE(ts_lsp) {
45654594

45664595
/* Stress tests */
45674596
RUN_TEST(tslsp_stress_many_classes);
4597+
RUN_TEST(tslsp_stress_declarations_past_stack_cap);
45684598
RUN_TEST(tslsp_stress_deep_inheritance);
45694599
RUN_TEST(tslsp_stress_long_method_chain);
45704600
RUN_TEST(tslsp_stress_megafile_mixed);

0 commit comments

Comments
 (0)