Skip to content

Commit 25d2897

Browse files
committed
Keep overload parameter shapes on incremental proxies
The closure-repair route rebuilds the resolver graph from proxy nodes that carried only a few projected properties. A same-file overload member lost its overload_params value, so a re-resolved call bound only to the group's first member, and members 2..n entered the short-name index. An edit to a caller file alone then gave a different edge set than a full index. The preseed now projects overload_params for Function and Method rows and merges it into the same properties object as the other projected keys. The full-graph incremental route already loads all properties. No index format change. New tests in the overloads suite (Java, Kotlin, Swift): full index, edit only the caller file, re-index on the closure-repair route, and require the same edge set as a fresh full index. Signed-off-by: chidev <[email protected]>
1 parent 4cac258 commit 25d2897

2 files changed

Lines changed: 221 additions & 12 deletions

File tree

‎src/pipeline/pipeline_delta.c‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*/
3131
#include "foundation/constants.h"
3232
#include "pipeline/pipeline_internal.h"
33+
#include "callable_overload.h"
3334

3435
#include <stdint.h> /* intptr_t — SQLITE_TRANSIENT wrapper */
3536
#include <stdio.h>
@@ -590,16 +591,16 @@ static char *delta_owner_props(const char *project, const char *name, const char
590591
return out;
591592
}
592593

