Skip to content

Commit 9b5139d

Browse files
committed
fix(python): bound the function-local walks at the symbol's own file
A package __init__.py can define a function whose QN equals a submodule's (poetry: layouts/__init__.py `def layout` is the QN prefix of every symbol in layouts/layout.py). The prefix walks that recognise a def declared inside a function body accepted that other file's function as the enclosing def, so class Layout and its methods read as locals of layout(): they left the short-name index and the import symbol fallback, and the answer depended on which file registered first (discovery order on a full index, node-id order on an incremental one). Every walk now stops at the symbol's own module: - registration (pass_definitions.c, pass_parallel.c, and the incremental seed) passes the defining file's symbol prefix length, cbm_pipeline_fqn_symbol_prefix_len, computed without allocating, to cbm_registry_add_lang_in_module; cbm_registry_add_lang keeps its signature and does no local detection; - cbm_registry_resolve_lexical stops at the caller's module QN; - the import fallback's local test only counts an enclosing def in the same file. Also moves the DeusData#1548 registry tests out of the test they had been inserted into. Tests: registry_python_local_bound_at_own_file_issue1548 (both registration orders), fqn_symbol_prefix_len_matches_fqn_compute, pipeline_python_init_function_shares_submodule_qn{,_parallel}_issue1548 (src/ layout: the import and calls onto Layout survive, then again after an incremental re-index). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 6b1cd8a commit 9b5139d

10 files changed

Lines changed: 366 additions & 77 deletions

File tree

‎src/pipeline/fqn.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,30 @@ char *cbm_pipeline_resolve_relative_import(const char *source_rel, const char *m
355355
return resolve_js_relative(buf, sizeof(buf), module_path);
356356
}
357357

