Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(kotlin): inject kotlin.Any nodes so lsp_kt_any edges form

Martin Vogel006ea01d6092
diff --git a/internal/cbm/lsp/kotlin_builtins.c b/internal/cbm/lsp/kotlin_builtins.c
new file mode 100644
index 000000000..50d596a45
--- /dev/null
+++ b/internal/cbm/lsp/kotlin_builtins.c
@@ -0,0 +1,71 @@
+/*
+ * kotlin_builtins.c — Minimal Kotlin universal builtins as real graph nodes.
+ *
+ * When a method call lands on an unknown-typed receiver and the member is one
+ * of the universal kotlin.Any methods (toString / equals / hashCode), the
+ * Kotlin LSP resolves it to "kotlin.Any.<member>" and emits the lsp_kt_any
+ * strategy (kotlin_lsp.c, kt_emit_resolved). Any is the supertype of every
+ * Kotlin reference, so this is the same target the fwcd LSP resolves to.
+ *
+ * The missing piece is downstream: pass_calls.c only writes a CALLS edge when
+ * cbm_pipeline_lsp_target_node() resolves the callee_qn to a graph node
+ * (src/pipeline/lsp_resolve.h). There is no "kotlin.Any" node in the graph, so
+ * the resolved call is dropped and the strategy never lands on an edge
+ * (callable=0).
+ *
+ * Fix: inject a small, fixed set of kotlin.Any definitions into result->defs
+ * during the per-file Kotlin LSP run (cbm_run_kotlin_lsp, which executes inside
+ * cbm_extract_file, BEFORE the parallel pipeline mints def nodes from
+ * result->defs). The graph therefore gains real "kotlin.Any[.<method>]" nodes
+ * that the lsp_kt_any edges target. The QNs here MUST match what kt_emit_resolved
+ * emits as callee_qn ("kotlin.Any.<member>").
+ *
+ * Node minting upserts by QN (cbm_gbuf_upsert_node), so injecting the same
+ * builtins per Kotlin file collapses to one node per QN — no duplicates.
+ *
+ * Self-contained: #included from kotlin_lsp.c only (amalgamation pattern; see
+ * lsp_all.c). Not a standalone translation unit. Mirror of py_builtins.c.
+ */
+
+/* A single builtin entry to mint as a graph node. */
+typedef struct {
+ const char *qn; /* graph QN — MUST equal the kt_emit_resolved callee_qn */
+ const char *name; /* short name (last segment of qn) */
+ const char *label; /* "Class" | "Method" */
+} KtBuiltinNode;
+
+/*
+ * Universal kotlin.Any members the LSP falls back to (kt_any_methods in
+ * kotlin_lsp.c). The Any class node anchors the three methods.
+ */
+static const KtBuiltinNode kKtBuiltinNodes[] = {
+ {"kotlin.Any", "Any", "Class"},
+ {"kotlin.Any.toString", "toString", "Method"},
+ {"kotlin.Any.equals", "equals", "Method"},
+ {"kotlin.Any.hashCode", "hashCode", "Method"},
+};
+
+/*
+ * Inject the builtin definitions into result->defs so the pipeline mints them
+ * as graph nodes. All fields beyond name/qn/label are left zero/NULL: builtins
+ * have no body, so complexity/line-range/etc. are irrelevant, and a synthetic
+ * file_path keeps them out of any real source file's def list.
+ */
+static void kt_builtins_inject_defs(CBMFileResult *result, CBMArena *arena) {
+ if (!result || !arena) {
+ return;
+ }
+ const int n = (int)(sizeof(kKtBuiltinNodes) / sizeof(kKtBuiltinNodes[0]));
+ for (int i = 0; i < n; i++) {
+ const KtBuiltinNode *b = &kKtBuiltinNodes[i];
+ CBMDefinition def;
+ memset(&def, 0, sizeof(def));
+ def.name = b->name;
+ def.qualified_name = b->qn;
+ def.label = b->label;
+ def.file_path = "<kotlin-builtins>";
+ def.start_line = 1;
+ def.end_line = 1;
+ cbm_defs_push(&result->defs, arena, def);
+ }
+}
diff --git a/internal/cbm/lsp/kotlin_lsp.c b/internal/cbm/lsp/kotlin_lsp.c
index 84fc07cdb..7a3d843a6 100644
--- a/internal/cbm/lsp/kotlin_lsp.c
+++ b/internal/cbm/lsp/kotlin_lsp.c
@@ -42,6 +42,10 @@
#include <stdlib.h>
#include <string.h>
+/* Minimal Kotlin universal builtins as real graph nodes (kt_builtins_inject_defs).
+ * Amalgamation-included (see lsp_all.c); mirror of py_builtins.c. */
+#include "kotlin_builtins.c"
+
#define KT_EVAL_MAX_DEPTH 32
#define KT_IMPORT_INITIAL_CAP 16
@@ -4117,6 +4121,10 @@ void cbm_run_kotlin_lsp(CBMArena *arena, CBMFileResult *result, const char *sour
kotlin_lsp_process_file(&ctx, use_root);
+ /* Inject kotlin.Any universal-method nodes (toString/equals/hashCode) so the
+ * lsp_kt_any fallback emitted above has a target node to form a CALLS edge. */
+ kt_builtins_inject_defs(result, arena);
+
if (patched_tree) {
ts_tree_delete(patched_tree);
}
diff --git a/tests/repro/repro_lsp_kt_php_rust.c b/tests/repro/repro_lsp_kt_php_rust.c
index e5a801773..d51d517c2 100644
--- a/tests/repro/repro_lsp_kt_php_rust.c
+++ b/tests/repro/repro_lsp_kt_php_rust.c
@@ -536,13 +536,9 @@ TEST(repro_lsp_kt_lambda_it) {
return assert_lsp_strategy("main.kt", kKtLambdaIt, "lsp_kt_lambda_it");
}
TEST(repro_lsp_kt_any) {
- /* PARKED for release: `x.toString()` on an unknown-typed receiver resolves to
- * kotlin.Any.toString — a builtin with no node in the project, so no CALLS
- * edge can form (callable=0). Needs an Any/builtin node (a kotlin stdlib
- * registry) to anchor the edge. */
- printf(" %sSKIP%s parked: needs a kotlin.Any/builtin node (toString has no target)\n",
- tf_dim(), tf_reset());
- return -1; /* skip — not counted as pass or fail */
+ /* x.toString() on an unknown-typed receiver resolves to kotlin.Any.toString
+ * (the universal-method fallback) and forms a CALLS edge to the injected
+ * kotlin.Any.toString node (kotlin_builtins.c). */
return assert_lsp_strategy("main.kt", kKtAny, "lsp_kt_any");
}
TEST(repro_lsp_kt_destructure) {
diff --git a/tests/test_grammar_labels.c b/tests/test_grammar_labels.c
index 5f3bd324c..21f1a4704 100644
--- a/tests/test_grammar_labels.c
+++ b/tests/test_grammar_labels.c
@@ -86,7 +86,9 @@ static const LabelGolden LABEL_GOLDENS[] = {
{"typescript", "Class:1,Function:1,Module:1"},
{"tsx", "Function:1,Module:1"},
{"java", "Class:1,Method:1,Module:1"},
- {"kotlin", "Class:1,Function:1,Module:1"},
+ /* +Class:1 (kotlin.Any) +Method:3 (toString/equals/hashCode) injected by
+ * kotlin_builtins.c, as python's golden includes its injected builtins. */
+ {"kotlin", "Class:2,Function:1,Method:3,Module:1"},
{"rust", "Function:1,Module:1,Struct:1"},
{"ruby", "Class:1,Function:1,Module:1"},
{"php", "Class:1,Function:1,Module:1"},