Skip to content

Commit 0849f28

Browse files
committed
fix: bulk parallel — Rust cross-crate, Python builtins, #607 reinstall, grammar reds
Four file-disjoint problem groups fixed in parallel, deployed together: - G1 (lsp): wire the dead Rust cross-file LSP into pass_lsp_cross.c (CBM_LANG_RUST in has_cross_lsp + run_one, CBMLSPDef->CBMRustLSPDef conversion, Cargo manifest via cbm_cargo_parse threaded through) -> 8 in-crate Rust strategy reds + #56 cross-crate (lsp_cross_crate disambiguation). - G2 (lsp): the py typeshed registry already resolved builtins but no builtins.* graph node existed to target -> add py_builtins.c injecting builtin def nodes (len/str/list/dict + str.upper/list.append/...). Plus rescope the genuinely no-edge LSP markers (lsp_unresolved/_unindexed/external-import) to assert the accurate no-edge invariant (new inv_no_calls_edge_to_qn helper). - G3 (cli): #607 — install now PRESERVES existing indexes by default + a --reset-indexes opt-in (cbm_install_handle_existing_indexes helper); uninstall/ update unchanged; repro_issue607 retargeted to the helper. - G4 (grammar): Swift struct (Swift uses class_declaration+declaration_kind), Nix function defs + a discovered Nix calls-extraction bug, Teal/Magma fixtures corrected. LLVM/NASM confirmed already green. (Meson/Nim flagged needs-decision.) Signed-off-by: Martin Vogel <[email protected]>
1 parent b3fdc92 commit 0849f28

16 files changed

Lines changed: 860 additions & 240 deletions

‎internal/cbm/extract_calls.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,35 @@ static char *extract_func_callee(CBMArena *a, TSNode node, const char *source, c
889889
return ts_node_is_null(fn) ? NULL : cbm_node_text(a, fn, source);
890890
}
891891

