Skip to content

Commit 6541132

Browse files
committed
fix(doclinks): follow split overload nodes in the JVM doc-link resolver
fix/overload-identity gives same-file Java/Kotlin overloads a node each: the first keeps the plain qualified name, a later one gets its signature as a suffix. The JVM scope blob already reads slot/own off the extractor's definitions, so a signed reference such as Ov#run(String) now binds lib.Ov.run(String). An unsigned Ov#run stays ambiguous, as it was before the split and as C# does for an overload group. The incremental scope delta treated the remaining overload's renamed node (run(String) taking the plain name after run(int) is removed) as a new record and declined file-by-file repair. A member record that differs only in its slot is now reported by name, like a removed member; a changed own field still forces a global re-resolve. A run of one name is reported once. Test expectations that assumed one shared node per overload group now name the split nodes. Signed-off-by: chidev <[email protected]>
1 parent 4cac258 commit 6541132

3 files changed

Lines changed: 63 additions & 20 deletions

File tree

‎internal/cbm/doclink_jvm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
* start line, name and kind), so this file models neither the qualified-name
5959
* scheme nor its collisions: Kotlin flattens nested types and puts class
6060
* properties at module level, Java names a field after its whole declarator
61-
* (`MAX = 10`), overloads share one name, and whatever the scheme does next is
61+
* (`MAX = 10`), a later overload's name has its signature, and whatever the scheme does next is
6262
* followed without a change here.
6363
* pnames (the parameter names of a callable, the primary-constructor
6464
* parameters of a Kotlin class) and all line numbers serve the file's own

‎src/pipeline/doc_links_jvm.c‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
* Entities. A type entity is its binary-style name: package + '.' + the
1616
* dotted logical path (com.acme.Outer.Inner). Several declarations of one
1717
* name (source sets, expect/actual) are one entity. A member belongs to the
18-
* declaration that holds it; overloads of one type share a node.
18+
* declaration that holds it; overloads of one type have a node each (the
19+
* first keeps the plain name, the others get their signature as a suffix),
20+
* and a signature selects one (one signature written twice keeps one node).
1921
*
2022
* Lookup (the first level with a visible candidate decides; more than one
2123
* entity there is ambiguous, never a pick):
@@ -4394,6 +4396,7 @@ enum {
43944396
JR_F_M_FLAGS = 3,
43954397
JR_F_M_NAME = 5,
43964398
JR_F_M_RECV = 8,
4399+
JR_F_M_SLOT = 9,
43974400
JR_DELTA_IMPORTS = 16, /* changed imports compared by name; more: any name */
43984401
};
43994402

@@ -4439,6 +4442,31 @@ static bool jr_span_eq(jr_span_t a, jr_span_t b) {
44394442
return a.n == b.n && memcmp(a.p, b.p, a.n) == 0;
44404443
}
44414444

