Skip to content

Commit 50d19f8

Browse files
authored
Merge pull request #2347 from DeusData/fix/issue-2120
fix(cs-lsp): bind same-name C# types by declared namespace (#2120)
2 parents c202539 + eecf5ea commit 50d19f8

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
@@ -283,6 +283,10 @@ typedef struct {
283283
* qualified_name (base QN = the first qn_sig_off bytes); 0 = no suffix.
284284
* Always 0 until a language enables its callable_identity mode. */
285285
uint32_t qn_sig_off;
286+
/* C# only: declared namespace of a TOP-LEVEL type (NULL for nested types,
287+
* the global namespace and every other language). Per type, because the
288+
* file-level namespace_name records only a file's first namespace. */
289+
const char *decl_namespace;
286290
CBMTestDefinitionRole test_role; /* NONE preserves legacy helper/test-file semantics */
287291
/* Configured raw definitions only; zero for all legacy rows. Exact spans
288292
* are bound to the owning result's source identity before cross-file use. */

‎internal/cbm/extract_defs.c‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7097,6 +7097,58 @@ static void extract_py_field_types(CBMExtractCtx *ctx, TSNode class_node, const
70977097
}
70987098
}
70997099

7100+
/* Name of a C# namespace declaration node (block or file-scoped), or NULL. */
7101+
static const char *cs_namespace_decl_name(CBMArena *a, TSNode ns, const char *source) {
7102+
TSNode nm = ts_node_child_by_field_name(ns, TS_FIELD("name"));
7103+
if (ts_node_is_null(nm)) {
7104+
return NULL;
7105+
}
7106+
const char *text = cbm_node_text(a, nm, source);
7107+
return text && text[0] ? text : NULL;
7108+
}
7109+
7110+
/* Declared namespace of a C# type declaration: every enclosing namespace
7111+
* block (outer to inner) under an optional file-scoped namespace, which the
7112+
* grammar may attach either as an ancestor or as a preceding sibling of the
7113+
* compilation unit's members. NULL = global namespace. The file-level
7114+
* namespace_name keeps only the FIRST namespace of a file, which mislabels
7115+
* every type of a multi-namespace file (#2120). */
7116+
static const char *cs_type_decl_namespace(CBMArena *a, TSNode node, const char *source) {
7117+
const char *acc = NULL;
7118+
bool file_scoped = false;
7119+
TSNode top = node;
7120+
for (TSNode cur = ts_node_parent(node); !ts_node_is_null(cur); cur = ts_node_parent(cur)) {
7121+
const char *k = ts_node_type(cur);
7122+
bool is_file_scoped = strcmp(k, "file_scoped_namespace_declaration") == 0;
7123+
if (is_file_scoped || strcmp(k, "namespace_declaration") == 0) {
7124+
const char *name = cs_namespace_decl_name(a, cur, source);
7125+
if (name) {
7126+
acc = acc ? cbm_arena_sprintf(a, "%s.%s", name, acc) : name;
7127+
}
7128+
file_scoped = file_scoped || is_file_scoped;
7129+
}
7130+
top = cur;
7131+
}
7132+
if (file_scoped) {
7133+
return acc;
7134+
}
7135+
uint32_t start = ts_node_start_byte(node);
7136+
uint32_t n = ts_node_named_child_count(top);
7137+
for (uint32_t i = 0; i < n; i++) {
7138+
TSNode c = ts_node_named_child(top, i);
7139+
if (ts_node_start_byte(c) >= start) {
7140+
break;
7141+
}
7142+
if (strcmp(ts_node_type(c), "file_scoped_namespace_declaration") == 0) {
7143+
const char *name = cs_namespace_decl_name(a, c, source);
7144+
if (name) {
7145+
return acc ? cbm_arena_sprintf(a, "%s.%s", name, acc) : name;
7146+
}
7147+
}
7148+
}
7149+
return acc;
7150+
}
7151+
71007152
static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec) {
71017153
CBMArena *a = ctx->arena;
71027154
const char *kind = ts_node_type(node);
@@ -7497,6 +7549,9 @@ static void emit_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s
74977549
def.end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;
74987550
def.lines = (int)(def.end_line - def.start_line + TS_LINE_OFFSET);
74997551
def.is_exported = cbm_is_exported(name, ctx->language);
7552+
if (ctx->language == CBM_LANG_CSHARP && !ctx->enclosing_class_qn) {
7553+
def.decl_namespace = cs_type_decl_namespace(a, node, ctx->source);
7554+
}
75007555
def.base_classes = extract_base_classes(a, node, ctx->source, ctx->language);
75017556
def.decorators = extract_decorators(a, node, ctx->source, ctx->language, spec);
75027557
def.docstring = extract_docstring(ctx, node, name);

‎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
@@ -67,6 +67,12 @@ typedef struct {
6767
const CBMType *index_value_type; // V or NULL
6868
// Generic constraints, parallel to type_param_names. NULL or shorter array means "any".
6969
const CBMType **type_param_constraints; // NULL-terminated, parallel to type_param_names
70+
71+
// --- C#-specific (NULL for every other language) ---
72+
// Declared namespace of a TOP-LEVEL project type. Graph QNs are path-derived,
73+
// so this is the only way to bind a short name through `using` directives and
74+
// enclosing namespaces (#2120). NULL for nested types and unknown namespaces.
75+
const char *namespace_qn;
7076
} CBMRegisteredType;
7177

7278
// 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
@@ -276,6 +276,7 @@ static void cr_walk_def(cr_ctx_t *c, CBMDefinition *d) {
276276
cr_str(c, &d->http_client);
277277
cr_str(c, &d->http_base_url);
278278
cr_str(c, &d->variants);
279+
cr_str(c, &d->decl_namespace);
279280
}
280281

281282
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
@@ -441,6 +441,12 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
441441
dst->label = label;
442442
dst->def_module_qn = module_qn;
443443
dst->namespace_name = namespace_name ? cbm_arena_strdup(arena, namespace_name) : NULL;
444+
/* C# types carry their OWN declared namespace: the file-level one is only
445+
* the file's first namespace, wrong for every later namespace (#2120). */
446+
if (lang == CBM_LANG_CSHARP && cbm_label_is_type_like(src->label)) {
447+
dst->namespace_name =
448+
src->decl_namespace ? cbm_arena_strdup(arena, src->decl_namespace) : NULL;
449+
}
444450
dst->is_interface = (strcmp(label, "Interface") == 0 || strcmp(label, "Protocol") == 0);
445451
/* Single return-type string. The per-language registrars split on '|'
446452
* 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
@@ -1850,7 +1850,8 @@ static void log_result_census(const char *tag, CBMFileResult **cache, int file_c
18501850
}
18511851
str_def_fp += def->fingerprint ? (size_t)def->fingerprint_k * sizeof(uint32_t) : 0;
18521852
str_def_misc += census_len(def->route_path) + census_len(def->route_method) +
1853-
census_len(def->impl_trait) + census_len(def->http_base_url);
1853+
census_len(def->impl_trait) + census_len(def->http_base_url) +
1854+
census_len(def->decl_namespace);
18541855
}
18551856
for (int c = 0; c < r->calls.count; c++) {
18561857
const CBMCall *call = &r->calls.items[c];

0 commit comments

Comments
 (0)