Skip to content

Commit 95f0912

Browse files
committed
fix(cs-lsp): bind same-name C# types by declared namespace (#2120)
Two projects declaring a class with the same simple name in namespaces that share a root (Contoso.Platform.Identity.UserIdentityInfo vs Contoso.Platform.BackOffice.Identity.UserIdentityInfo) had every CALLS edge attached to whichever variant registered first, ignoring the caller's `using` directive, enclosing namespace, fully-qualified name and using-alias. Root cause: C# graph QNs are path-derived, so cs_resolve_type_name's namespace-prefix and using-namespace lookups (steps 5 and 7) never hit a project type. Resolution fell through to the short-name fallback, whose prefix score compares the file namespace against the path QN and is 0 for every candidate, so registry order decided. Fix: - Record the declared namespace of each top-level C# type at extraction (CBMDefinition.decl_namespace, walked from the AST). The file-level namespace_name keeps only a file's FIRST namespace, which mislabels every type of a multi-namespace file such as a reference assembly. pxc_build_lsp_def carries it into the type's CBMLSPDef, and result_compact relocates it. - CBMRegisteredType.namespace_qn (C# only, top-level types only) is set by cs_register_lsp_defs, which both the Tier-2 shared registry and the per-file registry use, so sequential and parallel stay in lockstep. - New resolution step 8b binds a simple or qualified type name to the visible declaration in C# lookup order: enclosing namespaces innermost outward, then `using` namespaces (or the global namespace for a qualified name). A `using A = Ns.Type` alias binds the same way. - cs_lookup_method also searches the type's other same-namespace declarations (`partial` pieces, reference-assembly copies). These are separate registry entries but one C# type. Without this, choosing the namespace-correct piece that lacks the member would lose the edge to a name-only fallback. Proof on dotnet/runtime src/libraries (20,866 .cs files, same machine, both binaries deterministic across two runs): CALLS 649,226 -> 657,564; nodes unchanged (595,714). Most retargeted edges move from a target the caller cannot see to one it can see through an enclosing namespace or `using`. Many more move from non-method targets (enum members such as ExpressionType.Constant) to the real partial-class methods (Expression.Constant). The new edges are unqualified and typed calls into sibling `partial` pieces (ImmutableArray.Create, XmlReader.Create, AsnReader.ReadBoolean). Test: parallel::parallel_csharp_same_name_class_binds_by_namespace covers plain `using` in both directions, the enclosing namespace, a fully-qualified name, a multi-namespace file and a `partial` member, in the sequential pipeline and in the parallel pipeline with the production Tier-2 C# registry. The harness gains an opt-in flag for that registry, because cbm_pxc_run_one has no C# case. Signed-off-by: Martin Vogel <[email protected]>
1 parent 0f52d30 commit 95f0912

8 files changed

Lines changed: 416 additions & 7 deletions

File tree

‎internal/cbm/cbm.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ typedef struct {
246246
* HTTP_CALLS edge to base + path. Tail fields: zero-init stays valid. */
247247
const char *http_client;
248248
const char *http_base_url;
249+
/* C# only: declared namespace of a TOP-LEVEL type (NULL for nested types,
250+
* the global namespace and every other language). Per type, because the
251+
* file-level namespace_name records only a file's first namespace. */
252+
const char *decl_namespace;
249253
} CBMDefinition;
250254

251255
/* Argument captured from a call expression */

‎internal/cbm/extract_defs.c‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4446,6 +4446,58 @@ static bool extract_sql_ddl_class_def(CBMExtractCtx *ctx, TSNode node, const cha
44464446
return true;
44474447
}
44484448

