Skip to content

Commit c6e8672

Browse files
committed
Stop Kotlin library calls from binding to same-named project methods
A call such as lifecycleScope.launch or dialog.dismiss on a library receiver bound by name to a project method with the same leaf. The Kotlin LSP now marks call sites whose receiver resolves to a type outside the project (strategy lsp_kt_external_receiver), and the plain call guards in the sequential and parallel venues drop weak name matches to project type members at those sites. Dotted callees whose leaf only comes from an external import are dropped the same way. Project extension functions and real project receivers keep their edges. Signed-off-by: chidev <[email protected]>
1 parent cf70a88 commit c6e8672

10 files changed

Lines changed: 423 additions & 2 deletions

File tree

‎Makefile.cbm‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,8 @@ TEST_PY_LSP_SCALE_SRCS = tests/test_py_lsp_scale.c
901901

902902
TEST_TS_LSP_SRCS = tests/test_ts_lsp.c
903903
TEST_JAVA_LSP_SRCS = tests/test_java_lsp.c tests/test_java_lsp_coverage.c
904-
TEST_KOTLIN_LSP_SRCS = tests/test_kotlin_lsp.c tests/test_kotlin_typed_calls.c
904+
TEST_KOTLIN_LSP_SRCS = tests/test_kotlin_lsp.c tests/test_kotlin_typed_calls.c \
905+
tests/test_kotlin_library_receivers.c
905906
TEST_RUST_LSP_SRCS = tests/test_rust_lsp.c
906907

907908
TEST_INTEGRATION_SRCS = tests/test_integration.c tests/test_incremental.c tests/test_lang_contract.c tests/test_edge_imports.c tests/test_edge_structural.c tests/test_lsp_resolution_probe.c tests/test_node_creation_probe.c tests/test_edge_types_probe.c tests/test_convergence_probe.c tests/test_matrix_known_classes.c tests/test_matrix_new_constructs.c tests/test_grammar_probe_a.c tests/test_grammar_probe_b.c tests/test_grammar_probe_c.c tests/test_grammar_probe_d.c tests/test_grammar_probe_e.c tests/test_grammar_probe_f.c tests/test_grammar_probe_g.c tests/test_conditional_variants.c tests/test_spawns.c

