Skip to content

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

fix(elixir): open a function scope for a guarded clause body

Chad198e211c3001
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.c
index e74801b47..c4891c8e0 100644
--- a/internal/cbm/extract_calls.c
+++ b/internal/cbm/extract_calls.c
@@ -725,17 +725,24 @@ static bool elixir_call_head_in(TSNode call, const char *source, const char *con
* definition name is recorded by another as a reference to that very name:
* - extract_defs.c, extract_elixir_func_def, unwraps it to NAME the
* definition. It and this file disagreeing mints the phantom above.
- * - extract_usages.c, is_elixir_def_binding, treats the whole first argument
- * as a binding occurrence rather than a reference, which is what keeps a
- * head suppressed here from re-emerging as a USAGE edge. It is deliberately
- * WIDER than the unwrap -- it covers the guard expression too -- and that
- * width is load-bearing: a suppression in one extractor of the unified walk
- * only REMOVES a phantom if the others also decline the node.
- * - extract_unified.c, compute_elixir_func_qn, resolves a def's QN for call
- * scope tracking and does NOT unwrap, so a guarded clause pushes no
- * function scope and its call edges are sourced from the File rather than
- * from the enclosing Function. Pre-existing, independent of the two above
- * (it decides edge SOURCE, not edge existence), and fixed separately.
+ * - extract_usages.c, is_elixir_def_binding, treats an identifier inside that
+ * head as a binding occurrence rather than a reference, which is what keeps
+ * a head suppressed here from re-emerging as a USAGE edge: a suppression in
+ * one extractor of the unified walk only REMOVES a phantom if the others
+ * also decline the node. The two files ask different questions of the same
+ * helper -- this one whether the node IS the head or a `when` wrapper above
+ * it, that one whether the node sits INSIDE the head -- so there is one
+ * definition of the head and they cannot drift apart. That containment
+ * stops at the head rather than covering the whole `when` operator, which
+ * is deliberate in the other direction: only the parameters being BOUND are
+ * excluded, and a parameter READ in the guard is a reference, exactly as
+ * the same read in the body is.
+ * - extract_unified.c, compute_elixir_func_qn, resolves a def's QN to open
+ * the function's call scope and reads the head through the same helper.
+ * Handed the `when` operator instead it returns NULL, no scope is pushed,
+ * and every call in a guarded body is sourced from the File rather than
+ * from the enclosing Function. That failure is independent of the two above
+ * because it decides edge SOURCE, not edge existence.
*
* Suppressing the head does not only delete edges; it moves metadata on edges
* that SURVIVE, and that is worth stating because a caller reading an edge's
@@ -800,7 +807,23 @@ static bool call_node_is_definition_container(CBMLanguage lang, TSNode node, con
if (lang == CBM_LANG_AGDA && strcmp(kind, "expr") == 0) {
return agda_expr_is_definition_role(node);
}
- return lang == CBM_LANG_ELIXIR && strcmp(kind, "call") == 0 &&
+ /* A guarded head is a `when` binary_operator, not a `call`, and Elixir's
+ * call node types include binary_operator -- so the head reaches this walk
+ * and must be able to answer that it is a definition. A paren-less clause
+ * (`def f when g`) has no inner call at all, so the operator node itself
+ * reaches extract_callee_name and, with no callee of its own, takes that
+ * function's last resort -- the first identifier child -- minting a phantom
+ * CALLS edge onto the very function being defined.
+ *
+ * That last resort overrides a deliberate NULL for every Elixir
+ * binary_operator extract_scripting_callee declines (`=`, `<-`, `->`, `\\`,
+ * `::`, `when`), so `def g(c \\ 2)` still emits a phantom `c` and
+ * `e = target(d)` a phantom `e`. That is the pre-existing behaviour for
+ * those operators and is untouched here: only a `when` operator is
+ * admitted, so an operator definition's own head (`def a + b`) stays an
+ * ordinary node, as it was before guards were handled at all. */
+ return lang == CBM_LANG_ELIXIR &&
+ (strcmp(kind, "call") == 0 || cbm_elixir_is_when_guard(node)) &&
elixir_call_is_definition_role(node, source);
}
diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c
index 086f501a0..68e0fbdcc 100644
--- a/internal/cbm/extract_unified.c
+++ b/internal/cbm/extract_unified.c
@@ -602,6 +602,11 @@ static const char *compute_elixir_func_qn(CBMExtractCtx *ctx, TSNode node) {
if (ts_node_is_null(first_arg)) {
return NULL;
}
+ /* A guard wraps the whole head in a `when` operator, so the head naming the
+ * function is its left operand. Left wrapped, this returns NULL and the def
+ * opens NO function scope: every call in a guarded body then sources to the
+ * FILE node and the function itself reports no outgoing edges. */
+ first_arg = cbm_elixir_def_head_unwrap_guard(first_arg);
const char *fk = ts_node_type(first_arg);
char *name = NULL;
if (strcmp(fk, "call") == 0 && ts_node_child_count(first_arg) > 0) {
diff --git a/internal/cbm/extract_usages.c b/internal/cbm/extract_usages.c
index f6b391315..57594c665 100644
--- a/internal/cbm/extract_usages.c
+++ b/internal/cbm/extract_usages.c
@@ -617,6 +617,11 @@ static bool is_elixir_def_binding(CBMExtractCtx *ctx, TSNode node) {
TSNode signature = ts_node_named_child_count(arguments) > 0
? ts_node_named_child(arguments, 0)
: arguments;
+ /* A guard makes that signature the whole `when` operator, which spans the
+ * guard expression as well as the head. Unwrapped to the head, only the
+ * parameters being BOUND are excluded here; a parameter READ inside the
+ * guard stays a usage, as the same read in the body already is. */
+ signature = cbm_elixir_def_head_unwrap_guard(signature);
return node_contains(head, node) || node_contains(signature, node);
}
return false;
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index 887457cde..1adca8bce 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -91,6 +91,47 @@ static int count_defs_named(CBMFileResult *r, const char *label, const char *nam
return count;
}
+/* Count calls to `callee` whose enclosing scope is exactly `scope_qn`. A call the
+ * scope walk failed to place carries the FILE or module QN here, so a bare count
+ * of the callee passes just as happily with every call homed on the file. */
+static int count_calls_from(CBMFileResult *r, const char *callee, const char *scope_qn) {
+ int count = 0;
+ for (int i = 0; i < r->calls.count; i++) {
+ if (r->calls.items[i].callee_name && r->calls.items[i].enclosing_func_qn &&
+ strcmp(r->calls.items[i].callee_name, callee) == 0 &&
+ strcmp(r->calls.items[i].enclosing_func_qn, scope_qn) == 0) {
+ count++;
+ }
+ }
+ return count;
+}
+
+/* Count usages of `ref` attributed to exactly `scope_qn`, and to any scope. A
+ * def head is a BINDING, not a usage, so the pair separates two different
+ * mistakes: too few rows means a read was swallowed by the binding, too many
+ * means a binding site was counted as a read. */
+static int count_usages_from(CBMFileResult *r, const char *ref, const char *scope_qn) {
+ int count = 0;
+ for (int i = 0; i < r->usages.count; i++) {
+ if (r->usages.items[i].ref_name && r->usages.items[i].enclosing_func_qn &&
+ strcmp(r->usages.items[i].ref_name, ref) == 0 &&
+ strcmp(r->usages.items[i].enclosing_func_qn, scope_qn) == 0) {
+ count++;
+ }
+ }
+ return count;
+}
+
+static int count_usages_named(CBMFileResult *r, const char *ref) {
+ int count = 0;
+ for (int i = 0; i < r->usages.count; i++) {
+ if (r->usages.items[i].ref_name && strcmp(r->usages.items[i].ref_name, ref) == 0) {
+ count++;
+ }
+ }
+ return count;
+}
+
static int count_calls_named(CBMFileResult *r, const char *callee) {
int count = 0;
for (int i = 0; i < r->calls.count; i++) {
@@ -1457,6 +1498,95 @@ TEST(extract_elixir_guarded_def_head) {
PASS();
}
+/* A guard hides the head from the scope walk and from the calls walk.
+ * compute_elixir_func_qn accepts only a `call` or an `identifier` as a def's
+ * first argument, so a guarded clause resolved no QN and opened NO function
+ * scope: every call in its body was attributed to the FILE node, the function
+ * reported no outgoing edges, and its callees gained an in-edge from the file
+ * instead of from their caller. A paren-less guarded clause (`def f when g`) is
+ * worse still: it has no inner call node at all, so the guard operator itself
+ * falls through to extract_callee_name's first-identifier last resort and mints
+ * a phantom CALLS edge naming the function being defined. */
+TEST(extract_elixir_guarded_def_head_scope) {
+ CBMFileResult *r = extract("defmodule Guards do\n"
+ " def target(x), do: x\n"
+ " def other(x), do: x\n"
+ " def unguarded_caller(a), do: target(a)\n"
+ " def guarded_caller(a) when is_binary(a) do\n"
+ " target(a)\n"
+ " other(a)\n"
+ " end\n"
+ " def bare_guarded when true, do: target(:ok)\n"
+ "end\n",
+ CBM_LANG_ELIXIR, "t", "guard_scope.ex");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+
+ /* A paren-less guarded head is a definition, never a call to itself. */
+ ASSERT_EQ(count_calls_named(r, "bare_guarded"), 0);
+
+ /* Both calls in the guarded body source to the guarded function, and the
+ * paren-less clause's body call to that clause. */
+ ASSERT_EQ(count_calls_from(r, "target", "t.guard_scope.guarded_caller"), 1);
+ ASSERT_EQ(count_calls_from(r, "other", "t.guard_scope.guarded_caller"), 1);
+ ASSERT_EQ(count_calls_from(r, "target", "t.guard_scope.bare_guarded"), 1);
+
+ /* Control on the path the fix does not touch: an unguarded clause still
+ * sources its body call to itself. This is NOT a leak detector -- the walk
+ * expires scopes by tree depth (pop_expired_scopes), so a sibling clause
+ * always unwinds the previous one before pushing its own and "pushed but
+ * never popped" is not a state this walk can reach. */
+ ASSERT_EQ(count_calls_from(r, "target", "t.guard_scope.unguarded_caller"), 1);
+
+ /* Nothing falls back to the file node. */
+ ASSERT_EQ(count_calls_from(r, "target", "t.guard_scope"), 0);
+ ASSERT_EQ(count_calls_from(r, "other", "t.guard_scope"), 0);
+
+ cbm_free_result(r);
+ PASS();
+}
+
+/* The half the calls and scope assertions above cannot see: the usages walk
+ * asks whether an identifier sits inside the def's SIGNATURE, and treats
+ * everything that does as part of the binding rather than as a reference. A
+ * guard makes that signature the whole `when` operator, which spans the guard
+ * expression too -- so `a` read by `is_binary(a)` was classified as its own
+ * binding and emitted no USAGE row at all. A guard is an expression, not a
+ * pattern: the parameters are bound by `guarded(a, b)` and every mention of one
+ * to its right is a read, exactly as the same mention in the body already is.
+ * Unwrapping the signature to the head restores those rows without admitting
+ * the binding sites themselves, which is why each name is asserted both within
+ * its scope and in total. */
+TEST(extract_elixir_guarded_def_head_guard_usages) {
+ CBMFileResult *r = extract("defmodule GuardUsage do\n"
+ " def plain(x), do: x\n"
+ " def guarded(a, b) when is_binary(a) and byte_size(a) > 3 do\n"
+ " b\n"
+ " end\n"
+ "end\n",
+ CBM_LANG_ELIXIR, "t", "guard_usage.ex");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+
+ /* Both guard reads of the parameter are usages of the guarded clause. */
+ ASSERT_EQ(count_usages_from(r, "a", "t.guard_usage.guarded"), 2);
+ /* ...and nothing else claims `a`: the `a` in `guarded(a, b)` binds it. */
+ ASSERT_EQ(count_usages_named(r, "a"), 2);
+
+ /* The body read is unchanged, and the head's `b` is still a binding --
+ * an unwrap that took the guard instead of the head would add both head
+ * parameters here. */
+ ASSERT_EQ(count_usages_from(r, "b", "t.guard_usage.guarded"), 1);
+ ASSERT_EQ(count_usages_named(r, "b"), 1);
+
+ /* Control: an unguarded clause was never affected either way. */
+ ASSERT_EQ(count_usages_from(r, "x", "t.guard_usage.plain"), 1);
+ ASSERT_EQ(count_usages_named(r, "x"), 1);
+
+ cbm_free_result(r);
+ PASS();
+}
+
/* Every clause of an Elixir function is its own `def` call, and an Elixir QN
* carries neither module nor arity, so all clauses compute one qualified name
* and collide on a single graph node. cbm_gbuf_upsert_node ranks a same-QN
@@ -9039,6 +9169,8 @@ SUITE(extraction) {
RUN_TEST(extract_elixir_declaration_head_mints_no_reference_of_any_kind);
RUN_TEST(extract_elixir_real_recursion_survives_head_suppression);
RUN_TEST(elixir_call_string_argument);
+ RUN_TEST(extract_elixir_guarded_def_head_scope);
+ RUN_TEST(extract_elixir_guarded_def_head_guard_usages);
RUN_TEST(haskell_function);
RUN_TEST(ocaml_function);
RUN_TEST(erlang_function);