Skip to content

Commit 006ea01

Browse files
committed
fix(kotlin): inject kotlin.Any nodes so lsp_kt_any edges form
The Kotlin LSP resolves a method call on an unknown-typed receiver whose member is a universal kotlin.Any method (toString/equals/hashCode) to kotlin.Any.<m> and emits lsp_kt_any, but no kotlin.Any node existed in the graph, so pass_calls had no target node and dropped the call (callable=0). Add kotlin_builtins.c (mirror of py_builtins.c): inject kotlin.Any plus its three universal methods into result->defs during cbm_run_kotlin_lsp, before the pipeline mints def nodes. The lsp_kt_any call now targets the injected node and forms a CALLS edge. Update the kotlin grammar-label golden to include the 4 injected nodes, exactly as python's golden already includes its injected builtins. Un-skips repro_lsp_kt_any. Repro suite 312/0, unit suite 5693/0. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 6b1e3a0 commit 006ea01

4 files changed

Lines changed: 85 additions & 8 deletions

File tree

‎internal/cbm/lsp/kotlin_builtins.c‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* kotlin_builtins.c — Minimal Kotlin universal builtins as real graph nodes.
3+
*
4+
* When a method call lands on an unknown-typed receiver and the member is one
5+
* of the universal kotlin.Any methods (toString / equals / hashCode), the
6+
* Kotlin LSP resolves it to "kotlin.Any.<member>" and emits the lsp_kt_any
7+
* strategy (kotlin_lsp.c, kt_emit_resolved). Any is the supertype of every
8+
* Kotlin reference, so this is the same target the fwcd LSP resolves to.
9+
*
10+
* The missing piece is downstream: pass_calls.c only writes a CALLS edge when
11+
* cbm_pipeline_lsp_target_node() resolves the callee_qn to a graph node
12+
* (src/pipeline/lsp_resolve.h). There is no "kotlin.Any" node in the graph, so
13+
* the resolved call is dropped and the strategy never lands on an edge
14+
* (callable=0).
15+
*
16+
* Fix: inject a small, fixed set of kotlin.Any definitions into result->defs
17+
* during the per-file Kotlin LSP run (cbm_run_kotlin_lsp, which executes inside
18+
* cbm_extract_file, BEFORE the parallel pipeline mints def nodes from
19+
* result->defs). The graph therefore gains real "kotlin.Any[.<method>]" nodes
20+
* that the lsp_kt_any edges target. The QNs here MUST match what kt_emit_resolved
21+
* emits as callee_qn ("kotlin.Any.<member>").
22+
*
23+
* Node minting upserts by QN (cbm_gbuf_upsert_node), so injecting the same
24+
* builtins per Kotlin file collapses to one node per QN — no duplicates.
25+
*
26+
* Self-contained: #included from kotlin_lsp.c only (amalgamation pattern; see
27+
* lsp_all.c). Not a standalone translation unit. Mirror of py_builtins.c.
28+
*/
29+
30+
/* A single builtin entry to mint as a graph node. */
31+
typedef struct {
32+
const char *qn; /* graph QN — MUST equal the kt_emit_resolved callee_qn */
33+
const char *name; /* short name (last segment of qn) */
34+
const char *label; /* "Class" | "Method" */
35+
} KtBuiltinNode;
36+
37+
/*
38+
* Universal kotlin.Any members the LSP falls back to (kt_any_methods in
39+
* kotlin_lsp.c). The Any class node anchors the three methods.
40+
*/
41+
static const KtBuiltinNode kKtBuiltinNodes[] = {
42+
{"kotlin.Any", "Any", "Class"},
43+
{"kotlin.Any.toString", "toString", "Method"},
44+
{"kotlin.Any.equals", "equals", "Method"},
45+
{"kotlin.Any.hashCode", "hashCode", "Method"},
46+
};
47+
48+
/*
49+
* Inject the builtin definitions into result->defs so the pipeline mints them
50+
* as graph nodes. All fields beyond name/qn/label are left zero/NULL: builtins
51+
* have no body, so complexity/line-range/etc. are irrelevant, and a synthetic
52+
* file_path keeps them out of any real source file's def list.
53+
*/
54+
static void kt_builtins_inject_defs(CBMFileResult *result, CBMArena *arena) {
55+
if (!result || !arena) {
56+
return;
57+
}
58+
const int n = (int)(sizeof(kKtBuiltinNodes) / sizeof(kKtBuiltinNodes[0]));
59+
for (int i = 0; i < n; i++) {
60+
const KtBuiltinNode *b = &kKtBuiltinNodes[i];
61+
CBMDefinition def;
62+
memset(&def, 0, sizeof(def));
63+
def.name = b->name;
64+
def.qualified_name = b->qn;
65+
def.label = b->label;
66+
def.file_path = "<kotlin-builtins>";
67+
def.start_line = 1;
68+
def.end_line = 1;
69+
cbm_defs_push(&result->defs, arena, def);
70+
}
71+
}

