Skip to content

Commit bf8680c

Browse files
committed
fix(extract): model Nix attrpaths — leaf is the name, the path is scope
A Nix binding's name is a PATH, and the resolver took only its first segment (`child_by_field_name(attrpath, "attr")`). Three defects followed from that one modelling gap: - `setA = { dup = …; }` and `setB = { dup = …; }` both minted the qualified name proj.file.dup. The second definition, and every CALLS edge sourced from it, was silently discarded at write. - `a.b.fn = …` minted a definition named `a`, colliding with every other binding whose path began `a`. - `"kebab-case" = …` minted a name with the quote characters in it, so every consumer keying on the name had to know to re-quote. Adopts the convention already used for C++ namespaces — name is the leaf segment, qualified_name is enclosing scope plus leaf, so `ns::serialize` is name `serialize` and QN proj.file.ns.serialize. For Nix that means: - the resolver returns the LAST attrpath segment; - leading segments become scope, so `a.b.fn = …` and the nested spelling `a = { b = { fn = …; }; }` produce the same QN, as they must — one is sugar for the other; - a binding whose value is an attribute set is a scope, mirroring is_namespace_scope_kind's treatment of a C++ namespace. `let` bindings are deliberately NOT scopes: they are lexical, and C++ does not qualify by block scope either; - a quoted segment is unquoted, and an interpolated one (`"${x}" = …`) mints nothing at all, since it has no statically knowable name — the same call the Makefile dot-prefix guard makes. The two scope sources compose: `setA = { a.b.fn = …; }` is proj.file.setA.a.b.fn. Definition QNs and call-scope QNs are computed by two different functions — extract_defs.c's compute_class_qn and extract_unified.c's own. If they disagree by one segment the CALLS edge names a source node that was never minted and is dropped at write, with no error and no failing extraction assertion. Rather than implement the rule twice, the Nix computation lives in helpers.c and both call it, which makes that class of drift impossible instead of merely unlikely. Note also that the def-side enclosing-QN gate is language-gated while the call-side is not, so widening one without the other would have produced exactly that mismatch. Signed-off-by: Jason Bowman <jason@json64.dev>
1 parent 63a244a commit bf8680c

4 files changed

Lines changed: 303 additions & 18 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -821,15 +821,22 @@ TSNode cbm_resolve_func_name(TSNode node, CBMLanguage lang) {
821821
/* Nix: a named function is a `function_expression` (lambda `x: body`) with
822822
* no name of its own — the binding name lives on the enclosing `binding`'s
823823
* `attrpath` field (`name = x: ...`). Resolve through the parent binding to
824-
* the attrpath's `attr` identifier so `addOne = x: ...` mints a Function
825-
* def. A lambda whose parent is not a binding (e.g. an inline `map (x: x)`
826-
* argument) resolves null and stays out of func_types. */
824+
* the attrpath's LAST `attr` so `addOne = x: ...` mints a Function def. A
825+
* lambda whose parent is not a binding (e.g. an inline `map (x: x)`
826+
* argument) resolves null and stays out of func_types.
827+
*
828+
* The last segment, not the first: an attrpath is a PATH, and `a.b.fn = …`
829+
* is sugar for `a = { b = { fn = …; }; };`. Both spellings must mint the
830+
* same name (`fn`) and the same QN (`proj.mod.a.b.fn`) — the leading
831+
* segments are scope, supplied by cbm_nix_attrpath_scope. Taking the first
832+
* segment named `a.b.fn` "a", which collided with every other binding
833+
* whose path began `a`. */
827834
if (lang == CBM_LANG_NIX && strcmp(kind, "function_expression") == 0) {
828835
TSNode parent = ts_node_parent(node);
829836
if (!ts_node_is_null(parent) && strcmp(ts_node_type(parent), "binding") == 0) {
830837
TSNode attrpath = ts_node_child_by_field_name(parent, TS_FIELD("attrpath"));
831838
if (!ts_node_is_null(attrpath)) {
832-
TSNode attr = ts_node_child_by_field_name(attrpath, TS_FIELD("attr"));
839+
TSNode attr = cbm_nix_attrpath_last_attr(attrpath);
833840
return ts_node_is_null(attr) ? attrpath : attr;
834841
}
835842
}
@@ -3265,7 +3272,7 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
32653272
return;
32663273
}
32673274

3268-
char *name = cbm_func_name_node_text(a, name_node, ctx->source);
3275+
char *name = cbm_func_name_node_text(a, name_node, ctx->source, ctx->language);
32693276
if (!name || !name[0] || strcmp(name, "function") == 0) {
32703277
return;
32713278
}
@@ -3292,28 +3299,53 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
32923299
}
32933300
}
32943301