4449+
/* Name of a C# namespace declaration node (block or file-scoped), or NULL. */
4450+
static const char *cs_namespace_decl_name(CBMArena *a, TSNode ns, const char *source) {
4451+
TSNode nm = ts_node_child_by_field_name(ns, TS_FIELD("name"));
4452+
if (ts_node_is_null(nm)) {
4453+
return NULL;
4454+
}
4455+
const char *text = cbm_node_text(a, nm, source);
4456+
return text && text[0] ? text : NULL;
4457+
}
4458+
4459+
/* Declared namespace of a C# type declaration: every enclosing namespace
4460+
* block (outer to inner) under an optional file-scoped namespace, which the
4461+
* grammar may attach either as an ancestor or as a preceding sibling of the
4462+
* compilation unit's members. NULL = global namespace. The file-level
4463+
* namespace_name keeps only the FIRST namespace of a file, which mislabels
4464+
* every type of a multi-namespace file (#2120). */
4465+
static const char *cs_type_decl_namespace(CBMArena *a, TSNode node, const char *source) {
4466+
const char *acc = NULL;
4467+
bool file_scoped = false;
4468+
TSNode top = node;
4469+
for (TSNode cur = ts_node_parent(node); !ts_node_is_null(cur); cur = ts_node_parent(cur)) {
4470+
const char *k = ts_node_type(cur);
4471+
bool is_file_scoped = strcmp(k, "file_scoped_namespace_declaration") == 0;
4472+
if (is_file_scoped || strcmp(k, "namespace_declaration") == 0) {
4473+
const char *name = cs_namespace_decl_name(a, cur, source);
4474+
if (name) {
4475+
acc = acc ? cbm_arena_sprintf(a, "%s.%s", name, acc) : name;
4476+
}
4477+
file_scoped = file_scoped || is_file_scoped;
4478+
}
4479+
top = cur;
4480+
}
4481+
if (file_scoped) {
4482+
return acc;
4483+
}
4484+
uint32_t start = ts_node_start_byte(node);
4485+
uint32_t n = ts_node_named_child_count(top);
4486+
for (uint32_t i = 0; i < n; i++) {
4487+
TSNode c = ts_node_named_child(top, i);
4488+
if (ts_node_start_byte(c) >= start) {
4489+
break;
4490+
}
4491+
if (strcmp(ts_node_type(c), "file_scoped_namespace_declaration") == 0) {
4492+
const char *name = cs_namespace_decl_name(a, c, source);
4493+
if (name) {
4494+
return acc ? cbm_arena_sprintf(a, "%s.%s", name, acc) : name;
4495+
}
4496+
}
4497+
}
4498+
return acc;
4499+
}
4500+
44494501
static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec) {
44504502
CBMArena *a = ctx->arena;
44514503
const char *kind = ts_node_type(node);
@@ -4813,6 +4865,9 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
48134865
def.end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;
48144866
def.lines = (int)(def.end_line - def.start_line + TS_LINE_OFFSET);
48154867
def.is_exported = cbm_is_exported(name, ctx->language);
4868+
if (ctx->language == CBM_LANG_CSHARP && !ctx->enclosing_class_qn) {
4869+
def.decl_namespace = cs_type_decl_namespace(a, node, ctx->source);
4870+
}
48164871
def.base_classes = extract_base_classes(a, node, ctx->source, ctx->language);
48174872
def.decorators = extract_decorators(a, node, ctx->source, ctx->language, spec);
48184873
def.docstring = extract_docstring(a, node, ctx->source, ctx->language);

‎internal/cbm/lsp/cs_lsp.c‎

Lines changed: 145 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ static char *cs_node_text_cached(CSLSPContext *ctx, TSNode node);
7474
static const CBMType *cs_unwrap_task(CSLSPContext *ctx, const CBMType *t);
7575
static const CBMType *cs_unwrap_nullable(const CBMType *t);
7676
static const char *cs_resolve_callable_value(CSLSPContext *ctx, TSNode node, TSNode *callable_leaf);
77+
static const char *cs_top_level_namespace(const char *type_qn, const char *short_name,
78+
const char *module_qn, const char *namespace_name);
7779

7880
/* ── small helpers ──────────────────────────────────────────────── */
7981

@@ -356,6 +358,72 @@ static const char *cs_try_type_qn(CSLSPContext *ctx, const char *qn) {
356358
return NULL;
357359
}
358360

361+
/* Namespaces through which `qualifier` (the part of a type reference before
362+
* its simple name, NULL for a simple name) can reach a top-level type, in C#
363+
* lookup order: each enclosing namespace innermost outward, then the global
364+
* namespace (qualified references only) or the file's `using` namespaces
365+
* (simple names only -- a using-namespace directive does not import nested
366+
* namespaces). Returns the count written to `out` (at most `cap`). */
367+
static int cs_visible_namespaces(CSLSPContext *ctx, const char *qualifier, const char **out,
368+
int cap) {
369+
int n = 0;
370+
const char *ns = ctx->namespace_count > 0 ? cs_namespace_qn(ctx) : NULL;
371+
while (ns && *ns && n < cap) {
372+
out[n++] = qualifier ? cbm_arena_sprintf(ctx->arena, "%s.%s", ns, qualifier) : ns;
373+
const char *dot = strrchr(ns, '.');
374+
ns = dot ? cbm_arena_strndup(ctx->arena, ns, (size_t)(dot - ns)) : NULL;
375+
}
376+
if (qualifier) {
377+
if (n < cap)
378+
out[n++] = qualifier;
379+
return n;
380+
}
381+
for (int i = 0; i < ctx->using_count && n < cap; i++) {
382+
const CBMCSUsing *u = &ctx->usings[i];
383+
if (u->kind == CBM_CS_USING_NAMESPACE && u->target_qn && u->target_qn[0])
384+
out[n++] = u->target_qn;
385+
}
386+
return n;
387+
}
388+
389+
/* Bind a (possibly namespace-qualified) type reference to the project type
390+
* whose DECLARED namespace the current file can see, preferring the earliest
391+
* namespace in C# lookup order; equal ranks keep registration order. NULL when
392+
* no top-level type with that simple name is visible (#2120). */
393+
static const char *cs_resolve_by_declared_namespace(CSLSPContext *ctx, const char *name) {
394+
if (!ctx->registry || !name || !*name)
395+
return NULL;
396+
const char *simple = cs_short_name(name);
397+
if (!simple || !*simple)
398+
return NULL;
399+
const char *qualifier =
400+
simple > name ? cbm_arena_strndup(ctx->arena, name, (size_t)(simple - name - 1)) : NULL;
401+
enum { CS_VISIBLE_NS_MAX = 128 };
402+
const char *visible[CS_VISIBLE_NS_MAX];
403+
int visible_count = cs_visible_namespaces(ctx, qualifier, visible, CS_VISIBLE_NS_MAX);
404+
if (visible_count == 0)
405+
return NULL;
406+
407+
const CBMRegisteredType *best = NULL;
408+
int best_rank = visible_count;
409+
CBMTypeShortIter it;
410+
cbm_registry_types_by_short_name_chain(ctx->registry, simple, &it);
411+
int i;
412+
while ((i = cbm_type_short_iter_next(&it)) >= 0) {
413+
const CBMRegisteredType *cand = &it.reg->types[i];
414+
if (!cand->namespace_qn || !cand->short_name || strcmp(cand->short_name, simple) != 0)
415+
continue;
416+
for (int r = 0; r < best_rank; r++) {
417+
if (strcmp(cand->namespace_qn, visible[r]) == 0) {
418+
best = cand;
419+
best_rank = r;
420+
break;
421+
}
422+
}
423+
}
424+
return best ? best->qualified_name : NULL;
425+
}
426+
359427
/* Returns a fully-qualified type name resolved against:
360428
* 1. predefined alias (int -> System.Int32)
361429
* 2. type-parameter substitution map
@@ -365,7 +433,9 @@ static const char *cs_try_type_qn(CSLSPContext *ctx, const char *qn) {
365433
* 6. module-prefixed (file-local QN that the unified extractor produced)
366434
* 7. each `using namespace X` prefix
367435
* 8. each `using A = X` alias substitution
368-
* 9. fall back to bare name (registry will fail; pipeline drops the call)
436+
* 8b. declared namespace of a top-level project type (enclosing namespaces,
437+
* then `using` namespaces)
438+
* 9. short-name fallback, then the bare name (registry miss drops the call)
369439
*/
370440
const char *cs_resolve_type_name(CSLSPContext *ctx, const char *raw) {
371441
if (!raw || !*raw)
@@ -457,6 +527,11 @@ const char *cs_resolve_type_name(CSLSPContext *ctx, const char *raw) {
457527
char *target_bare = cs_strip_generic_args(ctx->arena, u->target_qn);
458528
if (target_bare && cs_lookup_type_qn(ctx, target_bare))
459529
return target_bare;
530+
/* A project type is registered under its path-derived QN, so
531+
* `using A = Ns.Type;` binds through the declared namespace. */
532+
const char *declared = cs_resolve_by_declared_namespace(ctx, target_bare);
533+
if (declared)
534+
return declared;
460535
return cbm_arena_strdup(ctx->arena, u->target_qn);
461536
}
462537
if (bare[alias_len] == '.') {
@@ -470,6 +545,15 @@ const char *cs_resolve_type_name(CSLSPContext *ctx, const char *raw) {
470545
}
471546
}
472547

548+
/* 8b. Declared-namespace binding for project types (#2120): their QNs are
549+
* path-derived, so steps 5 and 7 cannot hit them; bind by the namespace
550+
* each top-level type declares, in C# lookup order. */
551+
{
552+
const char *declared = cs_resolve_by_declared_namespace(ctx, bare);
553+
if (declared)
554+
return declared;
555+
}
556+
473557
/* 9. Short-name fallback: scan the registry for any type whose
474558
* short_name equals `bare` and whose QN shares the longest namespace
475559
* prefix with the file's namespace stack. This recovers cross-file
@@ -525,6 +609,28 @@ const char *cs_resolve_type_name(CSLSPContext *ctx, const char *raw) {
525609
return bare;
526610
}
527611

612+
/* Other top-level declarations of the type `t` names: same simple name, same
613+
* declared namespace, different entry. Registration order; at most `cap`. */
614+
static int cs_namespace_twins(CSLSPContext *ctx, const CBMRegisteredType *t,
615+
const CBMRegisteredType **out, int cap) {
616+
if (!ctx->registry || !t || !t->namespace_qn || !t->short_name)
617+
return 0;
618+
int n = 0;
619+
CBMTypeShortIter it;
620+
cbm_registry_types_by_short_name_chain(ctx->registry, t->short_name, &it);
621+
int i;
622+
while (n < cap && (i = cbm_type_short_iter_next(&it)) >= 0) {
623+
const CBMRegisteredType *c = &it.reg->types[i];
624+
if (!c->namespace_qn || !c->short_name || !c->qualified_name ||
625+
strcmp(c->short_name, t->short_name) != 0 ||
626+
strcmp(c->namespace_qn, t->namespace_qn) != 0 ||
627+
strcmp(c->qualified_name, t->qualified_name) == 0)
628+
continue;
629+
out[n++] = c;
630+
}
631+
return n;
632+
}
633+
528634
/* Look up a method on a type, walking inheritance chain. */
529635
const CBMRegisteredFunc *cs_lookup_method(CSLSPContext *ctx, const char *type_qn,
530636
const char *method_name) {
@@ -535,19 +641,34 @@ const CBMRegisteredFunc *cs_lookup_method(CSLSPContext *ctx, const char *type_qn
535641
if (f)
536642
return f;
537643

538-
/* Walk inheritance chain (base + interfaces + transitive bases). */
539644
const CBMRegisteredType *t = cs_lookup_type_qn(ctx, type_qn);
540645
if (!t)
541646
return NULL;
542647

648+
/* The other declarations of this same type -- `partial` pieces in other
649+
* files, reference-assembly copies -- are separate registry entries
650+
* (path-derived QNs) but one C# type: a member declared on any of them is
651+
* a member of this one, and so is a base declared on any of them. */
652+
const CBMRegisteredType *twins[CS_LSP_PARENT_WALK_MAX] = {0};
653+
int twin_count = cs_namespace_twins(ctx, t, twins, CS_LSP_PARENT_WALK_MAX);
654+
for (int k = 0; k < twin_count; k++) {
655+
f = cbm_registry_lookup_method(ctx->registry, twins[k]->qualified_name, method_name);
656+
if (f)
657+
return f;
658+
}
659+
660+
/* Walk inheritance chain (base + interfaces + transitive bases). */
543661
const char *visited[CS_LSP_PARENT_WALK_MAX];
544662
int visited_count = 0;
545663
const char *frontier[CS_LSP_PARENT_WALK_MAX];
546664
int frontier_count = 0;
547665

548-
if (t->embedded_types) {
549-
for (int i = 0; t->embedded_types[i] && frontier_count < CS_LSP_PARENT_WALK_MAX; i++) {
550-
frontier[frontier_count++] = t->embedded_types[i];
666+
for (int k = -1; k < twin_count; k++) {
667+
const CBMRegisteredType *decl = k < 0 ? t : twins[k];
668+
for (int i = 0; decl->embedded_types && decl->embedded_types[i] &&
669+
frontier_count < CS_LSP_PARENT_WALK_MAX;
670+
i++) {
671+
frontier[frontier_count++] = decl->embedded_types[i];
551672
}
552673
}
553674

@@ -3469,6 +3590,8 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
34693590
memset(&rt, 0, sizeof(rt));
34703591
rt.qualified_name = d->qualified_name;
34713592
rt.short_name = d->name;
3593+
rt.namespace_qn =
3594+
cs_top_level_namespace(d->qualified_name, d->name, module_qn, d->decl_namespace);
34723595
rt.is_interface = (strcmp(d->label, "Interface") == 0);
34733596
if (d->base_classes) {
34743597
int bc = 0;
@@ -3620,6 +3743,21 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
36203743

36213744
/* ── cross-file entry ───────────────────────────────────────────── */
36223745

3746+
/* The type's declared `namespace_name` when `type_qn` is a TOP-LEVEL type of
3747+
* the file whose module QN is `module_qn` (top-level C# type QNs are exactly
3748+
* module.Name; nested ones are module.Outer.Name and stay NULL, since a nested
3749+
* type is not reachable by its simple name through a namespace). */
3750+
static const char *cs_top_level_namespace(const char *type_qn, const char *short_name,
3751+
const char *module_qn, const char *namespace_name) {
3752+
if (!type_qn || !short_name || !module_qn || !namespace_name || !namespace_name[0])
3753+
return NULL;
3754+
size_t mlen = strlen(module_qn);
3755+
if (strncmp(type_qn, module_qn, mlen) != 0 || type_qn[mlen] != '.' ||
3756+
strcmp(type_qn + mlen + 1, short_name) != 0)
3757+
return NULL;
3758+
return namespace_name;
3759+
}
3760+
36233761
/* Register one batch of CBMLSPDef[] into a registry. Shared by the
36243762
* per-file cross-LSP path and the Tier 2 pre-built registry builder.
36253763
* Def-driven (no per-file AST mutation) so deterministic per def set. */
@@ -3636,6 +3774,8 @@ static void cs_register_lsp_defs(CBMArena *arena, CBMTypeRegistry *reg, CBMLSPDe
36363774
memset(&rt, 0, sizeof(rt));
36373775
rt.qualified_name = d->qualified_name;
36383776
rt.short_name = d->short_name;
3777+
rt.namespace_qn = cs_top_level_namespace(d->qualified_name, d->short_name,
3778+
d->def_module_qn, d->namespace_name);
36393779
rt.is_interface = d->is_interface || (strcmp(d->label, "Interface") == 0);
36403780
if (d->embedded_types && *d->embedded_types) {
36413781
/* Parse "|"-separated list. */

‎internal/cbm/lsp/type_registry.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ typedef struct {
6464
const CBMType *index_value_type; // V or NULL
6565
// Generic constraints, parallel to type_param_names. NULL or shorter array means "any".
6666
const CBMType **type_param_constraints; // NULL-terminated, parallel to type_param_names
67+
68+
// --- C#-specific (NULL for every other language) ---
69+
// Declared namespace of a TOP-LEVEL project type. Graph QNs are path-derived,
70+
// so this is the only way to bind a short name through `using` directives and
71+
// enclosing namespaces (#2120). NULL for nested types and unknown namespaces.
72+
const char *namespace_qn;
6773
} CBMRegisteredType;
6874

6975
// Hash-table bucket entry. Chains collisions via next-index list for overload sets.

‎internal/cbm/result_compact.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ static void cr_walk_def(cr_ctx_t *c, CBMDefinition *d) {
275275
cr_str(c, &d->impl_trait);
276276
cr_str(c, &d->http_client);
277277
cr_str(c, &d->http_base_url);
278+
cr_str(c, &d->decl_namespace);
278279
}
279280

280281
static void cr_walk_call(cr_ctx_t *c, CBMCall *call) {

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,12 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
409409
dst->label = label;
410410
dst->def_module_qn = module_qn;
411411
dst->namespace_name = namespace_name ? cbm_arena_strdup(arena, namespace_name) : NULL;
412+
/* C# types carry their OWN declared namespace: the file-level one is only
413+
* the file's first namespace, wrong for every later namespace (#2120). */
414+
if (lang == CBM_LANG_CSHARP && cbm_label_is_type_like(src->label)) {
415+
dst->namespace_name =
416+
src->decl_namespace ? cbm_arena_strdup(arena, src->decl_namespace) : NULL;
417+
}
412418
dst->is_interface = (strcmp(label, "Interface") == 0 || strcmp(label, "Protocol") == 0);
413419
/* Single return-type string. The per-language registrars split on '|'
414420
* for multi-return languages (Go); single-return languages just see one

‎src/pipeline/pipeline.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,8 @@ static void log_result_census(const char *tag, CBMFileResult **cache, int file_c
11941194
}
11951195
str_def_fp += def->fingerprint ? (size_t)def->fingerprint_k * sizeof(uint32_t) : 0;
11961196
str_def_misc += census_len(def->route_path) + census_len(def->route_method) +
1197-
census_len(def->impl_trait) + census_len(def->http_base_url);
1197+
census_len(def->impl_trait) + census_len(def->http_base_url) +
1198+
census_len(def->decl_namespace);
11981199
}
11991200
for (int c = 0; c < r->calls.count; c++) {
12001201
const CBMCall *call = &r->calls.items[c];

0 commit comments

Comments
 (0)