‎internal/cbm/lsp/kotlin_lsp.c‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ typedef struct {
7474
#define KT_CONF_TOP_LEVEL 0.95f
7575
#define KT_CONF_LAMBDA_IT 0.78f
7676
#define KT_CONF_IMPORT 0.92f
77+
/* Below CBM_LSP_CONFIDENCE_FLOOR: an evidence row, never an edge. */
78+
#define KT_CONF_EXTERNAL_RECEIVER 0.55f
7779

7880
/* ── forward declarations ─────────────────────────────────────────── */
7981

@@ -446,7 +448,11 @@ static void kt_emit_resolved_kind(KotlinLSPContext *ctx, const char *callee_qn,
446448
if (!ctx->resolved_calls || !ctx->enclosing_func_qn || !callee_qn) {
447449
return;
448450
}
449-
if (confidence < 0.60f) {
451+
/* Sub-floor rows are dropped, except the external-receiver evidence row:
452+
* it is meant to stay below CBM_LSP_CONFIDENCE_FLOOR (never an edge) and
453+
* is read only by cbm_pipeline_kotlin_receiver_external. */
454+
if (confidence < 0.60f &&
455+
!(strategy && strcmp(strategy, "lsp_kt_external_receiver") == 0)) {
450456
return;
451457
}
452458

@@ -3219,6 +3225,21 @@ static TSNode kt_nav_member_node(TSNode nav) {
32193225
return sel;
32203226
}
32213227

3228+
/* True when recv_qn is a qualified type whose simple name no project type
3229+
* carries. Only the cross-file pass knows every project type
3230+
* (cross_type_short); the per-file pass answers false. A project type that
3231+
* shares the simple name keeps the receiver "maybe project" (fail open). */
3232+
static bool kt_receiver_is_library_type(const KotlinLSPContext *ctx, const char *recv_qn) {
3233+
if (!ctx || !ctx->cross_type_short || !recv_qn || !strchr(recv_qn, '.')) {
3234+
return false;
3235+
}
3236+
const char *short_name = kt_short(recv_qn);
3237+
if (!short_name || !short_name[0]) {
3238+
return false;
3239+
}
3240+
return cbm_ht_get((const CBMHashTable *)ctx->cross_type_short, short_name) == NULL;
3241+
}
3242+
32223243
static const CBMType *kt_eval_navigation_expression_type(KotlinLSPContext *ctx, TSNode node) {
32233244
return kt_eval_navigation_expression_type_at(ctx, node, node);
32243245
}
@@ -3279,6 +3300,16 @@ static const CBMType *kt_eval_navigation_expression_type_at(KotlinLSPContext *ct
32793300
recv_type = kt_unwrap_nullable(recv_type);
32803301
const char *recv_qn = kt_type_qn_of(recv_type);
32813302

3303+
/* A call on a receiver typed as a library type (no project type has its
3304+
* simple name) cannot reach a project method. Leave a sub-floor
3305+
* `lsp_kt_external_receiver` row at the call so the pipeline refuses a
3306+
* weak same-name guess (`progressDialog.dismiss()` -> a project
3307+
* `dismiss`). Cross pass only: it alone holds every project type. */
3308+
if (kt_receiver_is_library_type(ctx, recv_qn) && !ts_node_eq(site, node)) {
3309+
kt_emit_resolved_at(ctx, kt_join_dot(ctx->arena, recv_qn, member_text),
3310+
"lsp_kt_external_receiver", KT_CONF_EXTERNAL_RECEIVER, site);
3311+
}
3312+
32823313
/* Receiver might be a class reference itself — `Foo.bar()` where Foo is
32833314
* a singleton object or an enum class with a companion. */
32843315
if (recv_qn && ctx->registry) {

‎src/pipeline/lsp_resolve.h‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,67 @@ static inline bool cbm_pipeline_php_receiver_type_unindexed(const CBMResolvedCal
451451
return false;
452452
}
453453

454+
/* True when `qn` names a member of a project TYPE: the node one segment up is
455+
* type-like (Class, Interface, Enum, ...). A Kotlin top-level extension
456+
* function (`fun String.tokenize()`, a Method node directly under its file's
457+
* module) is not, and stays callable on a library receiver. */
458+
static inline bool cbm_pipeline_qn_is_type_member(const cbm_gbuf_t *gbuf, const char *qn) {
459+
if (!gbuf || !qn) {
460+
return false;
461+
}
462+
const char *dot = strrchr(qn, '.');
463+
if (!dot || dot == qn) {
464+
return false;
465+
}
466+
size_t len = (size_t)(dot - qn);
467+
char *owner = (char *)malloc(len + 1);
468+
if (!owner) {
469+
return false;
470+
}
471+
memcpy(owner, qn, len);
472+
owner[len] = '\0';
473+
const cbm_gbuf_node_t *n = cbm_gbuf_find_by_qn(gbuf, owner);
474+
free(owner);
475+
return n && n->label && cbm_label_is_type_like(n->label);
476+
}
477+
478+
/* A sub-floor `lsp_kt_external_receiver` row: the cross-file Kotlin LSP typed
479+
* this call's receiver as a type the project does not declare (a library or
480+
* stdlib class such as android.app.ProgressDialog or a CoroutineScope), so a
481+
* weak short-name fallback must not bind the call to a same-named project
482+
* method (callers also check cbm_pipeline_qn_is_type_member: a project
483+
* extension function on that type still binds). The row names no graph node
484+
* and never becomes an edge itself. */
485+
static inline bool cbm_pipeline_kotlin_receiver_external(const CBMResolvedCallArray *arr,
486+
const CBMCall *call) {
487+
if (!arr || !call || !call->enclosing_func_qn || !call->callee_name) {
488+
return false;
489+
}
490+
/* The Kotlin LSP spells callers by package (com.acme.Screen.close), the
491+
* graph by path: an exact occurrence span, the exact caller, or the same
492+
* Class.method tail joins them (as cbm_pipeline_find_lsp_resolution_in_graph
493+
* does for JVM rows). */
494+
const char *call_tail = cbm_pipeline_qn_class_method_tail(call->enclosing_func_qn);
495+
for (int i = 0; i < arr->count; i++) {
496+
const CBMResolvedCall *rc = &arr->items[i];
497+
if (rc->kind != CBM_RESOLVED_INVOCATION || !rc->caller_qn || !rc->callee_qn ||
498+
!rc->strategy || strcmp(rc->strategy, "lsp_kt_external_receiver") != 0) {
499+
continue;
500+
}
501+
int site_rank = cbm_pipeline_invocation_site_rank(rc, call);
502+
if (site_rank == 0 || !cbm_pipeline_invocation_leaf_matches(rc, call, site_rank)) {
503+
continue;
504+
}
505+
bool caller_matches =
506+
site_rank == 2 || strcmp(rc->caller_qn, call->enclosing_func_qn) == 0 ||
507+
(call_tail && cbm_pipeline_qn_class_method_tail_eq(rc->caller_qn, call_tail));
508+
if (caller_matches) {
509+
return true;
510+
}
511+
}
512+
return false;
513+
}
514+
454515
/* Look up the highest-confidence LSP-resolved call entry whose caller QN
455516
* matches the textual call's enclosing function and whose callee QN
456517
* short-name matches the textual callee. Returns a pointer into `arr`

‎src/pipeline/pass_calls.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,17 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
13521352
cbm_suppress_external_import_shadow(call->callee_name, res.strategy, imports, imp_keys,
13531353
imp_count, cbm_pipeline_get_pkgmap(),
13541354
cbm_pipeline_get_nsmap()) ||
1355+
/* Kotlin: a member call on an untyped receiver whose leaf the file
1356+
* imports from a library, or whose receiver the Kotlin LSP typed as a
1357+
* library type, is never a project method. MUST match pass_parallel.c. */
1358+
/* (Kotlin calls carry no is_method flag; the helper reads the dotted
1359+
* callee as the member form.) */
1360+
cbm_suppress_kotlin_external_import_member(
1361+
lang == CBM_LANG_KOTLIN, true, call->callee_name, res.strategy, imports,
1362+
imp_keys, imp_count, cbm_pipeline_get_pkgmap(), cbm_pipeline_get_nsmap()) ||
1363+
(lang == CBM_LANG_KOTLIN && cbm_suppress_weak_member_match(true, true, res.strategy) &&
1364+
cbm_pipeline_kotlin_receiver_external(lsp_calls, call) &&
1365+
cbm_pipeline_qn_is_type_member(ctx->gbuf, res.qualified_name)) ||
13551366
/* MUST match pass_parallel.c exactly. */
13561367
(lang == CBM_LANG_PHP && cbm_suppress_weak_member_match(true, true, res.strategy) &&
13571368
cbm_pipeline_php_receiver_type_unindexed(lsp_calls, call, ctx->registry));

‎src/pipeline/pass_parallel.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3551,6 +3551,14 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
35513551
cbm_suppress_external_import_shadow(call->callee_name, res.strategy, &result->imports,
35523552
imp_keys, imp_count, cbm_pipeline_get_pkgmap(),
35533553
cbm_pipeline_get_nsmap()) ||
3554+
/* Kotlin library receivers — MUST match pass_calls.c exactly. */
3555+
cbm_suppress_kotlin_external_import_member(
3556+
lang == CBM_LANG_KOTLIN, true, call->callee_name, res.strategy,
3557+
&result->imports, imp_keys, imp_count, cbm_pipeline_get_pkgmap(),
3558+
cbm_pipeline_get_nsmap()) ||
3559+
(lang == CBM_LANG_KOTLIN && cbm_suppress_weak_member_match(true, true, res.strategy) &&
3560+
cbm_pipeline_kotlin_receiver_external(&result->resolved_calls, call) &&
3561+
cbm_pipeline_qn_is_type_member(rc->main_gbuf, res.qualified_name)) ||
35543562
/* MUST match pass_calls.c exactly. */
35553563
(lang == CBM_LANG_PHP && cbm_suppress_weak_member_match(true, true, res.strategy) &&
35563564
cbm_pipeline_php_receiver_type_unindexed(&result->resolved_calls, call, rc->registry));

‎src/pipeline/pipeline.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,17 @@ bool cbm_suppress_external_import_shadow(const char *callee_name, const char *st
467467
const CBMHashTable *indexed_packages,
468468
const CBMHashTable *declared_packages);
469469

470+
/* Kotlin member call `x.leaf()` whose leaf name the file imports from a
471+
* package the tree does not declare (`import kotlinx.coroutines.launch`): a
472+
* weak project-wide guess must not bind it. this/super receivers keep it.
473+
* Pure; unit-tested in test_registry.c. */
474+
bool cbm_suppress_kotlin_external_import_member(bool is_kotlin, bool is_method,
475+
const char *callee_name, const char *strategy,
476+
const CBMImportArray *file_imports,
477+
const char **import_map_keys, int import_map_count,
478+
const CBMHashTable *indexed_packages,
479+
const CBMHashTable *declared_packages);
480+
470481
/* Go analog of the TS/JS guard, same failure class: a selector call whose
471482
* receiver the Go LSP could not type must not be bound by a receiver-blind
472483
* short-name strategy. Drops suffix_match / fuzzy always, and unique_name only

‎src/pipeline/registry.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,50 @@ bool cbm_suppress_external_import_shadow(const char *callee_name, const char *st
986986
return external_binding;
987987
}
988988

989+
/* Kotlin member-call counterpart of the import-shadow guard above. Kotlin
990+
* reaches a library extension function through an explicit import of its
991+
* NAME: `import kotlinx.coroutines.launch` makes `scope.launch {}` the
992+
* coroutine builder. When such a call reaches the registry, the Kotlin LSP
993+
* could not type the receiver, and a project-wide same-name guess binds it to
994+
* whatever project method shares the name (`lifecycleScope.launch {}` ->
995+
* a project ScreenModel.launch). The import of the leaf name from a package
996+
* the tree does not declare is the same positive evidence the bare-call guard
997+
* uses, so the weak guess is refused. A `this.` / `super.` receiver names the
998+
* enclosing class, never the import, and keeps the guess; so does every
999+
* receiver- or import-aware
1000+
* strategy (cbm_suppress_external_import_shadow's own drop-list). A leaf
1001+
* imported from a package the tree declares (`import com.acme.util.launch`)
1002+
* is in-tree and keeps the guess. Pure; unit-tested in test_registry.c. */
1003+
bool cbm_suppress_kotlin_external_import_member(bool is_kotlin, bool is_method,
1004+
const char *callee_name, const char *strategy,
1005+
const CBMImportArray *file_imports,
1006+
const char **import_map_keys, int import_map_count,
1007+
const CBMHashTable *indexed_packages,
1008+
const CBMHashTable *declared_packages) {
1009+
if (!is_kotlin || !is_method || !callee_name) {
1010+
return false;
1011+
}
1012+
const char *dot = strrchr(callee_name, '.');
1013+
if (!dot || dot == callee_name || !dot[1]) {
1014+
return false;
1015+
}
1016+
if (strncmp(callee_name, "this", 4) == 0 &&
1017+
(callee_name[4] == '.' || callee_name[4] == '@' || callee_name[4] == '?')) {
1018+
return false;
1019+
}
1020+
if (strncmp(callee_name, "super", 5) == 0 &&
1021+
(callee_name[5] == '.' || callee_name[5] == '@' || callee_name[5] == '<')) {
1022+
return false;
1023+
}
1024+
/* The Kotlin import map lists every import's local name, resolved or
1025+
* not (pass_lsp_cross.c metadata entries), so a key is no proof that the
1026+
* import names project code; the declared-package test decides instead. */
1027+
(void)import_map_keys;
1028+
(void)import_map_count;
1029+
return cbm_suppress_external_import_shadow(dot + 1, strategy, file_imports, NULL, 0,
1030+
indexed_packages, declared_packages);
1031+
}
1032+
9891033
bool cbm_go_suppress_weak_method_match(bool is_go, bool is_method, const char *strategy,
9901034
double confidence) {
9911035
/* Go analog of the TS/JS guard above, same failure class: a selector call

0 commit comments

Comments
 (0)