Skip to content

Commit 4a78fe6

Browse files
committed
fix(go): a bare init never binds to a package init or an init method
Go gives `init` no binding in the package block: init functions cannot be referred to from anywhere in a program, and a method or field named init needs a selector. A bare `init` is therefore always a local name - a closure (`init := func(ctx) {...}; init(ctx)`), a parameter, or the `package init` clause of a directory named init. Before DeusData#1915 same_module bound such a name to the flat package init. With per-file init QNs it now falls through to the short-name fallback and binds unrelated methods named init. On kubernetes that minted 24 CALLS (22 e2e storage DefineTests closures among them), 28 WRITES, 7 USAGE and one derived TESTS edge, all false. cbm_go_suppress_bare_init_bind refuses, for a bare Go `init`, any registry binding onto a Function (every Go Function named init is a package init) and a weak short-name binding onto a Method or Field. lsp_* answers and selector calls (`w.init()`) are untouched. Wired at the CALLS, USAGE and READS/WRITES seams of both resolvers. Kubernetes, full index, main vs branch: only those 60 edges removed, nodes identical, no resolver edge added; the 14 LSP-typed selector CALLS into init methods are unchanged. Tests: pipeline_go_bare_init_binds_nothing (+ parallel twin) are RED with the predicate disabled (closure CALLS, parameter CALLS, closure WRITES, package-clause USAGE onto Watcher.init) and GREEN with it; the selector control keeps Watcher.Start/Boot -> Watcher.init. go_bare_init_never_binds_init_namesake pins the predicate. Refs DeusData#1915 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 9faa3e5 commit 4a78fe6

7 files changed

Lines changed: 327 additions & 0 deletions

File tree

