@@ -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. */
253275static 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. */
404424static 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
612630static 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 ) {
0 commit comments