593-
/* `props` (a JSON object) with a generated member's mark (cbm.h
594-
* CBM_PROP_GENERATED) added, or NULL when `generated` is NULL/empty
595-
* (CBM_MEM_CLASS_OTHER; the caller frees). */
596-
static char *delta_generated_props(const char *props, const char *generated) {
597-
if (!generated || !generated[0]) {
594+
/* `props` (a JSON object) with the string member `key` = `value` added, or
595+
* NULL when `value` is NULL/empty (CBM_MEM_CLASS_OTHER; the caller frees).
596+
* `key` is a fixed identifier; `value` is escaped. */
597+
static char *delta_with_prop(const char *props, const char *key, const char *value) {
598+
if (!value || !value[0]) {
598599
return NULL;
599600
}
600601
size_t pl = props ? strlen(props) : 0;
601602
bool has_members = pl > PAIR_LEN && props[0] == '{' && props[pl - SKIP_ONE] == '}';
602-
size_t cap = pl + PAIR_LEN * strlen(generated) + CBM_SZ_32;
603+
size_t cap = pl + strlen(key) + PAIR_LEN * strlen(value) + CBM_SZ_32;
603604
char *out = (char *)cbm_alloc(CBM_MEM_CLASS_OTHER, cap);
604605
if (!out) {
605606
return NULL;
@@ -612,8 +613,8 @@ static char *delta_generated_props(const char *props, const char *generated) {
612613
} else {
613614
out[k++] = '{';
614615
}
615-
k += (size_t)snprintf(out + k, cap - k, "\"%s\":\"", CBM_PROP_GENERATED);
616-
for (const char *p = generated; *p && k + CBM_SZ_3 < cap; p++) {
616+
k += (size_t)snprintf(out + k, cap - k, "\"%s\":\"", key);
617+
for (const char *p = value; *p && k + CBM_SZ_3 < cap; p++) {
617618
if (*p == '"' || *p == '\\') {
618619
out[k++] = '\\';
619620
}
@@ -672,7 +673,15 @@ int64_t cbm_delta_preseed(cbm_store_t *store, const char *project, cbm_gbuf_t *g
672673
* And a generated member's mark (a Java record's implicit accessor, a
673674
* Method node): the registry seed reads it to register the member by
674675
* qualified name only, as the full build does (cbm_node_props_generated);
675-
* without it the proxy came back into the short-name index. */
676+
* without it the proxy came back into the short-name index.
677+
*
678+
* And a same-file overload's parameter shape (callable_overload.h
679+
* CBM_OVERLOAD_PARAMS_PROP, on Function/Method rows of a split group):
680+
* cbm_pipeline_overload_targets binds a re-resolved call to the member
681+
* its arguments fit by reading it off every member, and the registry seed
682+
* keeps members 2..n out of the short-name index by it
683+
* (cbm_node_is_split_overload). Without it an incremental run bound
684+
* every call to the group's first member. */
676685
sqlite3_stmt *stmt = NULL;
677686
if (sqlite3_prepare_v2(
678687
db,
@@ -687,7 +696,10 @@ int64_t cbm_delta_preseed(cbm_store_t *store, const char *project, cbm_gbuf_t *g
687696
" THEN json_extract(properties, '$.parent_class') END,"
688697
" CASE WHEN label = 'Method'"
689698
" AND instr(properties, '\"" CBM_PROP_GENERATED "\"') > 0"
690-
" THEN json_extract(properties, '$." CBM_PROP_GENERATED "') END"
699+
" THEN json_extract(properties, '$." CBM_PROP_GENERATED "') END,"
700+
" CASE WHEN label IN ('Function','Method')"
701+
" AND instr(properties, '\"" CBM_OVERLOAD_PARAMS_PROP "\"') > 0"
702+
" THEN json_extract(properties, '$." CBM_OVERLOAD_PARAMS_PROP "') END"
691703
" FROM nodes"
692704
" WHERE project = ?1 AND label NOT IN"
693705
" ('Macro','Comment','Section','Branch','Commit','Tag')"
@@ -712,14 +724,23 @@ int64_t cbm_delta_preseed(cbm_store_t *store, const char *project, cbm_gbuf_t *g
712724
int end_line = sqlite3_column_int(stmt, 7);
713725
const char *parent = (const char *)sqlite3_column_text(stmt, 8);
714726
const char *generated = (const char *)sqlite3_column_text(stmt, 9);
727+
const char *overload = (const char *)sqlite3_column_text(stmt, 10);
715728
char *owner_props =
716729
parent ? delta_owner_props(project, name, qn, parent, client_props) : NULL;
717730
const char *props = owner_props ? owner_props : (client_props ? client_props : "{}");
718-
char *generated_props = delta_generated_props(props, generated);
731+
char *generated_props = delta_with_prop(props, CBM_PROP_GENERATED, generated);
732+
if (generated_props) {
733+
props = generated_props;
734+
}
735+
char *overload_props = delta_with_prop(props, CBM_OVERLOAD_PARAMS_PROP, overload);
736+
if (overload_props) {
737+
props = overload_props;
738+
}
719739
/* Pin the gbuf id to the database id: proxies ARE their rows. */
720740
cbm_gbuf_set_next_id(gbuf, id);
721741
int64_t got = cbm_gbuf_upsert_node(gbuf, label, name, qn, fp ? fp : "", start_line,
722-
end_line, generated_props ? generated_props : props);
742+
end_line, props);
743+
cbm_free(CBM_MEM_CLASS_OTHER, overload_props);
723744
cbm_free(CBM_MEM_CLASS_OTHER, generated_props);
724745
cbm_free(CBM_MEM_CLASS_OTHER, owner_props);
725746
if (got != id) {

‎tests/test_overloads.c‎

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "foundation/arena.h"
1515
#include <foundation/compat_fs.h>
1616
#include <pipeline/pipeline.h>
17+
#include <pipeline/pipeline_internal.h>
1718
#include <store/store.h>
1819
#include <sqlite3.h>
1920

@@ -502,6 +503,190 @@ TEST(overload_calls_ambiguous_binds_every_candidate) {
502503
PASS();
503504
}
504505

506+
/* ── Incremental == full ───────────────────────────────────────── */
507+
508+
/* Every edge of the project as "TYPE source -> target" lines with the
509+
* project's QN prefix removed, sorted; NULL when the database cannot be read
510+
* (the caller frees). */
511+
static char *ovl_edge_set(const char *db_path) {
512+
cbm_store_t *store = cbm_store_open_path(db_path);
513+
sqlite3 *db = store ? cbm_store_get_db(store) : NULL;
514+
sqlite3_stmt *st = NULL;
515+
if (!db ||
516+
sqlite3_prepare_v2(db,
517+
"SELECT e.type || ' ' || replace(s.qualified_name, e.project || '.', '') "
518+
"|| ' -> ' || replace(t.qualified_name, e.project || '.', '') "
519+
"FROM edges e JOIN nodes s ON s.id=e.source_id "
520+
"JOIN nodes t ON t.id=e.target_id ORDER BY 1",
521+
-1, &st, NULL) != SQLITE_OK) {
522+
if (store) {
523+
cbm_store_close(store);
524+
}
525+
return NULL;
526+
}
527+
size_t cap = 4096;
528+
size_t len = 0;
529+
char *buf = malloc(cap);
530+
if (buf) {
531+
buf[0] = '\0';
532+
}
533+
while (buf && sqlite3_step(st) == SQLITE_ROW) {
534+
const char *line = (const char *)sqlite3_column_text(st, 0);
535+
size_t l = line ? strlen(line) : 0;
536+
if (len + l + 2 > cap) {
537+
cap = (len + l + 2) * 2;
538+
char *grown = realloc(buf, cap);
539+
if (!grown) {
540+
free(buf);
541+
buf = NULL;
542+
break;
543+
}
544+
buf = grown;
545+
}
546+
memcpy(buf + len, line, l);
547+
len += l;
548+
buf[len++] = '\n';
549+
buf[len] = '\0';
550+
}
551+
sqlite3_finalize(st);
552+
cbm_store_close(store);
553+
return buf;
554+
}
555+
556+
static int ovl_index(const char *repo, const char *db_path) {
557+
cbm_pipeline_t *pipeline = cbm_pipeline_new(repo, db_path, CBM_MODE_FULL);
558+
int rc = pipeline ? cbm_pipeline_run(pipeline) : -1;
559+
if (pipeline) {
560+
cbm_pipeline_free(pipeline);
561+
}
562+
return rc;
563+
}
564+
565+
/* Index `files`, rewrite only `files[1]` (the caller) as `edited`, re-index
566+
* incrementally, and require the closure-repair route, the same edge set as a
567+
* fresh full index of the edited tree, and `want` among those edges. */
568+
static int ovl_incremental_matches_full(const ovl_file_t files[2], const char *edited,
569+
const char *want) {
570+
char tmp[256];
571+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_ovl_incr_XXXXXX");
572+
if (!cbm_mkdtemp(tmp)) {
573+
return 1;
574+
}
575+
char repo[300];
576+
snprintf(repo, sizeof(repo), "%s/repo", tmp);
577+
char inc_db[400];
578+
char full_db[400];
579+
snprintf(inc_db, sizeof(inc_db), "%s/inc.db", tmp);
580+
snprintf(full_db, sizeof(full_db), "%s/full.db", tmp);
581+
ovl_env_t save = ovl_env_select(false);
582+
int rc = 1;
583+
char *inc = NULL;
584+
char *full = NULL;
585+
int route = -1;
586+
if (ovl_write(repo, files[0].path, files[0].content) &&
587+
ovl_write(repo, files[1].path, files[1].content) && ovl_index(repo, inc_db) == 0 &&
588+
ovl_write(repo, files[1].path, edited)) {
589+
cbm_pipeline_incremental_test_reset_faults();
590+
if (ovl_index(repo, inc_db) == 0) {
591+
route = (int)cbm_pipeline_incremental_test_last_route();
592+
if (ovl_index(repo, full_db) == 0) {
593+
inc = ovl_edge_set(inc_db);
594+
full = ovl_edge_set(full_db);
595+
}
596+
}
597+
}
598+
ovl_env_restore(&save);
599+
if (inc && full && strcmp(inc, full) == 0 && strstr(full, want) &&
600+
route == (int)CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR) {
601+
rc = 0;
602+
} else {
603+
fprintf(stderr, " route %d (want %d), want edge [%s]\n--- incremental\n%s--- full\n%s",
604+
route, (int)CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR, want, inc ? inc : "(null)",
605+
full ? full : "(null)");
606+
}
607+
free(inc);
608+
free(full);
609+
th_rmtree(tmp);
610+
return rc;
611+
}
612+
613+
/* The reviewer's case: an edit to the caller file alone re-resolves its calls
614+
* against proxies of the unchanged overloads, which must bind exactly as a
615+
* full index does (each call to the member its arguments fit). */
616+
TEST(overload_incremental_java_matches_full) {
617+
static const ovl_file_t files[] = {
618+
{"src/demo/Foo.java", "package demo;\n"
619+
"public class Foo {\n"
620+
" public void m(int a) {}\n"
621+
" public void m(String s) {}\n"
622+
" public void m(int a, int b) {}\n"
623+
"}\n"},
624+
{"src/demo/Caller.java", "package demo;\n"
625+
"public class Caller {\n"
626+
" private final Foo f = new Foo();\n"
627+
" void run() { f.m(1); f.m(1, 2); }\n"
628+
"}\n"},
629+
};
630+
static const char edited[] = "package demo;\n"
631+
"public class Caller {\n"
632+
" private final Foo f = new Foo();\n"
633+
" void run() { f.m(1); f.m(1, 2); f.m(\"s\"); }\n"
634+
"}\n";
635+
ASSERT_EQ(ovl_incremental_matches_full(
636+
files, edited, "CALLS src.demo.Caller.run -> src.demo.Foo.m(int,int)\n"),
637+
0);
638+
PASS();
639+
}
640+
641+
TEST(overload_incremental_kotlin_matches_full) {
642+
static const ovl_file_t files[] = {
643+
{"src/demo/Foo.kt", "package demo\n"
644+
"class Foo {\n"
645+
" fun m(a: Int) {}\n"
646+
" fun m(s: String) {}\n"
647+
" fun m(a: Int, b: Int) {}\n"
648+
"}\n"},
649+
{"src/demo/Caller.kt", "package demo\n"
650+
"class Caller {\n"
651+
" private val f = Foo()\n"
652+
" fun run() { f.m(1); f.m(1, 2) }\n"
653+
"}\n"},
654+
};
655+
static const char edited[] = "package demo\n"
656+
"class Caller {\n"
657+
" private val f = Foo()\n"
658+
" fun run() { f.m(1); f.m(1, 2); f.m(\"s\") }\n"
659+
"}\n";
660+
ASSERT_EQ(ovl_incremental_matches_full(files, edited,
661+
"CALLS src.demo.Caller.Caller.run -> "
662+
"src.demo.Foo.Foo.m(Int,Int)\n"),
663+
0);
664+
PASS();
665+
}
666+
667+
TEST(overload_incremental_swift_matches_full) {
668+
static const ovl_file_t files[] = {
669+
{"Sources/Demo/Foo.swift", "class Foo {\n"
670+
" func m(_ a: Int) {}\n"
671+
" func m(named: String) {}\n"
672+
" func m(_ a: Int, to b: Int) {}\n"
673+
"}\n"},
674+
{"Sources/Demo/Caller.swift", "class Caller {\n"
675+
" let f = Foo()\n"
676+
" func run() { f.m(1); f.m(1, to: 2) }\n"
677+
"}\n"},
678+
};
679+
static const char edited[] = "class Caller {\n"
680+
" let f = Foo()\n"
681+
" func run() { f.m(1); f.m(1, to: 2); f.m(named: \"x\") }\n"
682+
"}\n";
683+
ASSERT_EQ(ovl_incremental_matches_full(files, edited,
684+
"CALLS Sources.Demo.Caller.Caller.run -> "
685+
"Sources.Demo.Foo.Foo.m(named:String)\n"),
686+
0);
687+
PASS();
688+
}
689+
505690
SUITE(overloads) {
506691
RUN_TEST(overload_shape_swift_labels);
507692
RUN_TEST(overload_shape_counts);
@@ -514,4 +699,7 @@ SUITE(overloads) {
514699
RUN_TEST(overload_calls_swift_sequential);
515700
RUN_TEST(overload_calls_swift_parallel);
516701
RUN_TEST(overload_calls_ambiguous_binds_every_candidate);
702+
RUN_TEST(overload_incremental_java_matches_full);
703+
RUN_TEST(overload_incremental_kotlin_matches_full);
704+
RUN_TEST(overload_incremental_swift_matches_full);
517705
}

0 commit comments

Comments
 (0)