Skip to content

Commit f4c8971

Browse files
MopicMinystrclaude
andcommitted
fix(java): resolve record accessor calls, and never bind a call to data
A record's accessors are implicit, so `record Point(double x, double y, double z)` declares no `x()` and extraction emitted no node for one. The call `p.x()` had nothing to resolve to, fell through to the short-name registry, and bound to the first `x` in the project — typically an unrelated class's private field. Measured on two indexed Java trees, that was 278 of 2444 and 2649 of 29509 CALLS edges. Extraction now emits one Method definition per record component, skipping any accessor the record declares itself (JLS 8.10.3), so the accessor has a node and the call resolves to it. And a Java call may no longer bind to a Variable or a Field. A Java call expression never names data, so such an edge is a spelling collision, not a call. The guard follows cbm_go_suppress_bare_field_ref — a pure, label-keyed predicate — and is called from both pass_calls.c and pass_parallel.c so the sequential and parallel resolvers stay identical. Java-gated: Kotlin properties hold function values and C has function pointers, so both legitimately call through data. Signed-off-by: MopicMP <obshiq123@gmail.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent e783f73 commit f4c8971

7 files changed

Lines changed: 230 additions & 0 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const ch
204204
const CBMLangSpec *spec);
205205
static TSNode find_class_body(TSNode class_node, CBMLanguage lang);
206206
static void extract_enum_members(CBMExtractCtx *ctx, TSNode node, const char *class_qn);
207+
static void extract_record_accessors(CBMExtractCtx *ctx, TSNode node, const char *class_qn);
207208
static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);
208209

209210
// --- Helpers ---
@@ -4642,6 +4643,10 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
46424643
extract_enum_members(ctx, node, class_qn);
46434644
}
46444645

4646+
if (ctx->language == CBM_LANG_JAVA && strcmp(kind, "record_declaration") == 0) {
4647+
extract_record_accessors(ctx, node, class_qn);
4648+
}
4649+
46454650
// Extract methods inside the class
46464651
extract_class_methods(ctx, node, class_qn, spec);
46474652

@@ -5529,6 +5534,81 @@ static void extract_enum_members(CBMExtractCtx *ctx, TSNode node, const char *cl
55295534
}
55305535
}
55315536