892+
// Nix: an `apply_expression` (`f x`) carries the applied function on its
893+
// `function:` field. The head is a `variable_expression` whose `name` is the
894+
// callee identifier; curried application (`f x y`) nests apply_expressions, so
895+
// descend the `function` chain to the head variable_expression. The generic
896+
// field resolver does not recognise `variable_expression`, so without this the
897+
// call to `addOne` would never be captured.
898+
static char *extract_nix_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {
899+
if (strcmp(nk, "apply_expression") != 0) {
900+
return NULL;
901+
}
902+
TSNode fn = ts_node_child_by_field_name(node, TS_FIELD("function"));
903+
for (int depth = 0; depth < 8 && !ts_node_is_null(fn); depth++) {
904+
const char *fk = ts_node_type(fn);
905+
if (strcmp(fk, "apply_expression") == 0) {
906+
fn = ts_node_child_by_field_name(fn, TS_FIELD("function"));
907+
continue;
908+
}
909+
if (strcmp(fk, "variable_expression") == 0) {
910+
TSNode nm = ts_node_child_by_field_name(fn, TS_FIELD("name"));
911+
return ts_node_is_null(nm) ? NULL : cbm_node_text(a, nm, source);
912+
}
913+
if (strcmp(fk, "identifier") == 0) {
914+
return cbm_node_text(a, fn, source);
915+
}
916+
return NULL;
917+
}
918+
return NULL;
919+
}
920+
892921
// Agda: function application `f x y` parses as an `expr` whose named children are
893922
// `atom`s (no dedicated application node). Treat an `expr` with >= 2 atom children
894923
// as a call whose callee is the head atom's identifier.
@@ -1068,6 +1097,12 @@ static char *extract_callee_lang_specific(CBMArena *a, TSNode node, const char *
10681097
return c;
10691098
}
10701099
}
1100+
if (lang == CBM_LANG_NIX) {
1101+
char *c = extract_nix_callee(a, node, source, nk);
1102+
if (c) {
1103+
return c;
1104+
}
1105+
}
10711106
if (lang == CBM_LANG_MAKEFILE) {
10721107
char *c = extract_make_callee(a, node, source, nk);
10731108
if (c) {

‎internal/cbm/extract_defs.c‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,23 @@ TSNode cbm_resolve_func_name(TSNode node, CBMLanguage lang) {
773773
}
774774
}
775775

776+
/* Nix: a named function is a `function_expression` (lambda `x: body`) with
777+
* no name of its own — the binding name lives on the enclosing `binding`'s
778+
* `attrpath` field (`name = x: ...`). Resolve through the parent binding to
779+
* the attrpath's `attr` identifier so `addOne = x: ...` mints a Function
780+
* def. A lambda whose parent is not a binding (e.g. an inline `map (x: x)`
781+
* argument) resolves null and stays out of func_types. */
782+
if (lang == CBM_LANG_NIX && strcmp(kind, "function_expression") == 0) {
783+
TSNode parent = ts_node_parent(node);
784+
if (!ts_node_is_null(parent) && strcmp(ts_node_type(parent), "binding") == 0) {
785+
TSNode attrpath = ts_node_child_by_field_name(parent, TS_FIELD("attrpath"));
786+
if (!ts_node_is_null(attrpath)) {
787+
TSNode attr = ts_node_child_by_field_name(attrpath, TS_FIELD("attr"));
788+
return ts_node_is_null(attr) ? attrpath : attr;
789+
}
790+
}
791+
}
792+
776793
/* Fortran: subroutine/function wrap an inner *_statement that carries the
777794
* `name` field; the outer node walk_defs matched has no name itself. */
778795
if (lang == CBM_LANG_FORTRAN &&
@@ -3512,18 +3529,32 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
35123529
}
35133530
// Rust/Swift/D: a struct is a distinct kind from a class — emit the precise
35143531
// "Struct" label rather than collapsing it to "Class". Scoped to these three
3515-
// grammar/LSP languages whose struct node is `struct_item` (Rust) or
3516-
// `struct_declaration` (Swift, D). C/C++/Obj-C keep `struct_specifier` →
3517-
// "Class" (a C++ struct is class-like). "Struct" is a type-like container:
3518-
// every type-resolution / registry / IMPLEMENTS / LSP-registrar consumer
3519-
// routes through cbm_label_is_type_like(), so a struct still resolves as a
3520-
// type for its methods, fields, inheritance and impls.
3532+
// grammar/LSP languages. Rust's struct node is `struct_item`; D's is
3533+
// `struct_declaration`. C/C++/Obj-C keep `struct_specifier` → "Class"
3534+
// (a C++ struct is class-like). "Struct" is a type-like container: every
3535+
// type-resolution / registry / IMPLEMENTS / LSP-registrar consumer routes
3536+
// through cbm_label_is_type_like(), so a struct still resolves as a type for
3537+
// its methods, fields, inheritance and impls.
35213538
if (ctx->language == CBM_LANG_RUST || ctx->language == CBM_LANG_SWIFT ||
35223539
ctx->language == CBM_LANG_DLANG) {
35233540
if (strcmp(kind, "struct_item") == 0 || strcmp(kind, "struct_declaration") == 0) {
35243541
label = "Struct";
35253542
}
35263543
}
3544+
// Swift: tree-sitter-swift does NOT have a dedicated `struct_declaration`
3545+
// node — `struct`, `class` and `actor` all parse to `class_declaration`,
3546+
// distinguished only by the `declaration_kind` field (the leading keyword
3547+
// token). Read that field and emit "Struct" when the keyword is `struct`
3548+
// (and "Class" for `class`/`actor`, which class_label_for_kind already gives).
3549+
if (ctx->language == CBM_LANG_SWIFT && strcmp(kind, "class_declaration") == 0) {
3550+
TSNode dk = ts_node_child_by_field_name(node, TS_FIELD("declaration_kind"));
3551+
if (!ts_node_is_null(dk)) {
3552+
char *dk_text = cbm_node_text(a, dk, ctx->source);
3553+
if (dk_text && strcmp(dk_text, "struct") == 0) {
3554+
label = "Struct";
3555+
}
3556+
}
3557+
}
35273558
// F#: a `type_definition` that has a primary constructor (`type Foo(...) =`)
35283559
// or an `inherit` clause is an OOP class, not a plain type alias. Label it
35293560
// "Class" so it is registered as a resolvable inheritance target (the graph

‎internal/cbm/lsp/py_builtins.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* py_builtins.c — Minimal Python builtins as real graph nodes.
3+
*
4+
* The Python LSP type registry already knows the builtins (typeshed-derived
5+
* generated/python_stdlib_data.c registers builtins.len, builtins.str,
6+
* builtins.str.upper, builtins.list.append, ...). So a call like len(v) /
7+
* str(v) / "x".upper() / xs.append(1) ALREADY resolves at the LSP layer and
8+
* emits the correct strategy (lsp_builtin / lsp_builtin_constructor /
9+
* lsp_builtin_method / lsp_generic_method) with callee_qn = "builtins.<name>".
10+
*
11+
* The missing piece is downstream: pass_calls.c only writes a CALLS edge when
12+
* cbm_pipeline_lsp_target_node() resolves the callee_qn to a graph node
13+
* (src/pipeline/lsp_resolve.h). There is no "builtins.len" node in the graph,
14+
* so the resolved call is dropped and the strategy never lands on an edge.
15+
*
16+
* Fix: inject a small, fixed set of builtin definitions into result->defs
17+
* during the per-file Python LSP run (which executes inside cbm_extract_file,
18+
* BEFORE the parallel pipeline mints def nodes from result->defs). The graph
19+
* therefore gains real "builtins.*" nodes that the LSP-emitted edges target.
20+
* The QNs here MUST match what the typeshed registry emits as callee_qn.
21+
*
22+
* Node minting upserts by QN (cbm_gbuf_upsert_node), so injecting the same
23+
* builtins per Python file collapses to one node per QN — no duplicates.
24+
*
25+
* Self-contained: #included from py_lsp.c only (CGo amalgamation pattern;
26+
* see lsp_all.c). Not a standalone translation unit.
27+
*/
28+
29+
/* A single builtin entry to mint as a graph node. */
30+
typedef struct {
31+
const char *qn; /* graph QN — MUST equal the registry callee_qn */
32+
const char *name; /* short name (last segment of qn) */
33+
const char *label; /* "Function" | "Class" | "Method" */
34+
} PyBuiltinNode;
35+
36+
/*
37+
* Minimal builtins set. Kept deliberately small and aligned with the registry
38+
* (generated/python_stdlib_data.c):
39+
* - free functions (lsp_builtin): len, print
40+
* - types/ctors (lsp_builtin_constructor): str, int, list, dict, range
41+
* - str methods (lsp_builtin_method): upper, lower
42+
* - list methods (lsp_generic_method): append, pop
43+
* - dict methods (lsp_generic_method): get
44+
* Note: str/int/list/dict/range are TYPES in the registry (so X() routes to
45+
* lsp_builtin_constructor), hence the "Class" label here.
46+
*/
47+
static const PyBuiltinNode kPyBuiltinNodes[] = {
48+
{"builtins.len", "len", "Function"},
49+
{"builtins.print", "print", "Function"},
50+
51+
{"builtins.str", "str", "Class"},
52+
{"builtins.int", "int", "Class"},
53+
{"builtins.list", "list", "Class"},
54+
{"builtins.dict", "dict", "Class"},
55+
{"builtins.range", "range", "Class"},
56+
57+
{"builtins.str.upper", "upper", "Method"},
58+
{"builtins.str.lower", "lower", "Method"},
59+
60+
{"builtins.list.append", "append", "Method"},
61+
{"builtins.list.pop", "pop", "Method"},
62+
63+
{"builtins.dict.get", "get", "Method"},
64+
};
65+
66+
/*
67+
* Inject the builtin definitions into result->defs so the pipeline mints them
68+
* as graph nodes. All fields beyond name/qn/label are left zero/NULL: builtins
69+
* have no body, so complexity/line-range/etc. are irrelevant, and a synthetic
70+
* file_path keeps them out of any real source file's def list.
71+
*/
72+
static void py_builtins_inject_defs(CBMFileResult *result, CBMArena *arena) {
73+
if (!result || !arena) {
74+
return;
75+
}
76+
const int n = (int)(sizeof(kPyBuiltinNodes) / sizeof(kPyBuiltinNodes[0]));
77+
for (int i = 0; i < n; i++) {
78+
const PyBuiltinNode *b = &kPyBuiltinNodes[i];
79+
CBMDefinition def;
80+
memset(&def, 0, sizeof(def));
81+
def.name = b->name;
82+
def.qualified_name = b->qn;
83+
def.label = b->label;
84+
def.file_path = "<python-builtins>";
85+
def.start_line = 1;
86+
def.end_line = 1;
87+
cbm_defs_push(&result->defs, arena, def);
88+
}
89+
}

