From 0c1b45672bfee5525b76daacbda3f2d83b1e1b42 Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Thu, 12 Nov 2026 23:59:59 +0100 Subject: [PATCH 1/2] fix(registry): refuse name-only member calls nothing ties to the receiver #1897's receiver-chain gate judged only chains rooted in a type and let a lower-case root through ("a value whose type the chain does not show"), and the two suffix_match exits of resolve_multi_with_imports were never gated. So a member call no LSP had typed bound whatever project symbol shared its final name: kafka's `tp.topic()` on a TopicPartition bound ConsumerRecord.topic, gardener's `strings.Join` bound pkg/utils/net.Join, finagle's `Future.exception` a thrift service method. At the same name-only exits (unique_name, suffix_match, and now the import fallback) a CALLS resolve of a member call whose receiver is a value or a `new X(...)` constructor binds only on evidence that it means the chosen candidate: a chain segment names its ancestry word by word (`repo` / Repo, `cleanerManager` / LogCleanerManager, `otel_guard` / OtelGuard, the receiver of `verify(x)` / `when(x)`), it lives in the caller's own module, it is import-reachable, or, for a C/C++ call onto a struct member, the C member resolver decides it from the object's type. When another candidate is the field-type hint's own pick the call is left for that re-pick. Otherwise the guess is refused: a veto, never a re-pick. Self receivers (this, self, cls, super, base, this_, _this) and `::` paths are exempt; import_map, same_module, qualified_suffix and lsp_* are untouched. A type root is checked at the import fallback too. Scope by measured language (share of refused edges that were wrong, hand- read seeded samples): whole for Go (50/50), C++ (25/26), C#, Lua, Tcl, Groovy and C; suffix_match picks only for Scala (kafka 29/30, finagle on #2360 28/30). Left out: Java (suffix_match-only still 47/60), Kotlin (unmeasured), Ruby (3/15), and Python, the JS/TS family, Perl and PHP, whose own guards run after resolution and keep their HTTP classification. kafka 4.0.0 loses 5,189 CALLS (Scala 5,183) and 620 fake async routes, finagle on #2360 6,302 Scala CALLS and 13 false HTTP_CALLS, gardener 339 Go CALLS, redis 15, otel 168. Deterministic; incremental matches full. The #2121 controls for value roots now give their candidate a value receiver's evidence (the caller's own module), which a type root would not admit. Signed-off-by: Martin Vogel --- src/pipeline/registry.c | 522 ++++++++++++++++++++++++++++++++++++++-- tests/test_pipeline.c | 170 ++++++++++++- tests/test_registry.c | 160 ++++++++++++ 3 files changed, 829 insertions(+), 23 deletions(-) diff --git a/src/pipeline/registry.c b/src/pipeline/registry.c index f4aa04c1a..c8e681a48 100644 --- a/src/pipeline/registry.c +++ b/src/pipeline/registry.c @@ -472,6 +472,7 @@ typedef struct { cbm_resolution_t res; size_t scope_len; /* raw and Python-scoped requests may share one file cache */ bool skip_members; /* a C call and a C reference to the same name share it too */ + bool calls_path; /* a call and a reference to the same name share it too */ bool cs_keywords; /* a C# call and a C# reference to the same name share it too */ bool caller_test; /* a test and a production function of one file share it too */ } resolve_cache_entry_t; @@ -2405,10 +2406,11 @@ static bool cs_type_keyword_root(const char *dotted) { * appears somewhere in the chain. Calendar.utcGregorian.startOfDayUTC resolving * to AuthDTOs.Calendar.startOfDayUTC passes, because Calendar is in the chain. * - * Only an upper-case first segment is guarded. A lower-case root names a value - * (vm.load, http.Get, os.path.join) whose declared type the chain does not - * show, so the chain proves nothing there and the call passes through - * unchanged. A callee with no separator passes through as well. + * Only an upper-case first segment is guarded here. A lower-case root names a + * value (vm.load, http.Get, os.path.join) whose declared type the chain does + * not show, so the chain proves nothing there and the call passes through + * unchanged; on the CALLS path value_receiver_blind judges it instead. A + * callee with no separator passes through as well. * * One exception, for C# CALLS only (#2121): C# spells its built-in types as * lower-case keywords, so in `int.TryParse` or `string.Join` the root names a @@ -2497,6 +2499,470 @@ static bool receiver_chain_admits(const char *callee_name, const char *candidate return false; } +/* A receiver ROOT that is the calling object or its class hierarchy: a call + * through it names a member the caller's own class defines or inherits, so + * its receiver is not unknown. `this_` and `_this` are the local aliases of + * `this` generated protobuf code and compiled JavaScript use. Lower-case + * spellings only; an upper-case root (Self, Me, MyBase) stays with + * receiver_chain_admits as before. */ +static bool self_receiver_root(const char *root, size_t len) { + static const char *const roots[] = {"this", "self", "cls", "super", "base", + "$this", "$self", "this_", "_this"}; + for (size_t i = 0; i < sizeof(roots) / sizeof(roots[0]); i++) { + if (strlen(roots[i]) == len && strncmp(root, roots[i], len) == 0) { + return true; + } + } + static const char labelled[] = "this@"; /* Kotlin's this@Outer */ + return len > sizeof(labelled) - SKIP_ONE && strncmp(root, labelled, sizeof(labelled) - 1) == 0; +} + +static bool ascii_is_upper(char c) { + return c >= 'A' && c <= 'Z'; +} + +static char ascii_upper(char c) { + return c >= 'a' && c <= 'z' ? (char)(c - ('a' - 'A')) : c; +} + +static bool ascii_is_space(char c) { + return c == ' ' || c == '\t' || c == '\n' || c == '\r'; +} + +/* One receiver-chain segment, normalized for naming: surrounding whitespace, + * a constructor's `new `, a call's or an index's trailing `(...)` / `[...]` + * dropped, then a field prefix `_` / `m_` and a trailing number + * (`context2`). */ +typedef struct { + const char *text; + size_t len; +} gate_seg_t; + +static gate_seg_t gate_segment(const char *seg, size_t len) { + while (len > 0 && ascii_is_space(seg[0])) { + seg++; + len--; + } + static const char ctor[] = "new "; + if (len > sizeof(ctor) - SKIP_ONE && strncmp(seg, ctor, sizeof(ctor) - SKIP_ONE) == 0) { + seg += sizeof(ctor) - SKIP_ONE; + len -= sizeof(ctor) - SKIP_ONE; + } + for (size_t i = 0; i < len; i++) { + if (seg[i] == '(' || seg[i] == '[' || seg[i] == '<') { + len = i; + break; + } + } + while (len > 0 && ascii_is_space(seg[len - 1])) { + len--; + } + if (len > 0 && (seg[0] == '_' || seg[0] == '$')) { + seg++; + len--; + } + if (len > 2 && seg[0] == 'm' && seg[1] == '_') { + seg += 2; + len -= 2; + } + while (len > 1 && seg[len - 1] >= '0' && seg[len - 1] <= '9') { + len--; + } + return (gate_seg_t){seg, len}; +} + +/* A name split into words at `_` / `-` and at camel-case boundaries: + * fetchSize -> fetch|size, JSONParser -> JSON|parser, flagd_ui -> flagd|ui, + * otel_guard -> otel|guard. Compared case-blind, word by word. */ +enum { GATE_WORDS_MAX = 16, GATE_WORDS_MIN_CHARS = 3 }; + +typedef struct { + const char *at[GATE_WORDS_MAX]; + size_t len[GATE_WORDS_MAX]; + int n; + size_t chars; +} gate_words_t; + +static bool ascii_is_lower_or_digit(char c) { + return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'); +} + +static bool gate_word_sep(char c) { + return c == '_' || c == '-' || c == '$'; +} + +static void gate_words(const char *s, size_t len, gate_words_t *out) { + out->n = 0; + out->chars = 0; + size_t i = 0; + while (i < len && out->n < GATE_WORDS_MAX) { + while (i < len && gate_word_sep(s[i])) { + i++; + } + if (i >= len) { + break; + } + size_t start = i++; + while (i < len && !gate_word_sep(s[i])) { + bool acronym_end = ascii_is_upper(s[i - 1]) && i + SKIP_ONE < len && + s[i + SKIP_ONE] >= 'a' && s[i + SKIP_ONE] <= 'z'; + if (ascii_is_upper(s[i]) && (ascii_is_lower_or_digit(s[i - 1]) || acronym_end)) { + break; + } + i++; + } + out->at[out->n] = s + start; + out->len[out->n] = i - start; + out->chars += i - start; + out->n++; + } +} + +static bool gate_word_eq(const gate_words_t *a, int i, const gate_words_t *b, int j) { + if (a->len[i] != b->len[j]) { + return false; + } + for (size_t k = 0; k < a->len[i]; k++) { + if (ascii_upper(a->at[i][k]) != ascii_upper(b->at[j][k])) { + return false; + } + } + return true; +} + +static bool gate_words_equal(const gate_words_t *a, const gate_words_t *b) { + if (a->n == 0 || a->n != b->n) { + return false; + } + for (int i = 0; i < a->n; i++) { + if (!gate_word_eq(a, i, b, i)) { + return false; + } + } + return true; +} + +/* `a`'s words appear in order among `b`'s, ending on `b`'s last word: + * client in Mock|Client, transaction|manager in Transaction|State|Manager. */ +static bool gate_words_end_within(const gate_words_t *a, const gate_words_t *b) { + if (a->n == 0 || a->n > b->n || a->chars < GATE_WORDS_MIN_CHARS || + !gate_word_eq(a, a->n - 1, b, b->n - 1)) { + return false; + } + int j = b->n - 2; + for (int i = a->n - 2; i >= 0; i--) { + while (j >= 0 && !gate_word_eq(a, i, b, j)) { + j--; + } + if (j < 0) { + return false; + } + j--; + } + return true; +} + +/* A chain segment names an ancestry segment: the same words (`repo` / Repo, + * `store` / the store package, `otel_guard` / OtelGuard), or, for a type + * (upper-case) ancestry segment, the words of one end within the other's + * (`cleanerManager` / LogCleanerManager, `spyLogManager` / LogManager, + * `transactionManager` / TransactionStateManager, `client` / MockClient). */ +static bool segment_names(gate_seg_t s, const char *anc, size_t anc_len) { + if (s.len == 0 || anc_len == 0) { + return false; + } + gate_words_t sw; + gate_words_t aw; + gate_words(s.text, s.len, &sw); + gate_words(anc, anc_len, &aw); + if (gate_words_equal(&sw, &aw)) { + return true; + } + return ascii_is_upper(anc[0]) && + (gate_words_end_within(&sw, &aw) || gate_words_end_within(&aw, &sw)); +} + +/* The same words only: a type root (`Scheduler` for the scheduler module, + * `FlagdUi` for flagd_ui) names its type exactly. */ +static bool segment_names_exactly(gate_seg_t s, const char *anc, size_t anc_len) { + gate_words_t sw; + gate_words_t aw; + gate_words(s.text, s.len, &sw); + gate_words(anc, anc_len, &aw); + return gate_words_equal(&sw, &aw); +} + +/* Calls `visit` on each top-level segment of the receiver chain of + * `callee_name` (every segment before the final name; dots inside a call's + * arguments do not split), and on the receiver a mocking call hands in + * (`verify(consumer).commitAsync`, `when(rlm).fetch`, `doReturn(x).when(log)`: + * its first argument). Stops and returns true when `visit` does. */ +typedef bool (*gate_visit_fn)(gate_seg_t seg, const char *candidate_qn); + +static bool gate_visit_segment(const char *seg, size_t len, gate_visit_fn visit, + const char *candidate_qn) { + gate_seg_t s = gate_segment(seg, len); + if (s.len > 0 && visit(s, candidate_qn)) { + return true; + } + static const char *const mockers[] = {"verify", "when", "spy", "inOrder"}; + for (size_t m = 0; m < sizeof(mockers) / sizeof(mockers[0]); m++) { + size_t mlen = strlen(mockers[m]); + if (s.len != mlen || strncmp(s.text, mockers[m], mlen) != 0) { + continue; + } + const char *open = memchr(seg, '(', len); + if (!open) { + return false; + } + const char *arg = open + SKIP_ONE; + const char *end = arg; + while (end < seg + len && *end != ',' && *end != ')' && *end != '(') { + end++; + } + gate_seg_t a = gate_segment(arg, (size_t)(end - arg)); + return a.len > 0 && visit(a, candidate_qn); + } + return false; +} + +static bool gate_chain_any(const char *callee_name, gate_visit_fn visit, const char *candidate_qn) { + const char *seg = callee_name; + int depth = 0; + const char *last_top_dot = NULL; + for (const char *p = callee_name; *p; p++) { + if (*p == '(' || *p == '[') { + depth++; + } else if ((*p == ')' || *p == ']') && depth > 0) { + depth--; + } else if (*p == '.' && depth == 0) { + last_top_dot = p; + } + } + if (!last_top_dot) { + return false; + } + depth = 0; + for (const char *p = callee_name; p <= last_top_dot; p++) { + if (*p == '(' || *p == '[') { + depth++; + } else if ((*p == ')' || *p == ']') && depth > 0) { + depth--; + } else if (*p == '.' && depth == 0) { + if (gate_visit_segment(seg, (size_t)(p - seg), visit, candidate_qn)) { + return true; + } + seg = p + SKIP_ONE; + } + } + return false; +} + +/* Some ancestry segment of `candidate_qn` (everything before its final name) + * is named by `seg` (segment_names). */ +static bool seg_names_ancestry(gate_seg_t seg, const char *candidate_qn) { + const char *cand_last = strrchr(candidate_qn, '.'); + if (!cand_last || cand_last == candidate_qn) { + return false; + } + for (const char *anc = candidate_qn; anc < cand_last;) { + const char *anc_end = strchr(anc, '.'); + if (segment_names(seg, anc, (size_t)(anc_end - anc))) { + return true; + } + anc = anc_end + SKIP_ONE; + } + return false; +} + +/* As seg_names_ancestry, by segment_names_exactly. */ +static bool seg_names_ancestry_exactly(gate_seg_t seg, const char *candidate_qn) { + const char *cand_last = strrchr(candidate_qn, '.'); + if (!cand_last || cand_last == candidate_qn) { + return false; + } + for (const char *anc = candidate_qn; anc < cand_last;) { + const char *anc_end = strchr(anc, '.'); + if (segment_names_exactly(seg, anc, (size_t)(anc_end - anc))) { + return true; + } + anc = anc_end + SKIP_ONE; + } + return false; +} + +/* The field-type hint's own convention (try_field_type_hint, pass_parallel.c): + * the candidate's owner -- the segment before its final name -- is the + * receiver's name capitalized, or that name with an interface `I`. */ +static bool seg_is_hint_owner(gate_seg_t seg, const char *candidate_qn) { + const char *last = strrchr(candidate_qn, '.'); + if (!last || last == candidate_qn) { + return false; + } + const char *owner = last; + while (owner > candidate_qn && owner[-1] != '.') { + owner--; + } + size_t olen = (size_t)(last - owner); + if (olen == seg.len + SKIP_ONE && owner[0] == 'I') { + owner++; + olen--; + } + return olen == seg.len && seg.len > 0 && owner[0] == ascii_upper(seg.text[0]) && + strncmp(owner + SKIP_ONE, seg.text + SKIP_ONE, seg.len - SKIP_ONE) == 0; +} + +/* The first top-level dot of `callee_name` (not one inside a call's + * arguments), or NULL. */ +static const char *gate_first_dot(const char *callee_name) { + int depth = 0; + for (const char *p = callee_name; *p; p++) { + if (*p == '(' || *p == '[') { + depth++; + } else if ((*p == ')' || *p == ']') && depth > 0) { + depth--; + } else if (*p == '.' && depth == 0) { + return p; + } + } + return NULL; +} + +/* receiver_chain_admits judges a chain rooted in a TYPE and lets a lower-case + * root through: `tp.topic`, `vm.load`, `os.path.join` name a value or a module + * whose type the chain does not show. So on the CALLS path the name alone + * chose the target of every such member call the LSP had not typed: kafka's + * `tp.topic()` (a TopicPartition) bound ConsumerRecord.topic, the project's + * one `topic`, and `new DeleteRecordsPartitionResult().setErrorCode()` a + * builder of another type. This is the same gate for those calls, a value root + * or a constructor root (`new X(...)`, whose type the chain does name): the + * name-only exits (unique_name, suffix_match) bind such a call only on + * evidence that it means the chosen candidate -- + * - a chain segment names a segment of its ancestry (segment_names: `repo` + * for Repo, `store` for the store package, `cleanerManager` for + * LogCleanerManager, `new RaftContext.Builder()` for RaftContext.Builder); + * - it lives in the caller's own module (a class beside the caller, a nested + * class of its file); + * - it is reachable through the file's imports; + * - or, for a C or C++ call onto a struct member, the C member resolver + * decides it from the object's type (cbm_call_onto_field_policy). + * When another candidate is the field-type hint's own pick (its owner is the + * receiver's name capitalized) the call is left as before, for the parallel + * resolver to re-pick. Otherwise the guess is refused: a veto, never a + * re-pick, so a refused call stays unresolved instead of falling to the next + * name-only candidate. A self receiver (this, self, cls, super, base, $this, + * $self, this_, _this) is not unknown, and a `::` path (a Rust, C++ or PHP + * namespace) is not a receiver; both, and a type root, stay with + * receiver_chain_admits. Every strategy before these exits -- import_map, + * same_module, qualified_suffix and lsp_* -- is untouched. `lang` is the + * calling file's language (CALLS path only); a C# keyword root + * (`int.TryParse`) is a type there. */ +static bool value_receiver_blind(const cbm_registry_t *r, const qn_array_t *arr, + const char *callee_name, const char *chosen, const char *module_qn, + const char **import_vals, int import_count, CBMLanguage lang) { + const char *dot = gate_first_dot(callee_name); + const char *colons = strstr(callee_name, "::"); + if (!dot || (colons && colons < dot)) { + return false; /* bare name, or a :: path */ + } + const char *start = callee_name; + while (ascii_is_space(*start)) { + start++; + } + bool ctor = strncmp(start, "new ", 4) == 0; + if (!ctor && + (ascii_is_upper(start[0]) || (lang == CBM_LANG_CSHARP && cs_type_keyword_root(start)))) { + return false; /* a type root: receiver_chain_admits */ + } + gate_seg_t root = gate_segment(start, (size_t)(dot - start)); + if (!ctor && self_receiver_root(root.text, root.len)) { + return false; + } + if (import_count > 0 && is_import_reachable(chosen, import_vals, import_count)) { + return false; + } + size_t mlen = module_qn ? strlen(module_qn) : 0; + if (mlen > 0 && strncmp(chosen, module_qn, mlen) == 0 && chosen[mlen] == '.') { + return false; + } + if (lang == CBM_LANG_C || lang == CBM_LANG_CPP) { + const char *label = cbm_registry_label_of(r, chosen); + if (label && strcmp(label, "Field") == 0) { + return false; /* CBM_FIELD_CALL_BY_OWNER: the C member resolver decides */ + } + } + if (gate_chain_any(callee_name, seg_names_ancestry, chosen)) { + return false; + } + if (!ctor && lang != CBM_LANG_GO) { /* the hint never runs for Go */ + for (int i = 0; i < arr->count; i++) { + if (arr->items[i] != chosen && seg_is_hint_owner(root, arr->items[i])) { + return false; + } + } + } + return true; +} + +/* Where the receiver gate applies, by the calling file's language. Measured + * per language on hand-read samples of the edges it refuses (the share that + * were wrong): Go, C++, C#, Lua, Tcl, Groovy and the rest clear 85%, and the + * gate is whole there. Scala keeps its unique_name binds -- a project's one + * method of a name is mostly what an untyped receiver means there -- and only + * its suffix_match picks, a choice among same-named candidates by path + * proximity alone, are judged. Java stays below the bar even then: most of + * the right edges it would refuse sit behind a receiver that is a method's + * return value, which only the LSP can type. Kotlin is unmeasured. Ruby is + * duck-typed: its project-unique method names (`id.sampled?`) are mostly + * what the call means. All three are left out. So are the languages with + * their own member-call guard after resolution (Python and the JS/TS family: + * cbm_suppress_weak_member_match; Perl: cbm_perl_suppress_generic_match) and + * PHP's vendor- and namespace-aware resolution: the gate refused nothing + * there that their guard keeps, and refusing before resolution skipped the + * URL-argument HTTP classification their guards leave in place. */ +typedef enum { GATE_OFF, GATE_SUFFIX_ONLY, GATE_FULL } gate_scope_t; + +static gate_scope_t gate_scope(CBMLanguage lang) { + switch (lang) { + case CBM_LANG_JAVA: + case CBM_LANG_KOTLIN: + case CBM_LANG_RUBY: + case CBM_LANG_PYTHON: + case CBM_LANG_JAVASCRIPT: + case CBM_LANG_TYPESCRIPT: + case CBM_LANG_TSX: + case CBM_LANG_ARKTS: + case CBM_LANG_PERL: + case CBM_LANG_PHP: + case CBM_LANG_COUNT: /* not a CALLS resolve */ + return GATE_OFF; + case CBM_LANG_SCALA: + return GATE_SUFFIX_ONLY; + default: + return GATE_FULL; + } +} + +/* #1897's type check for the one name-only exit it did not cover: the + * fallback of resolve_multi_with_imports that ranks every candidate when the + * file's imports reach none (Scala `Future.exception` bound a Variable in a + * test, then MultiplexedFinagleService.exception). Refused when the chosen + * candidate's ancestry is not in the chain and no candidate's is by the same + * words (Elixir's `Scheduler` names the scheduler module) -- when one is, the + * pick is left for the parallel resolver's field-type hint. */ +static bool type_receiver_blind(const qn_array_t *arr, const char *callee_name, const char *chosen, + CBMLanguage lang) { + if (receiver_chain_admits(callee_name, chosen, lang)) { + return false; + } + for (int i = 0; i < arr->count; i++) { + if (gate_chain_any(callee_name, seg_names_ancestry_exactly, arr->items[i])) { + return false; + } + } + return true; +} + /* The same-name candidates without the members -- Field and Method nodes -- * in `view` (backed by the caller's buffers, at most REG_MAX_CANDIDATES * entries). Every entry of a non-empty bucket has its stored verdict and @@ -2522,12 +2988,14 @@ static void candidates_without_members(const cbm_registry_t *r, const qn_array_t } /* Strategies 3.5, 3 and 4 over one same-name candidate set. `lang` reaches - * receiver_chain_admits only (see there); `caller_test` reaches - * best_by_import_distance only. */ + * receiver_chain_admits and value_receiver_blind only (see there): it is the + * calling file's language on the CALLS path and CBM_LANG_COUNT on every other + * resolve. `caller_test` reaches best_by_import_distance only. */ static cbm_resolution_t resolve_name_candidates(const cbm_registry_t *r, const qn_array_t *arr, const char *callee_name, const char *module_qn, const char **import_vals, int import_count, CBMLanguage lang, bool caller_test) { + gate_scope_t gate = gate_scope(lang); /* Strategy 3.5: a qualified callee disambiguates among multiple same-name * candidates by full qualified tail, before bare-name scoring collapses * them onto a single winner. */ @@ -2538,9 +3006,13 @@ static cbm_resolution_t resolve_name_candidates(const cbm_registry_t *r, const q } } - /* Strategy 3: unique name */ + /* Strategy 3: unique name, judged by the receiver gate only where it is + * whole (gate_scope). */ if (arr->count == SKIP_ONE) { - if (!receiver_chain_admits(callee_name, arr->items[0], lang)) { + if (!receiver_chain_admits(callee_name, arr->items[0], lang) || + (gate == GATE_FULL && + value_receiver_blind(r, arr, callee_name, arr->items[0], module_qn, import_vals, + import_count, lang))) { return empty_result(); } double conf = CONF_UNIQUE_NAME; @@ -2553,14 +3025,27 @@ static cbm_resolution_t resolve_name_candidates(const cbm_registry_t *r, const q /* Strategy 4: multiple candidates */ if (import_vals && import_count > 0) { - return resolve_multi_with_imports(r, arr, module_qn, import_vals, import_count, - caller_test); + cbm_resolution_t res = + resolve_multi_with_imports(r, arr, module_qn, import_vals, import_count, caller_test); + /* An answer the file's imports do not reach came from the fallback + * that ranks every candidate: a name-only guess like the exits here, + * so on the CALLS path it passes the same receiver gates. */ + if (gate != GATE_OFF && res.qualified_name && res.qualified_name[0] && + !is_import_reachable(res.qualified_name, import_vals, import_count) && + (type_receiver_blind(arr, callee_name, res.qualified_name, lang) || + value_receiver_blind(r, arr, callee_name, res.qualified_name, module_qn, import_vals, + import_count, lang))) { + return empty_result(); + } + return res; } const char *best = best_by_import_distance(r, (const char **)arr->items, qn_test_flags(arr), qn_lang_flags(arr), arr->count, module_qn, caller_test); if (best) { - if (!receiver_chain_admits(callee_name, best, lang)) { + if (!receiver_chain_admits(callee_name, best, lang) || + (gate != GATE_OFF && + value_receiver_blind(r, arr, callee_name, best, module_qn, NULL, 0, lang))) { return empty_result(); } double conf = candidate_count_penalty(CONF_SUFFIX_MATCH, arr->count); @@ -2711,14 +3196,19 @@ static cbm_resolution_t registry_resolve_scoped(const cbm_registry_t *r, const c /* Per-file cache: same callee_name in N call sites → 1 chain walk * + N-1 O(1) hash hits. module_qn is constant per file so the - * cache key only needs callee_name. The chain reads call_lang only as - * "is it C#", so that one bit is all the entry has to record; the - * caller's test verdict can differ between two functions of one file. */ + * cache key only needs callee_name. Within one file call_lang is + * either the file's own language (a CALLS resolve, which the receiver + * gate and the C# keyword rule read) or CBM_LANG_COUNT, so "is it a + * CALLS resolve" pins it and is all the entry has to record of it + * beside the C# bit; the caller's test verdict can differ between two + * functions of one file. */ + bool calls_path = call_lang < CBM_LANG_COUNT; bool cs_keywords = call_lang == CBM_LANG_CSHARP; resolve_cache_entry_t *cached = _resolve_cache ? (resolve_cache_entry_t *)cbm_ht_get(_resolve_cache, callee_name) : NULL; if (cached && cached->scope_len == scope_len && cached->skip_members == skip_members && - cached->cs_keywords == cs_keywords && cached->caller_test == caller_test) + cached->calls_path == calls_path && cached->cs_keywords == cs_keywords && + cached->caller_test == caller_test) return cached->res; cbm_resolution_t res = @@ -2744,6 +3234,7 @@ static cbm_resolution_t registry_resolve_scoped(const cbm_registry_t *r, const c cached->res = res; cached->scope_len = scope_len; cached->skip_members = skip_members; + cached->calls_path = calls_path; cached->cs_keywords = cs_keywords; cached->caller_test = caller_test; } else if (_resolve_cache) { @@ -2752,6 +3243,7 @@ static cbm_resolution_t registry_resolve_scoped(const cbm_registry_t *r, const c e->res = res; e->scope_len = scope_len; e->skip_members = skip_members; + e->calls_path = calls_path; e->cs_keywords = cs_keywords; e->caller_test = caller_test; char *kdup = strdup(callee_name); diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c index 23fabaf56..ee33c8920 100644 --- a/tests/test_pipeline.c +++ b/tests/test_pipeline.c @@ -6907,6 +6907,156 @@ TEST(pipeline_registry_test_caller_ranks_test_helpers_full_and_incremental) { PASS(); } +/* The receiver gate's fixture: member calls whose receivers are lambda + * parameters nothing types, so every one of them reaches the registry's + * name-only exits, a C call through a function-pointer member, and a Go call + * on a standard-library package. */ +static const char GATE_CALLER_PATH[] = "streams/src/main/java/org/acme/streams/Assign.java"; +static const char GATE_CALLER[] = + "package org.acme.streams;\n\nimport java.util.List;\n\npublic class Assign {\n" + " long count(List tps, String t) {\n" + " return tps.stream().filter(tp -> tp.topic().equals(t)).count();\n }\n\n" + " long found(List repos) {\n" + " return repos.stream().filter(repo -> repo.find() > 0).count();\n }\n\n" + " long cleaned(List ms) {\n" + " return ms.stream().filter(cleanerManager -> cleanerManager.cleaningState() > 0)" + ".count();\n }\n\n" + " Object built() {\n return new RaftContext.Builder().withVoters();\n }\n\n" + " void named() {\n new External().setName(\"x\");\n }\n\n" + " long parts(List ts) {\n" + " return ts.stream().filter(topic -> topic.numPartitions() > 0).count();\n }\n" + "}\n"; +static const char GATE_C_PATH[] = "csrc/alloc.c"; +static const char GATE_C[] = "#include \n\n" + "typedef struct allocFuncs {\n void *(*mallocFn)(size_t);\n} " + "allocFuncs;\n\n" + "static void *plain_malloc(size_t n) { return NULL; }\n\n" + "allocFuncs allocFns = {plain_malloc};\n\n" + "void *my_malloc(size_t n) {\n return allocFns.mallocFn(n);\n}\n"; +static const char GATE_GO_PATH[] = "gosrc/cmd/main.go"; +static const char GATE_GO[] = "package cmd\n\nimport \"strings\"\n\n" + "func Run(parts []string) string {\n" + "\treturn strings.Join(parts, \",\")\n}\n"; +static const char GATE_RECORD[] = + "clients/src/main/java/org/acme/clients/consumer/ConsumerRecord.java"; +static const char GATE_PRODUCER[] = + "clients/src/main/java/org/acme/clients/producer/ProducerRecord.java"; +static const char GATE_REPO[] = "clients/src/main/java/org/acme/clients/store/Repo.java"; +static const char GATE_INDEX[] = "core/src/main/java/org/acme/index/Index.java"; +static const char GATE_LOGGER[] = "core/src/main/java/org/acme/log/Logger.java"; +static const char GATE_GO_JOIN[] = "gosrc/util/join.go"; +static const char GATE_SCALA_PATH[] = "streams/src/main/scala/org/acme/streams/Assigner.scala"; +static const char GATE_SCALA[] = + "package org.acme.streams\n\nobject Assigner {\n" + " def count(tps: Seq[AnyRef], t: String): Int = tps.count(tp => tp.topicName() == t)\n\n" + " def named(rs: Seq[AnyRef], t: String): Int =\n" + " rs.count(consumerRecordS => consumerRecordS.topicName() == t)\n}\n"; +static const char GATE_SCALA_CONSUMER[] = + "clients/src/main/scala/org/acme/clients/consumer/Record.scala"; +static const char GATE_SCALA_PRODUCER[] = + "clients/src/main/scala/org/acme/clients/producer/Record.scala"; + +static void gate_fixture(const char *tmp) { + write_temp_file(tmp, GATE_RECORD, + "package org.acme.clients.consumer;\n\npublic class ConsumerRecord {\n" + " public String topic() { return \"t\"; }\n}\n"); + write_temp_file(tmp, GATE_PRODUCER, + "package org.acme.clients.producer;\n\npublic class ProducerRecord {\n" + " public String topic() { return \"p\"; }\n}\n"); + write_temp_file(tmp, GATE_REPO, + "package org.acme.clients.store;\n\npublic class Repo {\n" + " public int find() { return 1; }\n}\n"); + write_temp_file(tmp, GATE_INDEX, + "package org.acme.index;\n\npublic class Index {\n" + " public int find() { return 2; }\n}\n"); + write_temp_file(tmp, "core/src/main/java/org/acme/log/LogCleanerManager.java", + "package org.acme.log;\n\npublic class LogCleanerManager {\n" + " public int cleaningState() { return 1; }\n}\n"); + write_temp_file(tmp, GATE_LOGGER, + "package org.acme.log;\n\npublic class Logger {\n" + " public void setName(String name) {}\n}\n"); + write_temp_file(tmp, "clients/src/main/java/org/acme/clients/RaftContext.java", + "package org.acme.clients;\n\npublic class RaftContext {\n" + " public static class Builder {\n" + " public Builder withVoters() { return this; }\n }\n}\n"); + write_temp_file(tmp, "streams/src/main/java/org/acme/streams/TopicInfo.java", + "package org.acme.streams;\n\npublic class TopicInfo {\n" + " public int numPartitions() { return 1; }\n}\n"); + write_temp_file(tmp, "go.mod", "module example.com/gate\n\ngo 1.22\n"); + write_temp_file(tmp, GATE_GO_JOIN, + "package util\n\nfunc Join(parts []string) string { return \"\" }\n"); + write_temp_file(tmp, GATE_SCALA_CONSUMER, + "package org.acme.clients.consumer\n\nclass ConsumerRecordS {\n" + " def topicName(): String = \"t\"\n}\n"); + write_temp_file(tmp, GATE_SCALA_PRODUCER, + "package org.acme.clients.producer\n\nclass ProducerRecordS {\n" + " def topicName(): String = \"p\"\n}\n"); + write_temp_file(tmp, GATE_CALLER_PATH, GATE_CALLER); + write_temp_file(tmp, GATE_SCALA_PATH, GATE_SCALA); + write_temp_file(tmp, GATE_C_PATH, GATE_C); + write_temp_file(tmp, GATE_GO_PATH, GATE_GO); +} + +/* A member call whose receiver type nothing resolved binds by name only on + * evidence that it means the candidate. Refused: Scala's proximity pick + * between two same-named methods for `tp.topicName()` (kafka's `tp.topic()` + * shape), and Go's `strings.Join` onto the project's one Join. Kept: the + * Scala receiver named after its owner, and a C call through a + * function-pointer member, which the C member resolver types. A veto, not a + * re-pick: a refused call stays unresolved. Full index (sequential and + * parallel) and both incremental routes. Java is left out of the gate (the + * registry test pins that); the Java files only share the fixture. */ +TEST(pipeline_registry_receiver_gate_full_and_incremental) { + static const struct { + const char *source; + const char *target; + const char *file; + const char *other; + const char *caller_path; + const char *caller; + int expect; + } cases[] = { + {"count", "topicName", GATE_SCALA_CONSUMER, GATE_SCALA_PRODUCER, GATE_SCALA_PATH, + GATE_SCALA, 0}, + {"named", "topicName", GATE_SCALA_CONSUMER, GATE_SCALA_PRODUCER, GATE_SCALA_PATH, + GATE_SCALA, 1}, + {"my_malloc", "mallocFn", GATE_C_PATH, GATE_INDEX, GATE_C_PATH, GATE_C, 1}, + {"Run", "Join", GATE_GO_JOIN, GATE_INDEX, GATE_GO_PATH, GATE_GO, 0}, + }; + for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + char tmp[256]; + snprintf(tmp, sizeof(tmp), "/tmp/cbm_gate_%zu_XXXXXX", i); + ASSERT_NOT_NULL(cbm_mkdtemp(tmp)); + gate_fixture(tmp); + rank_probe_t r; + rank_probe(tmp, cases[i].caller_path, cases[i].caller, cases[i].source, cases[i].target, + cases[i].file, cases[i].other, &r); + int par = -1; + int par_other = -1; + rank_probe_parallel(tmp, cases[i].source, cases[i].target, cases[i].file, cases[i].other, + &par, &par_other); + th_rmtree(tmp); + int want = cases[i].expect; + bool routes_ok = r.route[1] == CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR; +#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API + routes_ok = routes_ok && r.route[2] == CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL; +#endif + bool others_zero = r.other[0] == 0 && par_other == 0 && r.other[1] == 0 && r.other[2] == 0; + if (r.want[0] != want || par != want || !routes_ok || r.want[1] != want || + r.want[2] != want || !others_zero) { + char why[256]; + snprintf(why, sizeof(why), + "%s -> %s: want %d, got full %d parallel %d closure %d legacy %d, other " + "%d %d %d %d (routes %d %d)", + cases[i].source, cases[i].target, want, r.want[0], par, r.want[1], r.want[2], + r.other[0], par_other, r.other[1], r.other[2], (int)r.route[1], + (int)r.route[2]); + FAIL(why); + } + } + PASS(); +} + /* A semantic control file can change an unchanged source file's exact target. * Both target modules deliberately export the same short name: without the * tsconfig path mapping, project-wide unique-name fallback is ambiguous and @@ -28415,15 +28565,17 @@ TEST(registry_receiver_chain_keyword_list_gated_to_csharp_issue2121) { ASSERT_STR_EQ(r.strategy, "unique_name"); /* A Go call and the language-blind cbm_registry_resolve read a second - * keyword spelling as a value root as well. */ + * keyword spelling as a value root as well. A Go value receiver binds a + * name-only candidate on evidence (the receiver gate): here the candidate + * lives in the caller's own module, which a type root would not admit. */ cbm_registry_t *reg2 = cbm_registry_new(); - cbm_registry_add(reg2, "Join", "tmp.Decoys.Joiner.Join", "Method", false); + cbm_registry_add(reg2, "Join", "tmp.Demo.Joiner.Join", "Method", false); cbm_resolution_t r2 = cbm_registry_resolve_call_lang(reg2, "string.Join", "tmp.Demo", NULL, NULL, 0, CBM_LANG_GO); - ASSERT_STR_EQ(r2.qualified_name, "tmp.Decoys.Joiner.Join"); + ASSERT_STR_EQ(r2.qualified_name, "tmp.Demo.Joiner.Join"); ASSERT_STR_EQ(r2.strategy, "unique_name"); r2 = cbm_registry_resolve(reg2, "string.Join", "tmp.Demo", NULL, NULL, 0); - ASSERT_STR_EQ(r2.qualified_name, "tmp.Decoys.Joiner.Join"); + ASSERT_STR_EQ(r2.qualified_name, "tmp.Demo.Joiner.Join"); ASSERT_STR_EQ(r2.strategy, "unique_name"); cbm_registry_free(reg); @@ -28435,15 +28587,16 @@ TEST(registry_receiver_chain_keyword_list_gated_to_csharp_issue2121) { * `int`: in receiver position (`dynamic.Invoke(...)`) it is a variable that * happens to be called `dynamic`, whose type the chain does not show. In a C# * file it must stay an ordinary lower-case value root, like vm.load above, so - * the call still binds: the control that keeps the keyword list closed to - * words that can only name a type. */ + * the call still binds on a value receiver's evidence (here: a candidate in + * the caller's own module, which a type root would not admit): the control + * that keeps the keyword list closed to words that can only name a type. */ TEST(registry_receiver_chain_csharp_dynamic_variable_still_binds_issue2121) { cbm_registry_t *reg = cbm_registry_new(); - cbm_registry_add(reg, "Invoke", "tmp.Handlers.Handler.Invoke", "Method", false); + cbm_registry_add(reg, "Invoke", "tmp.Demo.Handler.Invoke", "Method", false); cbm_resolution_t r = cbm_registry_resolve_call_lang(reg, "dynamic.Invoke", "tmp.Demo", NULL, NULL, 0, CBM_LANG_CSHARP); - ASSERT_STR_EQ(r.qualified_name, "tmp.Handlers.Handler.Invoke"); + ASSERT_STR_EQ(r.qualified_name, "tmp.Demo.Handler.Invoke"); ASSERT_STR_EQ(r.strategy, "unique_name"); cbm_registry_free(reg); @@ -34779,6 +34932,7 @@ SUITE(pipeline) { RUN_TEST(pipeline_registry_ranks_test_file_variable_last_full_and_incremental); RUN_TEST(pipeline_registry_ranks_test_file_class_last_full_and_incremental); RUN_TEST(pipeline_registry_test_caller_ranks_test_helpers_full_and_incremental); + RUN_TEST(pipeline_registry_receiver_gate_full_and_incremental); RUN_TEST(pipeline_incremental_changed_target_invalidates_stale_inbound_call_reference); RUN_TEST(pipeline_incremental_parallel_registry_nodes_advance_shared_ids); #if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API diff --git a/tests/test_registry.c b/tests/test_registry.c index 10c9ef8e5..2b129ccce 100644 --- a/tests/test_registry.c +++ b/tests/test_registry.c @@ -1706,6 +1706,163 @@ TEST(c_bare_call_does_not_weigh_field_namesakes) { PASS(); } +static bool gate_bound(const cbm_resolution_t *res, const char *qn) { + return res->qualified_name && strcmp(res->qualified_name, qn) == 0; +} + +static bool gate_empty(const cbm_resolution_t *res) { + return !res->qualified_name || res->qualified_name[0] == '\0'; +} + +/* The receiver gate at the name-only CALLS exits judges a member call whose + * receiver is a value (`tp.topic`): it binds only on evidence that the call + * means the candidate, and is refused otherwise -- a veto, never a re-pick. + * The evidence rules are shown on C#, where unique_name binds are judged too. */ +TEST(registry_receiver_gate_judges_value_receivers) { + cbm_registry_t *r = cbm_registry_new(); + const char *mod = "proj.streams.src.main.java.org.acme.streams"; + const char *topic = "proj.clients.src.main.java.org.acme.clients.consumer.ConsumerRecord.topic"; + const char *clean = "proj.core.src.main.java.org.acme.log.LogCleanerManager.cleaningState"; + const char *with = "proj.clients.src.main.java.org.acme.clients.RaftContext.Builder.withVoters"; + const char *set_name = "proj.core.src.main.java.org.acme.log.Logger.setName"; + const char *parts = "proj.streams.src.main.java.org.acme.streams.TopicInfo.numPartitions"; + const char *guard = "proj.src.shipping.src.telemetry_conf.OtelGuard.shutdown"; + cbm_registry_add_lang(r, "topic", topic, "Method", CBM_LANG_CSHARP, false); + cbm_registry_add_lang(r, "cleaningState", clean, "Method", CBM_LANG_CSHARP, false); + cbm_registry_add_lang(r, "withVoters", with, "Method", CBM_LANG_CSHARP, false); + cbm_registry_add_lang(r, "setName", set_name, "Method", CBM_LANG_CSHARP, false); + cbm_registry_add_lang(r, "numPartitions", parts, "Method", CBM_LANG_CSHARP, false); + cbm_registry_add_lang(r, "shutdown", guard, "Method", CBM_LANG_RUST, false); + const CBMLanguage cs = CBM_LANG_CSHARP; + + /* Nothing ties `tp` to ConsumerRecord: refused on the CALLS path, while + * every other resolve keeps its answer. */ + cbm_resolution_t res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, cs); + ASSERT_TRUE(gate_empty(&res)); + res = cbm_registry_resolve_lang(r, "tp.topic", mod, NULL, NULL, 0, cs); + ASSERT_TRUE(gate_bound(&res, topic)); + /* The file imports the candidate's class: bound. */ + const char *vals[] = {"proj.clients.src.main.java.org.acme.clients.consumer.ConsumerRecord"}; + const char *keys[] = {"ConsumerRecord"}; + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, keys, vals, 1, cs); + ASSERT_TRUE(gate_bound(&res, topic)); + /* Self receivers are not unknown, protobuf's `this_` included. */ + res = cbm_registry_resolve_call_lang(r, "this.topic", mod, NULL, NULL, 0, cs); + ASSERT_TRUE(gate_bound(&res, topic)); + res = cbm_registry_resolve_call_lang(r, "this_.topic", mod, NULL, NULL, 0, CBM_LANG_CPP); + ASSERT_TRUE(gate_bound(&res, topic)); + res = + cbm_registry_resolve_call_lang(r, "self.items.topic", mod, NULL, NULL, 0, CBM_LANG_PYTHON); + ASSERT_TRUE(gate_bound(&res, topic)); + + /* A receiver named after the owner type, compared word by word: one name's + * words ending the other's, snake_case and a PHP sigil included. */ + res = cbm_registry_resolve_call_lang(r, "cleanerManager.cleaningState", mod, NULL, NULL, 0, cs); + ASSERT_TRUE(gate_bound(&res, clean)); + res = cbm_registry_resolve_call_lang(r, "spyLogCleanerManager2.cleaningState", mod, NULL, NULL, + 0, cs); + ASSERT_TRUE(gate_bound(&res, clean)); + res = + cbm_registry_resolve_call_lang(r, "anotherManagerX.cleaningState", mod, NULL, NULL, 0, cs); + ASSERT_TRUE(gate_empty(&res)); + res = cbm_registry_resolve_call_lang(r, "verify(cleanerManager).cleaningState", mod, NULL, NULL, + 0, cs); + ASSERT_TRUE(gate_bound(&res, clean)); + res = cbm_registry_resolve_call_lang(r, "otel_guard.shutdown", "proj.src.shipping.src.main", + NULL, NULL, 0, CBM_LANG_RUST); + ASSERT_TRUE(gate_bound(&res, guard)); + res = cbm_registry_resolve_call_lang(r, "$logger.setName", mod, NULL, NULL, 0, cs); + ASSERT_TRUE(gate_bound(&res, set_name)); + /* Languages with their own member-call guard after resolution are left + * to it. */ + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_TYPESCRIPT); + ASSERT_TRUE(gate_bound(&res, topic)); + /* A constructor root names its type. */ + res = cbm_registry_resolve_call_lang(r, "new RaftContext.Builder(voters, 1).withVoters", mod, + NULL, NULL, 0, cs); + ASSERT_TRUE(gate_bound(&res, with)); + res = cbm_registry_resolve_call_lang(r, "new External().setName", mod, NULL, NULL, 0, cs); + ASSERT_TRUE(gate_empty(&res)); + /* A candidate in the caller's own module. */ + res = cbm_registry_resolve_call_lang(r, "topic.numPartitions", mod, NULL, NULL, 0, cs); + ASSERT_TRUE(gate_bound(&res, parts)); + + /* C: a member call landing on a struct member is decided by the C member + * resolver from the object's type. */ + const char *c_mod = "proj.src.redis"; + cbm_registry_add_lang(r, "mallocFn", "proj.deps.hiredis.alloc.hiredisAllocFuncs.mallocFn", + "Field", CBM_LANG_C, false); + res = cbm_registry_resolve_call_lang(r, "hiredisAllocFns.mallocFn", c_mod, NULL, NULL, 0, + CBM_LANG_C); + ASSERT_TRUE(gate_bound(&res, "proj.deps.hiredis.alloc.hiredisAllocFuncs.mallocFn")); + cbm_registry_free(r); + PASS(); +} + +/* By measured language: Scala keeps its unique_name binds and has only its + * suffix_match picks judged; Java, Kotlin and Ruby are left out of the gate. */ +TEST(registry_receiver_gate_judges_only_suffix_picks_on_the_jvm) { + cbm_registry_t *r = cbm_registry_new(); + const char *mod = "proj.streams.src.main.scala.org.acme.streams"; + const char *consumer = + "proj.clients.src.main.java.org.acme.clients.consumer.ConsumerRecord.topic"; + const char *producer = + "proj.clients.src.main.java.org.acme.clients.producer.ProducerRecord.topic"; + cbm_registry_add_lang(r, "topic", consumer, "Method", CBM_LANG_JAVA, false); + /* One definition of the name: kept on the JVM, refused elsewhere. */ + cbm_resolution_t res = + cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_SCALA); + ASSERT_TRUE(gate_bound(&res, consumer)); + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_JAVA); + ASSERT_TRUE(gate_bound(&res, consumer)); + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_GO); + ASSERT_TRUE(gate_empty(&res)); + /* Two: Scala's proximity pick between them is refused unless the receiver + * names its owner; Java and Kotlin keep theirs. */ + cbm_registry_add_lang(r, "topic", producer, "Method", CBM_LANG_JAVA, false); + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_SCALA); + ASSERT_TRUE(gate_empty(&res)); + res = cbm_registry_resolve_call_lang(r, "consumerRecord.topic", mod, NULL, NULL, 0, + CBM_LANG_SCALA); + ASSERT_TRUE(gate_bound(&res, consumer)); + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_JAVA); + ASSERT_TRUE(gate_bound(&res, consumer)); + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_KOTLIN); + ASSERT_TRUE(gate_bound(&res, consumer)); + /* Ruby is left out too. */ + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_RUBY); + ASSERT_TRUE(gate_bound(&res, consumer)); + cbm_registry_free(r); + PASS(); +} + +/* A chain rooted in a TYPE (`Future.exception`) in a file with imports: no + * candidate is import-reachable, so the fallback ranks every candidate -- a + * name-only guess that now passes the same type check as the other exits. + * When some candidate's owner IS named, the pick is left alone for the + * parallel resolver's field-type hint. */ +TEST(registry_receiver_gate_judges_type_roots_at_import_fallback) { + cbm_registry_t *r = cbm_registry_new(); + const char *mod = "proj.finagle-core.src.main.scala.com.twitter.finagle.service"; + cbm_registry_add_lang(r, "exception", + "proj.finagle-thrift.src.main.scala.com.twitter.finagle.thrift." + "MultiplexedService.MultiplexedFinagleService.exception", + "Method", CBM_LANG_SCALA, false); + cbm_registry_add_lang( + r, "exception", "proj.finagle-mux.src.main.scala.com.twitter.finagle.mux.Message.exception", + "Method", CBM_LANG_SCALA, false); + const char *vals[] = {"proj.finagle-core.src.main.scala.com.twitter.finagle.Stack"}; + const char *keys[] = {"Stack"}; + cbm_resolution_t res = + cbm_registry_resolve_call_lang(r, "Future.exception", mod, keys, vals, 1, CBM_LANG_SCALA); + ASSERT_TRUE(gate_empty(&res)); + /* The reference resolver keeps its answer. */ + res = cbm_registry_resolve_lang(r, "Future.exception", mod, keys, vals, 1, CBM_LANG_SCALA); + ASSERT_FALSE(gate_empty(&res)); + cbm_registry_free(r); + PASS(); +} + TEST(call_onto_field_follows_language_and_shape) { /* A call the registry resolved onto a Field node. */ /* Another language: only the spelling connects them. */ @@ -3518,6 +3675,9 @@ SUITE(registry) { RUN_TEST(go_bare_ref_never_binds_field); RUN_TEST(go_bare_init_never_binds_init_namesake); RUN_TEST(c_bare_ref_never_binds_field); + RUN_TEST(registry_receiver_gate_judges_value_receivers); + RUN_TEST(registry_receiver_gate_judges_only_suffix_picks_on_the_jvm); + RUN_TEST(registry_receiver_gate_judges_type_roots_at_import_fallback); RUN_TEST(call_onto_field_follows_language_and_shape); RUN_TEST(c_bare_call_does_not_weigh_field_namesakes); RUN_TEST(c_arrow_member_call_is_c_family_and_arrow_only); From ff1fa7d55411d4c3af2bfc73ba0787e4d0fe4921 Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sat, 14 Nov 2026 00:14:04 +0100 Subject: [PATCH 2/2] fix(registry): gate only the languages measured above the bar The receiver gate now runs from an explicit per-language allowlist set by measurement instead of applying to every language not exempted: - full gate: Go, C++, C#, Lua, Tcl, Groovy, Elixir; - suffix_match-only: Scala, Rust, PHP; - off: everything else (Java, Kotlin, Swift, Ruby, Objective-C, Dart, C, Python, the JS/TS family, Perl, and every unmeasured language). Each language was measured on a pinned corpus with fresh seeds and hand-read samples of the refused CALLS; a language is gated only where the share of refused edges that were wrong is at least ~85% (Rust 88% for suffix_match, 80% for unique_name, so suffix-only; PHP 30/30 suffix, 60% unique; Elixir 30/30 and 24/25). Rust removes 4,503 false CALLS on meilisearch, PHP 938 on koel, Elixir 1,885 on plausible; off languages are byte-identical. Also new C/C++ evidence: a candidate whose owner type the caller's own module declares binds (`Widget w(n); w.get_id()`), which fixes the cpp/S3 lsp_resolution_probe case the full C++ gate refused in the previous commit. registry_receiver_gate_runs_by_measured_language replaces the JVM-only test and pins full, suffix-only and off languages, including an unmeasured one. Signed-off-by: Martin Vogel --- src/pipeline/registry.c | 87 +++++++++++++++++++++++++--------------- tests/test_pipeline.c | 9 +++-- tests/test_registry.c | 88 ++++++++++++++++++++++++++--------------- 3 files changed, 117 insertions(+), 67 deletions(-) diff --git a/src/pipeline/registry.c b/src/pipeline/registry.c index c8e681a48..73c2a4148 100644 --- a/src/pipeline/registry.c +++ b/src/pipeline/registry.c @@ -2844,8 +2844,10 @@ static const char *gate_first_dot(const char *callee_name) { * - it lives in the caller's own module (a class beside the caller, a nested * class of its file); * - it is reachable through the file's imports; - * - or, for a C or C++ call onto a struct member, the C member resolver - * decides it from the object's type (cbm_call_onto_field_policy). + * - or, in C and C++, the C member resolver decides a call onto a struct + * member from the object's type (cbm_call_onto_field_policy), and a + * candidate whose owner type the caller's own file declares + * (caller_declares_owner) is that type's member. * When another candidate is the field-type hint's own pick (its owner is the * receiver's name capitalized) the call is left as before, for the parallel * resolver to re-pick. Otherwise the guess is refused: a veto, never a @@ -2857,6 +2859,33 @@ static const char *gate_first_dot(const char *callee_name) { * same_module, qualified_suffix and lsp_* -- is untouched. `lang` is the * calling file's language (CALLS path only); a C# keyword root * (`int.TryParse`) is a type there. */ +/* C and C++: the caller's own file declares the candidate's owner type -- a + * class re-declared or declared ahead of its use in the calling translation + * unit (`class Widget { public: int get_id() const; };` above `Widget w(n); + * w.get_id()`). The C++ resolver types the receiver against that declaration, + * whose methods have no definition to bind; the candidate is the type's + * definition elsewhere. */ +static bool caller_declares_owner(const cbm_registry_t *r, const char *chosen, + const char *module_qn) { + const char *last = strrchr(chosen, '.'); + if (!last || !module_qn || !module_qn[0]) { + return false; + } + const char *owner = last; + while (owner > chosen && owner[-1] != '.') { + owner--; + } + if (owner == chosen || owner == last) { + return false; + } + char qn[CBM_SZ_1K]; + int n = snprintf(qn, sizeof(qn), "%s.%.*s", module_qn, (int)(last - owner), owner); + if (n < 0 || (size_t)n >= sizeof(qn)) { + return false; + } + return cbm_label_is_type_like(cbm_registry_label_of(r, qn)); +} + static bool value_receiver_blind(const cbm_registry_t *r, const qn_array_t *arr, const char *callee_name, const char *chosen, const char *module_qn, const char **import_vals, int import_count, CBMLanguage lang) { @@ -2890,6 +2919,9 @@ static bool value_receiver_blind(const cbm_registry_t *r, const qn_array_t *arr, if (label && strcmp(label, "Field") == 0) { return false; /* CBM_FIELD_CALL_BY_OWNER: the C member resolver decides */ } + if (caller_declares_owner(r, chosen, module_qn)) { + return false; + } } if (gate_chain_any(callee_name, seg_names_ancestry, chosen)) { return false; @@ -2904,42 +2936,35 @@ static bool value_receiver_blind(const cbm_registry_t *r, const qn_array_t *arr, return true; } -/* Where the receiver gate applies, by the calling file's language. Measured - * per language on hand-read samples of the edges it refuses (the share that - * were wrong): Go, C++, C#, Lua, Tcl, Groovy and the rest clear 85%, and the - * gate is whole there. Scala keeps its unique_name binds -- a project's one - * method of a name is mostly what an untyped receiver means there -- and only - * its suffix_match picks, a choice among same-named candidates by path - * proximity alone, are judged. Java stays below the bar even then: most of - * the right edges it would refuse sit behind a receiver that is a method's - * return value, which only the LSP can type. Kotlin is unmeasured. Ruby is - * duck-typed: its project-unique method names (`id.sampled?`) are mostly - * what the call means. All three are left out. So are the languages with - * their own member-call guard after resolution (Python and the JS/TS family: - * cbm_suppress_weak_member_match; Perl: cbm_perl_suppress_generic_match) and - * PHP's vendor- and namespace-aware resolution: the gate refused nothing - * there that their guard keeps, and refusing before resolution skipped the - * URL-argument HTTP classification their guards leave in place. */ +/* Where the receiver gate applies, by the calling file's language: an + * allowlist by measurement. A language is listed only where a hand-read + * seeded sample of the calls the gate refuses there, on a real corpus, was + * at least ~85% wrong. GATE_FULL: both its unique_name binds and its + * suffix_match picks cleared that. GATE_SUFFIX_ONLY: only the suffix_match + * picks (a choice among same-named candidates by path proximity alone) did, + * so the language keeps its unique_name binds. Every other language -- the + * ones measured below the bar (Java, Kotlin, Swift, Ruby, Objective-C), the + * ones where it refused nothing (C, Dart, and the languages with their own + * member-call guard after resolution: Python, the JS/TS family, Perl), and + * every language not measured -- keeps its name-only binds. */ typedef enum { GATE_OFF, GATE_SUFFIX_ONLY, GATE_FULL } gate_scope_t; static gate_scope_t gate_scope(CBMLanguage lang) { switch (lang) { - case CBM_LANG_JAVA: - case CBM_LANG_KOTLIN: - case CBM_LANG_RUBY: - case CBM_LANG_PYTHON: - case CBM_LANG_JAVASCRIPT: - case CBM_LANG_TYPESCRIPT: - case CBM_LANG_TSX: - case CBM_LANG_ARKTS: - case CBM_LANG_PERL: - case CBM_LANG_PHP: - case CBM_LANG_COUNT: /* not a CALLS resolve */ - return GATE_OFF; + case CBM_LANG_GO: + case CBM_LANG_CPP: + case CBM_LANG_CSHARP: + case CBM_LANG_LUA: + case CBM_LANG_TCL: + case CBM_LANG_GROOVY: + case CBM_LANG_ELIXIR: + return GATE_FULL; case CBM_LANG_SCALA: + case CBM_LANG_RUST: + case CBM_LANG_PHP: return GATE_SUFFIX_ONLY; default: - return GATE_FULL; + return GATE_OFF; } } diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c index ee33c8920..69f69710d 100644 --- a/tests/test_pipeline.c +++ b/tests/test_pipeline.c @@ -7002,10 +7002,11 @@ static void gate_fixture(const char *tmp) { * between two same-named methods for `tp.topicName()` (kafka's `tp.topic()` * shape), and Go's `strings.Join` onto the project's one Join. Kept: the * Scala receiver named after its owner, and a C call through a - * function-pointer member, which the C member resolver types. A veto, not a - * re-pick: a refused call stays unresolved. Full index (sequential and - * parallel) and both incremental routes. Java is left out of the gate (the - * registry test pins that); the Java files only share the fixture. */ + * function-pointer member, which the C member resolver types (C is off the + * gate's allowlist). A veto, not a re-pick: a refused call stays unresolved. + * Full index (sequential and parallel) and both incremental routes. Java is + * off the allowlist too (the registry test pins every language's scope); the + * Java files only share the fixture. */ TEST(pipeline_registry_receiver_gate_full_and_incremental) { static const struct { const char *source; diff --git a/tests/test_registry.c b/tests/test_registry.c index 2b129ccce..2baa5819a 100644 --- a/tests/test_registry.c +++ b/tests/test_registry.c @@ -1732,7 +1732,7 @@ TEST(registry_receiver_gate_judges_value_receivers) { cbm_registry_add_lang(r, "withVoters", with, "Method", CBM_LANG_CSHARP, false); cbm_registry_add_lang(r, "setName", set_name, "Method", CBM_LANG_CSHARP, false); cbm_registry_add_lang(r, "numPartitions", parts, "Method", CBM_LANG_CSHARP, false); - cbm_registry_add_lang(r, "shutdown", guard, "Method", CBM_LANG_RUST, false); + cbm_registry_add_lang(r, "shutdown", guard, "Method", CBM_LANG_CPP, false); const CBMLanguage cs = CBM_LANG_CSHARP; /* Nothing ties `tp` to ConsumerRecord: refused on the CALLS path, while @@ -1769,7 +1769,7 @@ TEST(registry_receiver_gate_judges_value_receivers) { 0, cs); ASSERT_TRUE(gate_bound(&res, clean)); res = cbm_registry_resolve_call_lang(r, "otel_guard.shutdown", "proj.src.shipping.src.main", - NULL, NULL, 0, CBM_LANG_RUST); + NULL, NULL, 0, CBM_LANG_CPP); ASSERT_TRUE(gate_bound(&res, guard)); res = cbm_registry_resolve_call_lang(r, "$logger.setName", mod, NULL, NULL, 0, cs); ASSERT_TRUE(gate_bound(&res, set_name)); @@ -1787,51 +1787,75 @@ TEST(registry_receiver_gate_judges_value_receivers) { res = cbm_registry_resolve_call_lang(r, "topic.numPartitions", mod, NULL, NULL, 0, cs); ASSERT_TRUE(gate_bound(&res, parts)); - /* C: a member call landing on a struct member is decided by the C member - * resolver from the object's type. */ + /* C++ (and C): a member call landing on a struct member is decided by the C + * member resolver from the object's type. */ const char *c_mod = "proj.src.redis"; cbm_registry_add_lang(r, "mallocFn", "proj.deps.hiredis.alloc.hiredisAllocFuncs.mallocFn", - "Field", CBM_LANG_C, false); + "Field", CBM_LANG_CPP, false); res = cbm_registry_resolve_call_lang(r, "hiredisAllocFns.mallocFn", c_mod, NULL, NULL, 0, - CBM_LANG_C); + CBM_LANG_CPP); ASSERT_TRUE(gate_bound(&res, "proj.deps.hiredis.alloc.hiredisAllocFuncs.mallocFn")); + /* C++: a candidate whose owner type the caller's own file declares (a class + * re-declared above `Widget w(n); w.get_id()`); refused from a file that + * declares no Widget. */ + const char *get_id = "proj.src.widget.Widget.get_id"; + cbm_registry_add_lang(r, "get_id", get_id, "Method", CBM_LANG_CPP, false); + cbm_registry_add_lang(r, "Widget", "proj.src.main.Widget", "Class", CBM_LANG_CPP, false); + res = + cbm_registry_resolve_call_lang(r, "w.get_id", "proj.src.main", NULL, NULL, 0, CBM_LANG_CPP); + ASSERT_TRUE(gate_bound(&res, get_id)); + res = cbm_registry_resolve_call_lang(r, "w.get_id", "proj.src.other", NULL, NULL, 0, + CBM_LANG_CPP); + ASSERT_TRUE(gate_empty(&res)); cbm_registry_free(r); PASS(); } -/* By measured language: Scala keeps its unique_name binds and has only its - * suffix_match picks judged; Java, Kotlin and Ruby are left out of the gate. */ -TEST(registry_receiver_gate_judges_only_suffix_picks_on_the_jvm) { +/* The gate runs by measured language (an allowlist). Whole for Go and Elixir: + * a unique_name bind is judged too. Suffix-only for Scala, Rust and PHP: they + * keep their unique_name binds and only a suffix_match pick is judged. Off + * for every other language -- measured below the bar (Java, Kotlin, Swift, + * Ruby, Objective-C), refusing nothing (C, Dart), or not measured (Zig). */ +TEST(registry_receiver_gate_runs_by_measured_language) { cbm_registry_t *r = cbm_registry_new(); const char *mod = "proj.streams.src.main.scala.org.acme.streams"; const char *consumer = "proj.clients.src.main.java.org.acme.clients.consumer.ConsumerRecord.topic"; const char *producer = "proj.clients.src.main.java.org.acme.clients.producer.ProducerRecord.topic"; + static const CBMLanguage full[] = {CBM_LANG_GO, CBM_LANG_ELIXIR}; + static const CBMLanguage suffix_only[] = {CBM_LANG_SCALA, CBM_LANG_RUST, CBM_LANG_PHP}; + static const CBMLanguage off[] = {CBM_LANG_JAVA, CBM_LANG_KOTLIN, CBM_LANG_SWIFT, CBM_LANG_RUBY, + CBM_LANG_OBJC, CBM_LANG_C, CBM_LANG_DART, CBM_LANG_ZIG}; cbm_registry_add_lang(r, "topic", consumer, "Method", CBM_LANG_JAVA, false); - /* One definition of the name: kept on the JVM, refused elsewhere. */ - cbm_resolution_t res = - cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_SCALA); - ASSERT_TRUE(gate_bound(&res, consumer)); - res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_JAVA); - ASSERT_TRUE(gate_bound(&res, consumer)); - res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_GO); - ASSERT_TRUE(gate_empty(&res)); - /* Two: Scala's proximity pick between them is refused unless the receiver - * names its owner; Java and Kotlin keep theirs. */ + /* One definition of the name: refused only where the gate is whole. */ + cbm_resolution_t res; + for (size_t i = 0; i < sizeof(full) / sizeof(full[0]); i++) { + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, full[i]); + ASSERT_TRUE(gate_empty(&res)); + } + for (size_t i = 0; i < sizeof(suffix_only) / sizeof(suffix_only[0]); i++) { + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, suffix_only[i]); + ASSERT_TRUE(gate_bound(&res, consumer)); + } + for (size_t i = 0; i < sizeof(off) / sizeof(off[0]); i++) { + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, off[i]); + ASSERT_TRUE(gate_bound(&res, consumer)); + } + /* Two: the proximity pick between them is refused where the gate runs, + * unless the receiver names its owner; the other languages keep theirs. */ cbm_registry_add_lang(r, "topic", producer, "Method", CBM_LANG_JAVA, false); - res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_SCALA); - ASSERT_TRUE(gate_empty(&res)); - res = cbm_registry_resolve_call_lang(r, "consumerRecord.topic", mod, NULL, NULL, 0, - CBM_LANG_SCALA); - ASSERT_TRUE(gate_bound(&res, consumer)); - res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_JAVA); - ASSERT_TRUE(gate_bound(&res, consumer)); - res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_KOTLIN); - ASSERT_TRUE(gate_bound(&res, consumer)); - /* Ruby is left out too. */ - res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_RUBY); - ASSERT_TRUE(gate_bound(&res, consumer)); + for (size_t i = 0; i < sizeof(suffix_only) / sizeof(suffix_only[0]); i++) { + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, suffix_only[i]); + ASSERT_TRUE(gate_empty(&res)); + res = cbm_registry_resolve_call_lang(r, "consumerRecord.topic", mod, NULL, NULL, 0, + suffix_only[i]); + ASSERT_TRUE(gate_bound(&res, consumer)); + } + for (size_t i = 0; i < sizeof(off) / sizeof(off[0]); i++) { + res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, off[i]); + ASSERT_TRUE(gate_bound(&res, consumer)); + } cbm_registry_free(r); PASS(); } @@ -3676,7 +3700,7 @@ SUITE(registry) { RUN_TEST(go_bare_init_never_binds_init_namesake); RUN_TEST(c_bare_ref_never_binds_field); RUN_TEST(registry_receiver_gate_judges_value_receivers); - RUN_TEST(registry_receiver_gate_judges_only_suffix_picks_on_the_jvm); + RUN_TEST(registry_receiver_gate_runs_by_measured_language); RUN_TEST(registry_receiver_gate_judges_type_roots_at_import_fallback); RUN_TEST(call_onto_field_follows_language_and_shape); RUN_TEST(c_bare_call_does_not_weigh_field_namesakes);