Skip to content

Commit 87cafb9

Browse files
committed
fix(extract): bump the index format for receiver-qualified Go method QNs
Review follow-up for DeusData#1913 (asks from the 2026-09-28 review), plus one finding from the review of the updated head: - store.h: CBM_INDEX_FORMAT_VERSION 3 -> 4. Every Go method QN changes (pkg.method -> pkg.Recv.method), so an index built before this change and refreshed incrementally would mix flat and receiver-qualified method nodes, with the old fused nodes keeping their stale edges. The bump routes such an index through one full rebuild. - pipeline_go_method_caller_keeps_lsp_join: the by-name assertion would also pass for a binding onto the decoy other.Decoy.helper. The test now pins the edge by QN suffix on both ends (.svc.Repo.Install -> .svc.Repo.helper) with an lsp_* strategy, and asserts no edge to the decoy. - extract_defs.c: the receiver-qualified def QN is Go-only. The templ grammar is Go-derived and reaches the same receiver branch, but its call-scope QN (compute_func_qn) stays flat, so a templ method would get a def QN no call names and its calls would degrade to the File node. extract_templ_method_qn_matches_call_scope pins def QN == call scope. - pass_semantic.c: the check_go_class_implements comment no longer describes Go method QNs as flat. Refs DeusData#1909 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 9aeea8e commit 87cafb9

5 files changed

Lines changed: 99 additions & 7 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6696,8 +6696,13 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
66966696
* call-scope side (compute_func_qn in extract_unified.c) mirrors
66976697
* this formula, and go_lsp consumers read the def QN and
66986698
* parent_class (receiver_type) verbatim, so resolution joins
6699-
* stay exact. */
6700-
def.qualified_name = cbm_arena_sprintf(a, "%s.%s", def.parent_class, name);
6699+
* stay exact. Go only: templ's Go-derived grammar reaches this
6700+
* branch too, but its call-scope QN (compute_func_qn) stays flat,
6701+
* and a def QN the calls do not share would orphan every call
6702+
* inside a templ method. */
6703+
if (ctx->language == CBM_LANG_GO) {
6704+
def.qualified_name = cbm_arena_sprintf(a, "%s.%s", def.parent_class, name);
6705+
}
67016706
}
67026707
}
67036708

‎src/pipeline/pass_semantic.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ static int check_go_class_implements(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_nod
266266
}
267267
/* Resolve the struct's methods two ways and use whichever finds them:
268268
* (a) the struct's DEFINES_METHOD edges, matched by method NAME — the real
269-
* pipeline path, where Go receiver methods carry a flat QN (e.g.
270-
* "pkg.Area" rather than "pkg.Circle.Area"), so QN-string
271-
* reconstruction would miss; and
269+
* pipeline path. Go method QNs are receiver-qualified since #1913
270+
* ("pkg.Circle.Area"), so (b) matches them as well, but the edges
271+
* stay the primary join; and
272272
* (b) the reconstructed QN "<ClassQN>.<methodName>" — used by tests and any
273273
* extractor that does emit class-qualified method QNs without
274274
* DEFINES_METHOD edges from the class. */

‎src/store/store.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ typedef struct cbm_store_read_scope cbm_store_read_scope_t;
3232
* Dockerfile.<variant> keeps its suffix in its QNs.
3333
* 3 Image identity invents no registry: a reference without a host is
3434
* __image__<repository as written>, registry "unknown" -- no longer
35-
* __image__docker.io/[library/]<repository>. */
36-
#define CBM_INDEX_FORMAT_VERSION 3
35+
* __image__docker.io/[library/]<repository>.
36+
* 4 #1913: Go method QNs are receiver-qualified (pkg.Recv.method, generic
37+
* receivers unwrapped); free functions stay pkg.func. */
38+
#define CBM_INDEX_FORMAT_VERSION 4
3739
#define CBM_STORE_CANCELLED (-3)
3840
#define CBM_STORE_SCAN_LIMIT (-4)
3941
#define CBM_STORE_CALLBACK_ERR (-5)

‎tests/test_extraction.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7229,6 +7229,48 @@ TEST(extract_go_method_receiver_qualified_qn) {
72297229
PASS();
72307230
}
72317231

