Skip to content

Commit 679d1e1

Browse files
committed
fix(java): a JVM leaf-name fallback never picks a generated accessor
When a JVM LSP callee has no exact node and no Class.method tail match, cbm_pipeline_lsp_target_node takes the one project callable with that leaf name. A record accessor became such a callable: on elasticsearch `Objects.nonNull(x)` (37 CALLS) and `filter(Objects::nonNull)` (230 CALL_REFERENCE) bound a test record's nonNull() accessor. A generated member is registered by qualified name only. It still joins through its owner's Class.method tail, but the leaf-name fallback no longer considers it, either as the unique callable or as a rival that makes a same-named project callable ambiguous, so every other bind is decided exactly as before the accessors existed. The record pipeline fixture gains a record Seen(boolean nonNull) with `Objects.nonNull(o)` and `filter(Objects::nonNull)` callers. Without the change it fails in the sequential, parallel and incremental venues. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 6c52e82 commit 679d1e1

2 files changed

Lines changed: 46 additions & 15 deletions

File tree

‎src/pipeline/lsp_resolve.h‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,10 +1281,18 @@ static inline const cbm_gbuf_node_t *cbm_pipeline_lsp_target_node_policy(
12811281
if (strcmp(cand->label, "Function") != 0 && strcmp(cand->label, "Method") != 0) {
12821282
continue;
12831283
}
1284-
if (!unique_callable) {
1285-
unique_callable = cand;
1286-
} else if (unique_callable->id != cand->id) {
1287-
callable_ambiguous = true;
1284+
/* A generated member (a Java record's implicit accessor) is registered
1285+
* by qualified name only: it joins through its owner's Class.method
1286+
* tail below, never on its leaf name alone -- `Objects.nonNull(x)` is
1287+
* no call of some record's nonNull(). Nor does it make a same-named
1288+
* callable ambiguous: the leaf fallback sees the graph as if it were
1289+
* not there. */
1290+
if (!cbm_node_props_generated(cand->properties_json)) {
1291+
if (!unique_callable) {
1292+
unique_callable = cand;
1293+
} else if (unique_callable->id != cand->id) {
1294+
callable_ambiguous = true;
1295+
}
12881296
}
12891297
if (!cbm_pipeline_qn_class_method_tail_eq(cand->qualified_name, callee_tail)) {
12901298
continue;

‎tests/test_pipeline.c‎

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17379,26 +17379,39 @@ TEST(pipeline_python_nested_def_local_after_incremental_issue1548) {
1737917379
* spells the accessor: the import's symbol-name fallback must not bind it
1738017380
* there (no IMPORTS edge), or `new Vector()` in `make` would follow the import
1738117381
* onto the accessor (elasticsearch: `import javax.crypto.spec.PBEKeySpec`
17382-
* bound a record's `spec()`). */
17382+
* bound a record's `spec()`). `stat` calls, and `refs` passes, the JDK's
17383+
* Objects.nonNull: no project callable is its target, and Seen's accessor --
17384+
* the only project callable called nonNull -- must not be taken for it on its
17385+
* leaf name alone (elasticsearch: `Objects.nonNull(x)` and
17386+
* `filter(Objects::nonNull)` bound a test record's nonNull()). */
1738317387
static const char jgen_point_java[] =
1738417388
"package com.example;\n"
1738517389
"public record Point(double x, double y, double z) {\n"
1738617390
" public double z() { return z; }\n"
1738717391
" public double norm() { return Math.sqrt(x * x + y * y + z * z); }\n"
1738817392
"}\n";
17393+
static const char jgen_seen_java[] = "package com.example;\n"
17394+
"record Seen(boolean nonNull) {}\n";
1738917395
static const char jgen_spring_java[] = "package com.example;\n"
1739017396
"public final class Spring {\n"
1739117397
" private double x;\n"
1739217398
" public void put(double v) { this.x = v; }\n"
1739317399
"}\n";
17394-
static const char jgen_caller_java[] = "package com.example;\n"
17395-
"import org.external.y.Vector;\n"
17396-
"public final class Caller {\n"
17397-
" public double near(Point p) { return p.y(); }\n"
17398-
" public double far(Vector v) { return v.y(); }\n"
17399-
" public double data(Vector v) { return v.x(); }\n"
17400-
" public Object make() { return new Vector(); }\n"
17401-
"}\n";
17400+
static const char jgen_caller_java[] =
17401+
"package com.example;\n"
17402+
"import java.util.List;\n"
17403+
"import java.util.Objects;\n"
17404+
"import org.external.y.Vector;\n"
17405+
"public final class Caller {\n"
17406+
" public double near(Point p) { return p.y(); }\n"
17407+
" public double far(Vector v) { return v.y(); }\n"
17408+
" public double data(Vector v) { return v.x(); }\n"
17409+
" public Object make() { return new Vector(); }\n"
17410+
" public boolean stat(Object o) { return Objects.nonNull(o); }\n"
17411+
" public long refs(List<Object> xs) {\n"
17412+
" return xs.stream().filter(Objects::nonNull).count();\n"
17413+
" }\n"
17414+
"}\n";
1740217415

1740317416
/* The id of the one `label` node called `name` in `file_path`; 0 when there is
1740417417
* none or more than one. `generated` (optional) receives whether its
@@ -17487,6 +17500,8 @@ static int jgen_check(cbm_store_t *s, const char *project) {
1748717500
bool gen_y = false;
1748817501
bool gen_z = true;
1748917502
bool gen_norm = true;
17503+
bool gen_nn = false;
17504+
int64_t acc_nn = jgen_node(s, project, "nonNull", "Method", "Seen.java", &gen_nn);
1749017505
int64_t acc_x = jgen_node(s, project, "x", "Method", "Point.java", &gen_x);
1749117506
int64_t acc_y = jgen_node(s, project, "y", "Method", "Point.java", &gen_y);
1749217507
int64_t own_z = jgen_node(s, project, "z", "Method", "Point.java", &gen_z);
@@ -17495,11 +17510,14 @@ static int jgen_check(cbm_store_t *s, const char *project) {
1749517510
int64_t far = jgen_node(s, project, "far", "Method", "Caller.java", NULL);
1749617511
int64_t data = jgen_node(s, project, "data", "Method", "Caller.java", NULL);
1749717512
int64_t make = jgen_node(s, project, "make", "Method", "Caller.java", NULL);
17498-
JGEN_EXPECT(acc_x > 0 && acc_y > 0 && own_z > 0 && norm > 0);
17499-
JGEN_EXPECT(near > 0 && far > 0 && data > 0 && make > 0);
17513+
int64_t stat = jgen_node(s, project, "stat", "Method", "Caller.java", NULL);
17514+
int64_t refs = jgen_node(s, project, "refs", "Method", "Caller.java", NULL);
17515+
JGEN_EXPECT(acc_x > 0 && acc_y > 0 && own_z > 0 && norm > 0 && acc_nn > 0);
17516+
JGEN_EXPECT(near > 0 && far > 0 && data > 0 && make > 0 && stat > 0 && refs > 0);
1750017517
/* The compiler's members are marked; the hand-written ones are not. */
1750117518
JGEN_EXPECT(gen_x);
1750217519
JGEN_EXPECT(gen_y);
17520+
JGEN_EXPECT(gen_nn);
1750317521
JGEN_EXPECT(!gen_z);
1750417522
JGEN_EXPECT(!gen_norm);
1750517523
/* Bound by qualified name through the receiver's type ... */
@@ -17512,12 +17530,17 @@ static int jgen_check(cbm_store_t *s, const char *project) {
1751217530
/* No import names a generated member, so nothing follows one onto it. */
1751317531
JGEN_EXPECT(jgen_edges_into(s, acc_y, "IMPORTS") == 0);
1751417532
JGEN_EXPECT(jgen_calls(s, make, 0, "y") == 0);
17533+
/* The JDK's Objects.nonNull is not Seen's accessor, called or referenced. */
17534+
JGEN_EXPECT(jgen_calls(s, stat, acc_nn, NULL) == 0);
17535+
JGEN_EXPECT(jgen_edges_into(s, acc_nn, "CALL_REFERENCE") == 0);
17536+
JGEN_EXPECT(jgen_edges_into(s, acc_nn, "USAGE") == 0);
1751517537
return failed;
1751617538
}
1751717539
#undef JGEN_EXPECT
1751817540

1751917541
static int jgen_write_fixture(const char *tmp) {
1752017542
write_temp_file(tmp, "Point.java", jgen_point_java);
17543+
write_temp_file(tmp, "Seen.java", jgen_seen_java);
1752117544
write_temp_file(tmp, "Spring.java", jgen_spring_java);
1752217545
write_temp_file(tmp, "Caller.java", jgen_caller_java);
1752317546
return 0;

0 commit comments

Comments
 (0)