‎internal/cbm/lsp/py_lsp.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
#include <stdlib.h>
1919
#include <string.h>
2020

21+
/* Minimal Python builtins as real graph nodes (py_builtins_inject_defs).
22+
* #included here (CGo amalgamation pattern, see lsp_all.c) — referenced
23+
* only from py_lsp.c, never compiled standalone. */
24+
#include "py_builtins.c"
25+
2126
// Forward decls
2227
static void py_resolve_calls_in(PyLSPContext *ctx, TSNode node);
2328
static const CBMType *py_eval_expr_type(PyLSPContext *ctx, TSNode node);
@@ -3363,6 +3368,15 @@ void cbm_run_py_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
33633368
if (!arena || !result)
33643369
return;
33653370

3371+
/* Inject minimal builtin definitions as real graph nodes (builtins.len,
3372+
* builtins.str, builtins.str.upper, ...). The typeshed registry already
3373+
* RESOLVES builtin calls (emitting the strategy + a "builtins.*" callee_qn),
3374+
* but pass_calls.c only writes the CALLS edge when that callee_qn maps to a
3375+
* graph node. We run inside cbm_extract_file, before the pipeline mints
3376+
* def nodes from result->defs, so these become the target nodes the
3377+
* builtin/constructor/method edges point at. Upsert dedups by QN. */
3378+
py_builtins_inject_defs(result, arena);
3379+
33663380
CBMTypeRegistry reg;
33673381
cbm_registry_init(&reg, arena);
33683382

