Skip to content

Commit 51a3e4e

Browse files
committed
fix(python): resolve a base class spelled through a root-binding import
`import pkg.mod.nodes` binds the ROOT name `pkg`, so `class X(pkg.mod.nodes.Context)` names module pkg.mod.nodes's Context. The cross-LSP base resolution asked the registry, whose import_map step joined the imported MODULE (the IMPORTS edge target, pkg.mod.nodes) with the whole remaining spelling (mod.nodes.Context) and missed; the base kept its raw spelling, so the Python LSP's inheritance view never saw it. Every member inherited through a dotted base fell to a name guess or to nothing: self.m() became unique_name, super().m() suffix_match, super().__init__() only resolved through a synthesized QN. pxc_resolve_base_qn now resolves such a spelling against the module the import names: the import map's module when the root local is unique there, else the dotted module path under the project's QN rule. Python only, import-backed and exact (no short-name step), and only a type-like project node is accepted. `import a.b as m` and `from a import b` bind the module itself and keep resolving through the import map. Tests: python_dotted_base_inheritance(_parallel): self.m(), super().m() and super().__init__() through a dotted base resolve by the LSP; the from-imported control keeps resolving; an untyped receiver stays without an LSP edge. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 9f85f10 commit 51a3e4e

2 files changed

Lines changed: 182 additions & 0 deletions

File tree

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,65 @@ typedef struct {
238238
CBMLanguage lang;
239239
} pxc_base_scope_t;
240240