‎src/pipeline/pass_calls.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,14 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
13441344
if (cbm_java_suppress_call_to_data_member(lang == CBM_LANG_JAVA, target_node->label)) {
13451345
return 0;
13461346
}
1347+
/* A bare Go `init(...)` calls a local closure or parameter: never a
1348+
* package init (unreferable in Go) and never a same-named method found
1349+
* by name alone — see cbm_go_suppress_bare_init_bind. MUST match
1350+
* pass_parallel.c exactly. */
1351+
if (cbm_go_suppress_bare_init_bind(lang == CBM_LANG_GO, call->is_method, call->callee_name,
1352+
res.strategy, target_node->label)) {
1353+
return 0;
1354+
}
13471355
evidence->candidate_qn = target_node->qualified_name;
13481356
evidence->calls_emitted |=
13491357
emit_classified_edge(ctx, call, source_node, target_node, &res, module_qn, imp_keys,

‎src/pipeline/pass_parallel.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3629,6 +3629,13 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
36293629
* Variable or a Field. */
36303630
continue;
36313631
}
3632+
if (target_node && source_node->id != target_node->id &&
3633+
cbm_go_suppress_bare_init_bind(lang == CBM_LANG_GO, call->is_method, call->callee_name,
3634+
res.strategy, target_node->label)) {
3635+
/* Same guard as pass_calls.c — a bare Go `init` is a local name,
3636+
* never a package init or a method found by name alone. */
3637+
continue;
3638+
}
36323639
if (!target_node || source_node->id == target_node->id) {
36333640
/* HTTP/ASYNC calls to an EXTERNAL client library (`requests.get(url)`)
36343641
* resolve to an unindexed QN (target_node == NULL), but their edge
@@ -3805,6 +3812,12 @@ static void resolve_file_usages(resolve_ctx_t *rc, resolve_worker_state_t *ws,
38053812
if (tgt && cbm_suppress_bare_field_ref(lang, usage->is_member_access, tgt->label)) {
38063813
continue;
38073814
}
3815+
/* A bare Go `init` names a local (cbm_go_suppress_bare_init_bind).
3816+
* Mirrors the sequential twin (pass_usages.c). */
3817+
if (tgt && cbm_go_suppress_bare_init_bind(lang == CBM_LANG_GO, usage->is_member_access,
3818+
usage->ref_name, res.strategy, tgt->label)) {
3819+
continue;
3820+
}
38083821
if (usage->semantic_reference_blocked && (usage->semantic_reference_local_shadow ||
38093822
cbm_pipeline_node_is_callable_target(tgt))) {
38103823
continue;
@@ -3933,6 +3946,12 @@ static void resolve_file_rw(resolve_ctx_t *rc, resolve_worker_state_t *ws, CBMFi
39333946
if (cbm_suppress_bare_field_ref(lang, rw->is_member_access, tgt->label)) {
39343947
continue;
39353948
}
3949+
/* A bare Go `init` names a local (cbm_go_suppress_bare_init_bind).
3950+
* Mirrors the sequential twin (pass_usages.c). */
3951+
if (cbm_go_suppress_bare_init_bind(lang == CBM_LANG_GO, rw->is_member_access, rw->var_name,
3952+
res.strategy, tgt->label)) {
3953+
continue;
3954+
}
39363955
const char *etype = rw->is_write ? "WRITES" : "READS";
39373956
cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, tgt->id, etype, "{}");
39383957
}

‎src/pipeline/pass_usages.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ static int resolve_usage_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *res
288288
if (tgt && cbm_suppress_bare_field_ref(lang, usage->is_member_access, tgt->label)) {
289289
continue;
290290
}
291+
/* A bare Go `init` names a local, never a package init or an init
292+
* method (cbm_go_suppress_bare_init_bind). Mirrors pass_parallel.c. */
293+
if (tgt && cbm_go_suppress_bare_init_bind(lang == CBM_LANG_GO, usage->is_member_access,
294+
usage->ref_name, res.strategy, tgt->label)) {
295+
continue;
296+
}
291297
if (usage->semantic_reference_blocked && (usage->semantic_reference_local_shadow ||
292298
cbm_pipeline_node_is_callable_target(tgt))) {
293299
continue;
@@ -420,6 +426,13 @@ static int resolve_rw_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *result
420426
if (cbm_suppress_bare_field_ref(lang, rw->is_member_access, tgt->label)) {
421427
continue;
422428
}
429+
/* `init := func(...)`: a bare Go `init` names a local, never a package
430+
* init or an init method (cbm_go_suppress_bare_init_bind). Mirrors
431+
* pass_parallel.c. */
432+
if (cbm_go_suppress_bare_init_bind(lang == CBM_LANG_GO, rw->is_member_access, rw->var_name,
433+
res.strategy, tgt->label)) {
434+
continue;
435+
}
423436

424437
const char *edge_type = rw->is_write ? "WRITES" : "READS";
425438
cbm_gbuf_insert_edge(ctx->gbuf, src->id, tgt->id, edge_type, "{}");

‎src/pipeline/pipeline.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,16 @@ bool cbm_suppress_external_import_shadow(const char *callee_name, const char *st
459459
bool cbm_go_suppress_weak_method_match(bool is_go, bool is_method, const char *strategy,
460460
double confidence);
461461

462+
/* A bare Go `init` (a call or reference that is not the member half of a
463+
* selector) is always a local name: Go forbids referring to a package init
464+
* function, and a method or field needs a selector. True when such a name
465+
* resolved onto a Function (every Go Function named init is a package init;
466+
* any registry strategy) or onto a Method or Field found by a weak short-name
467+
* strategy. lsp_* answers are kept. Go only. Pure; unit-tested in
468+
* test_registry.c; wired in pass_calls.c, pass_usages.c and pass_parallel.c. */
469+
bool cbm_go_suppress_bare_init_bind(bool is_go, bool is_member, const char *name,
470+
const char *strategy, const char *target_label);
471+
462472
/* Import-binding counterpart (#2127). True when the callee's root identifier
463473
* is bound by an import of this file whose module chain contradicts the
464474
* resolved target (`from unittest.mock import patch; patch()` must not bind a

‎src/pipeline/registry.c‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,46 @@ bool cbm_go_suppress_weak_method_match(bool is_go, bool is_method, const char *s
10151015
return confidence < CONF_UNIQUE_NAME;
10161016
}
10171017

1018+
bool cbm_go_suppress_bare_init_bind(bool is_go, bool is_member, const char *name,
1019+
const char *strategy, const char *target_label) {
1020+
/* The Go spec gives `init` no binding in the package block: the
1021+
* identifier declares init functions and nothing else, and "init
1022+
* functions cannot be referred to from anywhere in a program". A method
1023+
* or a field named init is reached only through a selector (`x.init()`,
1024+
* `e.init`), which arrives here as is_member and is left alone. So a
1025+
* BARE `init` is always a local name: a closure (`init :=
1026+
* func(ctx) {...}; init(ctx)`, 22 kubernetes e2e storage suites), a
1027+
* parameter (`func sum(init func() ref.Val)`), or the `package init`
1028+
* clause of a directory named init. None of them is a project node.
1029+
*
1030+
* Every Go Function named init is a package init (no other package-level
1031+
* `init` can be declared), so that target is refused for every registry
1032+
* strategy. Before #1915 the flat QN made same_module bind the bare name
1033+
* to its own package's init; after it, the per-file QN misses there and
1034+
* the name fallback binds unrelated init methods instead. A Method or
1035+
* Field namesake is refused when only the name found it (the shared weak
1036+
* drop-list), which is how every such edge was minted. lsp_* answers are
1037+
* untouched: the sequential resolver emits them before its registry
1038+
* guards run, so refusing one here would split the two resolvers.
1039+
*
1040+
* Go-gated: elsewhere `init` is an ordinary name. A bare `init()` in a
1041+
* Java, C#, C++, Kotlin or Ruby method calls the class's own method, and
1042+
* a C, Python or JS `init` function is called by its bare name. */
1043+
if (!is_go || is_member || !name || strcmp(name, "init") != 0 || !target_label) {
1044+
return false;
1045+
}
1046+
if (strategy && strncmp(strategy, "lsp_", strlen("lsp_")) == 0) {
1047+
return false;
1048+
}
1049+
if (strcmp(target_label, "Function") == 0) {
1050+
return true;
1051+
}
1052+
if (strcmp(target_label, "Method") == 0 || strcmp(target_label, "Field") == 0) {
1053+
return weak_short_name_strategy(strategy);
1054+
}
1055+
return false;
1056+
}
1057+
10181058
/* Import-binding counterpart of the parameter guard above (#2127). An import
10191059
* binds the identifier for the whole module; when that import is external it
10201060
* never materializes, and the chain falls through to project-wide short-name

‎tests/test_pipeline.c‎

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1107411269
/* Fixture for the #1928 cross-language reference-guard probes (sequential and
1107511270
* parallel twins). pad_files > 0 adds filler files to push the index over the
1107611271
* parallel-pipeline threshold, since USAGE/WRITES/READS have one resolver per
@@ -29621,6 +29816,8 @@ SUITE(pipeline) {
2962129816
RUN_TEST(pipeline_go_multi_init_nodes_survive);
2962229817
RUN_TEST(pipeline_go_init_lsp_edges_per_file);
2962329818
RUN_TEST(pipeline_go_init_lsp_edges_per_file_parallel);
29819+
RUN_TEST(pipeline_go_bare_init_binds_nothing);
29820+
RUN_TEST(pipeline_go_bare_init_binds_nothing_parallel);
2962429821
RUN_TEST(pipeline_go_crossfile_type_alias_method);
2962529822
RUN_TEST(pipeline_go_crossfile_type_alias_method_parallel);
2962629823
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c);

0 commit comments

Comments
 (0)