Skip to content

Commit f61d7c0

Browse files
authored
Merge pull request #2170 from AmirF194/fix/2121-csharp-lowercase-receiver-type-keyword
fix(registry): recognize C# lower-case type keywords in receiver_chain_admits
2 parents 14aeda1 + 55db73c commit f61d7c0

4 files changed

Lines changed: 260 additions & 24 deletions

File tree

‎src/pipeline/pipeline_internal.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ cbm_resolution_t cbm_registry_resolve_lang(const cbm_registry_t *r, const char *
260260
CBMLanguage lang);
261261
/* The CALLS variant: as cbm_registry_resolve_lang, except that a bare C call
262262
* (no `.` / `->`) does not consider Field or Method nodes, which it can never
263-
* call. */
263+
* call, and that in a C# file a built-in type keyword root (`int.TryParse`)
264+
* names a type for the receiver chain check (#2121). */
264265
cbm_resolution_t cbm_registry_resolve_call_lang(const cbm_registry_t *r, const char *callee_name,
265266
const char *module_qn, const char **import_map_keys,
266267
const char **import_map_vals, int import_map_count,

‎src/pipeline/registry.c‎

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ typedef struct {
485485
cbm_resolution_t res;
486486
size_t scope_len; /* raw and Python-scoped requests may share one file cache */
487487
bool skip_members; /* a C call and a C reference to the same name share it too */
488+
bool cs_keywords; /* a C# call and a C# reference to the same name share it too */
488489
} resolve_cache_entry_t;
489490

490491
static CBM_TLS CBMHashTable *_resolve_cache = NULL;
@@ -2384,6 +2385,26 @@ static const char *qualified_suffix_match(const qn_array_t *arr, const char *cal
23842385
return match;
23852386
}
23862387

2388+
/* True when the first segment of the dotted chain `dotted` is one of C#'s
2389+
* built-in type keywords (int, string, bool, ...; nint and nuint since C# 9).
2390+
* Closed list: these words are grammar, not a naming heuristic. `dynamic` is
2391+
* not on it: the type has no static members, so `dynamic.X` is a variable
2392+
* named `dynamic` and stays an ordinary lower-case value root. */
2393+
static bool cs_type_keyword_root(const char *dotted) {
2394+
static const char *const keywords[] = {
2395+
"bool", "byte", "char", "decimal", "double", "float", "int", "long", "nint",
2396+
"nuint", "object", "sbyte", "short", "string", "uint", "ulong", "ushort", "void",
2397+
};
2398+
const char *dot = strchr(dotted, '.');
2399+
size_t len = dot ? (size_t)(dot - dotted) : strlen(dotted);
2400+
for (size_t i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
2401+
if (strlen(keywords[i]) == len && strncmp(dotted, keywords[i], len) == 0) {
2402+
return true;
2403+
}
2404+
}
2405+
return false;
2406+
}
2407+
23872408
/* A dotted callee whose FIRST segment starts upper-case names a type — URLSession,
23882409
* Calendar, JSONEncoder. That receiver chain is evidence the bare-name scorers
23892410
* throw away, and throwing it away binds Foundation's URLSession.shared.data to
@@ -2397,9 +2418,17 @@ static const char *qualified_suffix_match(const qn_array_t *arr, const char *cal
23972418
* show, so the chain proves nothing there and the call passes through
23982419
* unchanged. A callee with no separator passes through as well.
23992420
*
2400-
* Language agnostic by design: the registry holds no language, and every
2401-
* language that writes receiver chains gains the same protection. */
2402-
static bool receiver_chain_admits(const char *callee_name, const char *candidate_qn) {
2421+
* One exception, for C# CALLS only (#2121): C# spells its built-in types as
2422+
* lower-case keywords, so in `int.TryParse` or `string.Join` the root names a
2423+
* type and gets the same check (cs_type_keyword_root). `lang` is the calling
2424+
* file's language on the CALLS path (cbm_registry_resolve_call_lang) and
2425+
* CBM_LANG_COUNT on every other resolve. In Python, JS or Go these words are
2426+
* ordinary identifiers, so a variable named `object` stays a value root there.
2427+
*
2428+
* Otherwise language agnostic by design: every language that writes receiver
2429+
* chains gains the same protection. */
2430+
static bool receiver_chain_admits(const char *callee_name, const char *candidate_qn,
2431+
CBMLanguage lang) {
24032432
/* Normalize "::" -> "." so the chain composes with dotted candidate QNs,
24042433
* the same way qualified_suffix_match does. */
24052434
char dotted[CBM_SZ_512];
@@ -2418,7 +2447,8 @@ static bool receiver_chain_admits(const char *callee_name, const char *candidate
24182447
if (!last_dot) {
24192448
return true; /* bare name — no receiver chain to judge */
24202449
}
2421-
if (dotted[0] < 'A' || dotted[0] > 'Z') {
2450+
if ((dotted[0] < 'A' || dotted[0] > 'Z') &&
2451+
!(lang == CBM_LANG_CSHARP && cs_type_keyword_root(dotted))) {
24222452
return true; /* lower-case root names a value, not a type */
24232453
}
24242454
/* A name written in capitals with underscores is a constant holding a
@@ -2506,10 +2536,12 @@ static void candidates_without_members(const cbm_registry_t *r, const qn_array_t
25062536
.lang = side ? lang : NULL};
25072537
}
25082538

2509-
/* Strategies 3.5, 3 and 4 over one same-name candidate set. */
2539+
/* Strategies 3.5, 3 and 4 over one same-name candidate set. `lang` reaches
2540+
* receiver_chain_admits only (see there). */
25102541
static cbm_resolution_t resolve_name_candidates(const cbm_registry_t *r, const qn_array_t *arr,
25112542
const char *callee_name, const char *module_qn,
2512-
const char **import_vals, int import_count) {
2543+
const char **import_vals, int import_count,
2544+
CBMLanguage lang) {
25132545
/* Strategy 3.5: a qualified callee disambiguates among multiple same-name
25142546
* candidates by full qualified tail, before bare-name scoring collapses
25152547
* them onto a single winner. */
@@ -2522,7 +2554,7 @@ static cbm_resolution_t resolve_name_candidates(const cbm_registry_t *r, const q
25222554

25232555
/* Strategy 3: unique name */
25242556
if (arr->count == SKIP_ONE) {
2525-
if (!receiver_chain_admits(callee_name, arr->items[0])) {
2557+
if (!receiver_chain_admits(callee_name, arr->items[0], lang)) {
25262558
return empty_result();
25272559
}
25282560
double conf = CONF_UNIQUE_NAME;
@@ -2540,7 +2572,7 @@ static cbm_resolution_t resolve_name_candidates(const cbm_registry_t *r, const q
25402572
const char *best = best_by_import_distance(r, (const char **)arr->items, qn_test_flags(arr),
25412573
qn_lang_flags(arr), arr->count, module_qn);
25422574
if (best) {
2543-
if (!receiver_chain_admits(callee_name, best)) {
2575+
if (!receiver_chain_admits(callee_name, best, lang)) {
25442576
return empty_result();
25452577
}
25462578
double conf = candidate_count_penalty(CONF_SUFFIX_MATCH, arr->count);
@@ -2558,7 +2590,7 @@ static cbm_resolution_t resolve_name_candidates(const cbm_registry_t *r, const q
25582590
* libc `read()` the unique name of a C++ adapter's Method instead. */
25592591
static cbm_resolution_t resolve_name_lookup(const cbm_registry_t *r, const char *callee_name,
25602592
const char *module_qn, const char **import_vals,
2561-
int import_count, bool skip_members) {
2593+
int import_count, bool skip_members, CBMLanguage lang) {
25622594
const char *lookup = simple_name(callee_name);
25632595
const qn_array_t *arr = cbm_ht_get(r->by_name, lookup);
25642596
if (!arr || arr->count == 0) {
@@ -2576,17 +2608,20 @@ static cbm_resolution_t resolve_name_lookup(const cbm_registry_t *r, const char
25762608
if (view.count == 0) {
25772609
return empty_result();
25782610
}
2579-
return resolve_name_candidates(r, &view, callee_name, module_qn, import_vals, import_count);
2611+
return resolve_name_candidates(r, &view, callee_name, module_qn, import_vals, import_count,
2612+
lang);
25802613
}
2581-
return resolve_name_candidates(r, arr, callee_name, module_qn, import_vals, import_count);
2614+
return resolve_name_candidates(r, arr, callee_name, module_qn, import_vals, import_count, lang);
25822615
}
25832616

25842617
/* The strategy chain shared by both public resolve variants (no caching here —
2585-
* cbm_registry_resolve owns the per-file cache). */
2618+
* cbm_registry_resolve owns the per-file cache). `lang`: see
2619+
* receiver_chain_admits. */
25862620
static cbm_resolution_t registry_resolve_chain(const cbm_registry_t *r, const char *callee_name,
25872621
const char *module_qn, const char **import_map_keys,
25882622
const char **import_map_vals, int import_map_count,
2589-
size_t scope_len, bool skip_members) {
2623+
size_t scope_len, bool skip_members,
2624+
CBMLanguage lang) {
25902625
/* Split callee at the first path separator: "pkg.Func" → prefix="pkg",
25912626
* suffix="Func". Rust/C++ use "::" rather than ".", so honor whichever
25922627
* separator appears first ("lib::square" → prefix="lib", suffix="square").
@@ -2624,7 +2659,7 @@ static cbm_resolution_t registry_resolve_chain(const cbm_registry_t *r, const ch
26242659
if (!(res.qualified_name && res.qualified_name[0])) {
26252660
/* Strategy 3+4: name lookup */
26262661
res = resolve_name_lookup(r, callee_name, module_qn, import_map_vals, import_map_count,
2627-
skip_members);
2662+
skip_members, lang);
26282663
}
26292664
return res;
26302665
}
@@ -2672,25 +2707,31 @@ bool cbm_registry_name_guess_vetoed(const cbm_registry_t *r, CBMLanguage caller_
26722707
registry_entry_language(r, res->qualified_name));
26732708
}
26742709

2710+
/* call_lang: the calling file's language for a CALLS resolve, CBM_LANG_COUNT
2711+
* for every other one (see receiver_chain_admits). */
26752712
static cbm_resolution_t registry_resolve_scoped(const cbm_registry_t *r, const char *callee_name,
26762713
const char *module_qn, const char **import_map_keys,
26772714
const char **import_map_vals, int import_map_count,
2678-
size_t scope_len, bool skip_members) {
2715+
size_t scope_len, bool skip_members,
2716+
CBMLanguage call_lang) {
26792717
if (!r || !callee_name) {
26802718
return empty_result();
26812719
}
26822720

26832721
/* Per-file cache: same callee_name in N call sites → 1 chain walk
26842722
* + N-1 O(1) hash hits. module_qn is constant per file so the
2685-
* cache key only needs callee_name. */
2723+
* cache key only needs callee_name. The chain reads call_lang only as
2724+
* "is it C#", so that one bit is all the entry has to record. */
2725+
bool cs_keywords = call_lang == CBM_LANG_CSHARP;
26862726
resolve_cache_entry_t *cached =
26872727
_resolve_cache ? (resolve_cache_entry_t *)cbm_ht_get(_resolve_cache, callee_name) : NULL;
2688-
if (cached && cached->scope_len == scope_len && cached->skip_members == skip_members)
2728+
if (cached && cached->scope_len == scope_len && cached->skip_members == skip_members &&
2729+
cached->cs_keywords == cs_keywords)
26892730
return cached->res;
26902731

26912732
cbm_resolution_t res =
26922733
registry_resolve_chain(r, callee_name, module_qn, import_map_keys, import_map_vals,
2693-
import_map_count, scope_len, skip_members);
2734+
import_map_count, scope_len, skip_members, call_lang);
26942735

26952736
/* Data relations (Table/View) are lineage-only registry members: common
26962737
* table names (users, orders, config) collide with code identifiers across
@@ -2711,12 +2752,14 @@ static cbm_resolution_t registry_resolve_scoped(const cbm_registry_t *r, const c
27112752
cached->res = res;
27122753
cached->scope_len = scope_len;
27132754
cached->skip_members = skip_members;
2755+
cached->cs_keywords = cs_keywords;
27142756
} else if (_resolve_cache) {
27152757
resolve_cache_entry_t *e = (resolve_cache_entry_t *)malloc(sizeof(*e));
27162758
if (e) {
27172759
e->res = res;
27182760
e->scope_len = scope_len;
27192761
e->skip_members = skip_members;
2762+
e->cs_keywords = cs_keywords;
27202763
char *kdup = strdup(callee_name);
27212764
if (kdup) {
27222765
cbm_ht_set(_resolve_cache, kdup, e);
@@ -3734,7 +3777,7 @@ int cbm_registry_swift_candidates(const cbm_registry_t *r, const CBMCall *call,
37343777
const char *qn = bucket->items[i];
37353778
swift_signature_t *meta = cbm_ht_get(r->swift_signatures, qn);
37363779
has_swift = has_swift || meta != NULL;
3737-
if (meta && receiver_chain_admits(call->callee_name, qn) &&
3780+
if (meta && receiver_chain_admits(call->callee_name, qn, CBM_LANG_SWIFT) &&
37383781
swift_signature_matches(meta, call)) {
37393782
matches[matched++] = qn;
37403783
}
@@ -4445,7 +4488,8 @@ cbm_resolution_t cbm_registry_resolve(const cbm_registry_t *r, const char *calle
44454488
const char *module_qn, const char **import_map_keys,
44464489
const char **import_map_vals, int import_map_count) {
44474490
return registry_resolve_scoped(r, callee_name, module_qn, import_map_keys, import_map_vals,
4448-
import_map_count, module_qn ? strlen(module_qn) : 0, false);
4491+
import_map_count, module_qn ? strlen(module_qn) : 0, false,
4492+
CBM_LANG_COUNT);
44494493
}
44504494

44514495
static size_t lang_scope_len(const char *module_qn, CBMLanguage lang) {
@@ -4458,7 +4502,8 @@ cbm_resolution_t cbm_registry_resolve_lang(const cbm_registry_t *r, const char *
44584502
const char **import_map_vals, int import_map_count,
44594503
CBMLanguage lang) {
44604504
return registry_resolve_scoped(r, callee_name, module_qn, import_map_keys, import_map_vals,
4461-
import_map_count, lang_scope_len(module_qn, lang), false);
4505+
import_map_count, lang_scope_len(module_qn, lang), false,
4506+
CBM_LANG_COUNT);
44624507
}
44634508

44644509
cbm_resolution_t cbm_registry_resolve_call_lang(const cbm_registry_t *r, const char *callee_name,
@@ -4472,7 +4517,8 @@ cbm_resolution_t cbm_registry_resolve_call_lang(const cbm_registry_t *r, const c
44724517
bool skip_members = lang == CBM_LANG_C && callee_name && !strchr(callee_name, '.') &&
44734518
!strstr(callee_name, "->");
44744519
return registry_resolve_scoped(r, callee_name, module_qn, import_map_keys, import_map_vals,
4475-
import_map_count, lang_scope_len(module_qn, lang), skip_members);
4520+
import_map_count, lang_scope_len(module_qn, lang), skip_members,
4521+
lang);
44764522
}
44774523

44784524
/* ── Class-qualified route handlers (PHP, #1146) ─────────────────── */
@@ -4640,7 +4686,8 @@ cbm_resolution_t cbm_registry_resolve_lineage(const cbm_registry_t *r, const cha
46404686
* it would poison one variant with the other's semantics. SQL files hold
46414687
* few distinct relation refs, so the chain walk stays cheap. */
46424688
return registry_resolve_chain(r, callee_name, module_qn, import_map_keys, import_map_vals,
4643-
import_map_count, module_qn ? strlen(module_qn) : 0, false);
4689+
import_map_count, module_qn ? strlen(module_qn) : 0, false,
4690+
CBM_LANG_COUNT);
46444691
}
46454692

46464693
/* Python looks a bare name up in the calling def's own scope, then in each

‎tests/test_parallel.c‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3331,6 +3331,100 @@ TEST(parallel_csharp_parse_damaged_namespace_is_not_global) {
33313331
PASS();
33323332
}
33333333

3334+
/* #2121, the issue's own two files: C# spells its built-in types as lower-case
3335+
* keywords, so `int.TryParse(...)` calls the BCL, never the project's only
3336+
* TryParse. The LSP's answer (System.Int32.TryParse) has no graph node, so the
3337+
* call falls through to the registry, whose receiver chain check used to skip
3338+
* every lower-case root. Both resolvers must refuse the name guess (also for
3339+
* the C# 9 `nint`), and a real call to the project type must still bind. */
3340+
TEST(parallel_csharp_keyword_receiver_no_name_guess_issue2121) {
3341+
static const char decoys_src[] =
3342+
"namespace Contoso.App;\n"
3343+
"\n"
3344+
"public class TsidNode\n"
3345+
"{\n"
3346+
" public static bool TryParse(string text, out TsidNode n)\n"
3347+
" {\n"
3348+
" n = null;\n"
3349+
" return false;\n"
3350+
" }\n"
3351+
"}\n";
3352+
static const char demo_src[] = "using Microsoft.Extensions.Options;\n"
3353+
"\n"
3354+
"namespace Contoso.App;\n"
3355+
"\n"
3356+
"public class Demo\n"
3357+
"{\n"
3358+
" public string M(string input)\n"
3359+
" {\n"
3360+
" _ = int.TryParse(input, out _);\n"
3361+
" _ = nint.TryParse(input, out _);\n"
3362+
" return input;\n"
3363+
" }\n"
3364+
"\n"
3365+
" public string Control(string input)\n"
3366+
" {\n"
3367+
" _ = TsidNode.TryParse(input, out _);\n"
3368+
" return input;\n"
3369+
" }\n"
3370+
"}\n";
3371+
const char *project = "cbm_cs_2121";
3372+
static const char *rels[] = {"src/Decoys.cs", "src/Demo.cs"};
3373+
const char *srcs[] = {decoys_src, demo_src};
3374+
enum { N_FILES = 2 };
3375+
3376+
char tmpdir[256];
3377+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_par_cs_2121_XXXXXX");
3378+
if (!cbm_mkdtemp(tmpdir)) {
3379+
FAIL("mkdtemp failed");
3380+
}
3381+
char paths[N_FILES][512];
3382+
cbm_file_info_t files[N_FILES] = {0};
3383+
for (int i = 0; i < N_FILES; i++) {
3384+
snprintf(paths[i], sizeof(paths[i]), "%s/%s", tmpdir, rels[i]);
3385+
if (th_write_file(paths[i], srcs[i]) != 0) {
3386+
th_rmtree(tmpdir);
3387+
FAIL("failed to write #2121 fixture");
3388+
}
3389+
files[i].path = paths[i];
3390+
files[i].rel_path = (char *)rels[i];
3391+
files[i].language = CBM_LANG_CSHARP;
3392+
}
3393+
3394+
cbm_gbuf_t *graphs[2] = {run_sequential_with_lsp_cross(project, tmpdir, files, N_FILES), NULL};
3395+
g_harness_cs_registry = true;
3396+
graphs[1] = run_parallel(project, tmpdir, files, N_FILES, 2);
3397+
g_harness_cs_registry = false;
3398+
ASSERT_NOT_NULL(graphs[0]);
3399+
ASSERT_NOT_NULL(graphs[1]);
3400+
3401+
const char *tsid = "cbm_cs_2121.src.Decoys.TsidNode.TryParse";
3402+
bool ok[2][4];
3403+
bool all = true;
3404+
for (int g = 0; g < 2; g++) {
3405+
cbm_gbuf_t *gb = graphs[g];
3406+
/* The negative below must not pass because a node went missing. */
3407+
ok[g][0] = cbm_gbuf_find_by_qn(gb, tsid) != NULL;
3408+
ok[g][1] = find_unique_callable_node_by_tail(gb, "Demo.M") != NULL;
3409+
ok[g][2] = !has_edge_from_callable_to_qn(gb, "Demo.M", tsid, "CALLS");
3410+
ok[g][3] = has_edge_from_callable_to_qn(gb, "Demo.Control", tsid, "CALLS");
3411+
for (int k = 0; k < 4; k++) {
3412+
all = all && ok[g][k];
3413+
}
3414+
}
3415+
if (!all) {
3416+
for (int g = 0; g < 2; g++) {
3417+
printf(" #2121 diagnostic %s: target=%d caller=%d no_keyword_guess=%d control=%d\n",
3418+
g == 0 ? "sequential" : "parallel", ok[g][0], ok[g][1], ok[g][2], ok[g][3]);
3419+
}
3420+
}
3421+
cbm_gbuf_free(graphs[0]);
3422+
cbm_gbuf_free(graphs[1]);
3423+
th_rmtree(tmpdir);
3424+
ASSERT_TRUE(all);
3425+
PASS();
3426+
}
3427+
33343428
/* External Kotlin protocol targets must not borrow a project method merely
33353429
* because the final Class.method segments agree. Raw assertions prove that the
33363430
* iterator/destructuring carriers and their stdlib semantic rows really exist;
@@ -6031,6 +6125,7 @@ SUITE(parallel) {
60316125
RUN_TEST(parallel_csharp_same_name_class_binds_by_namespace);
60326126
RUN_TEST(parallel_csharp_nested_type_and_global_namespace_binding);
60336127
RUN_TEST(parallel_csharp_parse_damaged_namespace_is_not_global);
6128+
RUN_TEST(parallel_csharp_keyword_receiver_no_name_guess_issue2121);
60346129
RUN_TEST(parallel_kotlin_external_protocol_does_not_use_project_class_method_tail);
60356130
RUN_TEST(parallel_kotlin_nonbinary_operator_carriers_reach_graph);
60366131
RUN_TEST(parallel_rust_cross_crate_worker_receives_workspace_manifest);

0 commit comments

Comments
 (0)