@@ -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. */
250272static 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. */
391411static 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
595613static 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 }
0 commit comments