Skip to content

Commit 6b1cc87

Browse files
committed
fix(pipeline): resolve overrides through nearest declaring ancestors
Preserve all equally near targets and guard Python inheritance against external import collisions. Refs DeusData#1278. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent bf2daa3 commit 6b1cc87

8 files changed

Lines changed: 844 additions & 71 deletions

File tree

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -220,25 +220,47 @@ static bool pxc_base_strategy_is_weak(const char *strategy) {
220220
strcmp(strategy, "field_type_hint") == 0 || strcmp(strategy, "fuzzy") == 0;
221221
}
222222

223+
/* One file's base-resolution scope: the registry + import map pass_semantic
224+
* uses, plus the facts the external-base gate reads. NULL (the surface-probe
225+
* path) keeps every base at its raw spelling. */
226+
typedef struct {
227+
const cbm_registry_t *reg;
228+
const char **imp_keys;
229+
const char **imp_vals;
230+
int imp_count;
231+
const CBMImportArray *imports;
232+
const cbm_gbuf_t *gbuf;
233+
const char *project_name;
234+
const char *rel;
235+
CBMLanguage lang;
236+
} pxc_base_scope_t;
237+
223238
/* Resolve one base-class spelling to a project QN. Mirrors
224239
* pass_semantic.c::resolve_as_class — same registry, same type-like veto —
225240
* then additionally rejects weak short-name strategies (see above).
226241
* Returns NULL when the base is not a confidently-known project type;
227242
* stdlib and third-party bases land here and keep their raw spelling. */
228-
static const char *pxc_resolve_base_qn(const cbm_registry_t *reg, const char *raw,
229-
const char *module_qn, const char **imp_keys,
230-
const char **imp_vals, int imp_count) {
231-
if (!reg || !raw || !raw[0]) {
243+
static const char *pxc_resolve_base_qn(const pxc_base_scope_t *bs, const char *raw,
244+
const char *module_qn) {
245+
if (!bs || !bs->reg || !raw || !raw[0]) {
232246
return NULL;
233247
}
234-
cbm_resolution_t res = cbm_registry_resolve(reg, raw, module_qn, imp_keys, imp_vals, imp_count);
248+
cbm_resolution_t res =
249+
cbm_registry_resolve(bs->reg, raw, module_qn, bs->imp_keys, bs->imp_vals, bs->imp_count);
235250
if (!res.qualified_name || !res.qualified_name[0]) {
236251
return NULL;
237252
}
238253
if (pxc_base_strategy_is_weak(res.strategy)) {
239254
return NULL;
240255
}
241-
if (!cbm_label_is_type_like(cbm_registry_label_of(reg, res.qualified_name))) {
256+
if (!cbm_label_is_type_like(cbm_registry_label_of(bs->reg, res.qualified_name))) {
257+
return NULL;
258+
}
259+
/* `unittest.TestCase` under an external `import unittest` survives the
260+
* weak-strategy veto through the same-module suffix fallback; the shared
261+
* external-base gate keeps it at its source spelling. */
262+
if (cbm_python_external_base_contradicts(bs->lang, bs->imports, raw, res.qualified_name,
263+
bs->gbuf, bs->project_name, bs->rel)) {
242264
return NULL;
243265
}
244266
return res.qualified_name;
@@ -248,8 +270,7 @@ static const char *pxc_resolve_base_qn(const cbm_registry_t *reg, const char *ra
248270
* source spelling. Unresolved entries pass through verbatim so a base the
249271
* registry does not know keeps working exactly as before. */
250272
static const char *pxc_join_base_qns(CBMArena *arena, const char *const *bases,
251-
const cbm_registry_t *reg, const char *module_qn,
252-
const char **imp_keys, const char **imp_vals, int imp_count) {
273+
const pxc_base_scope_t *bs, const char *module_qn) {
253274
if (!bases || !bases[0]) {
254275
return NULL;
255276
}
@@ -263,8 +284,7 @@ static const char *pxc_join_base_qns(CBMArena *arena, const char *const *bases,
263284
return pxc_join_pipe(arena, bases);
264285
}
265286
for (int i = 0; i < count; i++) {
266-
const char *qn =
267-
pxc_resolve_base_qn(reg, bases[i], module_qn, imp_keys, imp_vals, imp_count);
287+
const char *qn = pxc_resolve_base_qn(bs, bases[i], module_qn);
268288
resolved[i] = qn ? qn : bases[i];
269289
}
270290
resolved[count] = NULL;
@@ -390,8 +410,7 @@ static const char *pxc_qn_leaf(const char *name) {
390410
* pointers into src and into `arena` for synthesised composites. */
391411
static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const char *module_qn,
392412
const char *namespace_name, CBMLanguage lang, CBMLSPDef *dst,
393-
const cbm_registry_t *reg, const char **imp_keys,
394-
const char **imp_vals, int imp_count) {
413+
const pxc_base_scope_t *bs) {
395414
const char *label = pxc_map_label(arena, src->label);
396415
if (!label || !src->qualified_name || !src->name)
397416
return -1;
@@ -417,9 +436,8 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
417436
/* Languages whose cross registrars read embedded_types as QNs get their
418437
* bases resolved against the project registry; everyone else keeps the
419438
* raw source spelling their own registrar already knows how to handle. */
420-
dst->embedded_types = (reg && pxc_lang_resolves_base_qns(lang))
421-
? pxc_join_base_qns(arena, src->base_classes, reg, module_qn,
422-
imp_keys, imp_vals, imp_count)
439+
dst->embedded_types = (bs && bs->reg && pxc_lang_resolves_base_qns(lang))
440+
? pxc_join_base_qns(arena, src->base_classes, bs, module_qn)
423441
: pxc_join_pipe(arena, src->base_classes);
424442
dst->signature_param_types = NULL;
425443
dst->signature_param_count = 0;
@@ -593,11 +611,10 @@ typedef struct {
593611
} pxc_py_field_t;
594612

595613
static void pxc_fold_py_field_types(CBMArena *arena, const CBMFileResult *result, CBMLSPDef *defs,
596-
int start, int end, const cbm_registry_t *reg,
597-
const char *module_qn, const char **imp_keys,
598-
const char **imp_vals, int imp_count) {
614+
int start, int end, const pxc_base_scope_t *bs,
615+
const char *module_qn) {
599616
int nf = result ? result->field_types.count : 0;
600-
if (!arena || nf == 0 || !defs || start >= end || !reg) {
617+
if (!arena || nf == 0 || !defs || start >= end || !bs || !bs->reg) {
601618
return;
602619
}
603620
CBMHashTable *by_qn = cbm_ht_create((uint32_t)(end - start));
@@ -622,8 +639,7 @@ static void pxc_fold_py_field_types(CBMArena *arena, const CBMFileResult *result
622639
fields[f].type_qn = NULL;
623640
CBMLSPDef *owner = ft->class_qn ? (CBMLSPDef *)cbm_ht_get(by_qn, ft->class_qn) : NULL;
624641
const char *name = owner ? pxc_py_annotation_type_name(arena, ft->type_text) : NULL;
625-
const char *qn =
626-
name ? pxc_resolve_base_qn(reg, name, module_qn, imp_keys, imp_vals, imp_count) : NULL;
642+
const char *qn = name ? pxc_resolve_base_qn(bs, name, module_qn) : NULL;
627643
if (!qn || !ft->field_name || !ft->field_name[0]) {
628644
continue;
629645
}
@@ -744,27 +760,27 @@ CBMLSPDef *cbm_pxc_collect_all_defs(const cbm_pipeline_ctx_t *ctx, CBMArena *are
744760
* that consume resolved base QNs, and only when a caller supplied the
745761
* pipeline context (the surface-probe path passes NULL and keeps the
746762
* raw spelling). */
747-
const cbm_registry_t *base_reg = NULL;
748-
const char **imp_keys = NULL;
749-
const char **imp_vals = NULL;
750-
int imp_count = 0;
763+
pxc_base_scope_t bs = {.imports = &fr->imports,
764+
.project_name = project_name,
765+
.rel = files[fi].rel_path,
766+
.lang = files[fi].language};
751767
if (ctx && ctx->registry && pxc_lang_resolves_base_qns(files[fi].language)) {
752-
base_reg = ctx->registry;
768+
bs.reg = ctx->registry;
769+
bs.gbuf = ctx->gbuf;
753770
cbm_pxc_build_import_map(ctx->gbuf, project_name, files[fi].rel_path,
754-
files[fi].language, fr, &imp_keys, &imp_vals, &imp_count);
771+
files[fi].language, fr, &bs.imp_keys, &bs.imp_vals,
772+
&bs.imp_count);
755773
}
756774
for (int di = 0; di < fr->defs.count; di++) {
757775
if (pxc_build_lsp_def(arena, &fr->defs.items[di], def_modules[fi], namespace_name,
758-
files[fi].language, &defs[idx], base_reg, imp_keys, imp_vals,
759-
imp_count) == 0) {
776+
files[fi].language, &defs[idx], bs.reg ? &bs : NULL) == 0) {
760777
idx++;
761778
}
762779
}
763780
if (files[fi].language == CBM_LANG_PYTHON) {
764-
pxc_fold_py_field_types(arena, fr, defs, file_start, idx, base_reg, def_modules[fi],
765-
imp_keys, imp_vals, imp_count);
781+
pxc_fold_py_field_types(arena, fr, defs, file_start, idx, &bs, def_modules[fi]);
766782
}
767-
cbm_pxc_free_import_map(imp_keys, imp_vals, imp_count); /* NULL-safe */
783+
cbm_pxc_free_import_map(bs.imp_keys, bs.imp_vals, bs.imp_count); /* NULL-safe */
768784
if (files[fi].language == CBM_LANG_GO) {
769785
pxc_fold_go_struct_fields(arena, fr, defs, file_start, idx);
770786
}

‎src/pipeline/pass_parallel.c‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3357,16 +3357,27 @@ static void resolve_file_rw(resolve_ctx_t *rc, resolve_worker_state_t *ws, CBMFi
33573357
}
33583358
}
33593359

3360+
/* The per-file facts the base-class gate needs beyond the import map. */
3361+
typedef struct {
3362+
const CBMImportArray *imports;
3363+
const char *rel;
3364+
CBMLanguage lang;
3365+
} pp_file_scope_t;
3366+
33603367
/* Resolve base_classes → INHERITS edges for one definition. */
33613368
static void resolve_def_inherits(resolve_ctx_t *rc, resolve_worker_state_t *ws,
33623369
const CBMDefinition *def, const cbm_gbuf_node_t *node,
3363-
const char *mq, const char **ik, const char **iv, int ic) {
3370+
const pp_file_scope_t *fs, const char *mq, const char **ik,
3371+
const char **iv, int ic) {
33643372
if (!def->base_classes) {
33653373
return;
33663374
}
33673375
for (int b = 0; def->base_classes[b]; b++) {
33683376
const char *bqn = resolve_as_class(rc->registry, def->base_classes[b], mq, ik, iv, ic);
3369-
if (!bqn) {
3377+
/* Same external-base gate as the sequential semantic pass. */
3378+
if (!bqn ||
3379+
cbm_python_external_base_contradicts(fs->lang, fs->imports, def->base_classes[b], bqn,
3380+
rc->main_gbuf, rc->project_name, fs->rel)) {
33703381
continue;
33713382
}
33723383
const cbm_gbuf_node_t *bn = cbm_gbuf_find_by_qn(rc->main_gbuf, bqn);
@@ -3453,8 +3464,10 @@ static void resolve_def_decorators(resolve_ctx_t *rc, resolve_worker_state_t *ws
34533464

34543465
/* Resolve INHERITS + DECORATES + IMPLEMENTS for one file. */
34553466
static void resolve_file_semantic(resolve_ctx_t *rc, resolve_worker_state_t *ws,
3456-
CBMFileResult *result, const char *module_qn,
3457-
const char **imp_keys, const char **imp_vals, int imp_count) {
3467+
CBMFileResult *result, const char *rel, CBMLanguage lang,
3468+
const char *module_qn, const char **imp_keys,
3469+
const char **imp_vals, int imp_count) {
3470+
const pp_file_scope_t fs = {&result->imports, rel, lang};
34583471
for (int d = 0; d < result->defs.count; d++) {
34593472
CBMDefinition *def = &result->defs.items[d];
34603473
if (!def->qualified_name) {
@@ -3464,7 +3477,7 @@ static void resolve_file_semantic(resolve_ctx_t *rc, resolve_worker_state_t *ws,
34643477
if (!node) {
34653478
continue;
34663479
}
3467-
resolve_def_inherits(rc, ws, def, node, module_qn, imp_keys, imp_vals, imp_count);
3480+
resolve_def_inherits(rc, ws, def, node, &fs, module_qn, imp_keys, imp_vals, imp_count);
34683481
resolve_def_decorators(rc, ws, def, node, module_qn, imp_keys, imp_vals, imp_count);
34693482
}
34703483
for (int t = 0; t < result->impl_traits.count; t++) {
@@ -3813,7 +3826,7 @@ static void resolve_worker(int worker_id, void *ctx_ptr) {
38133826

38143827
/* ── INHERITS + DECORATES + IMPLEMENTS ──────────────────── */
38153828
_ph_t0 = extract_now_ns();
3816-
resolve_file_semantic(rc, ws, result, module_qn, imp_keys, imp_vals, imp_count);
3829+
resolve_file_semantic(rc, ws, result, rel, lang, module_qn, imp_keys, imp_vals, imp_count);
38173830
atomic_fetch_add_explicit(&rc->time_ns_semantic, extract_now_ns() - _ph_t0,
38183831
memory_order_relaxed);
38193832

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,11 @@ bool cbm_python_import_binding_contradicts(const CBMImportArray *imports, const
16761676
/* `import a.b` binds the ROOT package `a`: the callee already
16771677
* spells its own full dotted path (`a.b.f()`). */
16781678
snprintf(full, sizeof(full), "%s", callee_name);
1679+
} else if (!as && !path_leaf_seg && full[0] != '.' && strcmp(full, imp->local_name) != 0) {
1680+
/* `import m as y` (the extractor stores module_path `m`, local
1681+
* `y`; a from-import always spells a dotted path): `y` IS module
1682+
* `m`, so `y.T` spells `m.T`. */
1683+
snprintf(full, sizeof(full), "%s%s", imp->module_path, callee_name + root_len);
16791684
}
16801685
/* Otherwise (`from m import x [as y]`) only the import's own module
16811686
* chain is evidence: members reached THROUGH x (`x.objects.create`)
@@ -1691,6 +1696,27 @@ bool cbm_python_import_binding_contradicts(const CBMImportArray *imports, const
16911696
return bound;
16921697
}
16931698

1699+
/* Base-class twin of the #2127 call gate. `class SimpleTestCase(
1700+
* unittest.TestCase)` under `import unittest` names the STDLIB base; the
1701+
* registry's same-module suffix fallback (`unittest.TestCase` -> module's
1702+
* `TestCase`) or a short-name strategy bound it to a same-named PROJECT class
1703+
* (django: an inheritance cycle SimpleTestCase -> TestCase ->
1704+
* TransactionTestCase -> SimpleTestCase, plus fabricated OVERRIDEs). An
1705+
* external import binding (never materialized as an IMPORTS edge) whose
1706+
* module chain the target does not spell can never be that target, whatever
1707+
* the registry strategy. Exact metadata import maps remain valid when the
1708+
* target spells the imported module chain. Every base resolver (sequential
1709+
* + parallel semantic, cross-LSP pxc) calls this ONE
1710+
* gate so the venues cannot diverge. Python only. */
1711+
bool cbm_python_external_base_contradicts(CBMLanguage lang, const CBMImportArray *imports,
1712+
const char *base_spelling, const char *base_qn,
1713+
const cbm_gbuf_t *gbuf, const char *project_name,
1714+
const char *rel_path) {
1715+
return lang == CBM_LANG_PYTHON &&
1716+
cbm_python_import_binding_contradicts(imports, base_spelling, base_qn, gbuf,
1717+
project_name, rel_path);
1718+
}
1719+
16941720
/* #2127: whether a Python import's module lives in this project. Relative
16951721
* imports always do; otherwise the module part (the path minus the imported
16961722
* name) must resolve to a graph node. Strategy 1 has already failed on the

0 commit comments

Comments
 (0)