7232+
/* Templ shares Go's method grammar (method_declaration with a receiver), but
7233+
* only Go method QNs are receiver-qualified (#1913); templ's call-scope QN
7234+
* stays flat. The method's def QN and the enclosing-function QN of a call
7235+
* inside it must agree, or the call has no source node and degrades to the
7236+
* File node. */
7237+
TEST(extract_templ_method_qn_matches_call_scope) {
7238+
CBMFileResult *r = extract("package views\n"
7239+
"\n"
7240+
"type Page struct{}\n"
7241+
"\n"
7242+
"func (p Page) Title() string {\n"
7243+
"\treturn helper()\n"
7244+
"}\n"
7245+
"\n"
7246+
"func helper() string { return \"x\" }\n"
7247+
"\n"
7248+
"templ hello(name string) {\n"
7249+
"\t<div>{ name }</div>\n"
7250+
"}\n",
7251+
CBM_LANG_TEMPL, "t", "x.templ");
7252+
ASSERT_NOT_NULL(r);
7253+
const char *title_qn = NULL;
7254+
for (int i = 0; i < r->defs.count; i++) {
7255+
const CBMDefinition *d = &r->defs.items[i];
7256+
if (d->name && d->qualified_name && strcmp(d->name, "Title") == 0) {
7257+
title_qn = d->qualified_name;
7258+
}
7259+
}
7260+
const char *scope = NULL;
7261+
for (int i = 0; i < r->calls.count; i++) {
7262+
const CBMCall *c = &r->calls.items[i];
7263+
if (c->callee_name && strcmp(c->callee_name, "helper") == 0) {
7264+
scope = c->enclosing_func_qn;
7265+
}
7266+
}
7267+
ASSERT_NOT_NULL(title_qn);
7268+
ASSERT_NOT_NULL(scope);
7269+
ASSERT_STR_EQ(scope, title_qn);
7270+
cbm_free_result(r);
7271+
PASS();
7272+
}
7273+
72327274
/* TS/JS/TSX receiver-aware flag (#592/#606; same intent as the Perl flag above).
72337275
* A member call x.foo() with a non-this/super receiver is flagged is_method so
72347276
* the resolver can suppress a weak short-name match (`re.test()` must not bind a
@@ -12301,6 +12343,7 @@ SUITE(extraction) {
1230112343
RUN_TEST(extract_python_bare_call_flag_is_depth_independent);
1230212344
RUN_TEST(extract_go_selector_call_flags_is_method);
1230312345
RUN_TEST(extract_go_method_receiver_qualified_qn);
12346+
RUN_TEST(extract_templ_method_qn_matches_call_scope);
1230412347
RUN_TEST(extract_ts_member_call_flags_is_method);
1230512348
RUN_TEST(extract_ts_this_super_receiver_not_flagged);
1230612349
RUN_TEST(extract_js_member_call_flags_is_method);

‎tests/test_pipeline.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8650,6 +8650,41 @@ TEST(pipeline_html_embedded_member_call_stays_unbound) {
86508650
PASS();
86518651
}
86528652

8653+
/* CALLS edges whose source and target QNs end with the given suffixes and
8654+
* whose serialized properties contain `props_fragment` (NULL: any edge). QN
8655+
* suffixes pin the exact node, not just a same-named one. */
8656+
static int go_calls_between(cbm_store_t *s, const char *project, const char *caller_suffix,
8657+
const char *callee_suffix, const char *props_fragment) {
8658+
cbm_edge_t *edges = NULL;
8659+
int edge_count = 0;
8660+
cbm_store_find_edges_by_type(s, project, "CALLS", &edges, &edge_count);
8661+
size_t cs = strlen(caller_suffix);
8662+
size_t ts = strlen(callee_suffix);
8663+
int hits = 0;
8664+
for (int i = 0; i < edge_count; i++) {
8665+
cbm_node_t src = {0};
8666+
cbm_node_t tgt = {0};
8667+
if (cbm_store_find_node_by_id(s, edges[i].source_id, &src) == CBM_STORE_OK &&
8668+
cbm_store_find_node_by_id(s, edges[i].target_id, &tgt) == CBM_STORE_OK &&
8669+
src.qualified_name && tgt.qualified_name) {
8670+
size_t sl = strlen(src.qualified_name);
8671+
size_t tl = strlen(tgt.qualified_name);
8672+
if (sl >= cs && tl >= ts && strcmp(src.qualified_name + sl - cs, caller_suffix) == 0 &&
8673+
strcmp(tgt.qualified_name + tl - ts, callee_suffix) == 0 &&
8674+
(!props_fragment ||
8675+
(edges[i].properties_json && strstr(edges[i].properties_json, props_fragment)))) {
8676+
hits++;
8677+
}
8678+
}
8679+
cbm_node_free_fields(&src);
8680+
cbm_node_free_fields(&tgt);
8681+
}
8682+
if (edges) {
8683+
cbm_store_free_edges(edges, edge_count);
8684+
}
8685+
return hits;
8686+
}
8687+
86538688
TEST(pipeline_go_method_caller_keeps_lsp_join) {
86548689
/* #1909: receiver-qualifying method QNs moves the def/textual-call QN to
86558690
* package.Type.method — go_lsp's enclosing-function QN must move with it
@@ -8695,6 +8730,13 @@ TEST(pipeline_go_method_caller_keeps_lsp_join) {
86958730
cbm_store_t *s = cbm_store_open_path(db_path);
86968731
ASSERT_NOT_NULL(s);
86978732
ASSERT_TRUE(cross_file_call_exists(s, project, "Install", "helper"));
8733+
/* Pinned to the receiver-qualified target in svc/helper.go and to an LSP
8734+
* strategy: a name-only binding, or one onto other.Decoy.helper, would
8735+
* still satisfy the by-name check above. */
8736+
ASSERT_GTE(go_calls_between(s, project, ".svc.Repo.Install", ".svc.Repo.helper",
8737+
"\"strategy\":\"lsp_"),
8738+
1);
8739+
ASSERT_EQ(go_calls_between(s, project, ".svc.Repo.Install", ".other.Decoy.helper", NULL), 0);
86988740
cbm_store_close(s);
86998741
cbm_pipeline_free(p);
87008742
th_rmtree(tmp);

0 commit comments

Comments
 (0)