diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h index dc89a6b2d..360bd22aa 100644 --- a/internal/cbm/cbm.h +++ b/internal/cbm/cbm.h @@ -694,6 +694,24 @@ typedef struct { int count; } CBMReturnTypeTable; +// --- Scala companion cache --- +// The class/trait/enum names of every scope an object_definition has asked +// about, keyed by (scope node, name bytes). A scope is scanned once, on its +// first query; names are byte ranges into the source, never copied. One extra +// key per scanned scope (len == 0) records the scan itself, so a scope without +// any class is not rescanned either. Open addressing, power-of-two capacity. +typedef struct { + const void *scope; // ts_node id of the scope (parent) node; NULL = empty slot + uint32_t off; + uint32_t len; +} CBMScalaScopeName; + +typedef struct { + CBMScalaScopeName *slots; + uint32_t cap; // 0 until the first query + uint32_t count; +} CBMScalaCompanionCache; + typedef struct { CBMArena *arena; /* Scratch for AST traversal, owned by the cbm_extract_file_ex call that @@ -738,6 +756,9 @@ typedef struct { * POD section index. */ void *doc_memo; void *doc_pod_index; + /* Scala only; lives in `scratch` (or `arena` without one). Nothing in a + * CBMFileResult points at it. */ + CBMScalaCompanionCache scala_companions; } CBMExtractCtx; // --- Public API --- @@ -945,6 +966,11 @@ void cbm_impltrait_push(CBMImplTraitArray *arr, CBMArena *a, CBMImplTrait it); void cbm_resolvedcall_push(CBMResolvedCallArray *arr, CBMArena *a, CBMResolvedCall rc); void cbm_channels_push(CBMChannelArray *arr, CBMArena *a, CBMChannel ch); +/* True for a Scala object_definition with a same-named class/trait/enum + * sibling. O(1) per query after the first one in a scope, which scans the + * scope's named children once into ctx->scala_companions. */ +bool cbm_scala_is_companion_object(CBMExtractCtx *ctx, TSNode node); + // --- Sub-extractor entry points --- void cbm_extract_definitions(CBMExtractCtx *ctx); diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c index e35221799..550c3954b 100644 --- a/internal/cbm/extract_defs.c +++ b/internal/cbm/extract_defs.c @@ -6395,6 +6395,12 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec } else { class_qn = cbm_fqn_compute_source_lang(a, ctx->project, ctx->rel_path, name, ctx->language); } + /* A Scala class and its companion object are distinct owners despite sharing + * the source-level name. Use Scala's conventional singleton-class suffix so + * neither container nor their methods overwrite each other in the graph. */ + if (ctx->language == CBM_LANG_SCALA && cbm_scala_is_companion_object(ctx, node)) { + class_qn = cbm_arena_sprintf(a, "%s$", class_qn); + } const char *label = class_label_for_kind(kind); // Sway/WGSL: label struct defs as "Struct" and Sway `abi` blocks as @@ -9250,12 +9256,18 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const char char *cname = cbm_node_text(ctx->arena, name_node, ctx->source); if (cname && cname[0]) { if (saved_enclosing) { - return cbm_arena_sprintf(ctx->arena, "%s.%s", saved_enclosing, cname); + const char *qn = cbm_arena_sprintf(ctx->arena, "%s.%s", saved_enclosing, cname); + return ctx->language == CBM_LANG_SCALA && cbm_scala_is_companion_object(ctx, node) + ? cbm_arena_sprintf(ctx->arena, "%s$", qn) + : qn; } /* Top-level: language-aware module so Java/Go don't double the * filename stem (matches extract_class_def above). */ - return cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path, cname, - ctx->language); + const char *qn = cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path, + cname, ctx->language); + return ctx->language == CBM_LANG_SCALA && cbm_scala_is_companion_object(ctx, node) + ? cbm_arena_sprintf(ctx->arena, "%s$", qn) + : qn; } } return saved_enclosing; diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c index 086f501a0..7e1472660 100644 --- a/internal/cbm/extract_unified.c +++ b/internal/cbm/extract_unified.c @@ -1156,12 +1156,18 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const WalkS * scope QN matches the def-side class QN (extract_defs.c compute_class_qn / * extract_class_def), which the lsp_resolve join requires for nested types. */ if (state && state->enclosing_class_qn) { - return cbm_arena_sprintf(ctx->arena, "%s.%s", state->enclosing_class_qn, name); + const char *qn = cbm_arena_sprintf(ctx->arena, "%s.%s", state->enclosing_class_qn, name); + return ctx->language == CBM_LANG_SCALA && cbm_scala_is_companion_object(ctx, node) + ? cbm_arena_sprintf(ctx->arena, "%s$", qn) + : qn; } /* Java/Go: directory-based module (see compute_func_qn). */ - return cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path, name, - ctx->language); + const char *qn = + cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path, name, ctx->language); + return ctx->language == CBM_LANG_SCALA && cbm_scala_is_companion_object(ctx, node) + ? cbm_arena_sprintf(ctx->arena, "%s$", qn) + : qn; } /* Forward declaration */ diff --git a/internal/cbm/helpers.c b/internal/cbm/helpers.c index 4c4d340f0..25b8e3eae 100644 --- a/internal/cbm/helpers.c +++ b/internal/cbm/helpers.c @@ -173,6 +173,120 @@ bool cbm_label_is_type_like(const char *label) { strcmp(label, "Type") == 0 || strcmp(label, "Trait") == 0; } +/* --- Scala companion cache (CBMScalaCompanionCache) --- */ + +static uint64_t scc_hash(const void *scope, const char *bytes, uint32_t len) { + uint64_t h = 1469598103934665603ULL; + const unsigned char *p = (const unsigned char *)&scope; + for (size_t i = 0; i < sizeof(scope); i++) { + h = (h ^ p[i]) * 1099511628211ULL; + } + for (uint32_t i = 0; i < len; i++) { + h = (h ^ (unsigned char)bytes[i]) * 1099511628211ULL; + } + return h; +} + +/* Slot holding (scope, name), or the empty slot where it would go. */ +static CBMScalaScopeName *scc_find(const CBMScalaCompanionCache *c, const char *source, + const void *scope, uint32_t off, uint32_t len) { + uint32_t mask = c->cap - 1; + uint32_t i = (uint32_t)scc_hash(scope, source + off, len) & mask; + for (;;) { + CBMScalaScopeName *s = &c->slots[i]; + if (!s->scope) { + return s; + } + if (s->scope == scope && s->len == len && + (len == 0 || memcmp(source + s->off, source + off, len) == 0)) { + return s; + } + i = (i + 1) & mask; + } +} + +static bool scc_insert(CBMScalaCompanionCache *c, CBMArena *a, const char *source, + const void *scope, uint32_t off, uint32_t len) { + if ((c->count + 1) * 2 > c->cap) { + uint32_t new_cap = c->cap ? c->cap * 2 : 64; + CBMScalaScopeName *old = c->slots; + uint32_t old_cap = c->cap; + c->slots = cbm_arena_calloc(a, (size_t)new_cap * sizeof(*c->slots)); + if (!c->slots) { + c->slots = old; + return false; + } + c->cap = new_cap; + for (uint32_t i = 0; i < old_cap; i++) { + if (old[i].scope) { + *scc_find(c, source, old[i].scope, old[i].off, old[i].len) = old[i]; + } + } + } + CBMScalaScopeName *s = scc_find(c, source, scope, off, len); + if (!s->scope) { + s->scope = scope; + s->off = off; + s->len = len; + c->count++; + } + return true; +} + +/* First query in a scope: record its class/trait/enum names, plus the + * (scope, "") marker that says the scope has been scanned. */ +static bool scc_scan_scope(CBMScalaCompanionCache *c, CBMArena *a, const char *source, + TSNode scope) { + const void *id = scope.id; + if (c->cap && scc_find(c, source, id, 0, 0)->scope) { + return true; + } + if (!scc_insert(c, a, source, id, 0, 0)) { + return false; + } + uint32_t count = ts_node_named_child_count(scope); + for (uint32_t i = 0; i < count; i++) { + TSNode sibling = ts_node_named_child(scope, i); + const char *kind = ts_node_type(sibling); + if (strcmp(kind, "class_definition") != 0 && strcmp(kind, "trait_definition") != 0 && + strcmp(kind, "enum_definition") != 0) { + continue; + } + TSNode name = ts_node_child_by_field_name(sibling, TS_FIELD("name")); + if (ts_node_is_null(name)) { + continue; + } + uint32_t off = ts_node_start_byte(name); + uint32_t len = ts_node_end_byte(name) - off; + if (len && !scc_insert(c, a, source, id, off, len)) { + return false; + } + } + return true; +} + +bool cbm_scala_is_companion_object(CBMExtractCtx *ctx, TSNode node) { + if (!ctx || !ctx->source || strcmp(ts_node_type(node), "object_definition") != 0) { + return false; + } + TSNode name_node = ts_node_child_by_field_name(node, TS_FIELD("name")); + TSNode parent = ts_node_parent(node); + if (ts_node_is_null(name_node) || ts_node_is_null(parent)) { + return false; + } + uint32_t off = ts_node_start_byte(name_node); + uint32_t len = ts_node_end_byte(name_node) - off; + if (len == 0 || ts_node_end_byte(name_node) > (uint32_t)ctx->source_len) { + return false; + } + CBMArena *a = ctx->scratch ? ctx->scratch : ctx->arena; + CBMScalaCompanionCache *c = &ctx->scala_companions; + if (!a || !scc_scan_scope(c, a, ctx->source, parent)) { + return false; + } + return scc_find(c, ctx->source, parent.id, off, len)->scope != NULL; +} + // True when `label` names a data relation: SQL CREATE TABLE / CREATE VIEW, and // a dbt Model (a Jinja-templated .sql file, which materializes as a warehouse // table or view). Relations live in the registry so FROM/JOIN and dbt ref() diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.c index f7c2f4002..ea4ab16ac 100644 --- a/src/pipeline/pass_definitions.c +++ b/src/pipeline/pass_definitions.c @@ -396,7 +396,8 @@ static void process_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, const * (SQL FROM/JOIN lineage). pass_parallel.c and pipeline_incremental.c seed * through the same predicate, so the three registries cannot diverge. */ if (node_id > 0 && cbm_label_is_registry_symbol(def->label)) { - cbm_registry_add(ctx->registry, def->name, def->qualified_name, def->label); + cbm_registry_add_def(ctx->registry, def->name, def->qualified_name, def->label, + def->file_path ? def->file_path : rel); } char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__"); const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn); diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c index eab579f7d..9d28be1f4 100644 --- a/src/pipeline/pass_parallel.c +++ b/src/pipeline/pass_parallel.c @@ -1691,7 +1691,7 @@ int cbm_parallel_extract(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, /* `file_node_id` is the defining file's node id (0 when it has none), looked up * once per file by the caller: computing the file QN and finding its node for * every definition was 700 k allocations and lookups on the Go corpus. */ -static int register_and_link_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, +static int register_and_link_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, const char *rel, int64_t file_node_id, int *reg_entries) { int edges = 0; if (!def->name || !def->qualified_name || !def->label) { @@ -1700,7 +1700,8 @@ static int register_and_link_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *d /* Registry membership is defined ONCE by cbm_label_is_registry_symbol * (helpers.c) — see pass_definitions.c for the per-label rationale. */ if (cbm_label_is_registry_symbol(def->label)) { - cbm_registry_add(ctx->registry, def->name, def->qualified_name, def->label); + cbm_registry_add_def(ctx->registry, def->name, def->qualified_name, def->label, + def->file_path ? def->file_path : rel); (*reg_entries)++; } const cbm_gbuf_node_t *def_node = cbm_gbuf_find_by_qn(ctx->gbuf, def->qualified_name); @@ -1879,8 +1880,8 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t free(file_qn); pp_add_file_doc(file_node, result->module_doc); for (int d = 0; d < result->defs.count; d++) { - defines_edges += - register_and_link_def(ctx, &result->defs.items[d], file_node_id, ®_entries); + defines_edges += register_and_link_def(ctx, &result->defs.items[d], rel, + file_node_id, ®_entries); } } diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c index e8523b89a..9ec34ddf2 100644 --- a/src/pipeline/pass_pkgmap.c +++ b/src/pipeline/pass_pkgmap.c @@ -2248,15 +2248,20 @@ static const cbm_gbuf_node_t *resolve_scala_namespace_import(const cbm_pipeline_ return owner; } /* `import pkg.Owner.member`: the member must exist under the owner's - * QN. Binding the owner instead would mislabel the edge. */ - char member_qn[CBM_SZ_512]; - if (snprintf(member_qn, sizeof(member_qn), "%s.%s", owner->qualified_name, - remainder + top_len + 1) >= (int)sizeof(member_qn)) { - return NULL; - } - const cbm_gbuf_node_t *scoped = cbm_gbuf_find_by_qn(ctx->gbuf, member_qn); - if (scoped && scoped->label && import_targetable_label(scoped->label)) { - return scoped; + * QN. Binding the owner instead would mislabel the edge. Only object + * members are importable, so when Owner has a companion the `Owner$` + * scope is tried before the class. */ + static const char *const scopes[] = {"$", ""}; + for (size_t si = 0; si < sizeof(scopes) / sizeof(scopes[0]); si++) { + char member_qn[CBM_SZ_512]; + if (snprintf(member_qn, sizeof(member_qn), "%s%s.%s", owner->qualified_name, scopes[si], + remainder + top_len + 1) >= (int)sizeof(member_qn)) { + return NULL; + } + const cbm_gbuf_node_t *scoped = cbm_gbuf_find_by_qn(ctx->gbuf, member_qn); + if (scoped && scoped->label && import_targetable_label(scoped->label)) { + return scoped; + } } return NULL; } diff --git a/src/pipeline/pipeline.h b/src/pipeline/pipeline.h index c1d571ecd..80379e193 100644 --- a/src/pipeline/pipeline.h +++ b/src/pipeline/pipeline.h @@ -239,6 +239,14 @@ void cbm_registry_free(cbm_registry_t *r); void cbm_registry_add(cbm_registry_t *r, const char *name, const char *qualified_name, const char *label); +/* cbm_registry_add for a definition with a known declaring file. The one + * entry point the pipeline registers through: besides the plain add, a + * type-like `Foo$` declared in a Scala file is recorded as a companion owner, + * which is the only thing that lets the resolver read `Foo.m` as `Foo$.m`. + * `Foo$` from any other language is an ordinary name. */ +void cbm_registry_add_def(cbm_registry_t *r, const char *name, const char *qualified_name, + const char *label, const char *file_path); + /* Resolve a callee name using prioritized strategies. * import_map: NULL-terminated array of {local_name, resolved_qn} pairs, or NULL. * Returns result with qualified_name="" if unresolved. diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.c index 507b25b48..2f5bcdafa 100644 --- a/src/pipeline/pipeline_incremental.c +++ b/src/pipeline/pipeline_incremental.c @@ -1060,7 +1060,7 @@ static void registry_visitor(const cbm_gbuf_node_t *node, void *userdata) { if (!incr_label_is_registry_symbol(node->label)) { return; } - cbm_registry_add(r, node->name, node->qualified_name, node->label); + cbm_registry_add_def(r, node->name, node->qualified_name, node->label, node->file_path); } static void free_incremental_result_cache(CBMFileResult **cache, int count) { diff --git a/src/pipeline/registry.c b/src/pipeline/registry.c index ff56a41ac..0186da792 100644 --- a/src/pipeline/registry.c +++ b/src/pipeline/registry.c @@ -26,6 +26,7 @@ enum { REG_MAX_CANDIDATES = 256 }; #define DEFAULT_CONFIDENCE 0.5 #include "pipeline/pipeline.h" #include "cbm.h" /* cbm_label_is_relation — the resolve-time relation veto */ +#include "discover/discover.h" /* cbm_language_for_filename — Scala companion gate */ #include "foundation/compat.h" /* CBM_TLS */ #include "foundation/hash_table.h" #include "foundation/dyn_array.h" @@ -97,6 +98,12 @@ struct cbm_registry { /* byName: simpleName → qn_array_t* (heap-owned) */ CBMHashTable *by_name; + + /* Scala companion owners (`pkg.Foo$`), recorded by cbm_registry_add_def + * for type-like definitions in Scala files only. Keys borrow `exact`'s + * owned QN. NULL until the first companion, so every other language's + * registry never allocates it and every `$` probe below is one NULL test. */ + CBMHashTable *companions; }; /* ── Helpers ─────────────────────────────────────────────────────── */ @@ -865,6 +872,7 @@ void cbm_registry_free(cbm_registry_t *r) { /* by_name first: its items borrow exact's keys. */ cbm_ht_foreach(r->by_name, free_qn_array, NULL); cbm_ht_free(r->by_name); + cbm_ht_free(r->companions); /* keys borrow exact's */ cbm_ht_foreach(r->exact, free_label, NULL); cbm_ht_free(r->exact); for (int i = 0; i < r->label_pool_n; i++) { @@ -982,6 +990,54 @@ void cbm_registry_add(cbm_registry_t *r, const char *name, const char *qualified } } +static bool path_is_scala(const char *path) { + const char *base = path; + for (const char *p = path; *p; p++) { + if (*p == '/' || *p == '\\') { + base = p + SKIP_ONE; + } + } + return cbm_language_for_filename(base) == CBM_LANG_SCALA; +} + +void cbm_registry_add_def(cbm_registry_t *r, const char *name, const char *qualified_name, + const char *label, const char *file_path) { + cbm_registry_add(r, name, qualified_name, label); + if (!r || !qualified_name || !label || !file_path) { + return; + } + size_t len = strlen(qualified_name); + if (len < 2 || qualified_name[len - 1] != '$' || !cbm_label_is_type_like(label) || + !path_is_scala(file_path)) { + return; + } + const char *owned_qn = cbm_ht_get_key(r->exact, qualified_name); + if (!owned_qn) { + return; + } + if (!r->companions) { + r->companions = cbm_ht_create(CBM_SZ_256); + if (!r->companions) { + return; + } + } + cbm_ht_set(r->companions, owned_qn, (void *)owned_qn); +} + +/* True when the first `owner_len` bytes of `owner` name a class whose Scala + * companion (`$`) is registered. The only gate on every `$` probe in + * the resolver: a Java or JS `Foo$` next to `Foo` is never a companion. */ +static bool companion_known(const cbm_registry_t *r, const char *owner, size_t owner_len) { + if (!r->companions || owner_len == 0 || owner_len + 2 > CBM_SZ_512) { + return false; + } + char key[CBM_SZ_512]; + memcpy(key, owner, owner_len); + key[owner_len] = '$'; + key[owner_len + 1] = '\0'; + return cbm_ht_has(r->companions, key); +} + /* ── Lookup ──────────────────────────────────────────────────────── */ bool cbm_registry_exists(const cbm_registry_t *r, const char *qn) { @@ -1079,26 +1135,46 @@ static cbm_resolution_t resolve_import_map(const cbm_registry_t *r, const char * if (stored_key) { return (cbm_resolution_t){stored_key, "import_map", CONF_IMPORT_MAP, REG_RESOLVED}; } - - /* import_map_suffix fallback: find a QN starting with resolved+"." and - * ending with "."+suffix. Any such QN's last segment equals the last - * segment of suffix, so probe the by_name index and tail-check the (few) - * candidates instead of cbm_ht_foreach over the WHOLE exact table — that - * scan ran per unresolved call and dominated elasticsearch's resolve - * phase (94% of samples: 700k-entry foreach + strlen per entry). */ if (suffix && suffix[0]) { + /* Scala: an import binds the class, but `Foo.method()` on a class name + * is a call into the companion object, whose QN carries a trailing `$`. + * Probed only when `$` is a registered Scala companion, so a + * Java or JS class that happens to be named `Foo$` never takes part. */ + bool companion = companion_known(r, resolved, strlen(resolved)); + if (companion) { + snprintf(candidate, sizeof(candidate), "%s$.%s", resolved, suffix); + stored_key = cbm_ht_get_key(r->exact, candidate); + if (stored_key) { + return (cbm_resolution_t){stored_key, "import_map", CONF_IMPORT_MAP, REG_RESOLVED}; + } + } + + /* import_map_suffix fallback: find a QN starting with resolved+"." and + * ending with "."+suffix. Any such QN's last segment equals the last + * segment of suffix, so probe the by_name index and tail-check the + * (few) candidates instead of cbm_ht_foreach over the WHOLE exact + * table — that scan ran per unresolved call and dominated + * elasticsearch's resolve phase (94% of samples: 700k-entry foreach + + * strlen per entry). */ char resolved_dot[CBM_SZ_512]; + char companion_dot[CBM_SZ_512]; char dot_suffix[CBM_SZ_256]; snprintf(resolved_dot, sizeof(resolved_dot), "%s.", resolved); + snprintf(companion_dot, sizeof(companion_dot), "%s$.", resolved); snprintf(dot_suffix, sizeof(dot_suffix), ".%s", suffix); qn_array_t *arr = cbm_ht_get(r->by_name, simple_name(suffix)); if (arr) { size_t rd_len = strlen(resolved_dot); + size_t cd_len = strlen(companion_dot); size_t ds_len = strlen(dot_suffix); for (int i = 0; i < arr->count; i++) { const char *qn = arr->items[i]; size_t klen = strlen(qn); - if (klen >= rd_len + ds_len && strncmp(qn, resolved_dot, rd_len) == 0 && + bool under_owner = + klen >= rd_len + ds_len && strncmp(qn, resolved_dot, rd_len) == 0; + bool under_companion = + companion && klen >= cd_len + ds_len && strncmp(qn, companion_dot, cd_len) == 0; + if ((under_owner || under_companion) && strcmp(qn + klen - ds_len, dot_suffix) == 0) { return (cbm_resolution_t){qn, "import_map_suffix", CONF_IMPORT_MAP_SUFFIX, REG_RESOLVED}; @@ -1118,6 +1194,23 @@ static cbm_resolution_t resolve_same_module(const cbm_registry_t *r, const char if (stored_key) { return (cbm_resolution_t){stored_key, "same_module", CONF_SAME_MODULE, REG_RESOLVED}; } + /* Scala `Owner.member` in the declaring file: the member may live under + * the companion `Owner$`. Each owner segment is tried once, and only when + * `.$` is a registered Scala companion. */ + for (const char *dot = r->companions ? strchr(callee_name, '.') : NULL; dot; + dot = strchr(dot + 1, '.')) { + int owner_len = snprintf(candidate, sizeof(candidate), "%s.%.*s", module_qn, + (int)(dot - callee_name), callee_name); + if (owner_len < 0 || owner_len >= (int)sizeof(candidate) || + !companion_known(r, candidate, (size_t)owner_len)) { + continue; + } + snprintf(candidate + owner_len, sizeof(candidate) - (size_t)owner_len, "$%s", dot); + stored_key = cbm_ht_get_key(r->exact, candidate); + if (stored_key) { + return (cbm_resolution_t){stored_key, "same_module", CONF_SAME_MODULE, REG_RESOLVED}; + } + } if (suffix && suffix[0]) { snprintf(candidate, sizeof(candidate), "%s.%s", module_qn, suffix); stored_key = cbm_ht_get_key(r->exact, candidate); @@ -1171,6 +1264,49 @@ static cbm_resolution_t resolve_multi_with_imports(const qn_array_t *arr, const * as trustworthy as a same-module hit. */ #define CONF_QUALIFIED_SUFFIX 0.90 +/* Segment equality that lets a Scala companion object stand in for its class: + * a call written `Foo.make` reaches the method declared under `Foo$`. `cand` + * points into `cand_qn`; the `$` form is accepted only when the candidate QN + * up to and including that segment is a registered Scala companion, so a + * Java or JS segment `Foo$` is plain text that does not equal `Foo`. */ +static bool qn_seg_matches(const cbm_registry_t *r, const char *cand_qn, const char *seg, + size_t seg_len, const char *cand, size_t cand_len) { + if (cand_len == seg_len) { + return strncmp(seg, cand, seg_len) == 0; + } + return cand_len == seg_len + 1 && cand[seg_len] == '$' && strncmp(seg, cand, seg_len) == 0 && + companion_known(r, cand_qn, (size_t)(cand - cand_qn) + seg_len); +} + +/* True when `qn` equals `dotted` or ends with ".", compared segment by + * segment from the right so companion segments (`Foo$`) match `Foo`. */ +static bool qn_tail_matches(const cbm_registry_t *r, const char *qn, const char *dotted) { + const char *q_end = qn + strlen(qn); + const char *d_end = dotted + strlen(dotted); + for (;;) { + const char *q_dot = q_end; + while (q_dot > qn && q_dot[-1] != '.') { + q_dot--; + } + const char *d_dot = d_end; + while (d_dot > dotted && d_dot[-1] != '.') { + d_dot--; + } + if (!qn_seg_matches(r, qn, d_dot, (size_t)(d_end - d_dot), q_dot, + (size_t)(q_end - q_dot))) { + return false; + } + if (d_dot == dotted) { + return true; /* every callee segment matched at a segment boundary */ + } + if (q_dot == qn) { + return false; /* callee has more segments than the candidate */ + } + q_end = q_dot - 1; + d_end = d_dot - 1; + } +} + /* When a callee is package/namespace-qualified (Foo::Bar::sub or Foo.Bar.sub), * disambiguate among same-simple-name candidates by matching the FULL qualified * tail against each candidate QN at a segment boundary. Returns the sole @@ -1182,7 +1318,8 @@ static cbm_resolution_t resolve_multi_with_imports(const qn_array_t *arr, const * both reduce to "run", so the bare-name scorer would route every caller to a * single winner. Language agnostic: callees with no separator return NULL and * leave behavior unchanged. */ -static const char *qualified_suffix_match(const qn_array_t *arr, const char *callee_name) { +static const char *qualified_suffix_match(const cbm_registry_t *r, const qn_array_t *arr, + const char *callee_name) { /* Normalize "::" → "." so the tail composes with dotted candidate QNs. */ char dotted[CBM_SZ_512]; size_t w = 0; @@ -1203,16 +1340,7 @@ static const char *qualified_suffix_match(const qn_array_t *arr, const char *cal const char *match = NULL; for (int i = 0; i < arr->count; i++) { const char *qn = arr->items[i]; - size_t qlen = strlen(qn); - if (qlen < w) { - continue; - } - const char *tail = qn + (qlen - w); - if (strcmp(tail, dotted) != 0) { - continue; - } - /* Segment boundary: tail is the whole QN or is preceded by '.'. */ - if (tail != qn && tail[-1] != '.') { + if (!qn_tail_matches(r, qn, dotted)) { continue; } if (match) { @@ -1238,7 +1366,8 @@ static const char *qualified_suffix_match(const qn_array_t *arr, const char *cal * * Language agnostic by design: the registry holds no language, and every * language that writes receiver chains gains the same protection. */ -static bool receiver_chain_admits(const char *callee_name, const char *candidate_qn) { +static bool receiver_chain_admits(const cbm_registry_t *r, const char *callee_name, + const char *candidate_qn) { /* Normalize "::" -> "." so the chain composes with dotted candidate QNs, * the same way qualified_suffix_match does. */ char dotted[CBM_SZ_512]; @@ -1304,7 +1433,7 @@ static bool receiver_chain_admits(const char *callee_name, const char *candidate for (const char *anc = candidate_qn; anc < cand_last;) { const char *anc_end = strchr(anc, '.'); size_t anc_len = (size_t)(anc_end - anc); - if (anc_len == len && len > 0 && strncmp(seg, anc, len) == 0) { + if (len > 0 && qn_seg_matches(r, candidate_qn, seg, len, anc, anc_len)) { return true; } anc = anc_end + SKIP_ONE; @@ -1331,7 +1460,7 @@ static cbm_resolution_t resolve_name_lookup(const cbm_registry_t *r, const char * candidates by full qualified tail, before bare-name scoring collapses * them onto a single winner. */ if (arr->count > 1) { - const char *q = qualified_suffix_match(arr, callee_name); + const char *q = qualified_suffix_match(r, arr, callee_name); if (q) { return (cbm_resolution_t){q, "qualified_suffix", CONF_QUALIFIED_SUFFIX, REG_RESOLVED}; } @@ -1339,7 +1468,7 @@ static cbm_resolution_t resolve_name_lookup(const cbm_registry_t *r, const char /* Strategy 3: unique name */ if (arr->count == SKIP_ONE) { - if (!receiver_chain_admits(callee_name, arr->items[0])) { + if (!receiver_chain_admits(r, callee_name, arr->items[0])) { return empty_result(); } double conf = CONF_UNIQUE_NAME; @@ -1357,7 +1486,7 @@ static cbm_resolution_t resolve_name_lookup(const cbm_registry_t *r, const char const char *best = best_by_import_distance((const char **)arr->items, qn_test_flags(arr), arr->count, module_qn); if (best) { - if (!receiver_chain_admits(callee_name, best)) { + if (!receiver_chain_admits(r, callee_name, best)) { return empty_result(); } double conf = candidate_count_penalty(CONF_SUFFIX_MATCH, arr->count); diff --git a/tests/test_extraction.c b/tests/test_extraction.c index 0e459b7b3..fe155e566 100644 --- a/tests/test_extraction.c +++ b/tests/test_extraction.c @@ -6354,6 +6354,123 @@ TEST(extract_ts_member_call_flags_is_method) { PASS(); } +TEST(extract_scala_companion_owners_are_distinct) { + CBMFileResult *r = extract("case class Rational private (n: Int, d: Int) {\n" + " lazy val isWhole: Boolean = d == 1\n" + " def reciprocal: Rational = Rational(d, n)\n" + "}\n" + "case object Rational {\n" + " val zero: Rational = Rational(0, 1)\n" + " def apply(n: Int, d: Int): Rational = new Rational(n, d)\n" + "}\n", + CBM_LANG_SCALA, "t", "Rational.scala"); + ASSERT_NOT_NULL(r); + ASSERT_FALSE(r->has_error); + + ASSERT_TRUE(has_def_qn(r, "t.Rational.Rational")); + ASSERT_TRUE(has_def_qn(r, "t.Rational.Rational$")); + ASSERT_TRUE(has_def_qn(r, "t.Rational.Rational.reciprocal")); + ASSERT_TRUE(has_def_qn(r, "t.Rational.Rational$.apply")); + /* Class-body vals keep their module-level QN like every other language; + * parent_class tells the two owners apart. */ + ASSERT_TRUE(has_def_qn(r, "t.Rational.isWhole")); + ASSERT_TRUE(has_def_qn(r, "t.Rational.zero")); + + cbm_free_result(r); + PASS(); +} + +TEST(extract_scala_trait_companion_and_standalone_object) { + CBMFileResult *r = extract("trait Codec {\n" + " def encode(s: String): Array[Byte]\n" + "}\n" + "object Codec {\n" + " def utf8: Codec = ???\n" + "}\n" + "object Registry {\n" + " def lookup(name: String): Option[Codec] = None\n" + "}\n", + CBM_LANG_SCALA, "t", "Codec.scala"); + ASSERT_NOT_NULL(r); + ASSERT_FALSE(r->has_error); + + /* Trait companions collide just like class companions. */ + ASSERT_TRUE(has_def_qn(r, "t.Codec.Codec")); + ASSERT_TRUE(has_def_qn(r, "t.Codec.Codec$")); + ASSERT_TRUE(has_def_qn(r, "t.Codec.Codec.encode")); + ASSERT_TRUE(has_def_qn(r, "t.Codec.Codec$.utf8")); + /* A standalone object has nothing to collide with and keeps its plain QN. */ + ASSERT_TRUE(has_def_qn(r, "t.Codec.Registry")); + ASSERT_FALSE(has_def_qn(r, "t.Codec.Registry$")); + ASSERT_TRUE(has_def_qn(r, "t.Codec.Registry.lookup")); + + cbm_free_result(r); + PASS(); +} + +/* Companion detection is answered per scope from a cache built on the first + * query in that scope, so the same name must be judged against its own + * scope's siblings only: `object Node` is a companion at the top level and + * standalone inside `Tree`, and a scope with no class at all (`Ops`) must not + * turn any of its objects into companions. A flat sealed hierarchy of many + * case classes and case objects in one scope is the shape that used to be + * quadratic; every object there must still be classified correctly. */ +TEST(extract_scala_companion_detection_is_scoped) { + CBMFileResult *r = extract("class Node(v: Int)\n" + "object Node { def leaf: Node = new Node(0) }\n" + "object Tree {\n" + " object Node { def root: Int = 0 }\n" + " class Leaf\n" + " object Leaf { def make: Leaf = new Leaf }\n" + "}\n" + "object Ops {\n" + " object Node { def count: Int = 1 }\n" + " object Leaf { def count: Int = 2 }\n" + "}\n" + "sealed trait Op\n" + "case class Add(n: Int) extends Op\n" + "case object Add extends Op\n" + "case class Sub(n: Int) extends Op\n" + "case object Neg extends Op\n" + "case object Op { def all: Seq[Op] = Nil }\n", + CBM_LANG_SCALA, "t", "Ast.scala"); + ASSERT_NOT_NULL(r); + ASSERT_FALSE(r->has_error); + + ASSERT_TRUE(has_def_qn(r, "t.Ast.Node")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Node$")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Node$.leaf")); + /* Inside `Tree` there is no class Node, so its `object Node` is standalone, + * while `Leaf` has a class sibling there. */ + ASSERT_TRUE(has_def_qn(r, "t.Ast.Tree.Node")); + ASSERT_FALSE(has_def_qn(r, "t.Ast.Tree.Node$")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Tree.Node.root")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Tree.Leaf")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Tree.Leaf$")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Tree.Leaf$.make")); + /* `Ops` holds objects only; nothing there is a companion, whatever the + * neighbouring scopes declare. */ + ASSERT_TRUE(has_def_qn(r, "t.Ast.Ops.Node")); + ASSERT_FALSE(has_def_qn(r, "t.Ast.Ops.Node$")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Ops.Leaf")); + ASSERT_FALSE(has_def_qn(r, "t.Ast.Ops.Leaf$")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Ops.Node.count")); + /* Sealed hierarchy in one scope: companions of a case class and of the + * sealed trait get `$`, a case object without a class sibling does not. */ + ASSERT_TRUE(has_def_qn(r, "t.Ast.Add")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Add$")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Sub")); + ASSERT_FALSE(has_def_qn(r, "t.Ast.Sub$")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Neg")); + ASSERT_FALSE(has_def_qn(r, "t.Ast.Neg$")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Op")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Op$")); + ASSERT_TRUE(has_def_qn(r, "t.Ast.Op$.all")); + + cbm_free_result(r); + PASS(); +} + TEST(extract_scala_import_selectors_and_aliases) { CBMFileResult *r = extract("import foo.Direct\n" "import foo.{Selected, Original => Alias, Hidden => _, _}\n" @@ -9309,6 +9426,9 @@ SUITE(extraction) { RUN_TEST(extract_python_bare_call_flags_locally_bound_callee); RUN_TEST(extract_python_bare_call_flag_is_depth_independent); RUN_TEST(extract_ts_member_call_flags_is_method); + RUN_TEST(extract_scala_companion_owners_are_distinct); + RUN_TEST(extract_scala_trait_companion_and_standalone_object); + RUN_TEST(extract_scala_companion_detection_is_scoped); RUN_TEST(extract_scala_import_selectors_and_aliases); RUN_TEST(extract_scala_package_namespace); RUN_TEST(extract_scala_chained_package_clauses); diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c index dac29a552..d8606efe3 100644 --- a/tests/test_pipeline.c +++ b/tests/test_pipeline.c @@ -7390,6 +7390,101 @@ TEST(pipeline_scala_index_built_only_for_scala_importers) { PASS(); } +static void write_scala_companion_fixture(const char *tmp) { + write_temp_file( + tmp, "rational.scala", + "package numeric\n" + "class Rational(n: Int, d: Int) { def reciprocal: Rational = Rational.make(d, n) }\n" + "object Rational {\n" + " def make(n: Int, d: Int): Rational = new Rational(n, d)\n" + " def fromInt(n: Int): Rational = make(n, 1)\n" + "}\n"); + write_temp_file(tmp, "factory_user.scala", + "package app\n" + "import numeric.Rational\n" + "object FactoryUser { def half: Rational = Rational.make(1, 2) }\n"); + write_temp_file(tmp, "member_user.scala", + "package app\n" + "import numeric.Rational.fromInt\n" + "object MemberUser { def two: Rational = fromInt(2) }\n"); +} + +static bool scala_companion_edges_are_exact(cbm_store_t *s, const char *project) { + return cross_file_call_to_owner_has_strategy(s, project, "half", "make", "Rational$.make", + "import_map") && + cross_file_call_to_owner_has_strategy(s, project, "two", "fromInt", "Rational$.fromInt", + "import_map"); +} + +/* A class with a companion object is one importable name. `import pkg.Foo` + * must not fail closed as ambiguous now that class and companion have + * distinct QNs, `Foo.factory()` must bind into the companion (`Foo$`), and + * `import pkg.Foo.member` must bind the companion member. */ +TEST(pipeline_scala_companion_import_and_factory_call) { + char tmp[256]; + snprintf(tmp, sizeof(tmp), "/tmp/cbm_scala_companion_XXXXXX"); + if (!cbm_mkdtemp(tmp)) { + FAIL("tmpdir"); + } + write_scala_companion_fixture(tmp); + + char db_path[512]; + snprintf(db_path, sizeof(db_path), "%s/scala_companion.db", tmp); + cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL); + ASSERT_NOT_NULL(p); + ASSERT_EQ(cbm_pipeline_run(p), 0); + cbm_store_t *s = cbm_store_open_path(db_path); + ASSERT_NOT_NULL(s); + ASSERT_TRUE(scala_companion_edges_are_exact(s, cbm_pipeline_project_name(p))); + + cbm_store_close(s); + cbm_pipeline_free(p); + th_rmtree(tmp); + PASS(); +} + +/* The parallel route registers definitions through its own serial registry + * build; the companion owner must be recorded there too, or `Rational.make` + * falls back to the class on a 4-worker index. */ +TEST(pipeline_scala_companion_parallel_import_and_factory_call) { + char tmp[256]; + snprintf(tmp, sizeof(tmp), "/tmp/cbm_scala_companion_par_XXXXXX"); + if (!cbm_mkdtemp(tmp)) { + FAIL("tmpdir"); + } + write_scala_companion_fixture(tmp); + for (int i = 0; i < 52; i++) { + char name[64]; + char body[128]; + snprintf(name, sizeof(name), "companion_filler%d.scala", i); + snprintf(body, sizeof(body), "object CompanionFiller%d { def value: Int = %d }\n", i, i); + write_temp_file(tmp, name, body); + } + + char *old_workers = getenv("CBM_WORKERS"); + char *saved = old_workers ? strdup(old_workers) : NULL; + cbm_setenv("CBM_WORKERS", "4", 1); + char db_path[512]; + snprintf(db_path, sizeof(db_path), "%s/scala_companion_par.db", tmp); + cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL); + ASSERT_NOT_NULL(p); + ASSERT_EQ(cbm_pipeline_run(p), 0); + cbm_store_t *s = cbm_store_open_path(db_path); + ASSERT_NOT_NULL(s); + ASSERT_TRUE(scala_companion_edges_are_exact(s, cbm_pipeline_project_name(p))); + + cbm_store_close(s); + cbm_pipeline_free(p); + if (saved) { + cbm_setenv("CBM_WORKERS", saved, 1); + free(saved); + } else { + cbm_unsetenv("CBM_WORKERS"); + } + th_rmtree(tmp); + PASS(); +} + TEST(pipeline_scala_import_alias_parallel_resolves_exact_method) { char tmp[256]; snprintf(tmp, sizeof(tmp), "/tmp/cbm_scala_alias_par_XXXXXX"); @@ -17879,6 +17974,8 @@ SUITE(pipeline) { RUN_TEST(pipeline_scala_import_alias_resolves_exact_method); RUN_TEST(pipeline_scala_member_import_in_split_package_binds_owner_member); RUN_TEST(pipeline_scala_index_built_only_for_scala_importers); + RUN_TEST(pipeline_scala_companion_import_and_factory_call); + RUN_TEST(pipeline_scala_companion_parallel_import_and_factory_call); RUN_TEST(pipeline_scala_import_alias_parallel_resolves_exact_method); RUN_TEST(pipeline_parallel_python_cross_only_dunder_gets_synthetic_carrier); RUN_TEST(pipeline_parallel_rust_cross_only_macro_hidden_gets_synthetic_carrier); diff --git a/tests/test_registry.c b/tests/test_registry.c index 278e97d9b..590f8023d 100644 --- a/tests/test_registry.c +++ b/tests/test_registry.c @@ -1288,6 +1288,113 @@ TEST(resolve_import_map_alias_with_suffix_hits_method) { PASS(); } +/* Scala companion objects carry a `$` suffix on their QN. A call written on + * the class name (`Rational.make`) must reach the companion member through + * every strategy that compares owner segments: import_map, same_module and + * the qualified-tail disambiguator. Non-`$` QNs are matched exactly as before. */ +TEST(resolve_scala_companion_owner_matches_class_name) { + cbm_registry_t *r = cbm_registry_new(); + const char *f = "num/Rational.scala"; + cbm_registry_add_def(r, "Rational", "proj.num.Rational.Rational", "Class", f); + cbm_registry_add_def(r, "Rational", "proj.num.Rational.Rational$", "Class", f); + cbm_registry_add_def(r, "make", "proj.num.Rational.Rational$.make", "Method", f); + cbm_registry_add_def(r, "make", "proj.other.Builder.Builder.make", "Method", + "other/Builder.scala"); + + /* import_map: the import binds the class; the method lives in Rational$ */ + const char *keys[] = {"Rational"}; + const char *vals[] = {"proj.num.Rational.Rational"}; + cbm_resolution_t res = cbm_registry_resolve(r, "Rational.make", "proj.app.Main", keys, vals, 1); + ASSERT_STR_EQ(res.qualified_name, "proj.num.Rational.Rational$.make"); + ASSERT_STR_EQ(res.strategy, "import_map"); + + /* same_module: called from the declaring file */ + res = cbm_registry_resolve(r, "Rational.make", "proj.num.Rational", NULL, NULL, 0); + ASSERT_STR_EQ(res.qualified_name, "proj.num.Rational.Rational$.make"); + ASSERT_STR_EQ(res.strategy, "same_module"); + + /* qualified_suffix: two `make` candidates, the owner segment picks Rational$ */ + res = cbm_registry_resolve(r, "Rational.make", "proj.elsewhere.X", NULL, NULL, 0); + ASSERT_STR_EQ(res.qualified_name, "proj.num.Rational.Rational$.make"); + ASSERT_STR_EQ(res.strategy, "qualified_suffix"); + + /* A plain owner does not match a `$`-less candidate any more loosely. */ + res = cbm_registry_resolve(r, "Builder.make", "proj.elsewhere.X", NULL, NULL, 0); + ASSERT_STR_EQ(res.qualified_name, "proj.other.Builder.Builder.make"); + ASSERT_STR_EQ(res.strategy, "qualified_suffix"); + res = cbm_registry_resolve(r, "Other.make", "proj.elsewhere.X", NULL, NULL, 0); + ASSERT(res.strategy == NULL || strcmp(res.strategy, "qualified_suffix") != 0); + cbm_registry_free(r); + PASS(); +} + +/* The companion rules are keyed on the registry's own record of Scala + * companions, not on the `$` character: the plain cbm_registry_add records + * nothing, and neither does cbm_registry_add_def for a non-Scala file. */ +TEST(resolve_dollar_owner_is_companion_only_when_registered_from_scala) { + cbm_registry_t *r = cbm_registry_new(); + cbm_registry_add(r, "Rational", "proj.num.Rational.Rational", "Class"); + cbm_registry_add(r, "Rational", "proj.num.Rational.Rational$", "Class"); + cbm_registry_add(r, "make", "proj.num.Rational.Rational$.make", "Method"); + cbm_registry_add(r, "make", "proj.other.Builder.Builder.make", "Method"); + const char *keys[] = {"Rational"}; + const char *vals[] = {"proj.num.Rational.Rational"}; + cbm_resolution_t res = cbm_registry_resolve(r, "Rational.make", "proj.app.Main", keys, vals, 1); + ASSERT(res.strategy == NULL || strcmp(res.strategy, "import_map") != 0); + res = cbm_registry_resolve(r, "Rational.make", "proj.num.Rational", NULL, NULL, 0); + ASSERT(res.strategy == NULL || strcmp(res.strategy, "same_module") != 0); + res = cbm_registry_resolve(r, "Rational.make", "proj.elsewhere.X", NULL, NULL, 0); + ASSERT(res.strategy == NULL || strcmp(res.strategy, "qualified_suffix") != 0); + cbm_registry_free(r); + PASS(); +} + +/* `$` is a legal identifier character in Java and JS/TS (`user$` streams, + * `Foo$` helper classes), so a class `Foo$` next to `Foo` is an ordinary + * sibling there. With both registered from a Java or a TypeScript file, + * `Foo.m` resolves exactly as it does on a registry without any companion + * rule (the expectations below were taken from that registry): import_map, + * same_module and the qualified tail never reach the `$` owner's member, and + * the receiver chain does not admit it as a unique-name hit. */ +TEST(resolve_java_ts_dollar_class_next_to_class_is_unchanged) { + const char *files[] = {"a/Foo.java", "a/Foo.ts"}; + for (size_t fi = 0; fi < sizeof(files) / sizeof(files[0]); fi++) { + const char *f = files[fi]; + cbm_registry_t *r = cbm_registry_new(); + cbm_registry_add_def(r, "Foo", "proj.a.Foo", "Class", f); + cbm_registry_add_def(r, "Foo$", "proj.a.Foo$", "Class", f); + cbm_registry_add_def(r, "m", "proj.a.Foo$.m", "Method", f); + cbm_registry_add_def(r, "m", "proj.b.Bar.m", "Method", "b/Bar.java"); + + const char *keys[] = {"Foo"}; + const char *vals[] = {"proj.a.Foo"}; + /* Without companion rules the import path gives up and the bare-name + * scorer picks the import-nearest candidate at suffix_match confidence; + * that verdict must stay exactly as it is. */ + cbm_resolution_t res = cbm_registry_resolve(r, "Foo.m", "proj.app.Main", keys, vals, 1); + ASSERT_STR_EQ(res.strategy, "suffix_match"); + ASSERT_STR_EQ(res.qualified_name, "proj.a.Foo$.m"); + + res = cbm_registry_resolve(r, "Foo.m", "proj.a", NULL, NULL, 0); + ASSERT_NULL(res.strategy); + + res = cbm_registry_resolve(r, "Foo.m", "proj.elsewhere.X", NULL, NULL, 0); + ASSERT_NULL(res.strategy); + cbm_registry_free(r); + + /* Unique candidate: the receiver chain `Foo` must not be read as the + * ancestor `Foo$`. */ + r = cbm_registry_new(); + cbm_registry_add_def(r, "Foo", "proj.a.Foo", "Class", f); + cbm_registry_add_def(r, "Foo$", "proj.a.Foo$", "Class", f); + cbm_registry_add_def(r, "m", "proj.a.Foo$.m", "Method", f); + res = cbm_registry_resolve(r, "Foo.m", "proj.elsewhere.X", NULL, NULL, 0); + ASSERT_NULL(res.strategy); + cbm_registry_free(r); + } + PASS(); +} + SUITE(registry) { /* FQN */ RUN_TEST(fqn_simple); @@ -1325,6 +1432,9 @@ SUITE(registry) { RUN_TEST(resolve_import_map_bare_alias); RUN_TEST(resolve_import_map_aliased_from_import); RUN_TEST(resolve_import_map_alias_with_suffix_hits_method); + RUN_TEST(resolve_scala_companion_owner_matches_class_name); + RUN_TEST(resolve_dollar_owner_is_companion_only_when_registered_from_scala); + RUN_TEST(resolve_java_ts_dollar_class_next_to_class_is_unchanged); RUN_TEST(resolve_unique_name); RUN_TEST(resolve_unresolved); RUN_TEST(resolve_many_nodes);