Skip to content

Commit 9cc3b76

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/def-props-buffer-every-field
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2 parents b87e0c0 + 5f66c86 commit 9cc3b76

2 files changed

Lines changed: 184 additions & 0 deletions

File tree

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,67 @@ 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 ||
268+
(path[root_len] != '\0' && path[root_len] != '.')) {
269+
continue; /* not a root binding of this base's first segment */
270+
}
271+
size_t path_len = strlen(path);
272+
if (strncmp(raw, path, path_len) != 0 || raw[path_len] != '.') {
273+
continue; /* the base does not extend this import's module path */
274+
}
275+
const char *module_qn = NULL;
276+
int hits = 0;
277+
for (int k = 0; k < bs->imp_count; k++) {
278+
if (bs->imp_keys[k] && bs->imp_vals[k] && strcmp(bs->imp_keys[k], local) == 0) {
279+
module_qn = bs->imp_vals[k];
280+
hits++;
281+
}
282+
}
283+
const cbm_gbuf_node_t *node = NULL;
284+
if (hits == 1) {
285+
char candidate[CBM_SZ_512];
286+
int n = snprintf(candidate, sizeof(candidate), "%s%s", module_qn, raw + path_len);
287+
if (n > 0 && (size_t)n < sizeof(candidate)) {
288+
node = pxc_type_node(cbm_gbuf_find_by_qn(bs->gbuf, candidate));
289+
}
290+
}
291+
if (!node) {
292+
node =
293+
pxc_type_node(cbm_pipeline_lsp_target_node(bs->gbuf, bs->project_name, raw, false));
294+
}
295+
if (node) {
296+
return node->qualified_name;
297+
}
298+
}
299+
return NULL;
300+
}
301+
241302
/* Resolve one base-class spelling to a project QN. Mirrors
242303
* pass_semantic.c::resolve_as_class — same registry, same type-like veto —
243304
* then additionally rejects weak short-name strategies (see above).
@@ -248,6 +309,10 @@ static const char *pxc_resolve_base_qn(const pxc_base_scope_t *bs, const char *r
248309
if (!bs || !bs->reg || !raw || !raw[0]) {
249310
return NULL;
250311
}
312+
const char *dotted = pxc_python_dotted_base_qn(bs, raw);
313+
if (dotted) {
314+
return dotted;
315+
}
251316
cbm_resolution_t res = cbm_registry_resolve_lang(bs->reg, raw, module_qn, bs->imp_keys,
252317
bs->imp_vals, bs->imp_count, bs->lang);
253318
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
@@ -13628,6 +13628,123 @@ TEST(python_crossfile_typed_field_calls_issue1277_parallel) {
1362813628
return check_python_crossfile_typed_field_calls_issue1277(true);
1362913629
}
1363013630

13631+
/* A base class spelled through a root-binding import (`import pkg.mod.nodes`
13632+
* binds `pkg`; `class X(pkg.mod.nodes.Context)`) is the same class as the
13633+
* from-imported spelling. The Python LSP's inheritance view never resolved the
13634+
* dotted spelling, so a member inherited through it (self.m(), super().m(),
13635+
* super().__init__()) fell to a weak name guess or to nothing. The plain
13636+
* from-import control must keep resolving; the untyped receiver must not. */
13637+
static int check_python_dotted_base_inheritance(bool parallel) {
13638+
const char *nodes = "class Context:\n"
13639+
" def __init__(self, line=-1):\n"
13640+
" self.line = line\n\n"
13641+
" def ctx_only_helper(self):\n"
13642+
" return 1\n\n"
13643+
" def describe(self):\n"
13644+
" return 0\n";
13645+
const char *types = "import pkg.mod.nodes\n"
13646+
"from pkg.mod.nodes import Context\n\n"
13647+
"class DottedType(pkg.mod.nodes.Context):\n"
13648+
" def __init__(self, line=-1):\n"
13649+
" super().__init__(line)\n\n"
13650+
" def run(self):\n"
13651+
" return self.ctx_only_helper()\n\n"
13652+
" def describe(self):\n"
13653+
" return super().describe()\n\n"
13654+
"class PlainType(Context):\n"
13655+
" def run(self):\n"
13656+
" return self.ctx_only_helper()\n\n"
13657+
"def untyped(thing):\n"
13658+
" return thing.ctx_only_helper()\n";
13659+
if (setup_usages_repo("pkg/mod/nodes.py", nodes, "pkg/types.py", types) != 0) {
13660+
FAIL("failed to create temp dir");
13661+
}
13662+
char path[512];
13663+
const char *inits[] = {"pkg/__init__.py", "pkg/mod/__init__.py"};
13664+
for (size_t i = 0; i < sizeof(inits) / sizeof(inits[0]); i++) {
13665+
snprintf(path, sizeof(path), "%s/%s", g_usages_tmpdir, inits[i]);
13666+
if (th_write_file(path, "") != 0) {
13667+
teardown_usages_repo();
13668+
FAIL("failed to write package marker");
13669+
}
13670+
}
13671+
if (parallel) {
13672+
for (int i = 0; i < 50; i++) {
13673+
snprintf(path, sizeof(path), "%s/dotted_pad_%02d.py", g_usages_tmpdir, i);
13674+
if (th_write_file(path, "dotted_padding = 0\n") != 0) {
13675+
teardown_usages_repo();
13676+
FAIL("failed to write parallel-selection fixture");
13677+
}
13678+
}
13679+
}
13680+
13681+
const char *old_workers = getenv("CBM_WORKERS");
13682+
char *saved_workers = old_workers ? strdup(old_workers) : NULL;
13683+
const char *old_single = getenv("CBM_INDEX_SINGLE_THREAD");
13684+
char *saved_single = old_single ? strdup(old_single) : NULL;
13685+
if ((old_workers && !saved_workers) || (old_single && !saved_single)) {
13686+
free(saved_workers);
13687+
free(saved_single);
13688+
teardown_usages_repo();
13689+
FAIL("failed to save pipeline environment");
13690+
}
13691+
int pin_workers_rc = parallel ? cbm_setenv("CBM_WORKERS", "4", 1) : 0;
13692+
int pin_single_rc = parallel ? cbm_unsetenv("CBM_INDEX_SINGLE_THREAD") : 0;
13693+
13694+
char db_path[512];
13695+
snprintf(db_path, sizeof(db_path), "%s/test_dotted_base.db", g_usages_tmpdir);
13696+
cbm_pipeline_t *p = pin_workers_rc == 0 && pin_single_rc == 0
13697+
? cbm_pipeline_new(g_usages_tmpdir, db_path, CBM_MODE_FULL)
13698+
: NULL;
13699+
int run_rc = p ? cbm_pipeline_run(p) : -1;
13700+
cbm_store_t *s = run_rc == 0 ? cbm_store_open_path(db_path) : NULL;
13701+
bool store_opened = s != NULL;
13702+
int dotted_self = -1, dotted_super = -1, dotted_init = -1, plain_self = -1, untyped = -1;
13703+
if (s) {
13704+
const char *project = cbm_pipeline_project_name(p);
13705+
dotted_self = count_lsp_calls(s, project, "pkg.types.DottedType.run",
13706+
"pkg.mod.nodes.Context.ctx_only_helper");
13707+
dotted_super = count_lsp_calls(s, project, "pkg.types.DottedType.describe",
13708+
"pkg.mod.nodes.Context.describe");
13709+
dotted_init = count_lsp_calls(s, project, "pkg.types.DottedType.__init__",
13710+
"pkg.mod.nodes.Context.__init__");
13711+
plain_self = count_lsp_calls(s, project, "pkg.types.PlainType.run",
13712+
"pkg.mod.nodes.Context.ctx_only_helper");
13713+
untyped = count_lsp_calls(s, project, "pkg.types.untyped", "ctx_only_helper");
13714+
cbm_store_close(s);
13715+
}
13716+
13717+
cbm_pipeline_free(p);
13718+
int restore_workers_rc =
13719+
saved_workers ? cbm_setenv("CBM_WORKERS", saved_workers, 1) : cbm_unsetenv("CBM_WORKERS");
13720+
int restore_single_rc = saved_single ? cbm_setenv("CBM_INDEX_SINGLE_THREAD", saved_single, 1)
13721+
: cbm_unsetenv("CBM_INDEX_SINGLE_THREAD");
13722+
free(saved_workers);
13723+
free(saved_single);
13724+
teardown_usages_repo();
13725+
13726+
ASSERT_EQ(pin_workers_rc, 0);
13727+
ASSERT_EQ(pin_single_rc, 0);
13728+
ASSERT_EQ(restore_workers_rc, 0);
13729+
ASSERT_EQ(restore_single_rc, 0);
13730+
ASSERT_EQ(run_rc, 0);
13731+
ASSERT_TRUE(store_opened);
13732+
ASSERT_EQ(dotted_self, 1); /* self.m() inherited through a dotted base */
13733+
ASSERT_EQ(dotted_super, 1); /* super().m() through a dotted base */
13734+
ASSERT_EQ(dotted_init, 1); /* super().__init__() through a dotted base */
13735+
ASSERT_EQ(plain_self, 1); /* control: from-imported base */
13736+
ASSERT_EQ(untyped, 0); /* control: no receiver type, no LSP edge */
13737+
PASS();
13738+
}
13739+
13740+
TEST(python_dotted_base_inheritance) {
13741+
return check_python_dotted_base_inheritance(false);
13742+
}
13743+
13744+
TEST(python_dotted_base_inheritance_parallel) {
13745+
return check_python_dotted_base_inheritance(true);
13746+
}
13747+
1363113748
/* Bind the external-import veto specifically to #1277's typed-field fold.
1363213749
* No base classes or CALLS edges participate: a name-only call fallback
1363313750
* cannot turn this metadata assertion into a false positive. The unguarded
@@ -23778,6 +23895,8 @@ SUITE(pipeline) {
2377823895
RUN_TEST(override_python_through_intermediate_class);
2377923896
RUN_TEST(python_crossfile_typed_field_calls_issue1277);
2378023897
RUN_TEST(python_crossfile_typed_field_calls_issue1277_parallel);
23898+
RUN_TEST(python_dotted_base_inheritance);
23899+
RUN_TEST(python_dotted_base_inheritance_parallel);
2378123900
RUN_TEST(python_typed_field_external_import_stays_unresolved);
2378223901
RUN_TEST(python_typed_field_project_class_resolves);
2378323902
RUN_TEST(usages_creates_edges);

0 commit comments

Comments
 (0)