Skip to content

Commit e8ccc33

Browse files
fix(lsp): grow the ts_lsp walk stacks with cbm_walk_stack_reserve
Follow-up to the review of DeusData#2091, on top of the contributor's change. The three ts_lsp.c walks (infer_return_type_from_body, rebuild_signatures_from_ast, convert_signature_type_params) now keep their first 256 slots on the C stack, as the old fixed arrays did, and grow on the heap through cbm_walk_stack_reserve / cbm_walk_stack_release (DeusData#2375) instead of cutting TSNodeStack storage from ctx->arena: - Scratch lifetime: ctx->arena in the per-file pass is the result arena, kept until the result cache is freed, and the return walk runs once per untyped top-level function. The heap chunk is now released when each walk ends. - No silent drop: when the stack cannot grow, cbm_walk_stack_reserve logs extract.walk_stack_alloc_failed and the walk stops. The return walk then answers "unknown"; the signature walks leave the declarations not yet reached as extract_defs registered them. - Traversal order: children are filled in cursor order (still one O(N) pass) and popped last-child first, exactly as main's walks did, so of several returns the later one is still tried first. tslsp_return_walk_order_prefers_later_return pins it; it fails under a first-child-first walk. The TSNodeStack arena initializer in extract_node_stack.h is no longer needed and is dropped, so that header is unchanged from main. The two stack-cap tests now fail instead of passing when their buffer allocation fails. Co-authored-by: Joshua Richter <jrichter5781@gmail.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 34d9454 commit e8ccc33

3 files changed

Lines changed: 117 additions & 57 deletions

File tree

‎internal/cbm/extract_node_stack.h‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,18 @@ 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-
4937
/* Initialize a stack with the given initial capacity, allocated from the
5038
* context's traversal scratch. Taking the context rather than an arena is
5139
* deliberate: it makes handing over ctx->arena, or a local alias of it, a type
5240
* error rather than a retention bug nobody notices. A context built without a
5341
* scratch falls back to ctx->arena, which is the behaviour that shipped before
5442
* #1997, so no caller ever gets a NULL arena and silently loses nodes. */
5543
static inline void ts_nstack_init(TSNodeStack *s, const CBMExtractCtx *ctx, int initial_cap) {
56-
ts_nstack_init_arena(s, ctx->scratch ? ctx->scratch : ctx->arena, 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;
5749
}
5850

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

‎internal/cbm/lsp/ts_lsp.c‎

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#include "ts_lsp.h"
3030
#include "../../../src/foundation/platform.h"
31-
#include "extract_node_stack.h"
31+
#include "../helpers.h"
3232
#include <stdatomic.h>
3333
#include <stdio.h>
3434
#include <stdlib.h>
@@ -4685,21 +4685,61 @@ static void register_file_defs(CBMArena *arena, CBMTypeRegistry *reg, CBMFileRes
46854685
}
46864686
}
46874687

