Skip to content

Commit 8081009

Browse files
committed
feat(java): no name guess for calls on a proven-external receiver
When the Java resolver resolves a call's receiver to a type outside the project (a JDK package, or a package no project file declares), the call gets strategy lsp_external. Both pipeline paths (pass_calls.c and pass_parallel.c) then look the target up strictly by QN through cbm_pipeline_java_external_target and skip the registry's name guess, so no edge is created to an unrelated project method of the same name. This is the rule the Rust path follows since DeusData#2053. Positive proof only: a receiver of unknown type keeps the existing guess, and a seeded JDK type that lacks the called method stays no_method_match. Type variables (T, ID) are never taken for package-qualified types. A package counts as the project's when any registry layer holds a project type in it (the layer above the shared registry is a per-file overlay), and a path-derived spelling counts as the project's also for a file at the repository root. java_receiver_is_external memoizes its answer per package. A type of the caller's own package, or of its own file, now shadows java.lang (JLS 6.4.1): java_resolve_type_name looks the JVM package up before the java.lang auto-import. Before the gate that wrong first answer was harmless, since the name guess still found the project class; with the gate a project `Enum` or `Process` would read as external. Only the parallel path builds the Java cross registry, so on the sequential path the gate does not fire yet; the next commit adds it. Tests: java_typed_calls_parallel (external receivers get no edge; a project receiver keeps its edge, also when its type is missing from a project package or shares its simple name with a java.lang type), parallel_java_external_target_contract. Signed-off-by: Timur Nizametdinov <[email protected]>
1 parent 72c3604 commit 8081009

9 files changed

Lines changed: 540 additions & 21 deletions

File tree

‎Makefile.cbm‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,8 @@ TEST_PY_LSP_STRESS_SRCS = tests/test_py_lsp_stress.c
846846
TEST_PY_LSP_SCALE_SRCS = tests/test_py_lsp_scale.c
847847

848848
TEST_TS_LSP_SRCS = tests/test_ts_lsp.c
849-
TEST_JAVA_LSP_SRCS = tests/test_java_lsp.c tests/test_java_lsp_coverage.c
849+
TEST_JAVA_LSP_SRCS = tests/test_java_lsp.c tests/test_java_lsp_coverage.c \
850+
tests/test_java_typed_calls.c
850851
TEST_KOTLIN_LSP_SRCS = tests/test_kotlin_lsp.c tests/test_kotlin_typed_calls.c
851852
TEST_RUST_LSP_SRCS = tests/test_rust_lsp.c
852853

‎internal/cbm/lsp/java_lsp.c‎

Lines changed: 167 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,19 @@ const char *java_resolve_type_name(JavaLSPContext *ctx, const char *name) {
510510
}
511511
}
512512

