Skip to content

Commit d11be54

Browse files
committed
fix(cs-lsp): resolve nested types through type qualifiers, bind global-ns types
Follow-up to DeusData#2347 (issue DeusData#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 DeusData#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 <[email protected]>
1 parent 2986050 commit d11be54

2 files changed

Lines changed: 208 additions & 3 deletions

File tree

‎internal/cbm/lsp/cs_lsp.c‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,11 @@ static int cs_visible_namespaces(CSLSPContext *ctx, const char *qualifier, const
383383
if (u->kind == CBM_CS_USING_NAMESPACE && u->target_qn && u->target_qn[0])
384384
out[n++] = u->target_qn;
385385
}
386+
/* The global namespace is checked last: an unqualified name binds to a
387+
* top-level type declared without a namespace whatever the file imports
388+
* (#2120 residual — such types used to be invisible to step 8b). */
389+
if (n < cap)
390+
out[n++] = "";
386391
return n;
387392
}
388393

@@ -421,6 +426,20 @@ static const char *cs_resolve_by_declared_namespace(CSLSPContext *ctx, const cha
421426
}
422427
}
423428
}
429+
/* No namespace-qualified hit: a qualified `A.B` may name a NESTED type —
430+
* A a type in scope (resolved recursively, one segment shorter), B a
431+
* member type of it. Nested QNs are the parent QN plus the simple name,
432+
* which step 8b's namespace scan can never produce because nested types
433+
* carry no namespace_qn (#2120 residual: `new Command.Result(...)`,
434+
* `new Grain.Handler(dep).Handle(...)`). */
435+
if (!best && qualifier) {
436+
const char *qtype = cs_resolve_type_name(ctx, qualifier);
437+
if (qtype) {
438+
const char *nested = cbm_arena_sprintf(ctx->arena, "%s.%s", qtype, simple);
439+
if (cs_lookup_type_qn(ctx, nested))
440+
return nested;
441+
}
442+
}
424443
return best ? best->qualified_name : NULL;
425444
}
426445

