Skip to content

Commit c0a8ed8

Browse files
DeusDataersintarhan
andcommitted
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): cs_top_level_namespace returned NULL for it, making it invisible to step 8b. Top-level C# types now register namespace_qn "" and cs_visible_namespaces appends the global namespace last. Test: parallel_csharp_nested_type_and_global_namespace_binding covers a nested-type constructor and the member call on its result, a chained constructor receiver, 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. Decoys are registered first. Adapted to current main: the two constructor-edge checks gate the parallel path only, because the sequential constructor edge is still drawn by the textual constructor carrier, a layer this change does not touch. Distilled from DeusData#2401: the net change of commits d11be54 and 613060e. The temporary debug prints added in d11be54 and the stale copy of DeusData#2347 the branch carried (already on main as 50d19f8) are not taken. Co-authored-by: Ersin Tarhan <[email protected]> Signed-off-by: Martin Vogel <[email protected]>
1 parent c07cb8e commit c0a8ed8

2 files changed

Lines changed: 228 additions & 2 deletions

File tree

‎internal/cbm/lsp/cs_lsp.c‎

Lines changed: 24 additions & 2 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

@@ -3749,13 +3768,16 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
37493768
* type is not reachable by its simple name through a namespace). */
37503769
static const char *cs_top_level_namespace(const char *type_qn, const char *short_name,
37513770
const char *module_qn, const char *namespace_name) {
3752-
if (!type_qn || !short_name || !module_qn || !namespace_name || !namespace_name[0])
3771+
if (!type_qn || !short_name || !module_qn)
37533772
return NULL;
37543773
size_t mlen = strlen(module_qn);
37553774
if (strncmp(type_qn, module_qn, mlen) != 0 || type_qn[mlen] != '.' ||
37563775
strcmp(type_qn + mlen + 1, short_name) != 0)
37573776
return NULL;
3758-
return namespace_name;
3777+
/* Top-level C# type: its declared namespace, "" when it declares none
3778+
* (global namespace) so step 8b can bind it — NULL stays reserved for
3779+
* nested types, which no namespace can reach by simple name. */
3780+
return (namespace_name && namespace_name[0]) ? namespace_name : "";
37593781
}
37603782

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

‎tests/test_parallel.c‎

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3026,6 +3026,209 @@ TEST(parallel_csharp_same_name_class_binds_by_namespace) {
30263026
PASS();
30273027
}
30283028