513-
/* 3. java.lang auto-import. Try registered first, then the conventional QN. */
513+
/* 3. Java package prefix. A type of the own package shadows every
514+
* on-demand import, java.lang's included (JLS 6.4.1). */
515+
if (ctx->package_name && ctx->package_name[0]) {
516+
char buf[JAVA_LSP_BUF];
517+
int n = snprintf(buf, sizeof(buf), "%s.%s", ctx->package_name, name);
518+
if (n > 0 && (size_t)n < sizeof(buf)) {
519+
if (cbm_registry_lookup_type(ctx->registry, buf)) {
520+
return cbm_arena_strdup(ctx->arena, buf);
521+
}
522+
}
523+
}
524+
525+
/* 4. java.lang auto-import. Try registered first, then the conventional QN. */
514526
for (int i = 0; JAVA_LANG_TYPES[i]; i++) {
515527
if (strcmp(JAVA_LANG_TYPES[i], name) == 0) {
516528
char buf[JAVA_LSP_BUF];
@@ -521,7 +533,7 @@ const char *java_resolve_type_name(JavaLSPContext *ctx, const char *name) {
521533
}
522534
}
523535

524-
/* 4. On-demand imports. */
536+
/* 5. On-demand imports. */
525537
for (int i = 0; i < ctx->import_count; i++) {
526538
if (ctx->import_kinds[i] != CBM_JAVA_IMPORT_ON_DEMAND)
527539
continue;
@@ -534,17 +546,6 @@ const char *java_resolve_type_name(JavaLSPContext *ctx, const char *name) {
534546
}
535547
}
536548

537-
/* 5. Java package prefix. */
538-
if (ctx->package_name && ctx->package_name[0]) {
539-
char buf[JAVA_LSP_BUF];
540-
int n = snprintf(buf, sizeof(buf), "%s.%s", ctx->package_name, name);
541-
if (n > 0 && (size_t)n < sizeof(buf)) {
542-
if (cbm_registry_lookup_type(ctx->registry, buf)) {
543-
return cbm_arena_strdup(ctx->arena, buf);
544-
}
545-
}
546-
}
547-
548549
/* Exact already-qualified name. */
549550
if (cbm_registry_lookup_type(ctx->registry, name)) {
550551
return cbm_arena_strdup(ctx->arena, name);
@@ -969,6 +970,12 @@ const CBMRegisteredFunc *java_lookup_method(JavaLSPContext *ctx, const char *cla
969970
return fallback;
970971
}
971972

973+
/* A type declared in the project: the cross registrar and the own-file pass
974+
* stamp namespace_qn on every project type; the JDK seed leaves it NULL. */
975+
static bool java_is_project_type(const CBMRegisteredType *rt) {
976+
return rt && rt->namespace_qn && rt->namespace_qn[0];
977+
}
978+
972979
/* ── Method-invocation evaluation ─────────────────────────────────── */
973980

974981
static const CBMType *java_return_type_of(const CBMRegisteredFunc *f) {
@@ -1797,11 +1804,135 @@ void java_lsp_process_file(JavaLSPContext *ctx, TSNode root) {
17971804

17981805
/* ── Call-edge resolution ─────────────────────────────────────────── */
17991806

1807+
/* True when `callee_qn` names a member (or a constructor) of a registered type
1808+
* the project does not declare — a JDK seed type. Such a target has no graph
1809+
* node, and the pipeline must not hand it to a same-named project method. */
1810+
static bool java_callee_is_external(const JavaLSPContext *ctx, const char *callee_qn) {
1811+
if (!ctx->registry || !callee_qn)
1812+
return false;
1813+
const CBMRegisteredType *self = cbm_registry_lookup_type(ctx->registry, callee_qn);
1814+
if (self)
1815+
return !java_is_project_type(self);
1816+
const char *dot = strrchr(callee_qn, '.');
1817+
if (!dot || dot == callee_qn)
1818+
return false;
1819+
char owner[JAVA_LSP_BUF];
1820+
size_t n = (size_t)(dot - callee_qn);
1821+
if (n >= sizeof(owner))
1822+
return false;
1823+
memcpy(owner, callee_qn, n);
1824+
owner[n] = '\0';
1825+
const CBMRegisteredType *rt = cbm_registry_lookup_type(ctx->registry, owner);
1826+
return rt && !java_is_project_type(rt);
1827+
}
1828+
1829+
/* `T`, `ID`, `SELF`: a type-variable spelling — one or two characters, or
1830+
* capitals, digits and '_' only — that the file imports neither by name nor
1831+
* through its package (`import org.jooq.impl.*;` makes `DSL` a type). A type
1832+
* variable the resolver failed to bind can reach a receiver qualified by some
1833+
* package; it is unknown, not a library type. */
1834+
static bool java_is_type_var_spelling(const JavaLSPContext *ctx, const char *pkg, size_t pkg_len,
1835+
const char *simple) {
1836+
size_t n = strlen(simple);
1837+
bool caps = n > 0;
1838+
for (size_t i = 0; i < n && caps; i++) {
1839+
caps = isupper((unsigned char)simple[i]) || isdigit((unsigned char)simple[i]) ||
1840+
simple[i] == '_';
1841+
}
1842+
if (!caps && n > 2) {
1843+
return false;
1844+
}
1845+
for (int i = 0; i < ctx->import_count; i++) {
1846+
const char *target = ctx->import_target_qns[i];
1847+
if (ctx->import_kinds[i] == CBM_JAVA_IMPORT_TYPE && ctx->import_local_names[i] &&
1848+
strcmp(ctx->import_local_names[i], simple) == 0) {
1849+
return false;
1850+
}
1851+
if (ctx->import_kinds[i] == CBM_JAVA_IMPORT_ON_DEMAND && target &&
1852+
strlen(target) == pkg_len && strncmp(target, pkg, pkg_len) == 0) {
1853+
return false;
1854+
}
1855+
}
1856+
return true;
1857+
}
1858+
1859+
/* Whether the shared registry holds no project type under `prefix`
1860+
* ("org.jooq."), memoised per package. */
1861+
static bool java_package_is_external(JavaLSPContext *ctx, const CBMTypeRegistry *base,
1862+
const char *prefix) {
1863+
for (int i = 0; i < JAVA_EXT_MEMO; i++) {
1864+
if (ctx->ext_memo_pkg[i] && strcmp(ctx->ext_memo_pkg[i], prefix) == 0) {
1865+
return ctx->ext_memo_val[i];
1866+
}
1867+
}
1868+
CBMArena scratch;
1869+
cbm_arena_init(&scratch);
1870+
CBMQNPrefixIter it;
1871+
bool project = false;
1872+
/* The prefix search reads one layer, and the layer above the shared
1873+
* registry is a per-file overlay that is often empty: walk the chain. */
1874+
for (const CBMTypeRegistry *reg = base; reg && !project; reg = reg->fallback) {
1875+
if (!cbm_registry_types_with_qn_prefix(reg, prefix, true, &scratch, &it))
1876+
continue;
1877+
for (int ti = cbm_qn_prefix_iter_next(&it); ti >= 0 && !project;
1878+
ti = cbm_qn_prefix_iter_next(&it)) {
1879+
project = java_is_project_type(&reg->types[ti]);
1880+
}
1881+
}
1882+
cbm_arena_destroy(&scratch);
1883+
int slot = ctx->ext_memo_next++ % JAVA_EXT_MEMO;
1884+
ctx->ext_memo_pkg[slot] = prefix;
1885+
ctx->ext_memo_val[slot] = !project;
1886+
return !project;
1887+
}
1888+
1889+
/* A receiver type the registry does not know, spelled with a package the
1890+
* project declares no type in (`org.jooq.DSLContext` from an import): a
1891+
* library type. The package is the segments before the first capitalised
1892+
* one, so a nested type (`com.acme.Order.OrderBuilder`, registered flattened
1893+
* as `com.acme.OrderBuilder`) is judged by `com.acme`. Cross-file runs only —
1894+
* the shared registry is the one that knows every project package, and its
1895+
* type order makes the probe a binary search. A path-derived QN
1896+
* (project-prefixed) is an unresolved project spelling, never a library. */
1897+
static bool java_receiver_is_external(JavaLSPContext *ctx, const char *recv_qn) {
1898+
const CBMTypeRegistry *base = ctx->registry ? ctx->registry->fallback : NULL;
1899+
if (!base || !recv_qn || !islower((unsigned char)recv_qn[0]))
1900+
return false;
1901+
const char *type = NULL;
1902+
for (const char *p = recv_qn; (p = strchr(p, '.')) != NULL; p++) {
1903+
if (isupper((unsigned char)p[1])) {
1904+
type = p + 1;
1905+
break;
1906+
}
1907+
}
1908+
if (!type)
1909+
return false;
1910+
size_t pkg_len = (size_t)(type - 1 - recv_qn);
1911+
const char *last = strrchr(recv_qn, '.');
1912+
if (java_is_type_var_spelling(ctx, recv_qn, pkg_len, last + 1))
1913+
return false;
1914+
/* The project segment; a file at the repository root has a module QN
1915+
* that is the project name alone. */
1916+
const char *mod_dot = ctx->module_qn ? strchr(ctx->module_qn, '.') : NULL;
1917+
size_t mod_len = mod_dot ? (size_t)(mod_dot - ctx->module_qn)
1918+
: (ctx->module_qn ? strlen(ctx->module_qn) : 0);
1919+
if (mod_len && strncmp(recv_qn, ctx->module_qn, mod_len) == 0 && recv_qn[mod_len] == '.')
1920+
return false;
1921+
if (ctx->package_name && strlen(ctx->package_name) == pkg_len &&
1922+
strncmp(ctx->package_name, recv_qn, pkg_len) == 0)
1923+
return false;
1924+
/* "org.jooq." */
1925+
return java_package_is_external(ctx, base, cbm_arena_strndup(ctx->arena, recv_qn, pkg_len + 1));
1926+
}
1927+
18001928
static void java_emit_resolved_orig_kind(JavaLSPContext *ctx, const char *callee_qn,
18011929
const char *orig, const char *strategy, float confidence,
18021930
CBMResolvedKind kind) {
18031931
if (!ctx->resolved_calls || !ctx->enclosing_method_qn || !callee_qn)
18041932
return;
1933+
if (kind == CBM_RESOLVED_INVOCATION && java_callee_is_external(ctx, callee_qn)) {
1934+
strategy = "lsp_external";
1935+
}
18051936
CBMResolvedCall rc = {0};
18061937
rc.caller_qn = ctx->enclosing_method_qn;
18071938
rc.callee_qn = callee_qn;
@@ -2145,6 +2276,15 @@ static void resolve_method_call(JavaLSPContext *ctx, TSNode call) {
21452276
"lsp_interface_dispatch", 0.80f);
21462277
return;
21472278
}
2279+
/* A library type the registry does not hold: no method of it can be
2280+
* looked up, but no project method is its target either. A seeded
2281+
* JDK type with no such method stays a diagnostic below, as Rust's
2282+
* gate (#2053) needs a positively resolved std method. */
2283+
if (!rt && java_receiver_is_external(ctx, recv_qn)) {
2284+
java_emit_resolved(ctx, cbm_arena_sprintf(ctx->arena, "%s.%s", recv_qn, mname),
2285+
"lsp_external", 0.90f);
2286+
return;
2287+
}
21482288
java_emit_unresolved(ctx, cbm_arena_sprintf(ctx->arena, "%s.%s", recv_qn, mname),
21492289
"no_method_match");
21502290
return;
@@ -3214,6 +3354,9 @@ static void register_local_func_or_type_from_file(JavaLSPContext *ctx, CBMTypeRe
32143354
stub.qualified_name = d->qualified_name;
32153355
stub.short_name = d->name;
32163356
stub.is_interface = (strcmp(d->label, "Interface") == 0);
3357+
stub.namespace_qn = (ctx->package_name && ctx->package_name[0]) ? ctx->package_name
3358+
: (module_qn && module_qn[0]) ? module_qn
3359+
: d->qualified_name;
32173360
cbm_registry_add_type(reg, stub);
32183361
}
32193362

@@ -3740,6 +3883,11 @@ void cbm_java_register_lsp_defs(CBMArena *arena, CBMTypeRegistry *reg, const CBM
37403883
rt.qualified_name = d->qualified_name;
37413884
rt.short_name = d->short_name;
37423885
rt.is_interface = (strcmp(d->label, "Interface") == 0) || d->is_interface;
3886+
/* Project marker (java_is_project_type) and the package that
3887+
* qualifies the type's own member signatures. */
3888+
rt.namespace_qn = (d->namespace_name && d->namespace_name[0]) ? d->namespace_name
3889+
: (d->def_module_qn && d->def_module_qn[0]) ? d->def_module_qn
3890+
: d->qualified_name;
37433891
/* Embedded types from "|"-separated text. */
37443892
if (d->embedded_types && d->embedded_types[0]) {
37453893
int n = 1;
@@ -3883,6 +4031,9 @@ CBMTypeRegistry *cbm_java_build_cross_registry(CBMArena *arena, CBMLSPDef *defs,
38834031
cbm_java_register_lsp_defs(arena, reg, jvm + type_count, total - type_count);
38844032
}
38854033
cbm_registry_finalize(reg);
4034+
/* java_receiver_is_external asks "does the project declare a type in
4035+
* this package?" per unresolved receiver: a binary search on this order. */
4036+
cbm_registry_build_type_qn_order(reg, arena);
38864037
reg->read_only = true; /* seal: shared Tier-2 registry is read-only during resolve */
38874038
return reg;
38884039
}
@@ -3958,6 +4109,9 @@ void cbm_run_java_lsp_cross_with_registry(CBMArena *arena, CBMFileResult *result
39584109
}
39594110
}
39604111

4112+
/* The per-file fallback when no shared cross registry was built. Its
4113+
* registry has no fallback layer, so java_receiver_is_external never fires
4114+
* here: library calls keep the textual registry fallback, as before. */
39614115
void cbm_run_java_lsp_cross(CBMArena *arena, const char *source, int source_len,
39624116
const char *module_qn, CBMLSPDef *defs, int def_count,
39634117
const char **import_names, const char **import_qns, int import_count,

‎internal/cbm/lsp/java_lsp.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include "../cbm.h"
3636
#include "go_lsp.h" /* CBMLSPDef, CBMResolvedCallArray reused across languages */
3737

38+
#define JAVA_EXT_MEMO 8
39+
3840
/* Java `use`-style import kinds. Mirrors PHP's enum with Java semantics. */
3941
enum {
4042
CBM_JAVA_IMPORT_TYPE = 0, /* import com.foo.Bar; — type import */
@@ -90,6 +92,12 @@ typedef struct {
9092

9193
/* Debug mode (CBM_LSP_DEBUG env). */
9294
bool debug;
95+
96+
/* java_receiver_is_external's answers for the packages probed so far
97+
* (a few per file), so each costs one registry walk. */
98+
const char *ext_memo_pkg[JAVA_EXT_MEMO];
99+
bool ext_memo_val[JAVA_EXT_MEMO];
100+
int ext_memo_next;
93101
} JavaLSPContext;
94102

95103
/* ── Initialization / configuration ───────────────────────────────── */

‎src/pipeline/lsp_resolve.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,18 @@ static inline bool cbm_pipeline_kotlin_external_target(CBMLanguage lang, const c
635635
strncmp(callee_qn, "javax.", strlen("javax.")) == 0;
636636
}
637637

638+
/* Java: the LSP typed the receiver as a type OUTSIDE the project (a JDK or
639+
* library class, `lsp_external`), so the callee is `java.util.Optional.map`
640+
* and no project method. Without this gate the tail/unique-leaf node policy
641+
* and then the textual registry bound such a call to whatever project method
642+
* shares the leaf (`Optional.map` -> `Filters.map`). Such a call gets no
643+
* CALLS edge, as #2053 does for Rust. Pure; unit-tested. */
644+
static inline bool cbm_pipeline_java_external_target(CBMLanguage lang, const CBMResolvedCall *lsp) {
645+
return lang == CBM_LANG_JAVA && lsp && lsp->strategy &&
646+
strcmp(lsp->strategy, "lsp_external") == 0 && lsp->callee_qn &&
647+
strchr(lsp->callee_qn, '.') != NULL;
648+
}
649+
638650
/* Rust (#2053): true when the Rust LSP positively resolved a call to a
639651
* registered symbol that lives OUTSIDE the project — a std/core/alloc seed
640652
* method (`root.join()` on `root: &Path` -> std.path.Path.join) or a seeded

‎src/pipeline/pass_calls.c‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -892,14 +892,20 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
892892
* Service/route classification below still runs. Mirrors pass_parallel.c. */
893893
bool php_vendor_call = cbm_pipeline_php_vendor_call(php_vendor, result, call) ||
894894
cbm_pipeline_lsp_external_receiver(lsp);
895+
/* A Java call on a JDK/library type is never bound to a project method
896+
* found by name (no tail match, no registry guess). MUST match
897+
* pass_parallel.c. */
898+
bool java_external = cbm_pipeline_java_external_target(lang, lsp);
895899
if (lsp) {
896900
bool exact_external_target = call->requires_lsp_resolution &&
897901
cbm_pipeline_kotlin_external_target(lang, lsp->callee_qn);
898902
const cbm_gbuf_node_t *target_node =
899-
exact_external_target ? cbm_pipeline_lsp_target_node_strict(
900-
ctx->gbuf, ctx->project_name, lsp->callee_qn, allow_tail)
901-
: cbm_pipeline_lsp_target_node(ctx->gbuf, ctx->project_name,
902-
lsp->callee_qn, allow_tail);
903+
java_external ? cbm_pipeline_lsp_target_node_strict(ctx->gbuf, ctx->project_name,
904+
lsp->callee_qn, false)
905+
: exact_external_target ? cbm_pipeline_lsp_target_node_strict(
906+
ctx->gbuf, ctx->project_name, lsp->callee_qn, allow_tail)
907+
: cbm_pipeline_lsp_target_node(ctx->gbuf, ctx->project_name,
908+
lsp->callee_qn, allow_tail);
903909
if (target_node && source_node->id != target_node->id) {
904910
cbm_resolution_t res = {0};
905911
/* Use the gbuf node's QN so downstream edge props show the canonical
@@ -988,7 +994,7 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
988994
bool rust_external = lsp && cbm_pipeline_rust_external_target(
989995
lang, lsp->strategy, lsp->callee_qn, ctx->project_name);
990996
cbm_resolution_t res = {0};
991-
if (!rust_external && !php_vendor_call) {
997+
if (!rust_external && !php_vendor_call && !java_external) {
992998
/* Python LEGB fallback first (MUST match pass_parallel.c). */
993999
res =
9941000
cbm_registry_resolve_call_scoped(ctx->registry, call->callee_name, module_qn, imp_keys,

‎src/pipeline/pass_parallel.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3271,13 +3271,18 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
32713271
memory_order_relaxed);
32723272
_rc_t0 = extract_now_ns();
32733273
const cbm_gbuf_node_t *lsp_target = NULL;
3274+
/* Mirrors pass_calls.c: a Java call on a JDK/library type is never
3275+
* bound to a project method found by name. */
3276+
bool java_external = cbm_pipeline_java_external_target(lang, lsp);
32743277
if (lsp) {
32753278
/* Canonicalise to the gbuf node's QN so res.qualified_name matches
32763279
* the gbuf even when the cross-file fallback had to prefix the
32773280
* project name. */
32783281
bool exact_external_target = call->requires_lsp_resolution &&
32793282
cbm_pipeline_kotlin_external_target(lang, lsp->callee_qn);
3280-
lsp_target = exact_external_target
3283+
lsp_target = java_external ? cbm_pipeline_lsp_target_node_strict(
3284+
rc->main_gbuf, rc->project_name, lsp->callee_qn, false)
3285+
: exact_external_target
32813286
? cbm_pipeline_lsp_target_node_strict(rc->main_gbuf, rc->project_name,
32823287
lsp->callee_qn, allow_tail)
32833288
: cbm_pipeline_lsp_target_node(rc->main_gbuf, rc->project_name,
@@ -3314,7 +3319,7 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
33143319
bool rust_external = lsp && cbm_pipeline_rust_external_target(
33153320
lang, lsp->strategy, lsp->callee_qn, rc->project_name);
33163321
if ((!res.qualified_name || !res.qualified_name[0]) && !call->requires_lsp_resolution &&
3317-
!rust_external && !php_vendor_call) {
3322+
!rust_external && !php_vendor_call && !java_external) {
33183323
/* Python LEGB fallback first (MUST match pass_calls.c). */
33193324
res = cbm_registry_resolve_call_scoped(rc->registry, call->callee_name, module_qn,
33203325
imp_keys, imp_vals, imp_count, lang,

0 commit comments

Comments
 (0)