358+
size_t cbm_pipeline_fqn_symbol_prefix_len(const char *project, const char *rel_path) {
359+
if (!project) {
360+
return 0;
361+
}
362+
char path[FQN_PATH_BUF];
363+
int n = snprintf(path, sizeof(path), "%s", rel_path ? rel_path : "");
364+
if (n < 0 || (size_t)n >= sizeof(path)) {
365+
return 0;
366+
}
367+
cbm_normalize_path_sep(path);
368+
strip_file_extension(path);
369+
const char *segments[CBM_SZ_256];
370+
int seg_count = 0;
371+
segments[seg_count++] = project;
372+
seg_count += tokenize_path(path, segments + seg_count, FQN_MAX_PATH_SEGS);
373+
/* Any symbol name: the prefix cbm_pipeline_fqn_compute puts before it. */
374+
strip_init_or_index(segments, &seg_count, "symbol");
375+
size_t total = 0;
376+
for (int i = 0; i < seg_count; i++) {
377+
total += strlen(segments[i]) + (i > 0 ? FQN_SEP_LEN : 0);
378+
}
379+
return total;
380+
}
381+
358382
char *cbm_pipeline_fqn_folder(const char *project, const char *rel_dir) {
359383
if (!project) {
360384
return strdup("");

‎src/pipeline/pass_calls.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ static void handle_route_registration(cbm_pipeline_ctx_t *ctx, const CBMCall *ca
593593
cbm_resolution_t hres = {0};
594594
if (lang == CBM_LANG_PYTHON) {
595595
hres = cbm_registry_resolve_lexical(ctx->registry, call->second_arg_name,
596-
call->enclosing_func_qn);
596+
call->enclosing_func_qn, module_qn);
597597
}
598598
if (!hres.qualified_name) {
599599
hres = cbm_registry_resolve_handler(ctx->registry, call->second_arg_name, module_qn,

‎src/pipeline/pass_definitions.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ static void pd_add_file_doc(const cbm_gbuf_node_t *file_node, const char *doc) {
416416
* `file_doc`, the file's own doc (passed with its first def only), goes on the
417417
* File node. */
418418
static void process_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, const char *rel,
419-
const char *file_doc, CBMLanguage lang) {
419+
const char *file_doc, CBMLanguage lang, size_t module_prefix_len) {
420420
if (!def->qualified_name || !def->name) {
421421
return;
422422
}
@@ -442,7 +442,8 @@ static void process_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, const
442442
* (SQL FROM/JOIN lineage). pass_parallel.c and pipeline_incremental.c seed
443443
* through the same predicate, so the three registries cannot diverge. */
444444
if (node_id > 0 && cbm_label_is_registry_symbol(def->label)) {
445-
cbm_registry_add_lang(ctx->registry, def->name, def->qualified_name, def->label, lang);
445+
cbm_registry_add_lang_in_module(ctx->registry, def->name, def->qualified_name, def->label,
446+
lang, module_prefix_len);
446447
}
447448
char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
448449
const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
@@ -1019,10 +1020,14 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
10191020
"test_declarations");
10201021
}
10211022

1022-
/* Create nodes for each definition */
1023+
/* Create nodes for each definition. A Python file's module QN bounds
1024+
* the registry's function-local detection to this file. */
10231025
const char *file_doc = result->module_doc; /* goes with the first def */
1026+
size_t py_prefix_len = lang == CBM_LANG_PYTHON
1027+
? cbm_pipeline_fqn_symbol_prefix_len(ctx->project_name, rel)
1028+
: 0;
10241029
for (int d = 0; d < result->defs.count; d++) {
1025-
process_def(ctx, &result->defs.items[d], rel, file_doc, lang);
1030+
process_def(ctx, &result->defs.items[d], rel, file_doc, lang, py_prefix_len);
10261031
file_doc = NULL;
10271032
total_defs++;
10281033
}

‎src/pipeline/pass_parallel.c‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,15 +1773,17 @@ int cbm_parallel_extract(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
17731773
* once per file by the caller: computing the file QN and finding its node for
17741774
* every definition was 700 k allocations and lookups on the Go corpus. */
17751775
static int register_and_link_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def,
1776-
int64_t file_node_id, CBMLanguage lang, int *reg_entries) {
1776+
int64_t file_node_id, CBMLanguage lang, size_t module_prefix_len,
1777+
int *reg_entries) {
17771778
int edges = 0;
17781779
if (!def->name || !def->qualified_name || !def->label) {
17791780
return 0;
17801781
}
17811782
/* Registry membership is defined ONCE by cbm_label_is_registry_symbol
17821783
* (helpers.c) — see pass_definitions.c for the per-label rationale. */
17831784
if (cbm_label_is_registry_symbol(def->label)) {
1784-
cbm_registry_add_lang(ctx->registry, def->name, def->qualified_name, def->label, lang);
1785+
cbm_registry_add_lang_in_module(ctx->registry, def->name, def->qualified_name, def->label,
1786+
lang, module_prefix_len);
17851787
(*reg_entries)++;
17861788
}
17871789
char marked[CBM_SZ_4K];
@@ -1988,9 +1990,14 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
19881990
int64_t file_node_id = file_node ? file_node->id : 0;
19891991
free(file_qn);
19901992
pp_add_file_doc(file_node, result->module_doc);
1993+
/* Bounds the function-local detection to this file (pass_definitions.c). */
1994+
size_t py_prefix_len = files[i].language == CBM_LANG_PYTHON
1995+
? cbm_pipeline_fqn_symbol_prefix_len(ctx->project_name, rel)
1996+
: 0;
19911997
for (int d = 0; d < result->defs.count; d++) {
1992-
defines_edges += register_and_link_def(ctx, &result->defs.items[d], file_node_id,
1993-
files[i].language, &reg_entries);
1998+
defines_edges +=
1999+
register_and_link_def(ctx, &result->defs.items[d], file_node_id,
2000+
files[i].language, py_prefix_len, &reg_entries);
19942001
}
19952002
}
19962003

@@ -2415,7 +2422,8 @@ static void emit_route_registration(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *sou
24152422
* lexical lookup sees a function's locals. MUST match pass_calls.c. */
24162423
cbm_resolution_t hres = {0};
24172424
if (lang == CBM_LANG_PYTHON) {
2418-
hres = cbm_registry_resolve_lexical(registry, handler_ref, call->enclosing_func_qn);
2425+
hres = cbm_registry_resolve_lexical(registry, handler_ref, call->enclosing_func_qn,
2426+
module_qn);
24192427
}
24202428
if (!hres.qualified_name) {
24212429
hres = cbm_registry_resolve_handler(registry, handler_ref, module_qn, ik, iv, ic,

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,8 +1824,11 @@ bool cbm_import_symbol_fallback_allowed(CBMLanguage lang) {
18241824
* no import can name it. Its QN extends a Function/Method node's
18251825
* (proj.tests.mock_inputs.inner.wrapper.mock_getpass.getpass), which is how it
18261826
* is recognised; the symbol-name fallback bound `import getpass` to exactly
1827-
* that def. Mirrors cbm_registry_add_lang, which keeps the same
1828-
* symbols out of the call resolver's short-name index. */
1827+
* that def. Only an enclosing def of the SAME file counts: a package
1828+
* __init__.py function can share a submodule's QN (poetry: layouts/__init__.py
1829+
* `def layout` vs layouts/layout.py `class Layout`). Mirrors
1830+
* cbm_registry_add_lang_in_module, which keeps the same symbols out of the call
1831+
* resolver's short-name index. */
18291832
static bool import_candidate_is_python_local(const cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *cand) {
18301833
const char *qn = cand->qualified_name;
18311834
const char *slash = cand->file_path ? strrchr(cand->file_path, '/') : NULL;
@@ -1839,7 +1842,8 @@ static bool import_candidate_is_python_local(const cbm_gbuf_t *gbuf, const cbm_g
18391842
for (char *dot = strrchr(scope, '.'); dot; dot = strrchr(scope, '.')) {
18401843
*dot = '\0';
18411844
const cbm_gbuf_node_t *outer = cbm_gbuf_find_by_qn(gbuf, scope);
1842-
if (outer && outer->label &&
1845+
if (outer && outer->label && outer->file_path &&
1846+
strcmp(outer->file_path, cand->file_path) == 0 &&
18431847
(strcmp(outer->label, "Function") == 0 || strcmp(outer->label, "Method") == 0)) {
18441848
return true;
18451849
}

‎src/pipeline/pipeline.h‎

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ char *cbm_pipeline_fqn_compute(const char *project, const char *rel_path, const
200200
/* Module QN: project.dir.parts (no name). Caller must free(). */
201201
char *cbm_pipeline_fqn_module(const char *project, const char *rel_path);
202202

203+
/* Length of the prefix cbm_pipeline_fqn_compute(project, rel_path, name) puts
204+
* before a symbol `name` (the joining dot excluded): the QN prefix every
205+
* definition of that file carries, __init__ / index already dropped. Computed
206+
* without allocating; 0 when unavailable. */
207+
size_t cbm_pipeline_fqn_symbol_prefix_len(const char *project, const char *rel_path);
208+
203209
/* Language-aware module QN. When `module_is_dir` is true (Java/Go package
204210
* semantics) the module is derived from the CONTAINING DIRECTORY (the filename
205211
* stem is dropped), so it agrees with the extraction-side def QNs; when false
@@ -253,14 +259,24 @@ void cbm_registry_add(cbm_registry_t *r, const char *name, const char *qualified
253259

254260
/* As cbm_registry_add, recording `lang`: the DETECTED language of the file
255261
* that defines the symbol (CBM_LANG_COUNT = unknown), which the cross-language
256-
* veto (cbm_registry_name_guess_vetoed) checks against the caller. A Python
257-
* symbol declared inside a function body (see cbm_registry_qn_is_function_local)
258-
* is registered for exact lookups only, never offered to the short-name
259-
* strategies; every registration site seeds through this, so full and
260-
* incremental indexes agree. */
262+
* veto (cbm_registry_name_guess_vetoed) checks against the caller. Detects no
263+
* function-local symbols: that needs the defining file's module QN, see
264+
* cbm_registry_add_lang_in_module. */
261265
void cbm_registry_add_lang(cbm_registry_t *r, const char *name, const char *qualified_name,
262266
const char *label, CBMLanguage lang);
263267

268+
/* As cbm_registry_add_lang. A Python symbol declared inside a function body of
269+
* its own file (cbm_registry_qn_is_function_local, bounded by
270+
* `module_prefix_len`: cbm_pipeline_fqn_symbol_prefix_len of the defining file)
271+
* is registered for exact lookups only, never offered to the short-name
272+
* strategies. The three graph
273+
* registration sites (pass_definitions.c, pass_parallel.c,
274+
* pipeline_incremental.c) all register through this, so full and incremental
275+
* indexes agree. 0 = no local detection. */
276+
void cbm_registry_add_lang_in_module(cbm_registry_t *r, const char *name,
277+
const char *qualified_name, const char *label,
278+
CBMLanguage lang, size_t module_prefix_len);
279+
264280
/* True when code in `caller` can call a symbol defined in `target` by bare
265281
* name: same language, same interop family (JS/TS + script hosts, C/C++/CUDA/
266282
* ObjC, JVM, .NET, ...; the table lives in registry.c), or Swift with the C
@@ -272,16 +288,19 @@ bool cbm_lang_resolution_compatible(CBMLanguage caller, CBMLanguage target);
272288
* CBM_LANG_COUNT for anything else. */
273289
CBMLanguage cbm_registry_synthetic_path_language(const char *file_path);
274290

275-
/* True when a proper prefix of `qualified_name` is a registered Function or
276-
* Method, i.e. the symbol was declared inside a function body. */
277-
bool cbm_registry_qn_is_function_local(const cbm_registry_t *r, const char *qualified_name);
291+
/* True when a proper prefix of `qualified_name` longer than
292+
* `module_prefix_len` (cbm_pipeline_fqn_symbol_prefix_len of the defining file)
293+
* is a registered Function or Method, i.e. the symbol was declared inside a
294+
* function body of its own file. False when module_prefix_len is 0. */
295+
bool cbm_registry_qn_is_function_local(const cbm_registry_t *r, const char *qualified_name,
296+
size_t module_prefix_len);
278297

279298
/* Python bare-name lexical lookup: the def or class named `callee_name` bound
280299
* in the nearest function scope enclosing `scope_qn` (the caller's QN),
281-
* skipping class bodies, stopping at the module. Strategy "lexical_scope";
282-
* qualified_name is NULL when no such local exists. */
300+
* skipping class bodies, stopping at the caller's module (`module_qn`).
301+
* Strategy "lexical_scope"; qualified_name is NULL when no such local exists. */
283302
cbm_resolution_t cbm_registry_resolve_lexical(const cbm_registry_t *r, const char *callee_name,
284-
const char *scope_qn);
303+
const char *scope_qn, const char *module_qn);
285304

286305
/* Resolve a callee name using prioritized strategies.
287306
* import_map: NULL-terminated array of {local_name, resolved_qn} pairs, or NULL.

‎src/pipeline/pipeline_incremental.c‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,7 @@ static bool incr_label_is_registry_symbol(const char *label) {
13401340
typedef struct {
13411341
cbm_registry_t *registry;
13421342
CBMHashTable *lang_by_path; /* rel_path -> (CBMLanguage + 1); keys borrowed */
1343+
const char *project; /* bounds Python function-local detection per file */
13431344
} registry_seed_t;
13441345

13451346
static void registry_visitor(const cbm_gbuf_node_t *node, void *userdata) {
@@ -1354,14 +1355,20 @@ static void registry_visitor(const cbm_gbuf_node_t *node, void *userdata) {
13541355
lang = (CBMLanguage)((uintptr_t)v - SKIP_ONE);
13551356
}
13561357
}
1357-
cbm_registry_add_lang(seed->registry, node->name, node->qualified_name, node->label, lang);
1358+
/* Same per-file bound as the full index (pass_definitions.c). */
1359+
size_t prefix_len = lang == CBM_LANG_PYTHON
1360+
? cbm_pipeline_fqn_symbol_prefix_len(seed->project, node->file_path)
1361+
: 0;
1362+
cbm_registry_add_lang_in_module(seed->registry, node->name, node->qualified_name, node->label,
1363+
lang, prefix_len);
13581364
}
13591365

13601366
/* Seed the registry from every registry symbol in the graph buffer, with
13611367
* languages taken from the discovered files (see registry_visitor). */
13621368
static void registry_seed_from_gbuf(cbm_registry_t *registry, const cbm_gbuf_t *gbuf,
1363-
const cbm_file_info_t *files, int file_count) {
1364-
registry_seed_t seed = {.registry = registry, .lang_by_path = NULL};
1369+
const char *project, const cbm_file_info_t *files,
1370+
int file_count) {
1371+
registry_seed_t seed = {.registry = registry, .lang_by_path = NULL, .project = project};
13651372
if (files && file_count > 0) {
13661373
seed.lang_by_path = cbm_ht_create((size_t)file_count * PAIR_LEN);
13671374
}
@@ -3305,7 +3312,7 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
33053312
if (!registry) {
33063313
goto out;
33073314
}
3308-
registry_seed_from_gbuf(registry, gbuf, files, file_count);
3315+
registry_seed_from_gbuf(registry, gbuf, project, files, file_count);
33093316
cbm_log_info("delta.preseed_done", "registry", itoa_buf(cbm_registry_size(registry)),
33103317
"elapsed_ms", itoa_buf((int)elapsed_ms(t)));
33113318

@@ -4155,7 +4162,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
41554162
/* Step 3-5: Registry + extract + resolve */
41564163
cbm_registry_t *registry = cbm_registry_new();
41574164
cbm_clock_gettime(CLOCK_MONOTONIC, &t);
4158-
registry_seed_from_gbuf(registry, existing, files, file_count);
4165+
registry_seed_from_gbuf(registry, existing, project, files, file_count);
41594166
cbm_log_info("incremental.registry_seed", "symbols", itoa_buf(cbm_registry_size(registry)),
41604167
"elapsed_ms", itoa_buf((int)elapsed_ms(t)));
41614168

0 commit comments

Comments
 (0)