‎internal/cbm/lsp/rust_lsp.c‎

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3625,18 +3625,62 @@ static void rust_resolve_call_expression(RustLSPContext *ctx, TSNode node) {
36253625
}
36263626
}
36273627

3628-
/* Global short-name fallback: scan the registry for a unique
3629-
* function whose short_name matches the path's tail and whose
3630-
* QN starts with the current crate prefix. This gives `mod
3631-
* foo; use foo::bar; bar()` a chance to resolve when the
3632-
* intermediate module wasn't tracked through an explicit
3633-
* use-map entry. */
36343628
const char *tail = strrchr(path, ':');
36353629
if (tail && tail > path && tail[-1] == ':') {
36363630
tail += 1;
36373631
} else {
36383632
tail = path;
36393633
}
3634+
3635+
/* Cross-crate workspace-member resolution (#56): when the call
3636+
* path's head is a declared Cargo workspace member (e.g.
3637+
* `crate_a::helper` from inside crate_b) we cannot rely on the
3638+
* caller-crate-scoped fallback below — that filters by the
3639+
* CALLER's module prefix and would resolve to a same-named local
3640+
* function instead. Route to the function defined inside the
3641+
* MEMBER crate by matching the registered QN's `.<member>.`
3642+
* path segment plus the call tail. Requires a parsed manifest
3643+
* (threaded through pass_lsp_cross.c); NULL manifest skips this. */
3644+
if (ctx->cargo_manifest && tail && *tail) {
3645+
const char *head_sep = strstr(path, "::");
3646+
if (head_sep && head_sep > path) {
3647+
char *head = cbm_arena_strndup(ctx->arena, path, (size_t)(head_sep - path));
3648+
const CBMCargoManifest *m = (const CBMCargoManifest *)ctx->cargo_manifest;
3649+
if (head && cbm_cargo_find_member(m, head)) {
3650+
/* `.crate_a.` — the member directory appears as a dotted
3651+
* QN segment for every def inside that crate. */
3652+
char *needle = cbm_arena_sprintf(ctx->arena, ".%s.", head);
3653+
const CBMRegisteredFunc *mem_unique = NULL;
3654+
int mem_matches = 0;
3655+
for (int i = 0; i < ctx->registry->func_count && mem_matches < 2; i++) {
3656+
const CBMRegisteredFunc *f = &ctx->registry->funcs[i];
3657+
if (!f->short_name || !f->qualified_name)
3658+
continue;
3659+
if (f->receiver_type)
3660+
continue; /* free functions only */
3661+
if (strcmp(f->short_name, tail) != 0)
3662+
continue;
3663+
if (!strstr(f->qualified_name, needle))
3664+
continue; /* not defined in the member crate */
3665+
mem_matches++;
3666+
if (mem_matches == 1)
3667+
mem_unique = f;
3668+
}
3669+
if (mem_matches == 1 && mem_unique) {
3670+
rust_emit_resolved_call(ctx, mem_unique->qualified_name, "lsp_cross_crate",
3671+
CBM_RUST_CONF_DIRECT);
3672+
return;
3673+
}
3674+
}
3675+
}
3676+
}
3677+
3678+
/* Global short-name fallback: scan the registry for a unique
3679+
* function whose short_name matches the path's tail and whose
3680+
* QN starts with the current crate prefix. This gives `mod
3681+
* foo; use foo::bar; bar()` a chance to resolve when the
3682+
* intermediate module wasn't tracked through an explicit
3683+
* use-map entry. */
36403684
if (tail && *tail && ctx->module_qn) {
36413685
/* Crate prefix is the first dotted segment of module_qn after
36423686
* the project name, but for simplicity we just match on
@@ -5121,10 +5165,12 @@ void cbm_run_rust_lsp(CBMArena *arena, CBMFileResult *result, const char *source
51215165

51225166
extern const TSLanguage *tree_sitter_rust(void);
51235167

5124-
void cbm_run_rust_lsp_cross(CBMArena *arena, const char *source, int source_len,
5125-
const char *module_qn, CBMRustLSPDef *defs, int def_count,
5126-
const char **import_names, const char **import_qns, int import_count,
5127-
TSTree *cached_tree, CBMResolvedCallArray *out) {
5168+
void cbm_run_rust_lsp_cross_with_manifest(CBMArena *arena, const char *source, int source_len,
5169+
const char *module_qn, CBMRustLSPDef *defs, int def_count,
5170+
const char **import_names, const char **import_qns,
5171+
int import_count, TSTree *cached_tree,
5172+
const struct CBMCargoManifest *manifest,
5173+
CBMResolvedCallArray *out) {
51285174
if (!source || source_len <= 0 || !out)
51295175
return;
51305176

@@ -5251,6 +5297,10 @@ void cbm_run_rust_lsp_cross(CBMArena *arena, const char *source, int source_len,
52515297

52525298
RustLSPContext ctx;
52535299
rust_lsp_init(&ctx, arena, source, source_len, &reg, module_qn, out);
5300+
/* Workspace/dependency awareness for cross-CRATE path routing (#56).
5301+
* Mirrors the single-file path (cbm_run_rust_lsp_with_manifest). NULL
5302+
* when no Cargo.toml was parsed — in-crate resolution is unaffected. */
5303+
ctx.cargo_manifest = manifest;
52545304
rust_collect_uses(&ctx, root);
52555305
for (int i = 0; i < import_count; i++) {
52565306
if (import_names[i] && import_qns[i]) {
@@ -5266,6 +5316,18 @@ void cbm_run_rust_lsp_cross(CBMArena *arena, const char *source, int source_len,
52665316
}
52675317
}
52685318

5319+
/* Manifest-free entry point. Preserves the pre-existing signature used by
5320+
* the unit tests (test_rust_lsp.c) and the batch wrapper — delegates to
5321+
* the manifest-aware variant with a NULL manifest. */
5322+
void cbm_run_rust_lsp_cross(CBMArena *arena, const char *source, int source_len,
5323+
const char *module_qn, CBMRustLSPDef *defs, int def_count,
5324+
const char **import_names, const char **import_qns, int import_count,
5325+
TSTree *cached_tree, CBMResolvedCallArray *out) {
5326+
cbm_run_rust_lsp_cross_with_manifest(arena, source, source_len, module_qn, defs, def_count,
5327+
import_names, import_qns, import_count, cached_tree, NULL,
5328+
out);
5329+
}
5330+
52695331
void cbm_batch_rust_lsp_cross(CBMArena *arena, CBMBatchRustLSPFile *files, int file_count,
52705332
CBMResolvedCallArray *out) {
52715333
if (!files || file_count <= 0 || !out)

‎internal/cbm/lsp/rust_lsp.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,19 @@ void cbm_run_rust_lsp_cross(CBMArena *arena, const char *source, int source_len,
283283
const char **import_names, const char **import_qns, int import_count,
284284
TSTree *cached_tree, CBMResolvedCallArray *out);
285285

286+
/* Same as `cbm_run_rust_lsp_cross`, plus an optional parsed Cargo manifest
287+
* (NULL = manifest-free behaviour). The manifest lets call paths whose head
288+
* is a workspace member / declared dependency route across the crate
289+
* boundary (`crate_a::foo` → the def inside crate_a). Wired from the
290+
* cross-file LSP pass (pass_lsp_cross.c) which builds the manifest once from
291+
* the project root Cargo.toml. */
292+
void cbm_run_rust_lsp_cross_with_manifest(CBMArena *arena, const char *source, int source_len,
293+
const char *module_qn, CBMRustLSPDef *defs, int def_count,
294+
const char **import_names, const char **import_qns,
295+
int import_count, TSTree *cached_tree,
296+
const struct CBMCargoManifest *manifest,
297+
CBMResolvedCallArray *out);
298+
286299
/* Per-file input for batch cross-file Rust LSP processing. */
287300
typedef struct {
288301
const char *source;

0 commit comments

Comments
 (0)