4445+
/* True when two member records declare the same member and differ only in
4446+
* slot: its own node was renamed, e.g. a removed overload hands the plain
4447+
* qualified name to the next overload (cbm_split_overloads gives overloads
4448+
* 2..n a signature-suffixed node). A changed own is no such move: a member
4449+
* that gains or loses its node can change where a lookup ends. */
4450+
static bool jr_delta_slot_moved(jr_span_t a, jr_span_t b) {
4451+
if (a.n == 0 || b.n == 0 || a.p[0] != 'M' || b.p[0] != 'M') {
4452+
return false;
4453+
}
4454+
bool moved = false;
4455+
for (int k = 0;; k++) {
4456+
jr_span_t x = jr_delta_field(a, k);
4457+
jr_span_t y = jr_delta_field(b, k);
4458+
if (!x.p || !y.p) {
4459+
return moved && !x.p && !y.p;
4460+
}
4461+
if (!jr_span_eq(x, y)) {
4462+
if (k != JR_F_M_SLOT) {
4463+
return false;
4464+
}
4465+
moved = true;
4466+
}
4467+
}
4468+
}
4469+
44424470
/* The import records of a scope, in order. Returns the count, -1 when memory
44434471
* ran out. */
44444472
static int jr_delta_imports(const char *scope, jr_span_t **out) {
@@ -4547,6 +4575,9 @@ static int jr_delta_import_change(const char *stored, const char *fresh) {
45474575
* field, a property, an enum constant): a reference that depended on it
45484576
* either bound it (an edge into this file) or names it in its unresolved
45494577
* row, so the name is reported;
4578+
* - a member whose node was renamed (only its slot changed: an overload
4579+
* group lost a member, and the next overload took the plain qualified
4580+
* name): the same holds, so its name is reported too;
45504581
* - the file's own imports, unless one can re-bind a supertype or an
45514582
* extension receiver the file declares (jr_delta_import_change).
45524583
* Everything else is GLOBAL: a new or changed record (a type, a member, a
@@ -4561,6 +4592,7 @@ static int jr_scope_delta(const char *stored, const char *fresh, cbm_doclink_nam
45614592
const char *fp = fresh;
45624593
jr_span_t sl = {0};
45634594
jr_span_t fl = {0};
4595+
jr_span_t last = {0}; /* the name reported last */
45644596
bool hs = jr_delta_next(&sp, &sl, false);
45654597
bool hf = jr_delta_next(&fp, &fl, false);
45664598
while (hs || hf) {
@@ -4580,9 +4612,14 @@ static int jr_scope_delta(const char *stored, const char *fresh, cbm_doclink_nam
45804612
if (memchr(flags.p, 'p', flags.n)) {
45814613
return CBM_DOCLINK_DELTA_GLOBAL;
45824614
}
4583-
if (!removed || !removed(ud, name.p, name.n)) {
4615+
/* overloads are adjacent: a name is reported once in a row */
4616+
if (!jr_span_eq(name, last) && (!removed || !removed(ud, name.p, name.n))) {
45844617
return CBM_NOT_FOUND;
45854618
}
4619+
last = name;
4620+
if (hf && jr_delta_slot_moved(sl, fl)) {
4621+
hf = jr_delta_next(&fp, &fl, false); /* reported, and kept */
4622+
}
45864623
hs = jr_delta_next(&sp, &sl, false);
45874624
}
45884625
return jr_delta_import_change(stored, fresh);

‎tests/test_doc_mentions_jvm.c‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,11 @@ TEST(doc_mentions_jvm_java_scope_blob) {
352352
ASSERT(jt_has_line(s, "M\t7\tv\t\t0\tMAX\t\t-\t\t=\t1\t"));
353353
ASSERT(jt_has_line(s, "M\t8\tv\t\t0\tsize\t\t-\t\tWidget.size#field\t1\t"));
354354
ASSERT(jt_has_line(s, "M\t8\tv\t\t0\tother\t\t-\t\t-\t0\t"));
355-
/* constructors, overloads of one name: one node, owned by the last */
356-
ASSERT(jt_has_line(s, "M\t9\tc\tK\t0\tWidget\t\t\t\t=\t0\t"));
357-
ASSERT(jt_has_line(s, "M\t10\tc\tK\t0\tWidget\t\tint\t\t=\t1\t"));
355+
/* constructors, overloads of one name: a node each. The first keeps the
356+
* plain qualified name, a later one gets its signature as a suffix
357+
* (cbm_split_overloads); the slot follows the extractor */
358+
ASSERT(jt_has_line(s, "M\t9\tc\tK\t0\tWidget\t\t\t\t=\t1\t"));
359+
ASSERT(jt_has_line(s, "M\t10\tc\tK\t0\tWidget\t\tint\t\tWidget.Widget(int)\t1\t"));
358360
/* a signature: simple names, no type arguments, an ellipsis is an array */
359361
ASSERT(jt_has_line(s, "M\t11\tc\t\t0\tsize\tV:Number\tV|List|int[]\t\t=\t1\t"));
360362
ASSERT(jt_has_line(s, "T\t13\t15\tc\tWidget.Inner\t\t\t=\t1\t"));
@@ -375,7 +377,7 @@ TEST(doc_mentions_jvm_java_scope_blob) {
375377
char *portable = cbm_doclink_portable_scope(s);
376378
ASSERT_NOT_NULL(portable);
377379
ASSERT(jt_has_line(portable, "T\t0\t0\tc\tWidget\tT,U:Comparable\tBase|Runnable\t=\t1\t"));
378-
ASSERT(jt_has_line(portable, "M\t0\tc\tK\t0\tWidget\t\tint\t\t=\t1\t"));
380+
ASSERT(jt_has_line(portable, "M\t0\tc\tK\t0\tWidget\t\tint\t\tWidget.Widget(int)\t1\t"));
379381
ASSERT(jt_has_line(portable, "I\ts\tHelper\tcom.acme.util.Helper"));
380382
cbm_free(CBM_MEM_CLASS_OTHER, portable);
381383
cbm_free_result(r);
@@ -428,8 +430,9 @@ TEST(doc_mentions_jvm_kotlin_scope_blob) {
428430
ASSERT(jt_has_line(s, "M\t5\tv\tp\t0\tdelegate\t\t-\t\t-\t0\t"));
429431
/* a class-body property: its node is named below the FILE's module */
430432
ASSERT(jt_has_line(s, "M\t6\tv\t\t0\tbodyProp\t\t-\t\tbodyProp\t1\t"));
431-
ASSERT(jt_has_line(s, "M\t7\tc\t\t0\tmember\t\tInt\t\t=\t0\tparam"));
432-
ASSERT(jt_has_line(s, "M\t10\tc\t\t0\tmember\t\tString\t\t=\t1\tparam"));
433+
/* overloads: a node each, the later one's named with its signature */
434+
ASSERT(jt_has_line(s, "M\t7\tc\t\t0\tmember\t\tInt\t\t=\t1\tparam"));
435+
ASSERT(jt_has_line(s, "M\t10\tc\t\t0\tmember\t\tString\t\tAlias.member(String)\t1\tparam"));
433436
/* a nested type's node is flat: named below the file's module */
434437
ASSERT(jt_has_line(s, "T\t13\t13\tc\tAlias.Nested\t\t\tNested\t1\t"));
435438
/* an unnamed companion and its members: declared, no definitions */
@@ -449,7 +452,7 @@ TEST(doc_mentions_jvm_kotlin_scope_blob) {
449452
char *portable = cbm_doclink_portable_scope(s);
450453
ASSERT_NOT_NULL(portable);
451454
ASSERT(jt_has_line(portable, "T\t0\t0\tc\tAlias\tT\tBase|Marker\t=\t1\t"));
452-
ASSERT(jt_has_line(portable, "M\t0\tc\t\t0\tmember\t\tString\t\t=\t1\t"));
455+
ASSERT(jt_has_line(portable, "M\t0\tc\t\t0\tmember\t\tString\t\tAlias.member(String)\t1\t"));
453456
cbm_free(CBM_MEM_CLASS_OTHER, portable);
454457
cbm_free_result(r);
455458
PASS();
@@ -594,9 +597,10 @@ TEST(doc_mentions_jvm_java_families_and_reasons) {
594597
ASSERT_NOT_NULL(strstr(jt_props, "\"syntax\":\"link\""));
595598
ASSERT_NOT_NULL(strstr(jt_props, "\"tier\":\"unique\""));
596599
ASSERT_NOT_NULL(strstr(jt_props, "\"line\":8"));
597-
/* linkplain, with a signature that selects one overload (overloads of a
598-
* type share a node) */
599-
ASSERT_EQ(jt_edge(db, "a.Widget", "a.Gadget.spin"), 1);
600+
/* linkplain, with a signature that selects one overload: the second
601+
* overload has its own node, named with its signature (the first keeps
602+
* a.Gadget.spin) */
603+
ASSERT_EQ(jt_edge(db, "a.Widget", "a.Gadget.spin(int)"), 1);
600604
ASSERT_NOT_NULL(strstr(jt_props, "\"syntax\":\"linkplain\""));
601605
/* ... and nothing is relaxed: another parameter count, another parameter
602606
* type, a nested type named as a member and a method named as a value
@@ -2645,7 +2649,8 @@ TEST(doc_mentions_jvm_scope_delta_rules) {
26452649
JT_DJ_HEAD "import static acme.S.*;\n" JT_DJ_TYPES, global, NULL},
26462650
{"java static import of the supertype's name", java, JT_DJ_BASE,
26472651
JT_DJ_HEAD "import static acme.S.Base;\n" JT_DJ_TYPES, global, NULL},
2648-
/* a removed member is reported by name */
2652+
/* a removed member is reported by name; so is the overload whose node
2653+
* moved (run(String) takes the plain name run(int) left), once */
26492654
{"java overload removed", java, JT_DJ_BASE,
26502655
JT_DJ_HEAD JT_DJ_OPEN JT_DJ_RUN_STR JT_DJ_SIZE JT_DJ_CLOSE, local, "run"},
26512656
{"java field removed", java, JT_DJ_BASE,
@@ -2657,10 +2662,10 @@ TEST(doc_mentions_jvm_scope_delta_rules) {
26572662
"package n;\npublic class C {\n public void m() {}\n}\n", local, "C"},
26582663
{"java enum constant removed", java, "package n;\npublic enum Color { RED, GREEN }\n",
26592664
"package n;\npublic enum Color { RED }\n", local, "GREEN"},
2660-
/* ... unless the overloads' one node was its own: the node moves to
2661-
* the declaration that stays */
2662-
{"java node owner removed", java, JT_DJ_BASE,
2663-
JT_DJ_HEAD JT_DJ_OPEN JT_DJ_RUN_INT JT_DJ_SIZE JT_DJ_CLOSE, global, NULL},
2665+
/* the later overload had a node of its own (overloads no longer share
2666+
* one, cbm_split_overloads): removing it moves no other node */
2667+
{"java later overload removed", java, JT_DJ_BASE,
2668+
JT_DJ_HEAD JT_DJ_OPEN JT_DJ_RUN_INT JT_DJ_SIZE JT_DJ_CLOSE, local, "run"},
26642669
/* everything else can re-route references of files with no edge here */
26652670
{"java member added", java, JT_DJ_BASE,
26662671
JT_DJ_HEAD JT_DJ_OPEN JT_DJ_BODY " public int more;\n" JT_DJ_CLOSE, global, NULL},
@@ -2725,6 +2730,7 @@ TEST(doc_mentions_jvm_scope_delta_rules) {
27252730
{"kotlin receiver's import changed", kotlin,
27262731
"package n\n\nimport acme.local.Recv\n\nfun Recv.ext() {}\n",
27272732
"package n\n\nimport acme.other.Recv\n\nfun Recv.ext() {}\n", global, NULL},
2733+
/* the removed overload, and the one whose node moved: one name */
27282734
{"kotlin overload removed", kotlin, JT_DK_BASE,
27292735
JT_DK_HEAD JT_DK_OPEN JT_DK_RUN_STR JT_DK_SIZE JT_DK_CLOSE, local, "run"},
27302736
{"kotlin property removed", kotlin, JT_DK_BASE,
@@ -2803,7 +2809,7 @@ static const char JT_I_TARGET_RENAMED[] = "package lib;\n"
28032809
" public static final int LIMIT = 20;\n"
28042810
" public void go(int n) {}\n"
28052811
"}\n";
2806-
/* one node for both overloads; it belongs to the later declaration */
2812+
/* a node per overload: lib.Ov.run, then lib.Ov.run(String) */
28072813
static const char JT_I_OV[] = "package lib;\n"
28082814
"public class Ov {\n"
28092815
" public void run(int n) {}\n"
@@ -2956,7 +2962,7 @@ TEST(doc_mentions_jvm_incremental_equals_full) {
29562962
/* a doc-comment edit re-extracts the file alone */
29572963
jt_write(&r, main_java, JT_I_MAIN_EDITED);
29582964
ASSERT_EQ(dm_step(r.repo, db, full_db, "doc edit", repair), 0);
2959-
ASSERT_EQ(jt_edge(db, "app.Main", "lib.Ov.run"), 1);
2965+
ASSERT_EQ(jt_edge(db, "app.Main", "lib.Ov.run(String)"), 1); /* the second overload */
29602966

29612967
/* a body edit in a TARGET file: its scope is unchanged, and the edges
29622968
* into it stand */

0 commit comments

Comments
 (0)