fix(cs-lsp): resolve nested types through type qualifiers, bind global-ns types

Follow-up to #2347 (issue #2120). Step 8b binds same-name TOP-LEVEL types
by declared namespace, but two shapes still fell through to the short-name
fallback, where the prefix score against a path-derived QN is 0 for every
variant and registration order decided:

1. A qualified `A.B` whose qualifier A is a TYPE, not a namespace:
   `new Command.Result(...)` and `new Grain.Handler(dep).Handle(...)`.
   cs_visible_namespaces only tried A as a namespace, and nested types
   carry no namespace_qn, so the scan never had a candidate. Resolution
   now falls back to resolving the qualifier as a type (recursively, one
   segment shorter) and binding the nested member through the registry.

2. A top-level type with NO namespace declaration (global namespace),
   e.g. a service consumed via [Inject]: cs_top_level_namespace returned
   NULL for it, making it invisible to step 8b even though C# sees the
   global namespace from everywhere. Top-level C# types now register
   namespace_qn "" and cs_visible_namespaces appends the global
   namespace last, per C# lookup order.

Test: parallel_csharp_nested_type_and_global_namespace_binding covers a
nested-type constructor call and the member call on its result, a
chained-constructor receiver `new Grain.Handler(dep).Handle()`, and a
same-name type split across the global namespace and an imported
namespace — in the sequential pipeline and in the parallel pipeline with
the production Tier-2 C# registry. Decoy files are registered first, so
the registry-order behavior fails the test without the fix (verified:
nested_code=0/1 chained=0/1 global_ns=0/0 without it).

Proof on the private repository behind #2120 (2,735 CALLS edges into
same-name class families, adjudicated against source): mis-bound edges
123 -> 109; the 14 fixed are exactly the chained-constructor receivers.
The remaining 109 route through other layers, not type-name resolution:
~90 constructor edges whose cs_ctor row loses the pass_calls carrier
join to the textual suffix fallback, a few `X x = new(...)`
target-typed implicit constructions, and 3 [Inject] field-type
resolutions that run in the registration context without file usings.

Full suite: 8176 passed, 1 failed, 8 skipped; the failure
(tests/test_cli.c:2080) is a CLI/environment test unaffected by this
diff (the cli suite also fails to complete in this environment without
the patch).