4688-
// Find the first `return EXPR` inside a body and evaluate EXPR's type. Returns
4689-
// UNKNOWN if the body has no return or every return is unresolvable.
4688+
// AST walk stacks for the passes below. The first chunk lives on the C stack, the
4689+
// same 256 slots the old fixed arrays used; a wider pending set grows on the heap
4690+
// through cbm_walk_stack_reserve and is released when the walk ends, so nothing
4691+
// is cut from the per-file result arena. A fixed cap used to stop pushing without
4692+
// a word, which skipped every top-level declaration past the 256th and left the
4693+
// return walk short of returns it had never reached (#2388).
4694+
enum { TS_WALK_STACK_INLINE = 256 };
4695+
4696+
// Push every child of n in cursor order (one O(N) pass). The walk pops the stack,
4697+
// so children are visited last-child first: the order these walks have always
4698+
// used. False means the stack could not grow; cbm_walk_stack_reserve has logged it.
4699+
static bool ts_walk_push_children(TSNode **stack, int *cap, int *top, TSNode n,
4700+
const TSNode *inline_buf, const char *walker) {
4701+
uint32_t count = ts_node_child_count(n);
4702+
if (count == 0)
4703+
return true;
4704+
if (!cbm_walk_stack_reserve((void **)stack, cap, *top + (int)count, sizeof(TSNode), inline_buf,
4705+
walker))
4706+
return false;
4707+
TSTreeCursor cursor = ts_tree_cursor_new(n);
4708+
if (ts_tree_cursor_goto_first_child(&cursor)) {
4709+
uint32_t pushed = 0;
4710+
do {
4711+
(*stack)[(*top)++] = ts_tree_cursor_current_node(&cursor);
4712+
pushed++;
4713+
} while (pushed < count && ts_tree_cursor_goto_next_sibling(&cursor));
4714+
}
4715+
ts_tree_cursor_delete(&cursor);
4716+
return true;
4717+
}
4718+
4719+
static bool ts_walk_push(TSNode **stack, int *cap, int *top, TSNode n, const TSNode *inline_buf,
4720+
const char *walker) {
4721+
if (!cbm_walk_stack_reserve((void **)stack, cap, *top + 1, sizeof(TSNode), inline_buf, walker))
4722+
return false;
4723+
(*stack)[(*top)++] = n;
4724+
return true;
4725+
}
4726+
4727+
// Find a `return EXPR` inside a body and evaluate EXPR's type. Children are visited
4728+
// last-child first, so of several returns the one latest in the body is tried
4729+
// first; the first one that resolves wins. Returns UNKNOWN if the body has no
4730+
// return, every return is unresolvable, or the walk stack could not grow.
46904731
static const CBMType *infer_return_type_from_body(TSLSPContext *ctx, TSNode body) {
46914732
if (ts_node_is_null(body))
46924733
return cbm_type_unknown();
46934734

4694-
// Iterative DFS to avoid C-stack blowup on big bodies. The stack grows, so a
4695-
// body wider than any fixed cap is still walked in full: a fixed 256 slots
4696-
// used to stop pushing without a word, and this function then answered
4697-
// "unknown return type" for a body whose return it had never reached.
4698-
TSNodeStack stack;
4699-
ts_nstack_init_arena(&stack, ctx->arena, 256);
4700-
ts_nstack_push(&stack, body);
4701-
while (stack.count > 0) {
4702-
TSNode n = ts_nstack_pop(&stack);
4735+
TSNode inline_stack[TS_WALK_STACK_INLINE];
4736+
TSNode *stack = inline_stack;
4737+
int cap = TS_WALK_STACK_INLINE;
4738+
int top = 0;
4739+
const CBMType *found = cbm_type_unknown();
4740+
stack[top++] = body;
4741+
while (top > 0) {
4742+
TSNode n = stack[--top];
47034743
if (ts_node_is_null(n))
47044744
continue;
47054745
const char *k = ts_node_type(n);
@@ -4712,18 +4752,22 @@ static const CBMType *infer_return_type_from_body(TSLSPContext *ctx, TSNode body
47124752
if (ts_node_is_null(expr))
47134753
continue;
47144754
const CBMType *t = ts_eval_expr_type(ctx, expr);
4715-
if (t && !cbm_type_is_unknown(t))
4716-
return t;
4755+
if (t && !cbm_type_is_unknown(t)) {
4756+
found = t;
4757+
break;
4758+
}
47174759
continue;
47184760
}
47194761
// Don't recurse into nested function bodies — their returns belong to them.
47204762
if (strcmp(k, "function_declaration") == 0 || strcmp(k, "function_expression") == 0 ||
47214763
strcmp(k, "arrow_function") == 0 || strcmp(k, "method_definition") == 0)
47224764
continue;
47234765

4724-
ts_nstack_push_children(&stack, n);
4766+
if (!ts_walk_push_children(&stack, &cap, &top, n, inline_stack, "ts_lsp_return_walk"))
4767+
break; // "unknown" is the safe answer for a walk that could not finish
47254768
}
4726-
return cbm_type_unknown();
4769+
cbm_walk_stack_release(stack, inline_stack);
4770+
return found;
47274771
}
47284772

47294773
// Rebuild a function's CBMRegisteredFunc signature from the AST directly. extract_defs
@@ -4737,29 +4781,33 @@ static void rebuild_signatures_from_ast(TSLSPContext *ctx, TSNode root, CBMTypeR
47374781
if (ts_node_is_null(root) || !reg || !ctx->module_qn)
47384782
return;
47394783

4740-
/* The stack grows. A fixed 256 slots used to be filled by the seeding loop
4741-
* alone on a file with that many top-level children, and every declaration
4742-
* past it was then skipped without a word — it kept whatever signature
4743-
* extract_defs had guessed. */
4744-
TSNodeStack stack;
4745-
ts_nstack_init_arena(&stack, ctx->arena, 256);
4746-
ts_nstack_push_children(&stack, root);
4747-
4748-
while (stack.count > 0) {
4749-
TSNode n = ts_nstack_pop(&stack);
4784+
// Seeded with every top-level child, so a file with more than 256 of them
4785+
// needs the growing stack (see TS_WALK_STACK_INLINE). If it cannot grow, the
4786+
// walk stops and the declarations not yet reached keep extract_defs' signature.
4787+
TSNode inline_stack[TS_WALK_STACK_INLINE];
4788+
TSNode *stack = inline_stack;
4789+
int cap = TS_WALK_STACK_INLINE;
4790+
int top = 0;
4791+
bool walking =
4792+
ts_walk_push_children(&stack, &cap, &top, root, inline_stack, "ts_lsp_rebuild_signatures");
4793+
4794+
while (walking && top > 0) {
4795+
TSNode n = stack[--top];
47504796
if (ts_node_is_null(n))
47514797
continue;
47524798
const char *k = ts_node_type(n);
47534799

47544800
// Recurse into export_statement and class bodies.
47554801
if (strcmp(k, "export_statement") == 0 || strcmp(k, "class_body") == 0) {
4756-
ts_nstack_push_children(&stack, n);
4802+
walking = ts_walk_push_children(&stack, &cap, &top, n, inline_stack,
4803+
"ts_lsp_rebuild_signatures");
47574804
continue;
47584805
}
47594806
if (strcmp(k, "class_declaration") == 0) {
47604807
TSNode body = ts_node_child_by_field_name(n, "body", TS_LSP_FIELD_LEN("body"));
47614808
if (!ts_node_is_null(body))
4762-
ts_nstack_push(&stack, body);
4809+
walking = ts_walk_push(&stack, &cap, &top, body, inline_stack,
4810+
"ts_lsp_rebuild_signatures");
47634811
continue;
47644812
}
47654813

@@ -4889,6 +4937,7 @@ static void rebuild_signatures_from_ast(TSLSPContext *ctx, TSNode root, CBMTypeR
48894937
break;
48904938
}
48914939
}
4940+
cbm_walk_stack_release(stack, inline_stack);
48924941
}
48934942

48944943
// Convert NAMED references to TYPE_PARAM for any function whose declaration has a
@@ -4899,29 +4948,33 @@ static void convert_signature_type_params(TSLSPContext *ctx, TSNode root, CBMTyp
48994948
return;
49004949

49014950
// Walk: function_declaration, class_declaration { method_definition }, plus exported.
4902-
/* The stack grows. A fixed 256 slots used to be filled by the seeding loop
4903-
* alone on a file with that many top-level children, and every declaration
4904-
* past it was then skipped without a word — it kept whatever signature
4905-
* extract_defs had guessed. */
4906-
TSNodeStack stack;
4907-
ts_nstack_init_arena(&stack, ctx->arena, 256);
4908-
ts_nstack_push_children(&stack, root);
4909-
4910-
while (stack.count > 0) {
4911-
TSNode n = ts_nstack_pop(&stack);
4951+
// Seeded with every top-level child, so a file with more than 256 of them needs
4952+
// the growing stack (see TS_WALK_STACK_INLINE). If it cannot grow, the walk stops
4953+
// and the generics not yet reached keep their NAMED parameters.
4954+
TSNode inline_stack[TS_WALK_STACK_INLINE];
4955+
TSNode *stack = inline_stack;
4956+
int cap = TS_WALK_STACK_INLINE;
4957+
int top = 0;
4958+
bool walking =
4959+
ts_walk_push_children(&stack, &cap, &top, root, inline_stack, "ts_lsp_type_params");
4960+
4961+
while (walking && top > 0) {
4962+
TSNode n = stack[--top];
49124963
if (ts_node_is_null(n))
49134964
continue;
49144965
const char *k = ts_node_type(n);
49154966

49164967
// Recurse into export_statement and class_body.
49174968
if (strcmp(k, "export_statement") == 0 || strcmp(k, "class_body") == 0) {
4918-
ts_nstack_push_children(&stack, n);
4969+
walking =
4970+
ts_walk_push_children(&stack, &cap, &top, n, inline_stack, "ts_lsp_type_params");
49194971
continue;
49204972
}
49214973
if (strcmp(k, "class_declaration") == 0) {
49224974
TSNode body = ts_node_child_by_field_name(n, "body", TS_LSP_FIELD_LEN("body"));
49234975
if (!ts_node_is_null(body))
4924-
ts_nstack_push(&stack, body);
4976+
walking =
4977+
ts_walk_push(&stack, &cap, &top, body, inline_stack, "ts_lsp_type_params");
49254978
continue;
49264979
}
49274980

@@ -5020,6 +5073,7 @@ static void convert_signature_type_params(TSLSPContext *ctx, TSNode root, CBMTyp
50205073
break;
50215074
}
50225075
}
5076+
cbm_walk_stack_release(stack, inline_stack);
50235077
}
50245078

50255079
// Parse a JSDoc comment block for `@param {T} name` and `@returns {T}` tags. For each

‎tests/test_ts_lsp.c‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,21 @@ TEST(tslsp_return_inferred_local) {
310310
PASS();
311311
}
312312

313+
/* infer_return_type_from_body visits children last-child first, so of two returns
314+
* the one later in the body is tried first. Pinned so that a change to the walk
315+
* order (#2091) is a visible decision rather than a side effect. */
316+
TEST(tslsp_return_walk_order_prefers_later_return) {
317+
CBMFileResult *r =
318+
extract_ts("class A { a(): void {} }\n"
319+
"class B { b(): void {} }\n"
320+
"function pick(f) { if (f) { return new A(); } return new B(); }\n"
321+
"function go() { const x = pick(true); x.b(); }\n");
322+
ASSERT_NOT_NULL(r);
323+
ASSERT_GTE(require_resolved(r, ".go", "b"), 0);
324+
cbm_free_result(r);
325+
PASS();
326+
}
327+
313328
TEST(tslsp_return_async_promise) {
314329
CBMFileResult *r = extract_ts("class User { hello(): void {} }\n"
315330
"async function load(): Promise<User> { return new User(); }\n"
@@ -3321,8 +3336,7 @@ TEST(tslsp_stress_declarations_past_stack_cap) {
33213336
* the first extraction pass had guessed. */
33223337
enum { BUF_CAP = 64 * 1024 };
33233338
char *buf = (char *)malloc(BUF_CAP);
3324-
if (!buf)
3325-
PASS();
3339+
ASSERT_NOT_NULL(buf);
33263340
char *p = buf;
33273341
char *end = buf + BUF_CAP;
33283342
for (int i = 0; i < 300; i++) {
@@ -3350,8 +3364,7 @@ TEST(tslsp_stress_return_past_stack_cap) {
33503364
* and answer "unknown return type". */
33513365
enum { BUF_CAP = 64 * 1024 };
33523366
char *buf = (char *)malloc(BUF_CAP);
3353-
if (!buf)
3354-
PASS();
3367+
ASSERT_NOT_NULL(buf);
33553368
char *p = buf;
33563369
char *end = buf + BUF_CAP;
33573370
p += snprintf(p, (size_t)(end - p), "class Conn { ping(): void {} }\n");
@@ -4799,6 +4812,7 @@ SUITE(ts_lsp) {
47994812
RUN_TEST(tslsp_return_simple);
48004813
RUN_TEST(tslsp_return_chain);
48014814
RUN_TEST(tslsp_return_inferred_local);
4815+
RUN_TEST(tslsp_return_walk_order_prefers_later_return);
48024816
RUN_TEST(tslsp_return_async_promise);
48034817
RUN_TEST(tslsp_return_method_no_rttype);
48044818

0 commit comments

Comments
 (0)