Skip to content

Commit 19c8d07

Browse files
committed
fix(swift): unify incompatible-overload fallback across resolver paths
A Swift call that shares its bare name with project symbols but matches none of their overloads was handled differently by the two resolvers: the parallel path cleared the bare-name match and fell through as unresolved (keeping each resolver's empty-resolution service fallbacks), while the sequential path dropped the call outright. Skip the bare-name registry bind on the sequential path too — it would bind a wrong overload — and continue as unresolved, so count < 0 behaves exactly like any other unresolved call on each resolver. Pipeline regressions pin the global-fetch fallback (DeusData#856) on both paths: the incompatible local overload gets no wrong CALLS edge, and the URL call keeps its HTTP_CALLS edge. The route-registration fallback differs between the paths on main (sequential additionally requires a '/'-leading first argument); that pre-existing divergence is out of scope here. Refs DeusData#2061 Signed-off-by: DavidHLP <[email protected]>
1 parent d5cb9b5 commit 19c8d07

3 files changed

Lines changed: 152 additions & 8 deletions

File tree

‎src/pipeline/pass_calls.c‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -544,19 +544,28 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call,
544544
}
545545
}
546546

547+
/* Swift label-compatible overload selection (#2061). count > 0: emit an
548+
* edge to every compatible candidate and stop. count < 0: project symbols
549+
* share this name but none matches the call's labels — a bare-name
550+
* registry match would bind a WRONG overload, so resolution is skipped
551+
* and the call falls through as unresolved; the empty-resolution service
552+
* fallbacks below still run, mirroring the Swift block in pass_parallel.c
553+
* (service edges survive overload suppression, per #523/#606/#856).
554+
* count == 0: no overload metadata for this name — resolve normally. */
555+
int swift_candidates = 0;
547556
if (lang == CBM_LANG_SWIFT) {
548557
const char *candidates[CBM_SZ_256];
549-
int count = cbm_registry_swift_candidates(ctx->registry, call, module_qn, imp_vals,
550-
imp_count, candidates, CBM_SZ_256);
551-
if (count != 0) {
558+
swift_candidates = cbm_registry_swift_candidates(ctx->registry, call, module_qn, imp_vals,
559+
imp_count, candidates, CBM_SZ_256);
560+
if (swift_candidates > 0) {
552561
int emitted = 0;
553-
for (int i = 0; i < count; i++) {
562+
for (int i = 0; i < swift_candidates; i++) {
554563
const cbm_gbuf_node_t *target = cbm_gbuf_find_by_qn(ctx->gbuf, candidates[i]);
555564
if (target && target->id != source_node->id) {
556565
cbm_resolution_t selected = {.qualified_name = candidates[i],
557566
.strategy = "swift_labels",
558-
.confidence = count == 1 ? 0.90 : 0.55,
559-
.candidate_count = count};
567+
.confidence = swift_candidates == 1 ? 0.90 : 0.55,
568+
.candidate_count = swift_candidates};
560569
emit_classified_edge(ctx, call, source_node, target, &selected, module_qn,
561570
imp_keys, imp_vals, imp_count, false);
562571
emitted++;
@@ -566,8 +575,11 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call,
566575
}
567576
}
568577

569-
cbm_resolution_t res = cbm_registry_resolve(ctx->registry, call->callee_name, module_qn,
570-
imp_keys, imp_vals, imp_count);
578+
cbm_resolution_t res = {0};
579+
if (swift_candidates >= 0) {
580+
res = cbm_registry_resolve(ctx->registry, call->callee_name, module_qn, imp_keys, imp_vals,
581+
imp_count);
582+
}
571583
if (!res.qualified_name || res.qualified_name[0] == '\0') {
572584
/* Resolution is empty when the callee belongs to an EXTERNAL client
573585
* library whose source is not in the indexed tree (e.g. `requests.get`,

‎src/pipeline/pass_parallel.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,10 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
30163016
continue;
30173017
}
30183018
if (count < 0) {
3019+
/* Same-named Swift symbols exist but none is label-compatible:
3020+
* clear the bare-name match (it would bind a wrong overload)
3021+
* and fall through as unresolved — the empty-resolution
3022+
* fallbacks below still run. Mirrors pass_calls.c. */
30193023
res = (cbm_resolution_t){0};
30203024
}
30213025
}

‎tests/test_pipeline.c‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7094,6 +7094,132 @@ TEST(pipeline_swift_overloads_parallel_candidates_issue2061) {
70947094
PASS();
70957095
}
70967096

7097+
/* #2061: a Swift call matching NONE of a same-named project symbol's
7098+
* overloads must not bind to it (a bare-name match would bind a wrong
7099+
* overload), while the empty-resolution service fallbacks still apply —
7100+
* the contract both resolver paths share (Swift blocks in pass_calls.c
7101+
* and pass_parallel.c). Local `fetch(id:)` cannot take a one-argument
7102+
* unlabeled URL call, so the call stays unresolved and classifies as the
7103+
* global API (#856). */
7104+
TEST(pipeline_swift_incompatible_overload_unresolved_issue2061) {
7105+
char tmp[256];
7106+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_swiftincompat_XXXXXX");
7107+
if (!cbm_mkdtemp(tmp)) {
7108+
FAIL("tmpdir");
7109+
}
7110+
7111+
write_temp_file(tmp, "Sources/Api.swift",
7112+
"func fetch(id: Int) -> Int {\n"
7113+
" return id\n"
7114+
"}\n");
7115+
write_temp_file(tmp, "Sources/Caller.swift",
7116+
"class Caller {\n"
7117+
" func run() -> Int {\n"
7118+
" return fetch(\"https://api.example.com/data\")\n"
7119+
" }\n"
7120+
"}\n");
7121+
7122+
char db_path[512];
7123+
snprintf(db_path, sizeof(db_path), "%s/swiftincompat.db", tmp);
7124+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
7125+
ASSERT_NOT_NULL(p);
7126+
ASSERT_EQ(cbm_pipeline_run(p), 0);
7127+
const char *project = cbm_pipeline_project_name(p);
7128+
7129+
cbm_store_t *s = cbm_store_open_path(db_path);
7130+
ASSERT_NOT_NULL(s);
7131+
7132+
/* The incompatible local definition IS indexed (anti-vacuous guard) ... */
7133+
cbm_node_t *fetches = NULL;
7134+
int fetch_count = 0;
7135+
ASSERT_EQ(cbm_store_find_nodes_by_name(s, project, "fetch", &fetches, &fetch_count),
7136+
CBM_STORE_OK);
7137+
ASSERT_EQ(fetch_count, 1);
7138+
cbm_node_t *runs = NULL;
7139+
int run_count = 0;
7140+
ASSERT_EQ(cbm_store_find_nodes_by_name(s, project, "run", &runs, &run_count), CBM_STORE_OK);
7141+
ASSERT_EQ(run_count, 1);
7142+
/* ... but the call never binds to it ... */
7143+
ASSERT_EQ(pipeline_has_calls_edge(s, runs[0].id, fetches[0].id), 0);
7144+
cbm_store_free_nodes(fetches, fetch_count);
7145+
cbm_store_free_nodes(runs, run_count);
7146+
7147+
/* ... and the URL call still classifies as the global API. */
7148+
ASSERT_EQ(cbm_store_count_edges_by_type(s, project, "HTTP_CALLS"), 1);
7149+
7150+
cbm_store_close(s);
7151+
cbm_pipeline_free(p);
7152+
th_rmtree(tmp);
7153+
PASS();
7154+
}
7155+
7156+
/* The parallel resolver must reach the same verdict for the same fixture
7157+
* (>= 50 files routes calls through resolve_file_calls). */
7158+
TEST(pipeline_swift_incompatible_overload_parallel_fallback_issue2061) {
7159+
char tmp[256];
7160+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_swiftincompat_par_XXXXXX");
7161+
if (!cbm_mkdtemp(tmp)) {
7162+
FAIL("tmpdir");
7163+
}
7164+
7165+
write_temp_file(tmp, "Sources/Api.swift",
7166+
"func fetch(id: Int) -> Int {\n"
7167+
" return id\n"
7168+
"}\n");
7169+
write_temp_file(tmp, "Sources/Caller.swift",
7170+
"class Caller {\n"
7171+
" func run() -> Int {\n"
7172+
" return fetch(\"https://api.example.com/data\")\n"
7173+
" }\n"
7174+
"}\n");
7175+
for (int i = 0; i < 52; i++) {
7176+
char path[64], source[80];
7177+
snprintf(path, sizeof(path), "Sources/Filler%d.swift", i);
7178+
snprintf(source, sizeof(source), "func filler%d() {}\n", i);
7179+
write_temp_file(tmp, path, source);
7180+
}
7181+
7182+
char *previous_workers = getenv("CBM_WORKERS");
7183+
char *saved_workers = previous_workers ? strdup(previous_workers) : NULL;
7184+
cbm_setenv("CBM_WORKERS", "4", 1);
7185+
char db_path[512];
7186+
snprintf(db_path, sizeof(db_path), "%s/swiftincompat.db", tmp);
7187+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
7188+
int run_result = p ? cbm_pipeline_run(p) : -1;
7189+
if (saved_workers) {
7190+
cbm_setenv("CBM_WORKERS", saved_workers, 1);
7191+
} else {
7192+
cbm_unsetenv("CBM_WORKERS");
7193+
}
7194+
free(saved_workers);
7195+
ASSERT_NOT_NULL(p);
7196+
ASSERT_EQ(run_result, 0);
7197+
const char *project = cbm_pipeline_project_name(p);
7198+
7199+
cbm_store_t *s = cbm_store_open_path(db_path);
7200+
ASSERT_NOT_NULL(s);
7201+
7202+
cbm_node_t *fetches = NULL;
7203+
int fetch_count = 0;
7204+
ASSERT_EQ(cbm_store_find_nodes_by_name(s, project, "fetch", &fetches, &fetch_count),
7205+
CBM_STORE_OK);
7206+
ASSERT_EQ(fetch_count, 1);
7207+
cbm_node_t *runs = NULL;
7208+
int run_count = 0;
7209+
ASSERT_EQ(cbm_store_find_nodes_by_name(s, project, "run", &runs, &run_count), CBM_STORE_OK);
7210+
ASSERT_EQ(run_count, 1);
7211+
ASSERT_EQ(pipeline_has_calls_edge(s, runs[0].id, fetches[0].id), 0);
7212+
cbm_store_free_nodes(fetches, fetch_count);
7213+
cbm_store_free_nodes(runs, run_count);
7214+
7215+
ASSERT_EQ(cbm_store_count_edges_by_type(s, project, "HTTP_CALLS"), 1);
7216+
7217+
cbm_store_close(s);
7218+
cbm_pipeline_free(p);
7219+
th_rmtree(tmp);
7220+
PASS();
7221+
}
7222+
70977223
/* Native `fetch()` (#856), parallel path (>= 50 files -> pass_parallel.c's
70987224
* resolve_file_calls). Mirrors pipeline_native_fetch_classified_as_http_calls
70997225
* but forces the parallel resolver, since the empty-resolution fallback is a
@@ -15384,6 +15510,8 @@ SUITE(pipeline) {
1538415510
RUN_TEST(pipeline_swift_http_call_makes_route_issue1892);
1538515511
RUN_TEST(pipeline_swift_overloads_keep_argument_labels_issue2061);
1538615512
RUN_TEST(pipeline_swift_overloads_parallel_candidates_issue2061);
15513+
RUN_TEST(pipeline_swift_incompatible_overload_unresolved_issue2061);
15514+
RUN_TEST(pipeline_swift_incompatible_overload_parallel_fallback_issue2061);
1538715515
RUN_TEST(pipeline_native_fetch_parallel_classified_as_http_calls);
1538815516
RUN_TEST(pipeline_local_fetch_shadow_not_classified_as_http);
1538915517
/* Git history pass */

0 commit comments

Comments
 (0)