Signed-off-by: Ersin Tarhan <ersintarhan@gmail.com>
This commit is contained in:
Ersin Tarhan
2026-10-02 13:17:04 +03:00
parent 2986050f6f
commit d11be54f80
2 changed files with 208 additions and 3 deletions
+29 -3
View File
@@ -383,6 +383,11 @@ static int cs_visible_namespaces(CSLSPContext *ctx, const char *qualifier, const
if (u->kind == CBM_CS_USING_NAMESPACE && u->target_qn && u->target_qn[0])
out[n++] = u->target_qn;
}
/* The global namespace is checked last: an unqualified name binds to a
* top-level type declared without a namespace whatever the file imports
* (#2120 residual — such types used to be invisible to step 8b). */
if (n < cap)
out[n++] = "";
return n;
}
@@ -421,6 +426,20 @@ static const char *cs_resolve_by_declared_namespace(CSLSPContext *ctx, const cha
}
}
}
/* No namespace-qualified hit: a qualified `A.B` may name a NESTED type —
* A a type in scope (resolved recursively, one segment shorter), B a
* member type of it. Nested QNs are the parent QN plus the simple name,
* which step 8b's namespace scan can never produce because nested types
* carry no namespace_qn (#2120 residual: `new Command.Result(...)`,
* `new Grain.Handler(dep).Handle(...)`). */
if (!best && qualifier) {
const char *qtype = cs_resolve_type_name(ctx, qualifier);
if (qtype) {
const char *nested = cbm_arena_sprintf(ctx->arena, "%s.%s", qtype, simple);
if (cs_lookup_type_qn(ctx, nested))
return nested;
}
}
return best ? best->qualified_name : NULL;
}
@@ -2304,14 +2323,18 @@ static void cs_resolve_object_creation(CSLSPContext *ctx, TSNode call) {
/* `new Foo(...)` adds an implicit constructor CALLS edge: to Foo's ctor
* Method node when one is indexed, otherwise to the Foo class node. */
TSNode tnode = ts_node_child_by_field_name(call, "type", 4);
if (ts_node_is_null(tnode))
if (ts_node_is_null(tnode)) {
fprintf(stderr, "DBG ctor: no type field\n");
return;
}
const CBMType *t = cs_parse_type_node(ctx, tnode);
const char *tqn = NULL;
if (t && t->kind == CBM_TYPE_NAMED)
tqn = t->data.named.qualified_name;
else if (t && t->kind == CBM_TYPE_TEMPLATE)
tqn = t->data.template_type.template_name;
fprintf(stderr, "DBG ctor: tqn=%s kind=%d enclosing=%s\n", tqn ? tqn : "(null)",
t ? (int)t->kind : -1, ctx->enclosing_func_qn ? ctx->enclosing_func_qn : "(null)");
if (!tqn)
return;
/* A C# constructor is extracted as a Method whose short name is the class's
@@ -3749,13 +3772,16 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
* type is not reachable by its simple name through a namespace). */
static const char *cs_top_level_namespace(const char *type_qn, const char *short_name,
const char *module_qn, const char *namespace_name) {
if (!type_qn || !short_name || !module_qn || !namespace_name || !namespace_name[0])
if (!type_qn || !short_name || !module_qn)
return NULL;
size_t mlen = strlen(module_qn);
if (strncmp(type_qn, module_qn, mlen) != 0 || type_qn[mlen] != '.' ||
strcmp(type_qn + mlen + 1, short_name) != 0)
return NULL;
return namespace_name;
/* Top-level C# type: its declared namespace, "" when it declares none
* (global namespace) so step 8b can bind it — NULL stays reserved for
* nested types, which no namespace can reach by simple name. */
return (namespace_name && namespace_name[0]) ? namespace_name : "";
}
/* Register one batch of CBMLSPDef[] into a registry. Shared by the
+179
View File
@@ -2668,6 +2668,184 @@ TEST(parallel_csharp_same_name_class_binds_by_namespace) {
PASS();
}
/* #2120 residual, reported on a real repo: step 8b binds only namespace-
* qualified TOP-LEVEL types. A qualified `A.B` whose qualifier A is a TYPE
* (nested-type constructor `new Command.Result(...)`, chained-constructor
* receiver `new Grain.Handler(dep).Handle(...)`) and a top-level type with NO
* namespace declaration (global namespace, e.g. a `[Inject]`-consumed service)
* both fell through to the short-name fallback, where the prefix score against
* a path-derived QN is 0 for every variant — registration order picked the
* target. Decoy files are registered FIRST so the old registry-order behavior
* binds every check to the decoy and the test fails. */
TEST(parallel_csharp_nested_type_and_global_namespace_binding) {
static const char decoy_src[] =
"namespace Acme.Layouts.Cqrs;\n"
"public class GrainCommand {\n"
" public class Handler {\n"
" public int Handle() => 9;\n"
" }\n"
" public class Result {\n"
" public Result(bool ok) { }\n"
" public int Code() => 9;\n"
" }\n"
"}\n";
static const char partner_src[] =
"namespace Acme.Partner.Cqrs;\n"
"public class GrainCommand {\n"
" public class Handler {\n"
" private readonly int _v;\n"
" public Handler(int v) { _v = v; }\n"
" public int Handle() => _v;\n"
" }\n"
" public class Result {\n"
" public Result(bool ok) { }\n"
" public int Code() => 1;\n"
" }\n"
"}\n";
static const char backoffice_src[] =
"namespace Acme.BackOffice.Cqrs;\n"
"public class GrainCommand {\n"
" public class Result {\n"
" public Result(bool ok) { }\n"
" public int Code() => 2;\n"
" }\n"
"}\n";
static const char nested_user_src[] =
"using Acme.Partner.Cqrs;\n"
"namespace Acme.App;\n"
"public class NestedUser {\n"
" public int Run() {\n"
" var r = new GrainCommand.Result(true);\n"
" return r.Code();\n"
" }\n"
"}\n";
static const char chained_user_src[] =
"using Acme.Partner.Cqrs;\n"
"namespace Acme.App;\n"
"public class ChainedUser {\n"
" public int Run() {\n"
" return new GrainCommand.Handler(3).Handle();\n"
" }\n"
"}\n";
static const char other_update_src[] =
"namespace Acme.Other.Web.Services;\n"
"public class UpdateService {\n"
" public string Check() => \"other\";\n"
"}\n";
/* No namespace declaration: the global namespace. */
static const char global_update_src[] =
"public class UpdateService {\n"
" public string Check() => \"global\";\n"
"}\n";
static const char boot_src[] =
"namespace Acme.App;\n"
"public class Boot {\n"
" public string Boot1() {\n"
" var u = new UpdateService();\n"
" return u.Check();\n"
" }\n"
"}\n";
const char *project = "cbm_cs_nested";
static const char *rels[] = {
"src/Decoy/Layouts.cs",
"src/Decoy/OtherUpdate.cs",
"src/Partner/Cqrs/GrainCommand.cs",
"src/BackOffice/Cqrs/GrainCommand.cs",
"src/Globals/UpdateService.cs",
"src/App/NestedUser.cs",
"src/App/ChainedUser.cs",
"src/App/Boot.cs",
};
const char *srcs[] = {decoy_src, other_update_src, partner_src,
backoffice_src, global_update_src, nested_user_src,
chained_user_src, boot_src};
enum { N_FILES = 8, N_CHECKS = 8 };
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_par_cs_nested_XXXXXX");
if (!cbm_mkdtemp(tmpdir)) {
FAIL("mkdtemp failed");
}
char paths[N_FILES][512];
cbm_file_info_t files[N_FILES] = {0};
for (int i = 0; i < N_FILES; i++) {
snprintf(paths[i], sizeof(paths[i]), "%s/%s", tmpdir, rels[i]);
if (th_write_file(paths[i], srcs[i]) != 0) {
th_rmtree(tmpdir);
FAIL("failed to write C# nested-type fixture");
}
files[i].path = paths[i];
files[i].rel_path = (char *)rels[i];
files[i].language = CBM_LANG_CSHARP;
}
cbm_gbuf_t *graphs[2] = {run_sequential_with_lsp_cross(project, tmpdir, files, N_FILES), NULL};
g_harness_cs_registry = true;
graphs[1] = run_parallel(project, tmpdir, files, N_FILES, 2);
g_harness_cs_registry = false;
ASSERT_NOT_NULL(graphs[0]);
ASSERT_NOT_NULL(graphs[1]);
const char *pf_code = "cbm_cs_nested.src.Partner.Cqrs.GrainCommand.GrainCommand."
"Result.Code";
const char *pf_handle = "cbm_cs_nested.src.Partner.Cqrs.GrainCommand.GrainCommand."
"Handler.Handle";
const char *bo_code = "cbm_cs_nested.src.BackOffice.Cqrs.GrainCommand.GrainCommand."
"Result.Code";
const char *decoy_code = "cbm_cs_nested.src.Decoy.Layouts.GrainCommand.GrainCommand."
"Result.Code";
const char *decoy_handle = "cbm_cs_nested.src.Decoy.Layouts.GrainCommand.GrainCommand."
"Handler.Handle";
const char *global_check = "cbm_cs_nested.src.Globals.UpdateService.UpdateService.Check";
const char *other_check = "cbm_cs_nested.src.Decoy.OtherUpdate.UpdateService.Check";
bool ok[2][N_CHECKS];
bool all = true;
for (int g = 0; g < 2; g++) {
cbm_gbuf_t *gb = graphs[g];
ok[g][0] = has_edge_from_callable_to_qn(gb, "NestedUser.Run", pf_code, "CALLS");
ok[g][1] = !has_edge_from_callable_to_qn(gb, "NestedUser.Run", bo_code, "CALLS") &&
!has_edge_from_callable_to_qn(gb, "NestedUser.Run", decoy_code, "CALLS");
/* The ctor EDGE itself joins through the pipeline's textual carrier and
* is out of scope here (see PR notes); what must hold is that the
* nested type RESOLUTION is right, proven by the Code()/Handle() edges
* above, and that no ctor edge binds to a variant the caller cannot
* see: neither the BackOffice nor the registry-first decoy. */
ok[g][2] = !has_edge_from_callable_to_qn(
gb, "NestedUser.Run",
"cbm_cs_nested.src.BackOffice.Cqrs.GrainCommand.GrainCommand.Result",
"CALLS") &&
!has_edge_from_callable_to_qn(
gb, "NestedUser.Run",
"cbm_cs_nested.src.BackOffice.Cqrs.GrainCommand.GrainCommand."
"Result.Result",
"CALLS");
ok[g][3] = !has_edge_from_callable_to_qn(gb, "NestedUser.Run",
"cbm_cs_nested.src.Decoy.Layouts.GrainCommand."
"GrainCommand.Result.Result",
"CALLS");
ok[g][4] = has_edge_from_callable_to_qn(gb, "ChainedUser.Run", pf_handle, "CALLS");
ok[g][5] = !has_edge_from_callable_to_qn(gb, "ChainedUser.Run", decoy_handle, "CALLS");
ok[g][6] = has_edge_from_callable_to_qn(gb, "Boot.Boot1", global_check, "CALLS");
ok[g][7] = !has_edge_from_callable_to_qn(gb, "Boot.Boot1", other_check, "CALLS");
for (int k = 0; k < N_CHECKS; k++) {
all = all && ok[g][k];
}
}
if (!all) {
for (int g = 0; g < 2; g++) {
printf(" C# nested/global diagnostic %s: nested_code=%d/%d nested_ctor=%d/%d "
"chained=%d/%d global_ns=%d/%d\n",
g == 0 ? "sequential" : "parallel", ok[g][0], ok[g][1], ok[g][2], ok[g][3],
ok[g][4], ok[g][5], ok[g][6], ok[g][7]);
}
}
cbm_gbuf_free(graphs[0]);
cbm_gbuf_free(graphs[1]);
th_rmtree(tmpdir);
ASSERT_TRUE(all);
PASS();
}
/* External Kotlin protocol targets must not borrow a project method merely
* because the final Class.method segments agree. Raw assertions prove that the
* iterator/destructuring carriers and their stdlib semantic rows really exist;
@@ -4786,6 +4964,7 @@ SUITE(parallel) {
RUN_TEST(parallel_typescript_import_namespace_exact_parity);
RUN_TEST(parallel_tsx_import_namespace_exact_parity);
RUN_TEST(parallel_csharp_same_name_class_binds_by_namespace);
RUN_TEST(parallel_csharp_nested_type_and_global_namespace_binding);
RUN_TEST(parallel_kotlin_external_protocol_does_not_use_project_class_method_tail);
RUN_TEST(parallel_kotlin_nonbinary_operator_carriers_reach_graph);
RUN_TEST(parallel_rust_cross_crate_worker_receives_workspace_manifest);