241+
/* A type-like graph node, or NULL. */
242+
static const cbm_gbuf_node_t *pxc_type_node(const cbm_gbuf_node_t *node) {
243+
return node && node->qualified_name && cbm_label_is_type_like(node->label) ? node : NULL;
244+
}
245+
246+
/* Python `import a.b.c` binds the ROOT name `a`, so a base spelled through it
247+
* (`class X(a.b.c.Base)`) is module a.b.c's Base. The import map holds the
248+
* imported MODULE under that root local (the IMPORTS edge target), and the
249+
* registry's import_map step appends the whole remaining spelling to it
250+
* (a.b.c + .b.c.Base), so a dotted base never resolved and every member
251+
* inherited through it was invisible to the LSP. Resolve the spelling against
252+
* the module the import names: the import map's module when that local is
253+
* unique there, else the dotted module path under the project's QN rule.
254+
* Import-backed and exact -- no short-name step -- and only a type-like
255+
* project node is accepted. `import a.b as m` and `from a import b` bind the
256+
* module itself and keep resolving through the import map. */
257+
static const char *pxc_python_dotted_base_qn(const pxc_base_scope_t *bs, const char *raw) {
258+
if (bs->lang != CBM_LANG_PYTHON || !bs->imports || !bs->gbuf || !strchr(raw, '.')) {
259+
return NULL;
260+
}
261+
size_t root_len = strcspn(raw, ".");
262+
for (int i = 0; i < bs->imports->count; i++) {
263+
const CBMImport *imp = &bs->imports->items[i];
264+
const char *local = imp->local_name;
265+
const char *path = imp->module_path;
266+
if (!local || !path || strlen(local) != root_len || strncmp(raw, local, root_len) != 0 ||
267+
strncmp(path, local, root_len) != 0 || (path[root_len] != '\0' && path[root_len] != '.')) {
268+
continue; /* not a root binding of this base's first segment */
269+
}
270+
size_t path_len = strlen(path);
271+
if (strncmp(raw, path, path_len) != 0 || raw[path_len] != '.') {
272+
continue; /* the base does not extend this import's module path */
273+
}
274+
const char *module_qn = NULL;
275+
int hits = 0;
276+
for (int k = 0; k < bs->imp_count; k++) {
277+
if (bs->imp_keys[k] && bs->imp_vals[k] && strcmp(bs->imp_keys[k], local) == 0) {
278+
module_qn = bs->imp_vals[k];
279+
hits++;
280+
}
281+
}
282+
const cbm_gbuf_node_t *node = NULL;
283+
if (hits == 1) {
284+
char candidate[CBM_SZ_512];
285+
int n = snprintf(candidate, sizeof(candidate), "%s%s", module_qn, raw + path_len);
286+
if (n > 0 && (size_t)n < sizeof(candidate)) {
287+
node = pxc_type_node(cbm_gbuf_find_by_qn(bs->gbuf, candidate));
288+
}
289+
}
290+
if (!node) {
291+
node = pxc_type_node(cbm_pipeline_lsp_target_node(bs->gbuf, bs->project_name, raw, false));
292+
}
293+
if (node) {
294+
return node->qualified_name;
295+
}
296+
}
297+
return NULL;
298+
}
299+
241300
/* Resolve one base-class spelling to a project QN. Mirrors
242301
* pass_semantic.c::resolve_as_class — same registry, same type-like veto —
243302
* then additionally rejects weak short-name strategies (see above).
@@ -248,6 +307,10 @@ static const char *pxc_resolve_base_qn(const pxc_base_scope_t *bs, const char *r
248307
if (!bs || !bs->reg || !raw || !raw[0]) {
249308
return NULL;
250309
}
310+
const char *dotted = pxc_python_dotted_base_qn(bs, raw);
311+
if (dotted) {
312+
return dotted;
313+
}
251314
cbm_resolution_t res = cbm_registry_resolve_lang(bs->reg, raw, module_qn, bs->imp_keys,
252315
bs->imp_vals, bs->imp_count, bs->lang);
253316
if (!res.qualified_name || !res.qualified_name[0]) {

‎tests/test_pipeline.c‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13517,6 +13517,123 @@ TEST(python_crossfile_typed_field_calls_issue1277_parallel) {
1351713517
return check_python_crossfile_typed_field_calls_issue1277(true);
1351813518
}
1351913519

13520+
/* A base class spelled through a root-binding import (`import pkg.mod.nodes`
13521+
* binds `pkg`; `class X(pkg.mod.nodes.Context)`) is the same class as the
13522+
* from-imported spelling. The Python LSP's inheritance view never resolved the
13523+
* dotted spelling, so a member inherited through it (self.m(), super().m(),
13524+
* super().__init__()) fell to a weak name guess or to nothing. The plain
13525+
* from-import control must keep resolving; the untyped receiver must not. */
13526+
static int check_python_dotted_base_inheritance(bool parallel) {
13527+
const char *nodes = "class Context:\n"
13528+
" def __init__(self, line=-1):\n"
13529+
" self.line = line\n\n"
13530+
" def ctx_only_helper(self):\n"
13531+
" return 1\n\n"
13532+
" def describe(self):\n"
13533+
" return 0\n";
13534+
const char *types = "import pkg.mod.nodes\n"
13535+
"from pkg.mod.nodes import Context\n\n"
13536+
"class DottedType(pkg.mod.nodes.Context):\n"
13537+
" def __init__(self, line=-1):\n"
13538+
" super().__init__(line)\n\n"
13539+
" def run(self):\n"
13540+
" return self.ctx_only_helper()\n\n"
13541+
" def describe(self):\n"
13542+
" return super().describe()\n\n"
13543+
"class PlainType(Context):\n"
13544+
" def run(self):\n"
13545+
" return self.ctx_only_helper()\n\n"
13546+
"def untyped(thing):\n"
13547+
" return thing.ctx_only_helper()\n";
13548+
if (setup_usages_repo("pkg/mod/nodes.py", nodes, "pkg/types.py", types) != 0) {
13549+
FAIL("failed to create temp dir");
13550+
}
13551+
char path[512];
13552+
const char *inits[] = {"pkg/__init__.py", "pkg/mod/__init__.py"};
13553+
for (size_t i = 0; i < sizeof(inits) / sizeof(inits[0]); i++) {
13554+
snprintf(path, sizeof(path), "%s/%s", g_usages_tmpdir, inits[i]);
13555+
if (th_write_file(path, "") != 0) {
13556+
teardown_usages_repo();
13557+
FAIL("failed to write package marker");
13558+
}
13559+
}
13560+
if (parallel) {
13561+
for (int i = 0; i < 50; i++) {
13562+
snprintf(path, sizeof(path), "%s/dotted_pad_%02d.py", g_usages_tmpdir, i);
13563+
if (th_write_file(path, "dotted_padding = 0\n") != 0) {
13564+
teardown_usages_repo();
13565+
FAIL("failed to write parallel-selection fixture");
13566+
}
13567+
}
13568+
}
13569+
13570+
const char *old_workers = getenv("CBM_WORKERS");
13571+
char *saved_workers = old_workers ? strdup(old_workers) : NULL;
13572+
const char *old_single = getenv("CBM_INDEX_SINGLE_THREAD");
13573+
char *saved_single = old_single ? strdup(old_single) : NULL;
13574+
if ((old_workers && !saved_workers) || (old_single && !saved_single)) {
13575+
free(saved_workers);
13576+
free(saved_single);
13577+
teardown_usages_repo();
13578+
FAIL("failed to save pipeline environment");
13579+
}
13580+
int pin_workers_rc = parallel ? cbm_setenv("CBM_WORKERS", "4", 1) : 0;
13581+
int pin_single_rc = parallel ? cbm_unsetenv("CBM_INDEX_SINGLE_THREAD") : 0;
13582+
13583+
char db_path[512];
13584+
snprintf(db_path, sizeof(db_path), "%s/test_dotted_base.db", g_usages_tmpdir);
13585+
cbm_pipeline_t *p = pin_workers_rc == 0 && pin_single_rc == 0
13586+
? cbm_pipeline_new(g_usages_tmpdir, db_path, CBM_MODE_FULL)
13587+
: NULL;
13588+
int run_rc = p ? cbm_pipeline_run(p) : -1;
13589+
cbm_store_t *s = run_rc == 0 ? cbm_store_open_path(db_path) : NULL;
13590+
bool store_opened = s != NULL;
13591+
int dotted_self = -1, dotted_super = -1, dotted_init = -1, plain_self = -1, untyped = -1;
13592+
if (s) {
13593+
const char *project = cbm_pipeline_project_name(p);
13594+
dotted_self = count_lsp_calls(s, project, "pkg.types.DottedType.run",
13595+
"pkg.mod.nodes.Context.ctx_only_helper");
13596+
dotted_super = count_lsp_calls(s, project, "pkg.types.DottedType.describe",
13597+
"pkg.mod.nodes.Context.describe");
13598+
dotted_init = count_lsp_calls(s, project, "pkg.types.DottedType.__init__",
13599+
"pkg.mod.nodes.Context.__init__");
13600+
plain_self = count_lsp_calls(s, project, "pkg.types.PlainType.run",
13601+
"pkg.mod.nodes.Context.ctx_only_helper");
13602+
untyped = count_lsp_calls(s, project, "pkg.types.untyped", "ctx_only_helper");
13603+
cbm_store_close(s);
13604+
}
13605+
13606+
cbm_pipeline_free(p);
13607+
int restore_workers_rc =
13608+
saved_workers ? cbm_setenv("CBM_WORKERS", saved_workers, 1) : cbm_unsetenv("CBM_WORKERS");
13609+
int restore_single_rc = saved_single ? cbm_setenv("CBM_INDEX_SINGLE_THREAD", saved_single, 1)
13610+
: cbm_unsetenv("CBM_INDEX_SINGLE_THREAD");
13611+
free(saved_workers);
13612+
free(saved_single);
13613+
teardown_usages_repo();
13614+
13615+
ASSERT_EQ(pin_workers_rc, 0);
13616+
ASSERT_EQ(pin_single_rc, 0);
13617+
ASSERT_EQ(restore_workers_rc, 0);
13618+
ASSERT_EQ(restore_single_rc, 0);
13619+
ASSERT_EQ(run_rc, 0);
13620+
ASSERT_TRUE(store_opened);
13621+
ASSERT_EQ(dotted_self, 1); /* self.m() inherited through a dotted base */
13622+
ASSERT_EQ(dotted_super, 1); /* super().m() through a dotted base */
13623+
ASSERT_EQ(dotted_init, 1); /* super().__init__() through a dotted base */
13624+
ASSERT_EQ(plain_self, 1); /* control: from-imported base */
13625+
ASSERT_EQ(untyped, 0); /* control: no receiver type, no LSP edge */
13626+
PASS();
13627+
}
13628+
13629+
TEST(python_dotted_base_inheritance) {
13630+
return check_python_dotted_base_inheritance(false);
13631+
}
13632+
13633+
TEST(python_dotted_base_inheritance_parallel) {
13634+
return check_python_dotted_base_inheritance(true);
13635+
}
13636+
1352013637
/* Bind the external-import veto specifically to #1277's typed-field fold.
1352113638
* No base classes or CALLS edges participate: a name-only call fallback
1352213639
* cannot turn this metadata assertion into a false positive. The unguarded
@@ -23665,6 +23782,8 @@ SUITE(pipeline) {
2366523782
RUN_TEST(override_python_through_intermediate_class);
2366623783
RUN_TEST(python_crossfile_typed_field_calls_issue1277);
2366723784
RUN_TEST(python_crossfile_typed_field_calls_issue1277_parallel);
23785+
RUN_TEST(python_dotted_base_inheritance);
23786+
RUN_TEST(python_dotted_base_inheritance_parallel);
2366823787
RUN_TEST(python_typed_field_external_import_stays_unresolved);
2366923788
RUN_TEST(python_typed_field_project_class_resolves);
2367023789
RUN_TEST(usages_creates_edges);

0 commit comments

Comments
 (0)