5537+
/* Whether the record body declares this accessor itself (JLS §8.10.3 lets a
5538+
* record override any of them), so the implicit one is not emitted twice. */
5539+
static bool record_declares_accessor(CBMExtractCtx *ctx, TSNode node, const char *comp_name) {
5540+
TSNode body = find_class_body(node, ctx->language);
5541+
if (ts_node_is_null(body)) {
5542+
return false;
5543+
}
5544+
uint32_t mc = ts_node_named_child_count(body);
5545+
for (uint32_t mi = 0; mi < mc; mi++) {
5546+
TSNode member = ts_node_named_child(body, mi);
5547+
if (strcmp(ts_node_type(member), "method_declaration") != 0) {
5548+
continue;
5549+
}
5550+
TSNode mname = ts_node_child_by_field_name(member, TS_FIELD("name"));
5551+
if (ts_node_is_null(mname)) {
5552+
continue;
5553+
}
5554+
char *name = cbm_node_text(ctx->arena, mname, ctx->source);
5555+
if (!name || strcmp(name, comp_name) != 0) {
5556+
continue;
5557+
}
5558+
/* Same name, no parameters — that is the accessor. */
5559+
TSNode params = ts_node_child_by_field_name(member, TS_FIELD("parameters"));
5560+
if (ts_node_is_null(params) || ts_node_named_child_count(params) == 0) {
5561+
return true;
5562+
}
5563+
}
5564+
return false;
5565+
}
5566+
5567+
/* Java records: the compiler writes one accessor per component, so `p.x()`
5568+
* calls a method that appears nowhere in the source. Without a definition for
5569+
* it the call has nothing to resolve to and falls through to the project-wide
5570+
* name registry, which binds it to the first `x` in the tree — typically an
5571+
* unrelated class's private field. Emitting the accessors makes the record's
5572+
* own API visible, exactly as the language defines it (JLS §8.10.3). */
5573+
static void extract_record_accessors(CBMExtractCtx *ctx, TSNode node, const char *class_qn) {
5574+
CBMArena *a = ctx->arena;
5575+
TSNode params = ts_node_child_by_field_name(node, TS_FIELD("parameters"));
5576+
if (ts_node_is_null(params)) {
5577+
return;
5578+
}
5579+
uint32_t pc = ts_node_named_child_count(params);
5580+
for (uint32_t pi = 0; pi < pc; pi++) {
5581+
TSNode comp = ts_node_named_child(params, pi);
5582+
if (strcmp(ts_node_type(comp), "formal_parameter") != 0) {
5583+
continue;
5584+
}
5585+
TSNode cname = ts_node_child_by_field_name(comp, TS_FIELD("name"));
5586+
if (ts_node_is_null(cname)) {
5587+
continue;
5588+
}
5589+
char *comp_name = cbm_node_text(a, cname, ctx->source);
5590+
if (!comp_name || !comp_name[0] || record_declares_accessor(ctx, node, comp_name)) {
5591+
continue;
5592+
}
5593+
CBMDefinition adef;
5594+
memset(&adef, 0, sizeof(adef));
5595+
adef.name = comp_name;
5596+
adef.qualified_name = cbm_arena_sprintf(a, "%s.%s", class_qn, comp_name);
5597+
adef.label = "Method";
5598+
adef.file_path = ctx->rel_path;
5599+
adef.start_line = ts_node_start_point(comp).row + TS_LINE_OFFSET;
5600+
adef.end_line = ts_node_end_point(comp).row + TS_LINE_OFFSET;
5601+
adef.lines = 1;
5602+
adef.parent_class = class_qn;
5603+
adef.is_exported = true;
5604+
TSNode ctype = ts_node_child_by_field_name(comp, TS_FIELD("type"));
5605+
if (!ts_node_is_null(ctype)) {
5606+
adef.return_type = cbm_node_text(a, ctype, ctx->source);
5607+
}
5608+
cbm_defs_push(&ctx->result->defs, a, adef);
5609+
}
5610+
}
5611+
55325612
/* Resolve the identifier node from a destructure pattern child.
55335613
* pair_pattern → value field; shorthand/identifier → itself; others → first named child. */
55345614
static TSNode destructure_ident(TSNode pat_child) {

‎src/pipeline/pass_calls.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,12 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call,
682682
if (cbm_suppress_cross_language_suffix_match(lang, target_node->file_path, res.strategy)) {
683683
return 0;
684684
}
685+
/* A Java call never targets data — see cbm_java_suppress_call_to_data_member.
686+
* The language gate lives here, as with the guards above, and MUST match
687+
* pass_parallel.c exactly or the two resolvers diverge. */
688+
if (cbm_java_suppress_call_to_data_member(lang == CBM_LANG_JAVA, target_node->label)) {
689+
return 0;
690+
}
685691
emit_classified_edge(ctx, call, source_node, target_node, &res, module_qn, imp_keys, imp_vals,
686692
imp_count, drop_plain_call);
687693
return SKIP_ONE;

‎src/pipeline/pass_parallel.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,6 +3028,12 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
30283028
* CALLS edge across a language boundary. */
30293029
continue;
30303030
}
3031+
if (target_node && source_node->id != target_node->id &&
3032+
cbm_java_suppress_call_to_data_member(lang == CBM_LANG_JAVA, target_node->label)) {
3033+
/* Same guard as pass_calls.c — a Java call never targets a
3034+
* Variable or a Field. */
3035+
continue;
3036+
}
30313037
if (!target_node || source_node->id == target_node->id) {
30323038
/* HTTP/ASYNC calls to an EXTERNAL client library (`requests.get(url)`)
30333039
* resolve to an unindexed QN (target_node == NULL), but their edge

‎src/pipeline/pipeline.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ bool cbm_suppress_cross_language_ref(CBMLanguage caller_lang, const char *target
344344
* unit-tested in test_registry.c. */
345345
bool cbm_go_suppress_bare_field_ref(bool is_go, bool is_member_access, const char *target_label);
346346

347+
/* A Java call expression never names data: `p.x()` is a method invocation
348+
* whatever `x` spells elsewhere in the project. Drops a CALLS bind whose
349+
* target is a Variable or Field. Java only — Kotlin properties and C function
350+
* pointers are callable names that are not methods. Pure; unit-tested in
351+
* test_registry.c. */
352+
bool cbm_java_suppress_call_to_data_member(bool is_java, const char *target_label);
353+
347354
/* Get the label of a qualified name, or NULL if not found. */
348355
const char *cbm_registry_label_of(const cbm_registry_t *r, const char *qn);
349356

‎src/pipeline/registry.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,30 @@ bool cbm_go_suppress_bare_field_ref(bool is_go, bool is_member_access, const cha
811811
return strcmp(target_label, "Field") == 0;
812812
}
813813

814+
bool cbm_java_suppress_call_to_data_member(bool is_java, const char *target_label) {
815+
/* A Java call expression never names data. `p.x()` is a method invocation
816+
* whatever `x` spells elsewhere in the tree, so a CALLS edge into a
817+
* Variable or a Field is a spelling collision and not a call.
818+
*
819+
* The bind is reached when the type-aware resolver finds no method to
820+
* return: a record's accessors are implicit, so `record Point(double x,
821+
* double y, double z)` declares no `x()` for java_lookup_method to find,
822+
* and every `p.x()` falls through to the short-name registry — which
823+
* matches the first `x` anywhere in the project, typically an unrelated
824+
* class's private field. Measured on a 600-file Java tree (2026-09-22):
825+
* 278 of 2444 CALLS edges landed on fields that way, and the callers of
826+
* one four-field class absorbed every record read in the project.
827+
*
828+
* Java-gated, like the Go guard above, because the veto is only sound
829+
* where no callable name can be anything but a method: Kotlin properties
830+
* hold function values (`val f: () -> Unit; f()`) and C has function
831+
* pointers, so both legitimately call through data. */
832+
if (!is_java || !target_label) {
833+
return false;
834+
}
835+
return strcmp(target_label, "Variable") == 0 || strcmp(target_label, "Field") == 0;
836+
}
837+
814838
/* ── Lifecycle ──────────────────────────────────────────────────── */
815839

816840
cbm_registry_t *cbm_registry_new(void) {

‎tests/test_call_reference_contract.c‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,40 @@ static int crc_edge_count(cbm_store_t *store, const char *project, const char *t
3535
return matches;
3636
}
3737

38+
/* Edges of `type` out of `source_name` whose target's qualified name ends with
39+
* `target_qn_suffix`. Two methods in one project can share a short name, and
40+
* then only the qualified name tells them apart. */
41+
static int crc_edge_count_to_qn(cbm_store_t *store, const char *project, const char *type,
42+
const char *source_name, const char *target_qn_suffix) {
43+
cbm_edge_t *edges = NULL;
44+
int edge_count = 0;
45+
if (cbm_store_find_edges_by_type(store, project, type, &edges, &edge_count) != CBM_STORE_OK) {
46+
return -1;
47+
}
48+
int matches = 0;
49+
size_t suffix_len = strlen(target_qn_suffix);
50+
for (int i = 0; i < edge_count; i++) {
51+
cbm_node_t source = {0};
52+
cbm_node_t target = {0};
53+
bool source_ok =
54+
cbm_store_find_node_by_id(store, edges[i].source_id, &source) == CBM_STORE_OK;
55+
bool target_ok =
56+
cbm_store_find_node_by_id(store, edges[i].target_id, &target) == CBM_STORE_OK;
57+
if (source_ok && target_ok && source.name && target.qualified_name &&
58+
strcmp(source.name, source_name) == 0) {
59+
size_t qn_len = strlen(target.qualified_name);
60+
if (qn_len >= suffix_len &&
61+
strcmp(target.qualified_name + qn_len - suffix_len, target_qn_suffix) == 0) {
62+
matches++;
63+
}
64+
}
65+
cbm_node_free_fields(&source);
66+
cbm_node_free_fields(&target);
67+
}
68+
cbm_store_free_edges(edges, edge_count);
69+
return matches;
70+
}
71+
3872
static int crc_global_handler_edge_count(cbm_store_t *store, const char *project,
3973
const char *caller) {
4074
int references =
@@ -423,6 +457,56 @@ TEST(call_reference_go_ambiguous_promoted_method_stays_usage) {
423457
PASS();
424458
}
425459

460+
TEST(call_java_record_accessor_binds_the_record) {
461+
/* A record's accessors are implicit, so `record Point(double x, ...)`
462+
* declares no `x()` in its source. Extraction emits them, and `other.x()`
463+
* binds to the record's own accessor.
464+
*
465+
* Two things must not happen instead. The call must not reach the
466+
* project-wide short-name registry and land on the first `x` in the tree —
467+
* here an unrelated class's private field. And the guard that refuses such
468+
* data binds must not swallow a real method that merely shares the
469+
* spelling: the builder's `x(int)`, declared beside its own `x` field,
470+
* keeps its edge. */
471+
static const RFile files[] = {
472+
{"Point.java", "package com.example;\n"
473+
"public record Point(double x, double y, double z) {\n"
474+
" public double first(Point other) {\n"
475+
" return Maths.abs(other.x()) + new Builder().x(5).made();\n"
476+
" }\n"
477+
"}\n"},
478+
{"Spring.java", "package com.example;\n"
479+
"public final class Spring {\n"
480+
" private double x;\n"
481+
" public void put(double v) { this.x = v; }\n"
482+
"}\n"},
483+
{"Builder.java", "package com.example;\n"
484+
"public final class Builder {\n"
485+
" private int x;\n"
486+
" public Builder x(int v) { this.x = v; return this; }\n"
487+
" public int made() { return x; }\n"
488+
"}\n"},
489+
{"Maths.java", "package com.example;\n"
490+
"public final class Maths {\n"
491+
" public static double abs(double v) { return v < 0 ? -v : v; }\n"
492+
"}\n"}};
493+
RProj project;
494+
cbm_store_t *store = rh_index_files(&project, files, 4);
495+
ASSERT_NOT_NULL(store);
496+
int fabricated_field = crc_edge_count(store, project.project, "CALLS", "first", "Field", "x");
497+
int fabricated_var = crc_edge_count(store, project.project, "CALLS", "first", "Variable", "x");
498+
int genuine = crc_edge_count(store, project.project, "CALLS", "first", "Method", "abs");
499+
int accessor = crc_edge_count_to_qn(store, project.project, "CALLS", "first", ".Point.x");
500+
int builder = crc_edge_count_to_qn(store, project.project, "CALLS", "first", ".Builder.x");
501+
rh_cleanup(&project, store);
502+
ASSERT_EQ(fabricated_field, 0);
503+
ASSERT_EQ(fabricated_var, 0);
504+
ASSERT_EQ(genuine, 1);
505+
ASSERT_EQ(accessor, 1);
506+
ASSERT_EQ(builder, 1);
507+
PASS();
508+
}
509+
426510
SUITE(call_reference_contract) {
427511
RUN_TEST(call_reference_typescript_direct_argument_is_exact);
428512
RUN_TEST(call_reference_kotlin_alias_argument_is_exact);
@@ -439,4 +523,5 @@ SUITE(call_reference_contract) {
439523
RUN_TEST(call_reference_python_later_decorated_method_rebinding_stays_usage);
440524
RUN_TEST(call_reference_go_bound_method_argument_is_exact);
441525
RUN_TEST(call_reference_go_ambiguous_promoted_method_stays_usage);
526+
RUN_TEST(call_java_record_accessor_binds_the_record);
442527
}

‎tests/test_registry.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,27 @@ TEST(go_bare_ref_never_binds_field) {
987987
PASS();
988988
}
989989

990+
TEST(java_call_never_binds_data_member) {
991+
/* A Java call expression never names data, so a CALLS bind that landed on
992+
* a Variable or a Field is a spelling collision. The shape that produces
993+
* it: a record's accessors are implicit, `p.x()` finds no declared `x()`,
994+
* and the short-name registry hands back the first `x` in the tree — an
995+
* unrelated class's private field. */
996+
ASSERT_TRUE(cbm_java_suppress_call_to_data_member(true, "Field"));
997+
ASSERT_TRUE(cbm_java_suppress_call_to_data_member(true, "Variable"));
998+
/* Everything callable is kept, including the constructor's Class target. */
999+
ASSERT_FALSE(cbm_java_suppress_call_to_data_member(true, "Method"));
1000+
ASSERT_FALSE(cbm_java_suppress_call_to_data_member(true, "Function"));
1001+
ASSERT_FALSE(cbm_java_suppress_call_to_data_member(true, "Class"));
1002+
/* Other languages have callable data: a Kotlin property or a C function
1003+
* pointer is a name that is not a method and is called all the same. */
1004+
ASSERT_FALSE(cbm_java_suppress_call_to_data_member(false, "Field"));
1005+
ASSERT_FALSE(cbm_java_suppress_call_to_data_member(false, "Variable"));
1006+
/* Degenerate input → nothing to judge. */
1007+
ASSERT_FALSE(cbm_java_suppress_call_to_data_member(true, NULL));
1008+
PASS();
1009+
}
1010+
9901011
TEST(dynamic_suppress_drops_weak_method_matches) {
9911012
/* #592/#606/#1276: a member call whose receiver the LSP could not type, that
9921013
* landed via a WEAK short-name strategy, is generic-resolver noise → drop.
@@ -1244,6 +1265,7 @@ SUITE(registry) {
12441265
RUN_TEST(registry_tie_break_is_independent_of_registration_order);
12451266
RUN_TEST(cross_language_ref_drops_go_vs_c);
12461267
RUN_TEST(go_bare_ref_never_binds_field);
1268+
RUN_TEST(java_call_never_binds_data_member);
12471269
RUN_TEST(dynamic_suppress_drops_weak_method_matches);
12481270
RUN_TEST(dynamic_suppress_keeps_high_confidence_and_non_methods);
12491271
RUN_TEST(python_builtin_member_table_matches_builtin_type_methods);

0 commit comments

Comments
 (0)