@@ -3625,18 +3625,62 @@ static void rust_resolve_call_expression(RustLSPContext *ctx, TSNode node) {
36253625 }
36263626 }
36273627
3628- /* Global short-name fallback: scan the registry for a unique
3629- * function whose short_name matches the path's tail and whose
3630- * QN starts with the current crate prefix. This gives `mod
3631- * foo; use foo::bar; bar()` a chance to resolve when the
3632- * intermediate module wasn't tracked through an explicit
3633- * use-map entry. */
36343628 const char * tail = strrchr (path , ':' );
36353629 if (tail && tail > path && tail [-1 ] == ':' ) {
36363630 tail += 1 ;
36373631 } else {
36383632 tail = path ;
36393633 }
3634+
3635+ /* Cross-crate workspace-member resolution (#56): when the call
3636+ * path's head is a declared Cargo workspace member (e.g.
3637+ * `crate_a::helper` from inside crate_b) we cannot rely on the
3638+ * caller-crate-scoped fallback below — that filters by the
3639+ * CALLER's module prefix and would resolve to a same-named local
3640+ * function instead. Route to the function defined inside the
3641+ * MEMBER crate by matching the registered QN's `.<member>.`
3642+ * path segment plus the call tail. Requires a parsed manifest
3643+ * (threaded through pass_lsp_cross.c); NULL manifest skips this. */
3644+ if (ctx -> cargo_manifest && tail && * tail ) {
3645+ const char * head_sep = strstr (path , "::" );
3646+ if (head_sep && head_sep > path ) {
3647+ char * head = cbm_arena_strndup (ctx -> arena , path , (size_t )(head_sep - path ));
3648+ const CBMCargoManifest * m = (const CBMCargoManifest * )ctx -> cargo_manifest ;
3649+ if (head && cbm_cargo_find_member (m , head )) {
3650+ /* `.crate_a.` — the member directory appears as a dotted
3651+ * QN segment for every def inside that crate. */
3652+ char * needle = cbm_arena_sprintf (ctx -> arena , ".%s." , head );
3653+ const CBMRegisteredFunc * mem_unique = NULL ;
3654+ int mem_matches = 0 ;
3655+ for (int i = 0 ; i < ctx -> registry -> func_count && mem_matches < 2 ; i ++ ) {
3656+ const CBMRegisteredFunc * f = & ctx -> registry -> funcs [i ];
3657+ if (!f -> short_name || !f -> qualified_name )
3658+ continue ;
3659+ if (f -> receiver_type )
3660+ continue ; /* free functions only */
3661+ if (strcmp (f -> short_name , tail ) != 0 )
3662+ continue ;
3663+ if (!strstr (f -> qualified_name , needle ))
3664+ continue ; /* not defined in the member crate */
3665+ mem_matches ++ ;
3666+ if (mem_matches == 1 )
3667+ mem_unique = f ;
3668+ }
3669+ if (mem_matches == 1 && mem_unique ) {
3670+ rust_emit_resolved_call (ctx , mem_unique -> qualified_name , "lsp_cross_crate" ,
3671+ CBM_RUST_CONF_DIRECT );
3672+ return ;
3673+ }
3674+ }
3675+ }
3676+ }
3677+
3678+ /* Global short-name fallback: scan the registry for a unique
3679+ * function whose short_name matches the path's tail and whose
3680+ * QN starts with the current crate prefix. This gives `mod
3681+ * foo; use foo::bar; bar()` a chance to resolve when the
3682+ * intermediate module wasn't tracked through an explicit
3683+ * use-map entry. */
36403684 if (tail && * tail && ctx -> module_qn ) {
36413685 /* Crate prefix is the first dotted segment of module_qn after
36423686 * the project name, but for simplicity we just match on
@@ -5121,10 +5165,12 @@ void cbm_run_rust_lsp(CBMArena *arena, CBMFileResult *result, const char *source
51215165
51225166extern const TSLanguage * tree_sitter_rust (void );
51235167
5124- void cbm_run_rust_lsp_cross (CBMArena * arena , const char * source , int source_len ,
5125- const char * module_qn , CBMRustLSPDef * defs , int def_count ,
5126- const char * * import_names , const char * * import_qns , int import_count ,
5127- TSTree * cached_tree , CBMResolvedCallArray * out ) {
5168+ void cbm_run_rust_lsp_cross_with_manifest (CBMArena * arena , const char * source , int source_len ,
5169+ const char * module_qn , CBMRustLSPDef * defs , int def_count ,
5170+ const char * * import_names , const char * * import_qns ,
5171+ int import_count , TSTree * cached_tree ,
5172+ const struct CBMCargoManifest * manifest ,
5173+ CBMResolvedCallArray * out ) {
51285174 if (!source || source_len <= 0 || !out )
51295175 return ;
51305176
@@ -5251,6 +5297,10 @@ void cbm_run_rust_lsp_cross(CBMArena *arena, const char *source, int source_len,
52515297
52525298 RustLSPContext ctx ;
52535299 rust_lsp_init (& ctx , arena , source , source_len , & reg , module_qn , out );
5300+ /* Workspace/dependency awareness for cross-CRATE path routing (#56).
5301+ * Mirrors the single-file path (cbm_run_rust_lsp_with_manifest). NULL
5302+ * when no Cargo.toml was parsed — in-crate resolution is unaffected. */
5303+ ctx .cargo_manifest = manifest ;
52545304 rust_collect_uses (& ctx , root );
52555305 for (int i = 0 ; i < import_count ; i ++ ) {
52565306 if (import_names [i ] && import_qns [i ]) {
@@ -5266,6 +5316,18 @@ void cbm_run_rust_lsp_cross(CBMArena *arena, const char *source, int source_len,
52665316 }
52675317}
52685318
5319+ /* Manifest-free entry point. Preserves the pre-existing signature used by
5320+ * the unit tests (test_rust_lsp.c) and the batch wrapper — delegates to
5321+ * the manifest-aware variant with a NULL manifest. */
5322+ void cbm_run_rust_lsp_cross (CBMArena * arena , const char * source , int source_len ,
5323+ const char * module_qn , CBMRustLSPDef * defs , int def_count ,
5324+ const char * * import_names , const char * * import_qns , int import_count ,
5325+ TSTree * cached_tree , CBMResolvedCallArray * out ) {
5326+ cbm_run_rust_lsp_cross_with_manifest (arena , source , source_len , module_qn , defs , def_count ,
5327+ import_names , import_qns , import_count , cached_tree , NULL ,
5328+ out );
5329+ }
5330+
52695331void cbm_batch_rust_lsp_cross (CBMArena * arena , CBMBatchRustLSPFile * files , int file_count ,
52705332 CBMResolvedCallArray * out ) {
52715333 if (!files || file_count <= 0 || !out )
0 commit comments