3029+
/* #2120 residual, reported on a real repo: step 8b binds only namespace-
3030+
* qualified TOP-LEVEL types. A qualified `A.B` whose qualifier A is a TYPE
3031+
* (nested-type constructor `new Command.Result(...)`, chained-constructor
3032+
* receiver `new Grain.Handler(dep).Handle(...)`) and a top-level type with NO
3033+
* namespace declaration (global namespace, e.g. a `[Inject]`-consumed service)
3034+
* both fell through to the short-name fallback, where the prefix score against
3035+
* a path-derived QN is 0 for every variant — registration order picked the
3036+
* target. Decoy files are registered FIRST so the old registry-order behavior
3037+
* binds every check to the decoy and the test fails. */
3038+
TEST(parallel_csharp_nested_type_and_global_namespace_binding) {
3039+
static const char decoy_src[] =
3040+
"namespace Acme.Layouts.Cqrs;\n"
3041+
"public class GrainCommand {\n"
3042+
" public class Handler {\n"
3043+
" public int Handle() => 9;\n"
3044+
" }\n"
3045+
" public class Result {\n"
3046+
" public Result(bool ok) { }\n"
3047+
" public int Code() => 9;\n"
3048+
" }\n"
3049+
"}\n";
3050+
static const char partner_src[] =
3051+
"namespace Acme.Partner.Cqrs;\n"
3052+
"public class GrainCommand {\n"
3053+
" public class Handler {\n"
3054+
" private readonly int _v;\n"
3055+
" public Handler(int v) { _v = v; }\n"
3056+
" public int Handle() => _v;\n"
3057+
" }\n"
3058+
" public class Result {\n"
3059+
" public Result(bool ok) { }\n"
3060+
" public int Code() => 1;\n"
3061+
" }\n"
3062+
"}\n";
3063+
static const char backoffice_src[] =
3064+
"namespace Acme.BackOffice.Cqrs;\n"
3065+
"public class GrainCommand {\n"
3066+
" public class Result {\n"
3067+
" public Result(bool ok) { }\n"
3068+
" public int Code() => 2;\n"
3069+
" }\n"
3070+
"}\n";
3071+
static const char nested_user_src[] =
3072+
"using Acme.Partner.Cqrs;\n"
3073+
"namespace Acme.App;\n"
3074+
"public class NestedUser {\n"
3075+
" public int Run() {\n"
3076+
" var r = new GrainCommand.Result(true);\n"
3077+
" return r.Code();\n"
3078+
" }\n"
3079+
"}\n";
3080+
static const char chained_user_src[] =
3081+
"using Acme.Partner.Cqrs;\n"
3082+
"namespace Acme.App;\n"
3083+
"public class ChainedUser {\n"
3084+
" public int Run() {\n"
3085+
" return new GrainCommand.Handler(3).Handle();\n"
3086+
" }\n"
3087+
"}\n";
3088+
static const char other_update_src[] =
3089+
"namespace Acme.Other.Web.Services;\n"
3090+
"public class UpdateService {\n"
3091+
" public string Check() => \"other\";\n"
3092+
"}\n";
3093+
/* No namespace declaration: the global namespace. */
3094+
static const char global_update_src[] =
3095+
"public class UpdateService {\n"
3096+
" public string Check() => \"global\";\n"
3097+
"}\n";
3098+
static const char boot_src[] =
3099+
"namespace Acme.App;\n"
3100+
"public class Boot {\n"
3101+
" public string Boot1() {\n"
3102+
" var u = new UpdateService();\n"
3103+
" return u.Check();\n"
3104+
" }\n"
3105+
"}\n";
3106+
/* The lookup-order probe: the SAME simple name resolves through an
3107+
* imported namespace AND exists in the global namespace. C# binds the
3108+
* imported namespace first, so ImportUser.Run2 must hit the Other variant
3109+
* and never the global one — proving the global namespace is appended
3110+
* last in cs_visible_namespaces, not merged as an equal. */
3111+
static const char import_user_src[] =
3112+
"using Acme.Other.Web.Services;\n"
3113+
"namespace Acme.App;\n"
3114+
"public class ImportUser {\n"
3115+
" public string Run2() {\n"
3116+
" var u = new UpdateService();\n"
3117+
" return u.Check();\n"
3118+
" }\n"
3119+
"}\n";
3120+
const char *project = "cbm_cs_nested";
3121+
static const char *rels[] = {
3122+
"src/Decoy/Layouts.cs",
3123+
"src/Decoy/OtherUpdate.cs",
3124+
"src/Partner/Cqrs/GrainCommand.cs",
3125+
"src/BackOffice/Cqrs/GrainCommand.cs",
3126+
"src/Globals/UpdateService.cs",
3127+
"src/App/NestedUser.cs",
3128+
"src/App/ChainedUser.cs",
3129+
"src/App/Boot.cs",
3130+
"src/App/ImportUser.cs",
3131+
};
3132+
const char *srcs[] = {decoy_src, other_update_src, partner_src,
3133+
backoffice_src, global_update_src, nested_user_src,
3134+
chained_user_src, boot_src, import_user_src};
3135+
enum { N_FILES = 9, N_CHECKS = 10 };
3136+
3137+
char tmpdir[256];
3138+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_par_cs_nested_XXXXXX");
3139+
if (!cbm_mkdtemp(tmpdir)) {
3140+
FAIL("mkdtemp failed");
3141+
}
3142+
char paths[N_FILES][512];
3143+
cbm_file_info_t files[N_FILES] = {0};
3144+
for (int i = 0; i < N_FILES; i++) {
3145+
snprintf(paths[i], sizeof(paths[i]), "%s/%s", tmpdir, rels[i]);
3146+
if (th_write_file(paths[i], srcs[i]) != 0) {
3147+
th_rmtree(tmpdir);
3148+
FAIL("failed to write C# nested-type fixture");
3149+
}
3150+
files[i].path = paths[i];
3151+
files[i].rel_path = (char *)rels[i];
3152+
files[i].language = CBM_LANG_CSHARP;
3153+
}
3154+
3155+
cbm_gbuf_t *graphs[2] = {run_sequential_with_lsp_cross(project, tmpdir, files, N_FILES), NULL};
3156+
g_harness_cs_registry = true;
3157+
graphs[1] = run_parallel(project, tmpdir, files, N_FILES, 2);
3158+
g_harness_cs_registry = false;
3159+
ASSERT_NOT_NULL(graphs[0]);
3160+
ASSERT_NOT_NULL(graphs[1]);
3161+
3162+
const char *pf_code = "cbm_cs_nested.src.Partner.Cqrs.GrainCommand.GrainCommand."
3163+
"Result.Code";
3164+
const char *pf_handle = "cbm_cs_nested.src.Partner.Cqrs.GrainCommand.GrainCommand."
3165+
"Handler.Handle";
3166+
const char *bo_code = "cbm_cs_nested.src.BackOffice.Cqrs.GrainCommand.GrainCommand."
3167+
"Result.Code";
3168+
const char *decoy_code = "cbm_cs_nested.src.Decoy.Layouts.GrainCommand.GrainCommand."
3169+
"Result.Code";
3170+
const char *decoy_handle = "cbm_cs_nested.src.Decoy.Layouts.GrainCommand.GrainCommand."
3171+
"Handler.Handle";
3172+
const char *global_check = "cbm_cs_nested.src.Globals.UpdateService.UpdateService.Check";
3173+
const char *other_check = "cbm_cs_nested.src.Decoy.OtherUpdate.UpdateService.Check";
3174+
bool ok[2][N_CHECKS];
3175+
bool all = true;
3176+
for (int g = 0; g < 2; g++) {
3177+
cbm_gbuf_t *gb = graphs[g];
3178+
ok[g][0] = has_edge_from_callable_to_qn(gb, "NestedUser.Run", pf_code, "CALLS");
3179+
ok[g][1] = !has_edge_from_callable_to_qn(gb, "NestedUser.Run", bo_code, "CALLS") &&
3180+
!has_edge_from_callable_to_qn(gb, "NestedUser.Run", decoy_code, "CALLS");
3181+
/* The ctor EDGE itself joins through the pipeline's textual carrier and
3182+
* is out of scope here (see PR notes); what must hold is that the
3183+
* nested type RESOLUTION is right, proven by the Code()/Handle() edges
3184+
* above, and that no ctor edge binds to a variant the caller cannot
3185+
* see: neither the BackOffice nor the registry-first decoy. */
3186+
ok[g][2] = !has_edge_from_callable_to_qn(
3187+
gb, "NestedUser.Run",
3188+
"cbm_cs_nested.src.BackOffice.Cqrs.GrainCommand.GrainCommand.Result",
3189+
"CALLS") &&
3190+
!has_edge_from_callable_to_qn(
3191+
gb, "NestedUser.Run",
3192+
"cbm_cs_nested.src.BackOffice.Cqrs.GrainCommand.GrainCommand."
3193+
"Result.Result",
3194+
"CALLS");
3195+
ok[g][3] = !has_edge_from_callable_to_qn(gb, "NestedUser.Run",
3196+
"cbm_cs_nested.src.Decoy.Layouts.GrainCommand."
3197+
"GrainCommand.Result.Result",
3198+
"CALLS");
3199+
ok[g][4] = has_edge_from_callable_to_qn(gb, "ChainedUser.Run", pf_handle, "CALLS");
3200+
ok[g][5] = !has_edge_from_callable_to_qn(gb, "ChainedUser.Run", decoy_handle, "CALLS");
3201+
ok[g][6] = has_edge_from_callable_to_qn(gb, "Boot.Boot1", global_check, "CALLS");
3202+
ok[g][7] = !has_edge_from_callable_to_qn(gb, "Boot.Boot1", other_check, "CALLS");
3203+
ok[g][8] = has_edge_from_callable_to_qn(gb, "ImportUser.Run2", other_check, "CALLS");
3204+
ok[g][9] = !has_edge_from_callable_to_qn(gb, "ImportUser.Run2", global_check, "CALLS");
3205+
for (int k = 0; k < N_CHECKS; k++) {
3206+
/* The ctor-edge checks 2/3 gate the parallel path only. In the
3207+
* sequential path the `new GrainCommand.Result(true)` edge is
3208+
* still drawn by the textual ctor carrier, not by the resolved
3209+
* type (the out-of-scope carrier join above); on current main it
3210+
* lands on the BackOffice Result already with the nested-type
3211+
* resolution alone. The diagnostic below still prints both. */
3212+
if (g == 0 && (k == 2 || k == 3))
3213+
continue;
3214+
all = all && ok[g][k];
3215+
}
3216+
}
3217+
if (!all) {
3218+
for (int g = 0; g < 2; g++) {
3219+
printf(" C# nested/global diagnostic %s: nested_code=%d/%d nested_ctor=%d/%d "
3220+
"chained=%d/%d global_ns=%d/%d imported=%d/%d\n",
3221+
g == 0 ? "sequential" : "parallel", ok[g][0], ok[g][1], ok[g][2], ok[g][3],
3222+
ok[g][4], ok[g][5], ok[g][6], ok[g][7], ok[g][8], ok[g][9]);
3223+
}
3224+
}
3225+
cbm_gbuf_free(graphs[0]);
3226+
cbm_gbuf_free(graphs[1]);
3227+
th_rmtree(tmpdir);
3228+
ASSERT_TRUE(all);
3229+
PASS();
3230+
}
3231+
30293232
/* External Kotlin protocol targets must not borrow a project method merely
30303233
* because the final Class.method segments agree. Raw assertions prove that the
30313234
* iterator/destructuring carriers and their stdlib semantic rows really exist;
@@ -5694,6 +5897,7 @@ SUITE(parallel) {
56945897
RUN_TEST(parallel_typescript_import_namespace_exact_parity);
56955898
RUN_TEST(parallel_tsx_import_namespace_exact_parity);
56965899
RUN_TEST(parallel_csharp_same_name_class_binds_by_namespace);
5900+
RUN_TEST(parallel_csharp_nested_type_and_global_namespace_binding);
56975901
RUN_TEST(parallel_kotlin_external_protocol_does_not_use_project_class_method_tail);
56985902
RUN_TEST(parallel_kotlin_nonbinary_operator_carriers_reach_graph);
56995903
RUN_TEST(parallel_rust_cross_crate_worker_receives_workspace_manifest);

0 commit comments

Comments
 (0)