@@ -11071,6 +11071,201 @@ TEST(pipeline_go_init_lsp_edges_per_file_parallel) {
1107111071 PASS();
1107211072}
1107311073
11074+ /* Fixture for the bare-`init` probes (sequential and parallel twins), the
11075+ * shapes the kubernetes census found after #1915: a local closure `init :=
11076+ * func(ctx)` that is called, written and passed by value (the e2e storage
11077+ * suites), a parameter named init (`func sum(init func() ref.Val)`), and a
11078+ * `package init` directory. The decoys are a package init in the caller's
11079+ * own package (what same_module bound before #1915) and an unrelated
11080+ * Watcher.init method (what the name fallback binds after it). Watcher.Start
11081+ * and Boot call that method through a selector, the control that must keep
11082+ * resolving. pad_files > 0 adds filler Go files past MIN_FILES_FOR_PARALLEL. */
11083+ static void write_go_bare_init_fixture(const char *tmp, int pad_files) {
11084+ write_temp_file(tmp, "go.mod", "module example.com/fxbareinit\n\ngo 1.22\n");
11085+ write_temp_file(tmp, "suites/suite.go",
11086+ "package suites\n"
11087+ "\n"
11088+ "import \"context\"\n"
11089+ "\n"
11090+ "var registered []string\n"
11091+ "\n"
11092+ "func init() { registered = append(registered, \"volumes\") }\n"
11093+ "\n"
11094+ "func wrap(fn func(context.Context)) { _ = fn }\n"
11095+ "\n"
11096+ "func DefineTests(ctx context.Context) {\n"
11097+ "\tinit := func(ctx context.Context) {\n"
11098+ "\t\t_ = ctx\n"
11099+ "\t}\n"
11100+ "\tinit(ctx)\n"
11101+ "\twrap(init)\n"
11102+ "}\n"
11103+ "\n"
11104+ "func Sum(init func() int) int {\n"
11105+ "\treturn init()\n"
11106+ "}\n");
11107+ write_temp_file(tmp, "watcher/watcher.go",
11108+ "package watcher\n"
11109+ "\n"
11110+ "type Watcher struct{ ready bool }\n"
11111+ "\n"
11112+ "func NewWatcher() *Watcher { return &Watcher{} }\n"
11113+ "\n"
11114+ "func (w *Watcher) init() { w.ready = true }\n"
11115+ "\n"
11116+ "func (w *Watcher) Start() {\n"
11117+ "\tw.init()\n"
11118+ "}\n"
11119+ "\n"
11120+ "func Boot() {\n"
11121+ "\tw := NewWatcher()\n"
11122+ "\tw.init()\n"
11123+ "}\n");
11124+ write_temp_file(tmp, "boot/init/init.go",
11125+ "// Package init installs defaults for its importers.\n"
11126+ "package init\n"
11127+ "\n"
11128+ "func init() { setup() }\n"
11129+ "\n"
11130+ "func setup() {}\n");
11131+ for (int i = 0; i < pad_files; i++) {
11132+ char name[64];
11133+ char body[128];
11134+ snprintf(name, sizeof(name), "pad/filler%d.go", i);
11135+ snprintf(body, sizeof(body), "package pad\n\nfunc filler%d() int { return %d }\n", i, i);
11136+ write_temp_file(tmp, name, body);
11137+ }
11138+ }
11139+
11140+ static bool bare_init_qn_ends_with(const char *qn, const char *suffix) {
11141+ size_t ql = strlen(qn);
11142+ size_t sl = strlen(suffix);
11143+ return ql >= sl && strcmp(qn + ql - sl, suffix) == 0;
11144+ }
11145+
11146+ /* Resolver edges into any node named `init` whose source is not one of the
11147+ * two selector controls. Every such edge in the fixture binds a bare `init`,
11148+ * which in Go is always a local name. */
11149+ static int go_bare_init_false_edges(cbm_store_t *s, const char *project) {
11150+ static const char *const types[] = {"CALLS", "USAGE", "WRITES", "READS", "CALL_REFERENCE"};
11151+ int hits = 0;
11152+ for (size_t t = 0; t < sizeof(types) / sizeof(types[0]); t++) {
11153+ cbm_edge_t *edges = NULL;
11154+ int edge_count = 0;
11155+ cbm_store_find_edges_by_type(s, project, types[t], &edges, &edge_count);
11156+ for (int i = 0; i < edge_count; i++) {
11157+ cbm_node_t src = {0};
11158+ cbm_node_t tgt = {0};
11159+ if (cbm_store_find_node_by_id(s, edges[i].source_id, &src) == CBM_STORE_OK &&
11160+ cbm_store_find_node_by_id(s, edges[i].target_id, &tgt) == CBM_STORE_OK &&
11161+ src.qualified_name && tgt.name && strcmp(tgt.name, "init") == 0 &&
11162+ !bare_init_qn_ends_with(src.qualified_name, ".watcher.Watcher.Start") &&
11163+ !bare_init_qn_ends_with(src.qualified_name, ".watcher.Boot")) {
11164+ printf(" bare init bound: %s %s -> %s\n", types[t], src.qualified_name,
11165+ tgt.qualified_name ? tgt.qualified_name : "?");
11166+ hits++;
11167+ }
11168+ cbm_node_free_fields(&src);
11169+ cbm_node_free_fields(&tgt);
11170+ }
11171+ if (edges) {
11172+ cbm_store_free_edges(edges, edge_count);
11173+ }
11174+ }
11175+ return hits;
11176+ }
11177+
11178+ static int assert_go_bare_init_edges(cbm_store_t *s, const char *project) {
11179+ /* The decoys exist, so a zero below is a refused binding and not an
11180+ * absent target: two package inits and the Watcher method. */
11181+ cbm_node_t *inits = NULL;
11182+ int ic = 0;
11183+ cbm_store_find_nodes_by_name(s, project, "init", &inits, &ic);
11184+ int functions = 0;
11185+ int methods = 0;
11186+ for (int i = 0; i < ic; i++) {
11187+ functions += inits[i].label && strcmp(inits[i].label, "Function") == 0;
11188+ methods += inits[i].label && strcmp(inits[i].label, "Method") == 0;
11189+ }
11190+ cbm_store_free_nodes(inits, ic);
11191+ ASSERT_EQ(functions, 2);
11192+ ASSERT_EQ(methods, 1);
11193+ /* NEGATIVE: no bare `init` (closure call, closure write, closure passed
11194+ * by value, parameter call, `package init`) binds a package init or the
11195+ * unrelated method. */
11196+ ASSERT_EQ(go_bare_init_false_edges(s, project), 0);
11197+ /* CONTROL: a selector call `w.init()` is not bare and still resolves to
11198+ * the method, from a method body and from a free function. */
11199+ ASSERT_GTE(
11200+ go_calls_between(s, project, ".watcher.Watcher.Start", ".watcher.Watcher.init", NULL), 1);
11201+ ASSERT_GTE(go_calls_between(s, project, ".watcher.Boot", ".watcher.Watcher.init", NULL), 1);
11202+ return 0;
11203+ }
11204+
11205+ /* R45 (follow-up to #1915): a bare Go `init` never binds a package init or,
11206+ * by name alone, an init method (cbm_go_suppress_bare_init_bind). */
11207+ TEST(pipeline_go_bare_init_binds_nothing) {
11208+ char tmp[256];
11209+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_bare_init_XXXXXX");
11210+ if (!cbm_mkdtemp(tmp)) {
11211+ FAIL("tmpdir");
11212+ }
11213+ write_go_bare_init_fixture(tmp, 0);
11214+
11215+ char db_path[512];
11216+ snprintf(db_path, sizeof(db_path), "%s/go_bare_init.db", tmp);
11217+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
11218+ ASSERT_NOT_NULL(p);
11219+ ASSERT_EQ(cbm_pipeline_run(p), 0);
11220+ const char *project = cbm_pipeline_project_name(p);
11221+
11222+ cbm_store_t *s = cbm_store_open_path(db_path);
11223+ ASSERT_NOT_NULL(s);
11224+ ASSERT_EQ(assert_go_bare_init_edges(s, project), 0);
11225+
11226+ cbm_store_close(s);
11227+ cbm_pipeline_free(p);
11228+ th_rmtree(tmp);
11229+ PASS();
11230+ }
11231+
11232+ /* Parallel twin: >= MIN_FILES_FOR_PARALLEL (50) files plus CBM_WORKERS=4
11233+ * take the parallel resolver, which carries its own copy of each guard. */
11234+ TEST(pipeline_go_bare_init_binds_nothing_parallel) {
11235+ char tmp[256];
11236+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_go_bare_init_par_XXXXXX");
11237+ if (!cbm_mkdtemp(tmp)) {
11238+ FAIL("tmpdir");
11239+ }
11240+ write_go_bare_init_fixture(tmp, 52);
11241+
11242+ char *old_workers = getenv("CBM_WORKERS");
11243+ char *saved = old_workers ? strdup(old_workers) : NULL;
11244+ cbm_setenv("CBM_WORKERS", "4", 1);
11245+
11246+ char db_path[512];
11247+ snprintf(db_path, sizeof(db_path), "%s/go_bare_init_par.db", tmp);
11248+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
11249+ ASSERT_NOT_NULL(p);
11250+ ASSERT_EQ(cbm_pipeline_run(p), 0);
11251+ const char *project = cbm_pipeline_project_name(p);
11252+
11253+ cbm_store_t *s = cbm_store_open_path(db_path);
11254+ ASSERT_NOT_NULL(s);
11255+ ASSERT_EQ(assert_go_bare_init_edges(s, project), 0);
11256+
11257+ cbm_store_close(s);
11258+ cbm_pipeline_free(p);
11259+ if (saved) {
11260+ cbm_setenv("CBM_WORKERS", saved, 1);
11261+ free(saved);
11262+ } else {
11263+ cbm_unsetenv("CBM_WORKERS");
11264+ }
11265+ th_rmtree(tmp);
11266+ PASS();
11267+ }
11268+
1107411269TEST(pipeline_go_parallel_field_hint_requires_owner_segment) {
1107511270 /* #1927: try_field_type_hint (parallel resolver only) matched the hinted
1107611271 * type name as a raw SUBSTRING of the candidate QN. A single-letter
@@ -30040,6 +30235,8 @@ SUITE(pipeline) {
3004030235 RUN_TEST(pipeline_go_multi_init_nodes_survive);
3004130236 RUN_TEST(pipeline_go_init_lsp_edges_per_file);
3004230237 RUN_TEST(pipeline_go_init_lsp_edges_per_file_parallel);
30238+ RUN_TEST(pipeline_go_bare_init_binds_nothing);
30239+ RUN_TEST(pipeline_go_bare_init_binds_nothing_parallel);
3004330240 RUN_TEST(pipeline_go_crossfile_type_alias_method);
3004430241 RUN_TEST(pipeline_go_crossfile_type_alias_method_parallel);
3004530242 RUN_TEST(pipeline_go_rw_usage_never_cross_into_c);
0 commit comments