3302+
/* Nix `"${foo}" = x: …`: an interpolated attrpath segment has no statically
3303+
* knowable name. Minting it would produce a def literally named `"${foo}"`
3304+
* that nothing can ever look up or resolve a call against. An absent node is
3305+
* the honest answer — same reasoning as the Makefile guard above. */
3306+
if (ctx->language == CBM_LANG_NIX && cbm_nix_attr_is_interpolated(name_node)) {
3307+
return;
3308+
}
3309+
32953310
TSNode func_node = unwrap_template_inner(node, ctx->language);
32963311

32973312
CBMDefinition def;
32983313
memset(&def, 0, sizeof(def));
32993314

33003315
def.name = name;
3316+
/* Nix: a binding's name is a path. The leaf is the name; the leading segments
3317+
* are scope, so `a.b.fn = …` gets the same QN as `a = { b = { fn = …; }; }`.
3318+
* Without this every binding whose path shares a leaf name collapsed onto one
3319+
* node, silently discarding the later definition and its CALLS edges. */
3320+
const char *qn_name = name;
3321+
if (ctx->language == CBM_LANG_NIX) {
3322+
qn_name = cbm_nix_qn_name(a, node, ctx->source, name);
3323+
}
33013324
/* Java/Go derive the module from the containing directory (package), so the
33023325
* filename stem is NOT baked into the QN (Go func in myapp/db/conn.go ->
33033326
* proj.myapp.db.Func, not proj.myapp.db.conn.Func). Other langs unchanged. */
33043327
def.qualified_name =
3305-
cbm_fqn_compute_source_lang(a, ctx->project, ctx->rel_path, name, ctx->language);
3328+
cbm_fqn_compute_source_lang(a, ctx->project, ctx->rel_path, qn_name, ctx->language);
33063329
/* A free function declared inside a namespace (C++/C#/PHP) is qualified by
33073330
* the namespace scope the def walk carries (enclosing_class_qn was extended
33083331
* by is_namespace_scope_kind), so `ns::serialize` is `proj.file.ns.serialize`
33093332
* — without this it collapses to the file scope and namespace-aware
33103333
* resolution (ADL, namespace-function lookup) can never see it. Class methods
33113334
* never reach here (they go through extract_class_methods), so a set
33123335
* enclosing scope here is always a namespace. The out-of-line method path
3313-
* below overrides this for `Ns::Cls::method` definitions. */
3336+
* below overrides this for `Ns::Cls::method` definitions.
3337+
*
3338+
* Nix joins this list for the same reason: a binding inside an attrset is
3339+
* scoped by it, so `setA = { fn = ...; }` is proj.file.setA.fn. Uses qn_name,
3340+
* not name, so an attrpath's own leading segments compose with the enclosing
3341+
* scope. Note the call-scope side (compute_func_qn in extract_unified.c) is
3342+
* NOT language-gated — it qualifies whenever a scope is pushed — so this gate
3343+
* and the scope-push rule must move together, or a call QN names a def QN that
3344+
* was never minted and the edge is dropped at write. */
33143345
if (ctx->enclosing_class_qn &&
3315-
(ctx->language == CBM_LANG_CPP || ctx->language == CBM_LANG_CUDA)) {
3316-
def.qualified_name = cbm_arena_sprintf(a, "%s.%s", ctx->enclosing_class_qn, name);
3346+
(ctx->language == CBM_LANG_CPP || ctx->language == CBM_LANG_CUDA ||
3347+
ctx->language == CBM_LANG_NIX)) {
3348+
def.qualified_name = cbm_arena_sprintf(a, "%s.%s", ctx->enclosing_class_qn, qn_name);
33173349
}
33183350
def.label = "Function";
33193351
def.file_path = ctx->rel_path;
@@ -4309,7 +4341,7 @@ static void push_method_def(CBMExtractCtx *ctx, TSNode child, TSNode class_node,
43094341
const char *class_qn, const CBMLangSpec *spec, TSNode name_node) {
43104342
CBMArena *a = ctx->arena;
43114343

4312-
char *name = cbm_func_name_node_text(a, name_node, ctx->source);
4344+
char *name = cbm_func_name_node_text(a, name_node, ctx->source, ctx->language);
43134345
if (!name || !name[0]) {
43144346
return;
43154347
}
@@ -6306,14 +6338,31 @@ static bool is_template_class_node(TSNode node, CBMLanguage lang) {
63066338
* namespace emits no def of its own — it only extends the enclosing scope for
63076339
* its members. C#/PHP need the same treatment paired with their LSP resolvers
63086340
* (a def-only change breaks their existing namespace handling), done separately. */
6309-
static bool is_namespace_scope_kind(CBMLanguage lang, const char *kind) {
6341+
static bool is_namespace_scope_kind(CBMLanguage lang, const char *kind, TSNode node) {
63106342
if (lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA) {
63116343
return strcmp(kind, "namespace_definition") == 0;
63126344
}
6345+
/* Nix: a binding whose value is an attribute set is a named scope that is not
6346+
* itself a definition — the same shape as a C++ namespace. `setA = { fn = …; }`
6347+
* makes `fn` proj.file.setA.fn, so two attrsets can each hold a `fn` without
6348+
* the second silently overwriting the first.
6349+
*
6350+
* Deliberately NOT `let` bindings: those are lexical, and C++ does not qualify
6351+
* by block scope either. Takes the node because the decision depends on the
6352+
* binding's VALUE, which the kind string alone cannot express. */
6353+
if (lang == CBM_LANG_NIX && strcmp(kind, "binding") == 0) {
6354+
return cbm_nix_binding_is_attrset_scope(node);
6355+
}
63136356
return false;
63146357
}
63156358

63166359
static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const char *saved_enclosing) {
6360+
/* Nix scopes are `binding` nodes, which carry an `attrpath` rather than a
6361+
* `name` field. Shared with the unified extractor's own compute_class_qn so
6362+
* the def QN and the call-scope QN cannot drift. */
6363+
if (ctx->language == CBM_LANG_NIX) {
6364+
return cbm_nix_binding_scope_qn(ctx, node, saved_enclosing);
6365+
}
63176366
TSNode name_node = ts_node_child_by_field_name(node, TS_FIELD("name"));
63186367
if (ts_node_is_null(name_node) && ctx->language == CBM_LANG_OBJC) {
63196368
name_node = cbm_find_child_by_kind(node, "identifier");
@@ -6865,7 +6914,7 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
68656914
* is walked normally — functions AND classes, unlike a class body which
68666915
* routes methods through extract_class_methods. Do NOT emit a def or run
68676916
* the class/func paths on the namespace node itself. */
6868-
if (is_namespace_scope_kind(ctx->language, kind)) {
6917+
if (is_namespace_scope_kind(ctx->language, kind, node)) {
68696918
const char *new_enclosing = compute_class_qn(ctx, node, frame.enclosing_class_qn);
68706919
wd_push_children_reverse(&s, node, new_enclosing);
68716920
continue;

‎internal/cbm/extract_unified.c‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
623623
return NULL;
624624
}
625625

626-
char *name = cbm_func_name_node_text(ctx->arena, name_node, ctx->source);
626+
char *name = cbm_func_name_node_text(ctx->arena, name_node, ctx->source, ctx->language);
627627
if (!name || !name[0]) {
628628
return NULL;
629629
}
@@ -644,12 +644,24 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
644644
}
645645
}
646646

647+
/* Nix: a binding's own attrpath contributes scope (`a.b.fn = …`), and the def
648+
* extractor bakes it into the def QN. Compose it identically here — otherwise
649+
* an in-body call sources to a QN one or more segments short of the def, and
650+
* the edge is dropped at write. */
651+
const char *qn_name = name;
652+
if (ctx->language == CBM_LANG_NIX) {
653+
qn_name = cbm_nix_qn_name(ctx->arena, node, ctx->source, name);
654+
if (!qn_name || !qn_name[0]) {
655+
return NULL;
656+
}
657+
}
658+
647659
if (state->enclosing_class_qn) {
648-
return cbm_arena_sprintf(ctx->arena, "%s.%s", state->enclosing_class_qn, name);
660+
return cbm_arena_sprintf(ctx->arena, "%s.%s", state->enclosing_class_qn, qn_name);
649661
}
650662
/* Java/Go: directory-based module so this enclosing-func QN matches the def
651663
* QN and the LSP caller_qn (the lsp_resolve join keys on exact equality). */
652-
return cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path, name,
664+
return cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path, qn_name,
653665
ctx->language);
654666
}
655667

@@ -658,6 +670,13 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const WalkS
658670
if (ctx->language == CBM_LANG_OBJECTSCRIPT_UDL) {
659671
return objectscript_get_class_name(ctx, node);
660672
}
673+
/* Nix: an attrset-valued `binding` is a named scope. Same shared helper the def
674+
* extractor's compute_class_qn uses — these are two separate functions, and a
675+
* one-segment disagreement between them drops every CALLS edge sourced from a
676+
* nested binding. */
677+
if (ctx->language == CBM_LANG_NIX) {
678+
return cbm_nix_binding_scope_qn(ctx, node, state ? state->enclosing_class_qn : NULL);
679+
}
661680
TSNode name_node = ts_node_child_by_field_name(node, TS_FIELD("name"));
662681
/* Newer tree-sitter-kotlin: class/object name is a type_identifier child. */
663682
if (ts_node_is_null(name_node) && ctx->language == CBM_LANG_KOTLIN) {
@@ -1418,6 +1437,16 @@ static void push_boundary_scopes(CBMExtractCtx *ctx, TSNode node, const CBMLangS
14181437
state->os_type_map.class_base_count = 0;
14191438
}
14201439
}
1440+
} else if (ctx->language == CBM_LANG_NIX && strcmp(ts_node_type(node), "binding") == 0 &&
1441+
cbm_nix_binding_is_attrset_scope(node)) {
1442+
/* Nix: a binding whose value is an attribute set is a named scope, exactly
1443+
* as is_namespace_scope_kind treats it on the def side. Pushing it here is
1444+
* what makes an in-body call source to `proj.file.setA.fn` rather than the
1445+
* bare `proj.file.fn` that no node carries once defs are attrpath-qualified. */
1446+
const char *cqn = compute_class_qn(ctx, node, state);
1447+
if (cqn) {
1448+
push_scope(state, SCOPE_CLASS, depth, cqn);
1449+
}
14211450
} else if (ctx->language == CBM_LANG_RUST && strcmp(ts_node_type(node), "impl_item") == 0) {
14221451
TSNode type_node = ts_node_child_by_field_name(node, TS_FIELD("type"));
14231452
if (!ts_node_is_null(type_node)) {

0 commit comments

Comments
 (0)