@@ -2304,14 +2323,18 @@ static void cs_resolve_object_creation(CSLSPContext *ctx, TSNode call) {
23042323
/* `new Foo(...)` adds an implicit constructor CALLS edge: to Foo's ctor
23052324
* Method node when one is indexed, otherwise to the Foo class node. */
23062325
TSNode tnode = ts_node_child_by_field_name(call, "type", 4);
2307-
if (ts_node_is_null(tnode))
2326+
if (ts_node_is_null(tnode)) {
2327+
fprintf(stderr, "DBG ctor: no type field\n");
23082328
return;
2329+
}
23092330
const CBMType *t = cs_parse_type_node(ctx, tnode);
23102331
const char *tqn = NULL;
23112332
if (t && t->kind == CBM_TYPE_NAMED)
23122333
tqn = t->data.named.qualified_name;
23132334
else if (t && t->kind == CBM_TYPE_TEMPLATE)
23142335
tqn = t->data.template_type.template_name;
2336+
fprintf(stderr, "DBG ctor: tqn=%s kind=%d enclosing=%s\n", tqn ? tqn : "(null)",
2337+
t ? (int)t->kind : -1, ctx->enclosing_func_qn ? ctx->enclosing_func_qn : "(null)");
23152338
if (!tqn)
23162339
return;
23172340
/* 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,
37493772
* type is not reachable by its simple name through a namespace). */
37503773
static const char *cs_top_level_namespace(const char *type_qn, const char *short_name,
37513774
const char *module_qn, const char *namespace_name) {
3752-
if (!type_qn || !short_name || !module_qn || !namespace_name || !namespace_name[0])
3775+
if (!type_qn || !short_name || !module_qn)
37533776
return NULL;
37543777
size_t mlen = strlen(module_qn);
37553778
if (strncmp(type_qn, module_qn, mlen) != 0 || type_qn[mlen] != '.' ||
37563779
strcmp(type_qn + mlen + 1, short_name) != 0)
37573780
return NULL;
3758-
return namespace_name;
3781+
/* Top-level C# type: its declared namespace, "" when it declares none
3782+
* (global namespace) so step 8b can bind it — NULL stays reserved for
3783+
* nested types, which no namespace can reach by simple name. */
3784+
return (namespace_name && namespace_name[0]) ? namespace_name : "";
37593785
}
37603786

37613787
/* Register one batch of CBMLSPDef[] into a registry. Shared by the

‎tests/test_parallel.c‎

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,184 @@ TEST(parallel_csharp_same_name_class_binds_by_namespace) {
26682668
PASS();
26692669
}
26702670

2671+
/* #2120 residual, reported on a real repo: step 8b binds only namespace-
2672+
* qualified TOP-LEVEL types. A qualified `A.B` whose qualifier A is a TYPE
2673+
* (nested-type constructor `new Command.Result(...)`, chained-constructor
2674+
* receiver `new Grain.Handler(dep).Handle(...)`) and a top-level type with NO
2675+
* namespace declaration (global namespace, e.g. a `[Inject]`-consumed service)
2676+
* both fell through to the short-name fallback, where the prefix score against
2677+
* a path-derived QN is 0 for every variant — registration order picked the
2678+
* target. Decoy files are registered FIRST so the old registry-order behavior
2679+
* binds every check to the decoy and the test fails. */
2680+
TEST(parallel_csharp_nested_type_and_global_namespace_binding) {
2681+
static const char decoy_src[] =
2682+
"namespace Acme.Layouts.Cqrs;\n"
2683+
"public class GrainCommand {\n"
2684+
" public class Handler {\n"
2685+
" public int Handle() => 9;\n"
2686+
" }\n"
2687+
" public class Result {\n"
2688+
" public Result(bool ok) { }\n"
2689+
" public int Code() => 9;\n"
2690+
" }\n"
2691+
"}\n";
2692+
static const char partner_src[] =
2693+
"namespace Acme.Partner.Cqrs;\n"
2694+
"public class GrainCommand {\n"
2695+
" public class Handler {\n"
2696+
" private readonly int _v;\n"
2697+
" public Handler(int v) { _v = v; }\n"
2698+
" public int Handle() => _v;\n"
2699+
" }\n"
2700+
" public class Result {\n"
2701+
" public Result(bool ok) { }\n"
2702+
" public int Code() => 1;\n"
2703+
" }\n"
2704+
"}\n";
2705+
static const char backoffice_src[] =
2706+
"namespace Acme.BackOffice.Cqrs;\n"
2707+
"public class GrainCommand {\n"
2708+
" public class Result {\n"
2709+
" public Result(bool ok) { }\n"
2710+
" public int Code() => 2;\n"
2711+
" }\n"
2712+
"}\n";
2713+
static const char nested_user_src[] =
2714+
"using Acme.Partner.Cqrs;\n"
2715+
"namespace Acme.App;\n"
2716+
"public class NestedUser {\n"
2717+
" public int Run() {\n"
2718+
" var r = new GrainCommand.Result(true);\n"
2719+
" return r.Code();\n"
2720+
" }\n"
2721+
"}\n";
2722+
static const char chained_user_src[] =
2723+
"using Acme.Partner.Cqrs;\n"
2724+
"namespace Acme.App;\n"
2725+
"public class ChainedUser {\n"
2726+
" public int Run() {\n"
2727+
" return new GrainCommand.Handler(3).Handle();\n"
2728+
" }\n"
2729+
"}\n";
2730+
static const char other_update_src[] =
2731+
"namespace Acme.Other.Web.Services;\n"
2732+
"public class UpdateService {\n"
2733+
" public string Check() => \"other\";\n"
2734+
"}\n";
2735+
/* No namespace declaration: the global namespace. */
2736+
static const char global_update_src[] =
2737+
"public class UpdateService {\n"
2738+
" public string Check() => \"global\";\n"
2739+
"}\n";
2740+
static const char boot_src[] =
2741+
"namespace Acme.App;\n"
2742+
"public class Boot {\n"
2743+
" public string Boot1() {\n"
2744+
" var u = new UpdateService();\n"
2745+
" return u.Check();\n"
2746+
" }\n"
2747+
"}\n";
2748+
const char *project = "cbm_cs_nested";
2749+
static const char *rels[] = {
2750+
"src/Decoy/Layouts.cs",
2751+
"src/Decoy/OtherUpdate.cs",
2752+
"src/Partner/Cqrs/GrainCommand.cs",
2753+
"src/BackOffice/Cqrs/GrainCommand.cs",
2754+
"src/Globals/UpdateService.cs",
2755+
"src/App/NestedUser.cs",
2756+
"src/App/ChainedUser.cs",
2757+
"src/App/Boot.cs",
2758+
};
2759+
const char *srcs[] = {decoy_src, other_update_src, partner_src,
2760+
backoffice_src, global_update_src, nested_user_src,
2761+
chained_user_src, boot_src};
2762+
enum { N_FILES = 8, N_CHECKS = 8 };
2763+
2764+
char tmpdir[256];
2765+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_par_cs_nested_XXXXXX");
2766+
if (!cbm_mkdtemp(tmpdir)) {
2767+
FAIL("mkdtemp failed");
2768+
}
2769+
char paths[N_FILES][512];
2770+
cbm_file_info_t files[N_FILES] = {0};
2771+
for (int i = 0; i < N_FILES; i++) {
2772+
snprintf(paths[i], sizeof(paths[i]), "%s/%s", tmpdir, rels[i]);
2773+
if (th_write_file(paths[i], srcs[i]) != 0) {
2774+
th_rmtree(tmpdir);
2775+
FAIL("failed to write C# nested-type fixture");
2776+
}
2777+
files[i].path = paths[i];
2778+
files[i].rel_path = (char *)rels[i];
2779+
files[i].language = CBM_LANG_CSHARP;
2780+
}
2781+
2782+
cbm_gbuf_t *graphs[2] = {run_sequential_with_lsp_cross(project, tmpdir, files, N_FILES), NULL};
2783+
g_harness_cs_registry = true;
2784+
graphs[1] = run_parallel(project, tmpdir, files, N_FILES, 2);
2785+
g_harness_cs_registry = false;
2786+
ASSERT_NOT_NULL(graphs[0]);
2787+
ASSERT_NOT_NULL(graphs[1]);
2788+
2789+
const char *pf_code = "cbm_cs_nested.src.Partner.Cqrs.GrainCommand.GrainCommand."
2790+
"Result.Code";
2791+
const char *pf_handle = "cbm_cs_nested.src.Partner.Cqrs.GrainCommand.GrainCommand."
2792+
"Handler.Handle";
2793+
const char *bo_code = "cbm_cs_nested.src.BackOffice.Cqrs.GrainCommand.GrainCommand."
2794+
"Result.Code";
2795+
const char *decoy_code = "cbm_cs_nested.src.Decoy.Layouts.GrainCommand.GrainCommand."
2796+
"Result.Code";
2797+
const char *decoy_handle = "cbm_cs_nested.src.Decoy.Layouts.GrainCommand.GrainCommand."
2798+
"Handler.Handle";
2799+
const char *global_check = "cbm_cs_nested.src.Globals.UpdateService.UpdateService.Check";
2800+
const char *other_check = "cbm_cs_nested.src.Decoy.OtherUpdate.UpdateService.Check";
2801+
bool ok[2][N_CHECKS];
2802+
bool all = true;
2803+
for (int g = 0; g < 2; g++) {
2804+
cbm_gbuf_t *gb = graphs[g];
2805+
ok[g][0] = has_edge_from_callable_to_qn(gb, "NestedUser.Run", pf_code, "CALLS");
2806+
ok[g][1] = !has_edge_from_callable_to_qn(gb, "NestedUser.Run", bo_code, "CALLS") &&
2807+
!has_edge_from_callable_to_qn(gb, "NestedUser.Run", decoy_code, "CALLS");
2808+
/* The ctor EDGE itself joins through the pipeline's textual carrier and
2809+
* is out of scope here (see PR notes); what must hold is that the
2810+
* nested type RESOLUTION is right, proven by the Code()/Handle() edges
2811+
* above, and that no ctor edge binds to a variant the caller cannot
2812+
* see: neither the BackOffice nor the registry-first decoy. */
2813+
ok[g][2] = !has_edge_from_callable_to_qn(
2814+
gb, "NestedUser.Run",
2815+
"cbm_cs_nested.src.BackOffice.Cqrs.GrainCommand.GrainCommand.Result",
2816+
"CALLS") &&
2817+
!has_edge_from_callable_to_qn(
2818+
gb, "NestedUser.Run",
2819+
"cbm_cs_nested.src.BackOffice.Cqrs.GrainCommand.GrainCommand."
2820+
"Result.Result",
2821+
"CALLS");
2822+
ok[g][3] = !has_edge_from_callable_to_qn(gb, "NestedUser.Run",
2823+
"cbm_cs_nested.src.Decoy.Layouts.GrainCommand."
2824+
"GrainCommand.Result.Result",
2825+
"CALLS");
2826+
ok[g][4] = has_edge_from_callable_to_qn(gb, "ChainedUser.Run", pf_handle, "CALLS");
2827+
ok[g][5] = !has_edge_from_callable_to_qn(gb, "ChainedUser.Run", decoy_handle, "CALLS");
2828+
ok[g][6] = has_edge_from_callable_to_qn(gb, "Boot.Boot1", global_check, "CALLS");
2829+
ok[g][7] = !has_edge_from_callable_to_qn(gb, "Boot.Boot1", other_check, "CALLS");
2830+
for (int k = 0; k < N_CHECKS; k++) {
2831+
all = all && ok[g][k];
2832+
}
2833+
}
2834+
if (!all) {
2835+
for (int g = 0; g < 2; g++) {
2836+
printf(" C# nested/global diagnostic %s: nested_code=%d/%d nested_ctor=%d/%d "
2837+
"chained=%d/%d global_ns=%d/%d\n",
2838+
g == 0 ? "sequential" : "parallel", ok[g][0], ok[g][1], ok[g][2], ok[g][3],
2839+
ok[g][4], ok[g][5], ok[g][6], ok[g][7]);
2840+
}
2841+
}
2842+
cbm_gbuf_free(graphs[0]);
2843+
cbm_gbuf_free(graphs[1]);
2844+
th_rmtree(tmpdir);
2845+
ASSERT_TRUE(all);
2846+
PASS();
2847+
}
2848+
26712849
/* External Kotlin protocol targets must not borrow a project method merely
26722850
* because the final Class.method segments agree. Raw assertions prove that the
26732851
* iterator/destructuring carriers and their stdlib semantic rows really exist;
@@ -4786,6 +4964,7 @@ SUITE(parallel) {
47864964
RUN_TEST(parallel_typescript_import_namespace_exact_parity);
47874965
RUN_TEST(parallel_tsx_import_namespace_exact_parity);
47884966
RUN_TEST(parallel_csharp_same_name_class_binds_by_namespace);
4967+
RUN_TEST(parallel_csharp_nested_type_and_global_namespace_binding);
47894968
RUN_TEST(parallel_kotlin_external_protocol_does_not_use_project_class_method_tail);
47904969
RUN_TEST(parallel_kotlin_nonbinary_operator_carriers_reach_graph);
47914970
RUN_TEST(parallel_rust_cross_crate_worker_receives_workspace_manifest);

0 commit comments

Comments
 (0)