‎internal/cbm/lsp/kotlin_lsp.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
#include <stdlib.h>
4343
#include <string.h>
4444

45+
/* Minimal Kotlin universal builtins as real graph nodes (kt_builtins_inject_defs).
46+
* Amalgamation-included (see lsp_all.c); mirror of py_builtins.c. */
47+
#include "kotlin_builtins.c"
48+
4549
#define KT_EVAL_MAX_DEPTH 32
4650
#define KT_IMPORT_INITIAL_CAP 16
4751

@@ -4117,6 +4121,10 @@ void cbm_run_kotlin_lsp(CBMArena *arena, CBMFileResult *result, const char *sour
41174121

41184122
kotlin_lsp_process_file(&ctx, use_root);
41194123

4124+
/* Inject kotlin.Any universal-method nodes (toString/equals/hashCode) so the
4125+
* lsp_kt_any fallback emitted above has a target node to form a CALLS edge. */
4126+
kt_builtins_inject_defs(result, arena);
4127+
41204128
if (patched_tree) {
41214129
ts_tree_delete(patched_tree);
41224130
}

‎tests/repro/repro_lsp_kt_php_rust.c‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,9 @@ TEST(repro_lsp_kt_lambda_it) {
536536
return assert_lsp_strategy("main.kt", kKtLambdaIt, "lsp_kt_lambda_it");
537537
}
538538
TEST(repro_lsp_kt_any) {
539-
/* PARKED for release: `x.toString()` on an unknown-typed receiver resolves to
540-
* kotlin.Any.toString — a builtin with no node in the project, so no CALLS
541-
* edge can form (callable=0). Needs an Any/builtin node (a kotlin stdlib
542-
* registry) to anchor the edge. */
543-
printf(" %sSKIP%s parked: needs a kotlin.Any/builtin node (toString has no target)\n",
544-
tf_dim(), tf_reset());
545-
return -1; /* skip — not counted as pass or fail */
539+
/* x.toString() on an unknown-typed receiver resolves to kotlin.Any.toString
540+
* (the universal-method fallback) and forms a CALLS edge to the injected
541+
* kotlin.Any.toString node (kotlin_builtins.c). */
546542
return assert_lsp_strategy("main.kt", kKtAny, "lsp_kt_any");
547543
}
548544
TEST(repro_lsp_kt_destructure) {

‎tests/test_grammar_labels.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ static const LabelGolden LABEL_GOLDENS[] = {
8686
{"typescript", "Class:1,Function:1,Module:1"},
8787
{"tsx", "Function:1,Module:1"},
8888
{"java", "Class:1,Method:1,Module:1"},
89-
{"kotlin", "Class:1,Function:1,Module:1"},
89+
/* +Class:1 (kotlin.Any) +Method:3 (toString/equals/hashCode) injected by
90+
* kotlin_builtins.c, as python's golden includes its injected builtins. */
91+
{"kotlin", "Class:2,Function:1,Method:3,Module:1"},
9092
{"rust", "Function:1,Module:1,Struct:1"},
9193
{"ruby", "Class:1,Function:1,Module:1"},
9294
{"php", "Class:1,Function:1,Module:1"},

0 commit comments

Comments
 (0)