Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Merge origin/main into fix/go-field-type-hint-owner-segment

Martin Vogel257821805cfb
diff --cc tests/test_pipeline.c
index fce5c0986,057c43808..722f00169
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@@ -11071,221 -11071,201 +11071,416 @@@ TEST(pipeline_go_init_lsp_edges_per_fil
PASS();
}
+ /* Fixture for the bare-`init` probes (sequential and parallel twins), the
+ * shapes the kubernetes census found after #1915: a local closure `init :=
+ * func(ctx)` that is called, written and passed by value (the e2e storage
+ * suites), a parameter named init (`func sum(init func() ref.Val)`), and a
+ * `package init` directory. The decoys are a package init in the caller's
+ * own package (what same_module bound before #1915) and an unrelated
+ * Watcher.init method (what the name fallback binds after it). Watcher.Start
+ * and Boot call that method through a selector, the control that must keep
+ * resolving. pad_files > 0 adds filler Go files past MIN_FILES_FOR_PARALLEL. */
+ static void write_go_bare_init_fixture(const char *tmp, int pad_files) {
+ write_temp_file(tmp, "go.mod", "module example.com/fxbareinit\n\ngo 1.22\n");
+ write_temp_file(tmp, "suites/suite.go",
+ "package suites\n"
+ "\n"
+ "import \"context\"\n"
+ "\n"
+ "var registered []string\n"
+ "\n"
+ "func init() { registered = append(registered, \"volumes\") }\n"
+ "\n"
+ "func wrap(fn func(context.Context)) { _ = fn }\n"
+ "\n"
+ "func DefineTests(ctx context.Context) {\n"
+ "\tinit := func(ctx context.Context) {\n"
+ "\t\t_ = ctx\n"
+ "\t}\n"
+ "\tinit(ctx)\n"
+ "\twrap(init)\n"
+ "}\n"
+ "\n"
+ "func Sum(init func() int) int {\n"
+ "\treturn init()\n"
+ "}\n");
+ write_temp_file(tmp, "watcher/watcher.go",
+ "package watcher\n"
+ "\n"
+ "type Watcher struct{ ready bool }\n"
+ "\n"
+ "func NewWatcher() *Watcher { return &Watcher{} }\n"
+ "\n"
+ "func (w *Watcher) init() { w.ready = true }\n"
+ "\n"
+ "func (w *Watcher) Start() {\n"
+ "\tw.init()\n"
+ "}\n"
+ "\n"
+ "func Boot() {\n"
+ "\tw := NewWatcher()\n"
+ "\tw.init()\n"
+ "}\n");
+ write_temp_file(tmp, "boot/init/init.go",
+ "// Package init installs defaults for its importers.\n"
+ "package init\n"
+ "\n"
+ "func init() { setup() }\n"
+ "\n"
+ "func setup() {}\n");
+ for (int i = 0; i < pad_files; i++) {
+ char name[64];
+ char body[128];
+ snprintf(name, sizeof(name), "pad/filler%d.go", i);
+ snprintf(body, sizeof(body), "package pad\n\nfunc filler%d() int { return %d }\n", i, i);
+ write_temp_file(tmp, name, body);
+ }
+ }
+
+ static bool bare_init_qn_ends_with(const char *qn, const char *suffix) {
+ size_t ql = strlen(qn);
+ size_t sl = strlen(suffix);
+ return ql >= sl && strcmp(qn + ql - sl, suffix) == 0;
+ }
+
+ /* Resolver edges into any node named `init` whose source is not one of the
+ * two selector controls. Every such edge in the fixture binds a bare `init`,
+ * which in Go is always a local name. */
+ static int go_bare_init_false_edges(cbm_store_t *s, const char *project) {
+ static const char *const types[] = {"CALLS", "USAGE", "WRITES", "READS", "CALL_REFERENCE"};
+ int hits = 0;
+ for (size_t t = 0; t < sizeof(types) / sizeof(types[0]); t++) {
+ cbm_edge_t *edges = NULL;
+ int edge_count = 0;
+ cbm_store_find_edges_by_type(s, project, types[t], &edges, &edge_count);
+ for (int i = 0; i < edge_count; i++) {
+ cbm_node_t src = {0};
+ cbm_node_t tgt = {0};
+ if (cbm_store_find_node_by_id(s, edges[i].source_id, &src) == CBM_STORE_OK &&
+ cbm_store_find_node_by_id(s, edges[i].target_id, &tgt) == CBM_STORE_OK &&
+ src.qualified_name && tgt.name && strcmp(tgt.name, "init") == 0 &&
+ !bare_init_qn_ends_with(src.qualified_name, ".watcher.Watcher.Start") &&
+ !bare_init_qn_ends_with(src.qualified_name, ".watcher.Boot")) {
+ printf(" bare init bound: %s %s -> %s\n", types[t], src.qualified_name,
+ tgt.qualified_name ? tgt.qualified_name : "?");
+ hits++;
+ }
+ cbm_node_free_fields(&src);
+ cbm_node_free_fields(&tgt);
+ }
+ if (edges) {
+ cbm_store_free_edges(edges, edge_count);
+ }
+ }
+ return hits;
+ }
+
+ static int assert_go_bare_init_edges(cbm_store_t *s, const char *project) {
+ /* The decoys exist, so a zero below is a refused binding and not an
+ * absent target: two package inits and the Watcher method. */
+ cbm_node_t *inits = NULL;
+ int ic = 0;
+ cbm_store_find_nodes_by_name(s, project, "init", &inits, &ic);
+ int functions = 0;
+ int methods = 0;
+ for (int i = 0; i < ic; i++) {
+ functions += inits[i].label && strcmp(inits[i].label, "Function") == 0;
+ methods += inits[i].label && strcmp(inits[i].label, "Method") == 0;
+ }
+ cbm_store_free_nodes(inits, ic);
+ ASSERT_EQ(functions, 2);
+ ASSERT_EQ(methods, 1);
+ /* NEGATIVE: no bare `init` (closure call, closure write, closure passed
+ * by value, parameter call, `package init`) binds a package init or the
+ * unrelated method. */
+ ASSERT_EQ(go_bare_init_false_edges(s, project), 0);
+ /* CONTROL: a selector call `w.init()` is not bare and still resolves to
+ * the method, from a method body and from a free function. */
+ ASSERT_GTE(
+ go_calls_between(s, project, ".watcher.Watcher.Start", ".watcher.Watcher.init", NULL), 1);
+ ASSERT_GTE(go_calls_between(s, project, ".watcher.Boot", ".watcher.Watcher.init", NULL), 1);
+ return 0;
+ }
+
+ /* R45 (follow-up to #1915): a bare Go `init` never binds a package init or,
+ * by name alone, an init method (cbm_go_suppress_bare_init_bind). */
+ TEST(pipeline_go_bare_init_binds_nothing) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_bare_init_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_go_bare_init_fixture(tmp, 0);
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/go_bare_init.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ const char *project = cbm_pipeline_project_name(p);
+
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+ ASSERT_EQ(assert_go_bare_init_edges(s, project), 0);
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmp);
+ PASS();
+ }
+
+ /* Parallel twin: >= MIN_FILES_FOR_PARALLEL (50) files plus CBM_WORKERS=4
+ * take the parallel resolver, which carries its own copy of each guard. */
+ TEST(pipeline_go_bare_init_binds_nothing_parallel) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_bare_init_par_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_go_bare_init_fixture(tmp, 52);
+
+ char *old_workers = getenv("CBM_WORKERS");
+ char *saved = old_workers ? strdup(old_workers) : NULL;
+ cbm_setenv("CBM_WORKERS", "4", 1);
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/go_bare_init_par.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ const char *project = cbm_pipeline_project_name(p);
+
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+ ASSERT_EQ(assert_go_bare_init_edges(s, project), 0);
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ if (saved) {
+ cbm_setenv("CBM_WORKERS", saved, 1);
+ free(saved);
+ } else {
+ cbm_unsetenv("CBM_WORKERS");
+ }
+ th_rmtree(tmp);
+ PASS();
+ }
+
+TEST(pipeline_go_parallel_field_hint_requires_owner_segment) {
+ /* #1927: try_field_type_hint (parallel resolver only) matched the hinted
+ * type name as a raw SUBSTRING of the candidate QN. A single-letter
+ * receiver — `f.Close()` on an *os.File — hints "F", and "F" is a
+ * substring of "…FileStore.Close", so a stdlib call was rebound to an
+ * arbitrary project method at PP_FIELD_HINT_CONF and presented as
+ * field_type_hint (the one weak strategy the Go selector guard trusts).
+ * The hint must fire only when the candidate's OWNING segment — the
+ * dot-segment immediately before the method — EQUALS the hinted type;
+ * otherwise the resolution stays a weak short-name match and the Go
+ * guard (#1906) drops it. >= 50 files forces pass_parallel.c, the only
+ * path that runs try_field_type_hint(). */
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_fth_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+
+ write_temp_file(tmp, "go.mod", "module example.com/fxhint\n\ngo 1.22\n");
+ /* Two project methods named Close → the registry resolves f.Close() as an
+ * ambiguous short-name match (candidate_count 2), the shape the hint
+ * upgrades. FileStore's QN carries the capital F the buggy substring
+ * latches onto; Conn is the second candidate. */
+ write_temp_file(tmp, "filestore/filestore.go",
+ "package filestore\n"
+ "\n"
+ "type FileStore struct{ open bool }\n"
+ "\n"
+ "func (fs *FileStore) Close() {\n"
+ "\tfs.open = false\n"
+ "}\n");
+ write_temp_file(tmp, "conn/conn.go",
+ "package conn\n"
+ "\n"
+ "type Conn struct{ live bool }\n"
+ "\n"
+ "func (cn *Conn) Close() {\n"
+ "\tcn.live = false\n"
+ "}\n");
+ /* Two project methods named Find → same ambiguity, but here the receiver
+ * variable IS named after its type (`finder`), the case the hint exists
+ * for: the owning segment of Finder.Find equals the hinted "Finder". */
+ write_temp_file(tmp, "finder/finder.go",
+ "package finder\n"
+ "\n"
+ "type Finder struct{ n int }\n"
+ "\n"
+ "func NewFinder() *Finder { return &Finder{n: 1} }\n"
+ "\n"
+ "func (fi *Finder) Find(id int) int {\n"
+ "\treturn fi.n + id\n"
+ "}\n");
+ write_temp_file(tmp, "locator/locator.go",
+ "package locator\n"
+ "\n"
+ "type Locator struct{ n int }\n"
+ "\n"
+ "func (lo *Locator) Find(id int) int {\n"
+ "\treturn lo.n - id\n"
+ "}\n");
+ write_temp_file(tmp, "app/app.go",
+ "package app\n"
+ "\n"
+ "import (\n"
+ "\t\"os\"\n"
+ "\n"
+ "\txf \"example.com/fxhint/finder\"\n"
+ ")\n"
+ "\n"
+ "func Lookup(path string) int64 {\n"
+ "\tf, err := os.Open(path)\n"
+ "\tif err != nil {\n"
+ "\t\treturn 0\n"
+ "\t}\n"
+ "\tdefer f.Close()\n"
+ "\tst, err := f.Stat()\n"
+ "\tif err != nil {\n"
+ "\t\treturn 0\n"
+ "\t}\n"
+ "\treturn st.Size()\n"
+ "}\n"
+ "\n"
+ "func UseFinder(id int) int {\n"
+ "\tfinder := xf.NewFinder()\n"
+ "\treturn finder.Find(id)\n"
+ "}\n");
+ for (int i = 0; i < 52; i++) {
+ char name[64];
+ char body[128];
+ snprintf(name, sizeof(name), "pad/filler%d.go", i);
+ snprintf(body, sizeof(body), "package pad\n\nfunc filler%d() int { return %d }\n", i, i);
+ write_temp_file(tmp, name, body);
+ }
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/go_fth.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ const char *project = cbm_pipeline_project_name(p);
+
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+
+ /* (1) Reproduce-first: RED before the fix. The substring hint rebinds the
+ * stdlib f.Close() to FileStore.Close ("F" ⊂ QN) at high confidence and
+ * the Go guard keeps it. With segment equality the upgrade is refused,
+ * the match stays suffix_match, and the Go guard drops it. */
+ ASSERT_FALSE(cross_file_call_exists(s, project, "Lookup", "Close"));
+ /* (2) The intended hint case survives: `finder` names its type, so the
+ * owning segment of Finder.Find equals the hint. */
+ ASSERT_TRUE(cross_file_call_exists(s, project, "UseFinder", "Find"));
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmp);
+ PASS();
+}
+
+static int count_calls_with(cbm_store_t *s, const char *project, const char *caller_suffix,
+ const char *callee_suffix, const char *fragment);
+
+TEST(pipeline_csharp_parallel_field_hint_requires_owner_segment) {
+ /* #1927, non-Go twin. Go no longer runs try_field_type_hint (e554a2ab),
+ * but every other language on the parallel path still does, and C# is the
+ * hint's intended consumer (it strips the _ / m_ field prefixes). C#
+ * method names are PascalCase, so a one-letter receiver hints a letter
+ * that every candidate QN already contains in the METHOD name: `w.Write()`
+ * hints "W", and a raw substring test accepts "...FileStore.Write" and
+ * "...Conn.Write" alike, whichever the registry lists first. The hint must
+ * bind only a candidate whose OWNING segment equals the hinted type.
+ * >= 50 files forces pass_parallel.c, the only path with the hint. */
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_cs_fth_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+
+ /* Two project methods named Write: an untyped receiver resolves as an
+ * ambiguous short-name match (candidate_count 2), the shape the hint
+ * upgrades. */
+ write_temp_file(tmp, "Storage/FileStore.cs",
+ "namespace Storage {\n"
+ " public class FileStore {\n"
+ " private int written;\n"
+ " public void Write(int v) { written += v; }\n"
+ " public void Put(int v) { written = v; }\n"
+ " }\n"
+ "}\n");
+ write_temp_file(tmp, "Net/Conn.cs",
+ "namespace Net {\n"
+ " public class Conn {\n"
+ " private int sent;\n"
+ " public void Write(int v) { sent += v; }\n"
+ " }\n"
+ "}\n");
+ /* Two project methods named Put, one owned by Store: the receiver `store`
+ * names its type, the case the hint exists for. FileStore's QN also
+ * contains "Store", so only segment equality picks Store.Put whatever
+ * the candidate order. */
+ write_temp_file(tmp, "Repo/Store.cs",
+ "namespace Repo {\n"
+ " public class Store {\n"
+ " private int held;\n"
+ " public void Put(int v) { held = v; }\n"
+ " }\n"
+ "}\n");
+ write_temp_file(tmp, "App/Worker.cs",
+ "namespace App {\n"
+ " public class Worker {\n"
+ " public void Log(System.IO.StreamWriter w) {\n"
+ " w.Write(1);\n"
+ " }\n"
+ " public void Keep() {\n"
+ " var store = Services.Resolve();\n"
+ " store.Put(2);\n"
+ " }\n"
+ " }\n"
+ "}\n");
+ for (int i = 0; i < 52; i++) {
+ char name[64];
+ char body[192];
+ snprintf(name, sizeof(name), "Pad/Filler%d.cs", i);
+ snprintf(body, sizeof(body),
+ "namespace Pad {\n public class Filler%d {\n"
+ " public int Get%d() { return %d; }\n }\n}\n",
+ i, i, i);
+ write_temp_file(tmp, name, body);
+ }
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/cs_fth.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+ const char *project = cbm_pipeline_project_name(p);
+
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+
+ /* (1) Reproduce-first: RED before the fix. "W" is a substring of every
+ * Write candidate, so the old hint upgraded the StreamWriter call to an
+ * arbitrary project Write at PP_FIELD_HINT_CONF. No owner segment is "W",
+ * so the fixed hint must not fire at all. */
+ ASSERT_FALSE(cross_file_call_has_strategy(s, project, "Log", "Write", "field_type_hint"));
+ /* (2) The intended hint case binds the owner it names, never FileStore. */
+ ASSERT_EQ(count_calls_with(s, project, ".Keep", ".Store.Put", "field_type_hint"), 1);
+ ASSERT_EQ(count_calls_with(s, project, ".Keep", ".FileStore.Put", NULL), 0);
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmp);
+ PASS();
+}
+
/* Fixture for the #1928 cross-language reference-guard probes (sequential and
* parallel twins). pad_files > 0 adds filler files to push the index over the
* parallel-pipeline threshold, since USAGE/WRITES/READS have one resolver per