Skip to content

Commit c202539

Browse files
authored
Merge pull request DeusData#2514 from DeusData/fix/issue-1278-override-v3
Fix explicit overrides through non-redeclaring ancestors
2 parents 2c4c784 + 19b13ba commit c202539

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
@@ -223,25 +223,47 @@ static bool pxc_base_strategy_is_weak(const char *strategy) {
223223
strcmp(strategy, "field_type_hint") == 0 || strcmp(strategy, "fuzzy") == 0;
224224
}
225225

226+
/* One file's base-resolution scope: the registry + import map pass_semantic
227+
* uses, plus the facts the external-base gate reads. NULL (the surface-probe
228+
* path) keeps every base at its raw spelling. */
229+
typedef struct {
230+
const cbm_registry_t *reg;
231+
const char **imp_keys;
232+
const char **imp_vals;
233+
int imp_count;
234+
const CBMImportArray *imports;
235+
const cbm_gbuf_t *gbuf;
236+
const char *project_name;
237+
const char *rel;
238+
CBMLanguage lang;
239+
} pxc_base_scope_t;
240+
226241
/* Resolve one base-class spelling to a project QN. Mirrors
227242
* pass_semantic.c::resolve_as_class — same registry, same type-like veto —
228243
* then additionally rejects weak short-name strategies (see above).
229244
* Returns NULL when the base is not a confidently-known project type;
230245
* stdlib and third-party bases land here and keep their raw spelling. */
231-
static const char *pxc_resolve_base_qn(const cbm_registry_t *reg, const char *raw,
232-
const char *module_qn, const char **imp_keys,
233-
const char **imp_vals, int imp_count) {
234-
if (!reg || !raw || !raw[0]) {
246+
static const char *pxc_resolve_base_qn(const pxc_base_scope_t *bs, const char *raw,
247+
const char *module_qn) {
248+
if (!bs || !bs->reg || !raw || !raw[0]) {
235249
return NULL;
236250
}
237-
cbm_resolution_t res = cbm_registry_resolve(reg, raw, module_qn, imp_keys, imp_vals, imp_count);
251+
cbm_resolution_t res =
252+
cbm_registry_resolve(bs->reg, raw, module_qn, bs->imp_keys, bs->imp_vals, bs->imp_count);
238253
if (!res.qualified_name || !res.qualified_name[0]) {
239254
return NULL;
240255
}
241256
if (pxc_base_strategy_is_weak(res.strategy)) {
242257
return NULL;
243258
}
244-
if (!cbm_label_is_type_like(cbm_registry_label_of(reg, res.qualified_name))) {
259+
if (!cbm_label_is_type_like(cbm_registry_label_of(bs->reg, res.qualified_name))) {
260+
return NULL;
261+
}
262+
/* `unittest.TestCase` under an external `import unittest` survives the
263+
* weak-strategy veto through the same-module suffix fallback; the shared
264+
* external-base gate keeps it at its source spelling. */
265+
if (cbm_python_external_base_contradicts(bs->lang, bs->imports, raw, res.qualified_name,
266+
bs->gbuf, bs->project_name, bs->rel)) {
245267
return NULL;
246268
}
247269
return res.qualified_name;
@@ -251,8 +273,7 @@ static const char *pxc_resolve_base_qn(const cbm_registry_t *reg, const char *ra
251273
* source spelling. Unresolved entries pass through verbatim so a base the
252274
* registry does not know keeps working exactly as before. */
253275
static const char *pxc_join_base_qns(CBMArena *arena, const char *const *bases,
254-
const cbm_registry_t *reg, const char *module_qn,
255-
const char **imp_keys, const char **imp_vals, int imp_count) {
276+
const pxc_base_scope_t *bs, const char *module_qn) {
256277
if (!bases || !bases[0]) {
257278
return NULL;
258279
}
@@ -266,8 +287,7 @@ static const char *pxc_join_base_qns(CBMArena *arena, const char *const *bases,
266287
return pxc_join_pipe(arena, bases);
267288
}
268289
for (int i = 0; i < count; i++) {
269-
const char *qn =
270-
pxc_resolve_base_qn(reg, bases[i], module_qn, imp_keys, imp_vals, imp_count);
290+
const char *qn = pxc_resolve_base_qn(bs, bases[i], module_qn);
271291
resolved[i] = qn ? qn : bases[i];
272292
}
273293
resolved[count] = NULL;
@@ -403,8 +423,7 @@ static const char *pxc_qn_leaf(const char *name) {
403423
* pointers into src and into `arena` for synthesised composites. */
404424
static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const char *module_qn,
405425
const char *namespace_name, CBMLanguage lang, CBMLSPDef *dst,
406-
const cbm_registry_t *reg, const char **imp_keys,
407-
const char **imp_vals, int imp_count) {
426+
const pxc_base_scope_t *bs) {
408427
const char *label = pxc_map_label(arena, src->label);
409428
if (!label || !src->qualified_name || !src->name)
410429
return -1;
@@ -430,9 +449,8 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
430449
/* Languages whose cross registrars read embedded_types as QNs get their
431450
* bases resolved against the project registry; everyone else keeps the
432451
* raw source spelling their own registrar already knows how to handle. */
433-
dst->embedded_types = (reg && pxc_lang_resolves_base_qns(lang))
434-
? pxc_join_base_qns(arena, src->base_classes, reg, module_qn,
435-
imp_keys, imp_vals, imp_count)
452+
dst->embedded_types = (bs && bs->reg && pxc_lang_resolves_base_qns(lang))
453+
? pxc_join_base_qns(arena, src->base_classes, bs, module_qn)
436454
: pxc_join_pipe(arena, src->base_classes);
437455
dst->signature_param_types = NULL;
438456
dst->signature_param_count = 0;
@@ -610,11 +628,10 @@ typedef struct {
610628
} pxc_py_field_t;
611629

612630
static void pxc_fold_py_field_types(CBMArena *arena, const CBMFileResult *result, CBMLSPDef *defs,
613-
int start, int end, const cbm_registry_t *reg,
614-
const char *module_qn, const char **imp_keys,
615-
const char **imp_vals, int imp_count) {
631+
int start, int end, const pxc_base_scope_t *bs,
632+
const char *module_qn) {
616633
int nf = result ? result->field_types.count : 0;
617-
if (!arena || nf == 0 || !defs || start >= end || !reg) {
634+
if (!arena || nf == 0 || !defs || start >= end || !bs || !bs->reg) {
618635
return;
619636
}
620637
CBMHashTable *by_qn = cbm_ht_create((uint32_t)(end - start));
@@ -639,8 +656,7 @@ static void pxc_fold_py_field_types(CBMArena *arena, const CBMFileResult *result
639656
fields[f].type_qn = NULL;
640657
CBMLSPDef *owner = ft->class_qn ? (CBMLSPDef *)cbm_ht_get(by_qn, ft->class_qn) : NULL;
641658
const char *name = owner ? pxc_py_annotation_type_name(arena, ft->type_text) : NULL;
642-
const char *qn =
643-
name ? pxc_resolve_base_qn(reg, name, module_qn, imp_keys, imp_vals, imp_count) : NULL;
659+
const char *qn = name ? pxc_resolve_base_qn(bs, name, module_qn) : NULL;
644660
if (!qn || !ft->field_name || !ft->field_name[0]) {
645661
continue;
646662
}
@@ -782,27 +798,27 @@ CBMLSPDef *cbm_pxc_collect_all_defs(const cbm_pipeline_ctx_t *ctx, CBMArena *are
782798
* that consume resolved base QNs, and only when a caller supplied the
783799
* pipeline context (the surface-probe path passes NULL and keeps the
784800
* raw spelling). */
785-
const cbm_registry_t *base_reg = NULL;
786-
const char **imp_keys = NULL;
787-
const char **imp_vals = NULL;
788-
int imp_count = 0;
801+
pxc_base_scope_t bs = {.imports = &fr->imports,
802+
.project_name = project_name,
803+
.rel = files[fi].rel_path,
804+
.lang = files[fi].language};
789805
if (ctx && ctx->registry && pxc_lang_resolves_base_qns(files[fi].language)) {
790-
base_reg = ctx->registry;
806+
bs.reg = ctx->registry;
807+
bs.gbuf = ctx->gbuf;
791808
cbm_pxc_build_import_map(ctx->gbuf, project_name, files[fi].rel_path,
792-
files[fi].language, fr, &imp_keys, &imp_vals, &imp_count);
809+
files[fi].language, fr, &bs.imp_keys, &bs.imp_vals,
810+
&bs.imp_count);
793811
}
794812
for (int di = 0; di < fr->defs.count; di++) {
795813
if (pxc_build_lsp_def(arena, &fr->defs.items[di], def_modules[fi], namespace_name,
796-
files[fi].language, &defs[idx], base_reg, imp_keys, imp_vals,
797-
imp_count) == 0) {
814+
files[fi].language, &defs[idx], bs.reg ? &bs : NULL) == 0) {
798815
idx++;
799816
}
800817
}
801818
if (files[fi].language == CBM_LANG_PYTHON) {
802-
pxc_fold_py_field_types(arena, fr, defs, file_start, idx, base_reg, def_modules[fi],
803-
imp_keys, imp_vals, imp_count);
819+
pxc_fold_py_field_types(arena, fr, defs, file_start, idx, &bs, def_modules[fi]);
804820
}
805-
cbm_pxc_free_import_map(imp_keys, imp_vals, imp_count); /* NULL-safe */
821+
cbm_pxc_free_import_map(bs.imp_keys, bs.imp_vals, bs.imp_count); /* NULL-safe */
806822
if (files[fi].language == CBM_LANG_GO) {
807823
pxc_fold_struct_fields(arena, fr, defs, file_start, idx, "Struct");
808824
} else if (files[fi].language == CBM_LANG_C || files[fi].language == CBM_LANG_CPP) {

‎src/pipeline/pass_parallel.c‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3686,16 +3686,27 @@ static void resolve_file_rw(resolve_ctx_t *rc, resolve_worker_state_t *ws, CBMFi
36863686
cbm_pipeline_lsp_field_index_free(&field_index);
36873687
}
36883688

3689+
/* The per-file facts the base-class gate needs beyond the import map. */
3690+
typedef struct {
3691+
const CBMImportArray *imports;
3692+
const char *rel;
3693+
CBMLanguage lang;
3694+
} pp_file_scope_t;
3695+
36893696
/* Resolve base_classes → INHERITS edges for one definition. */
36903697
static void resolve_def_inherits(resolve_ctx_t *rc, resolve_worker_state_t *ws,
36913698
const CBMDefinition *def, const cbm_gbuf_node_t *node,
3692-
const char *mq, const char **ik, const char **iv, int ic) {
3699+
const pp_file_scope_t *fs, const char *mq, const char **ik,
3700+
const char **iv, int ic) {
36933701
if (!def->base_classes) {
36943702
return;
36953703
}
36963704
for (int b = 0; def->base_classes[b]; b++) {
36973705
const char *bqn = resolve_as_class(rc->registry, def->base_classes[b], mq, ik, iv, ic);
3698-
if (!bqn) {
3706+
/* Same external-base gate as the sequential semantic pass. */
3707+
if (!bqn ||
3708+
cbm_python_external_base_contradicts(fs->lang, fs->imports, def->base_classes[b], bqn,
3709+
rc->main_gbuf, rc->project_name, fs->rel)) {
36993710
continue;
37003711
}
37013712
const cbm_gbuf_node_t *bn = cbm_gbuf_find_by_qn(rc->main_gbuf, bqn);
@@ -3782,8 +3793,10 @@ static void resolve_def_decorators(resolve_ctx_t *rc, resolve_worker_state_t *ws
37823793

37833794
/* Resolve INHERITS + DECORATES + IMPLEMENTS for one file. */
37843795
static void resolve_file_semantic(resolve_ctx_t *rc, resolve_worker_state_t *ws,
3785-
CBMFileResult *result, const char *module_qn,
3786-
const char **imp_keys, const char **imp_vals, int imp_count) {
3796+
CBMFileResult *result, const char *rel, CBMLanguage lang,
3797+
const char *module_qn, const char **imp_keys,
3798+
const char **imp_vals, int imp_count) {
3799+
const pp_file_scope_t fs = {&result->imports, rel, lang};
37873800
for (int d = 0; d < result->defs.count; d++) {
37883801
CBMDefinition *def = &result->defs.items[d];
37893802
if (!def->qualified_name) {
@@ -3793,7 +3806,7 @@ static void resolve_file_semantic(resolve_ctx_t *rc, resolve_worker_state_t *ws,
37933806
if (!node) {
37943807
continue;
37953808
}
3796-
resolve_def_inherits(rc, ws, def, node, module_qn, imp_keys, imp_vals, imp_count);
3809+
resolve_def_inherits(rc, ws, def, node, &fs, module_qn, imp_keys, imp_vals, imp_count);
37973810
resolve_def_decorators(rc, ws, def, node, module_qn, imp_keys, imp_vals, imp_count);
37983811
}
37993812
for (int t = 0; t < result->impl_traits.count; t++) {
@@ -4215,7 +4228,7 @@ static void resolve_worker(int worker_id, void *ctx_ptr) {
42154228

42164229
/* ── INHERITS + DECORATES + IMPLEMENTS ──────────────────── */
42174230
_ph_t0 = extract_now_ns();
4218-
resolve_file_semantic(rc, ws, result, module_qn, imp_keys, imp_vals, imp_count);
4231+
resolve_file_semantic(rc, ws, result, rel, lang, module_qn, imp_keys, imp_vals, imp_count);
42194232
atomic_fetch_add_explicit(&rc->time_ns_semantic, extract_now_ns() - _ph_t0,
42204233
memory_order_relaxed);
42214234

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,11 @@ bool cbm_python_import_binding_contradicts(const CBMImportArray *imports, const
17001700
/* `import a.b` binds the ROOT package `a`: the callee already
17011701
* spells its own full dotted path (`a.b.f()`). */
17021702
snprintf(full, sizeof(full), "%s", callee_name);
1703+
} else if (!as && !path_leaf_seg && full[0] != '.' && strcmp(full, imp->local_name) != 0) {
1704+
/* `import m as y` (the extractor stores module_path `m`, local
1705+
* `y`; a from-import always spells a dotted path): `y` IS module
1706+
* `m`, so `y.T` spells `m.T`. */
1707+
snprintf(full, sizeof(full), "%s%s", imp->module_path, callee_name + root_len);
17031708
}
17041709
/* Otherwise (`from m import x [as y]`) only the import's own module
17051710
* chain is evidence: members reached THROUGH x (`x.objects.create`)
@@ -1715,6 +1720,27 @@ bool cbm_python_import_binding_contradicts(const CBMImportArray *imports, const
17151720
return bound;
17161721
}
17171722

1723+
/* Base-class twin of the #2127 call gate. `class SimpleTestCase(
1724+
* unittest.TestCase)` under `import unittest` names the STDLIB base; the
1725+
* registry's same-module suffix fallback (`unittest.TestCase` -> module's
1726+
* `TestCase`) or a short-name strategy bound it to a same-named PROJECT class
1727+
* (django: an inheritance cycle SimpleTestCase -> TestCase ->
1728+
* TransactionTestCase -> SimpleTestCase, plus fabricated OVERRIDEs). An
1729+
* external import binding (never materialized as an IMPORTS edge) whose
1730+
* module chain the target does not spell can never be that target, whatever
1731+
* the registry strategy. Exact metadata import maps remain valid when the
1732+
* target spells the imported module chain. Every base resolver (sequential
1733+
* + parallel semantic, cross-LSP pxc) calls this ONE
1734+
* gate so the venues cannot diverge. Python only. */
1735+
bool cbm_python_external_base_contradicts(CBMLanguage lang, const CBMImportArray *imports,
1736+
const char *base_spelling, const char *base_qn,
1737+
const cbm_gbuf_t *gbuf, const char *project_name,
1738+
const char *rel_path) {
1739+
return lang == CBM_LANG_PYTHON &&
1740+
cbm_python_import_binding_contradicts(imports, base_spelling, base_qn, gbuf,
1741+
project_name, rel_path);
1742+
}
1743+
17181744
/* #2127: whether a Python import's module lives in this project. Relative
17191745
* imports always do; otherwise the module part (the path minus the imported
17201746
* name) must resolve to a graph node. Strategy 1 has already failed on the

0 commit comments

Comments
 (0)