Skip to content

Commit 198e211

Browse files
committed
fix(elixir): open a function scope for a guarded clause body
A guard wraps the whole head in a `when` binary_operator, and three walks that read a def's first argument were not expecting one. The three failures are independent, and none is an error at runtime: The unified walk resolves a def's QN to open the function's call scope, and compute_elixir_func_qn accepts only a `call` or an `identifier` there. Handed a binary_operator it returns NULL, no function scope is pushed, and every call in a guarded body is attributed to the File node. The function reports no outgoing edges at all, and each of its callees gains an in-edge from the file. So "what does this function call" answers empty for a guarded clause, and "who calls this" answers with a file. The calls walk tells a definition head apart from an invocation through cbm_elixir_def_head_is, which covers the head and the `when` operators above it. A paren-less guarded clause (`def bare_guarded when true, do: ...`) never reaches that check: call_node_is_definition_container only admitted a `call` node, and here the head is the operator itself. Elixir's call node types include binary_operator, so the operator reached extract_callee_name and, with no callee of its own, took that function's last resort, the first identifier child, minting a phantom CALLS edge onto the very function being defined. The usages walk asks whether an identifier sits inside the def's signature and treats everything that does as a binding site rather than a reference. A guard makes that signature the whole `when` operator, which spans the guard expression too, so a parameter read by the guard was classified as its own binding and emitted no USAGE row at all. A guard is an expression over parameters that `guarded(a, b)` has already bound, so every mention of one to its right is a read, exactly as the same mention in the body already is. Measured by indexing one 970-file Elixir lib tree with the binary built from the base, 5df8b04, and with this one, and diffing the `edges` rows out of the two SQLite stores: Counted as the cumulative effect of the first four commits against that base, because this commit's attribution change is only observable once guarded clauses have nodes to be attributed to: CALLS sourced from a Module node 11,061 -> 5,840 (-5,221) CALLS sourced from a Function node 42,659 -> 49,579 (+6,920) CALLS sourced from a File node 2,131 -> 1,269 (-862) Calls written inside a guarded body stop being attributed to the enclosing module and are attributed to the function that writes them. The File count falls rather than rises, because the guarded-def-heads commit earlier in this stack gives those functions a node to source from. Against this commit's own parent, 5,825 USAGE rows appear for parameters read inside a guard that had been classified as their own binding. Node counts are identical in the two stores: the change moves where an edge comes from and records reads that were dropped. This also closes the WRITES phantom disclosed in the guarded-def-heads commit, though the extraction-level row does not go away. `def bare_guarded when true` still records one CBMReadWrite with var_name "bare_guarded" and is_write true. Its scope changes: with a function scope now open, enclosing_func_qn is the guarded clause where it had been the enclosing module, so resolve_rw_edges finds src->id == tgt->id and drops the self-edge. Measured on that exact fixture: before, one row scoped to `t.store`; after, one row scoped to `t.store.bare_guarded`. Corpus WRITES at this commit is 12,098 against 11,220 on the base; the typespec commit at the end of this stack brings it to 11,207. The row is still wrong at extraction and would reappear as an edge the moment anything sourced it elsewhere; removing it means teaching the read/write extractor about `when`, which is a fourth walk and belongs in its own change. tests/test_extraction.c: two tests. extract_elixir_guarded_def_head_scope asserts call attribution by exact scope QN rather than by callee name, because a call the scope walk failed to place carries the FILE QN, and a bare count of the callee passes just as happily with every call homed on the file. The same test pins the file-node count at 0. extract_elixir_guarded_def_head_guard_usages asserts each name both within its scope and in total, so an unwrap that took the guard instead of the head (admitting the binding sites themselves) fails on the totals. Only a `when` operator is admitted to the definition-container check. The generic first-identifier last resort still overrides a deliberate NULL for every other Elixir binary_operator that extract_scripting_callee declines (`=`, `<-`, `->`, `\\`, `::`), so `def g(c \\ 2)` still emits a phantom `c` and `e = target(d)` a phantom `e`. That is pre-existing behaviour for those operators and is untouched here. Depends on the shared def-head helper and multi-clause span commits. Signed-off-by: Chad <4307099+BobbieBarker@users.noreply.github.com>
1 parent 77d4beb commit 198e211

4 files changed

Lines changed: 177 additions & 12 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -725,17 +725,24 @@ static bool elixir_call_head_in(TSNode call, const char *source, const char *con
725725
* definition name is recorded by another as a reference to that very name:
726726
* - extract_defs.c, extract_elixir_func_def, unwraps it to NAME the
727727
* definition. It and this file disagreeing mints the phantom above.
728-
* - extract_usages.c, is_elixir_def_binding, treats the whole first argument
729-
* as a binding occurrence rather than a reference, which is what keeps a
730-
* head suppressed here from re-emerging as a USAGE edge. It is deliberately
731-
* WIDER than the unwrap -- it covers the guard expression too -- and that
732-
* width is load-bearing: a suppression in one extractor of the unified walk
733-
* only REMOVES a phantom if the others also decline the node.
734-
* - extract_unified.c, compute_elixir_func_qn, resolves a def's QN for call
735-
* scope tracking and does NOT unwrap, so a guarded clause pushes no
736-
* function scope and its call edges are sourced from the File rather than
737-
* from the enclosing Function. Pre-existing, independent of the two above
738-
* (it decides edge SOURCE, not edge existence), and fixed separately.
728+
* - extract_usages.c, is_elixir_def_binding, treats an identifier inside that
729+
* head as a binding occurrence rather than a reference, which is what keeps
730+
* a head suppressed here from re-emerging as a USAGE edge: a suppression in
731+
* one extractor of the unified walk only REMOVES a phantom if the others
732+
* also decline the node. The two files ask different questions of the same
733+
* helper -- this one whether the node IS the head or a `when` wrapper above
734+
* it, that one whether the node sits INSIDE the head -- so there is one
735+
* definition of the head and they cannot drift apart. That containment
736+
* stops at the head rather than covering the whole `when` operator, which
737+
* is deliberate in the other direction: only the parameters being BOUND are
738+
* excluded, and a parameter READ in the guard is a reference, exactly as
739+
* the same read in the body is.
740+
* - extract_unified.c, compute_elixir_func_qn, resolves a def's QN to open
741+
* the function's call scope and reads the head through the same helper.
742+
* Handed the `when` operator instead it returns NULL, no scope is pushed,
743+
* and every call in a guarded body is sourced from the File rather than
744+
* from the enclosing Function. That failure is independent of the two above
745+
* because it decides edge SOURCE, not edge existence.
739746
*
740747
* Suppressing the head does not only delete edges; it moves metadata on edges
741748
* 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
800807
if (lang == CBM_LANG_AGDA && strcmp(kind, "expr") == 0) {
801808
return agda_expr_is_definition_role(node);
802809
}
803-
return lang == CBM_LANG_ELIXIR && strcmp(kind, "call") == 0 &&
810+
/* A guarded head is a `when` binary_operator, not a `call`, and Elixir's
811+
* call node types include binary_operator -- so the head reaches this walk
812+
* and must be able to answer that it is a definition. A paren-less clause
813+
* (`def f when g`) has no inner call at all, so the operator node itself
814+
* reaches extract_callee_name and, with no callee of its own, takes that
815+
* function's last resort -- the first identifier child -- minting a phantom
816+
* CALLS edge onto the very function being defined.
817+
*
818+
* That last resort overrides a deliberate NULL for every Elixir
819+
* binary_operator extract_scripting_callee declines (`=`, `<-`, `->`, `\\`,
820+
* `::`, `when`), so `def g(c \\ 2)` still emits a phantom `c` and
821+
* `e = target(d)` a phantom `e`. That is the pre-existing behaviour for
822+
* those operators and is untouched here: only a `when` operator is
823+
* admitted, so an operator definition's own head (`def a + b`) stays an
824+
* ordinary node, as it was before guards were handled at all. */
825+
return lang == CBM_LANG_ELIXIR &&
826+
(strcmp(kind, "call") == 0 || cbm_elixir_is_when_guard(node)) &&
804827
elixir_call_is_definition_role(node, source);
805828
}
806829

‎internal/cbm/extract_unified.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,11 @@ static const char *compute_elixir_func_qn(CBMExtractCtx *ctx, TSNode node) {
602602
if (ts_node_is_null(first_arg)) {
603603
return NULL;
604604
}
605+
/* A guard wraps the whole head in a `when` operator, so the head naming the
606+
* function is its left operand. Left wrapped, this returns NULL and the def
607+
* opens NO function scope: every call in a guarded body then sources to the
608+
* FILE node and the function itself reports no outgoing edges. */
609+
first_arg = cbm_elixir_def_head_unwrap_guard(first_arg);
605610
const char *fk = ts_node_type(first_arg);
606611
char *name = NULL;
607612
if (strcmp(fk, "call") == 0 && ts_node_child_count(first_arg) > 0) {

‎internal/cbm/extract_usages.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,11 @@ static bool is_elixir_def_binding(CBMExtractCtx *ctx, TSNode node) {
617617
TSNode signature = ts_node_named_child_count(arguments) > 0
618618
? ts_node_named_child(arguments, 0)
619619
: arguments;
620+
/* A guard makes that signature the whole `when` operator, which spans the
621+
* guard expression as well as the head. Unwrapped to the head, only the
622+
* parameters being BOUND are excluded here; a parameter READ inside the
623+
* guard stays a usage, as the same read in the body already is. */
624+
signature = cbm_elixir_def_head_unwrap_guard(signature);
620625
return node_contains(head, node) || node_contains(signature, node);
621626
}
622627
return false;

‎tests/test_extraction.c‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,47 @@ static int count_defs_named(CBMFileResult *r, const char *label, const char *nam
9191
return count;
9292
}
9393

94+
/* Count calls to `callee` whose enclosing scope is exactly `scope_qn`. A call the
95+
* scope walk failed to place carries the FILE or module QN here, so a bare count
96+
* of the callee passes just as happily with every call homed on the file. */
97+
static int count_calls_from(CBMFileResult *r, const char *callee, const char *scope_qn) {
98+
int count = 0;
99+
for (int i = 0; i < r->calls.count; i++) {
100+
if (r->calls.items[i].callee_name && r->calls.items[i].enclosing_func_qn &&
101+
strcmp(r->calls.items[i].callee_name, callee) == 0 &&
102+
strcmp(r->calls.items[i].enclosing_func_qn, scope_qn) == 0) {
103+
count++;
104+
}
105+
}
106+
return count;
107+
}
108+
109+
/* Count usages of `ref` attributed to exactly `scope_qn`, and to any scope. A
110+
* def head is a BINDING, not a usage, so the pair separates two different
111+
* mistakes: too few rows means a read was swallowed by the binding, too many
112+
* means a binding site was counted as a read. */
113+
static int count_usages_from(CBMFileResult *r, const char *ref, const char *scope_qn) {
114+
int count = 0;
115+
for (int i = 0; i < r->usages.count; i++) {
116+
if (r->usages.items[i].ref_name && r->usages.items[i].enclosing_func_qn &&
117+
strcmp(r->usages.items[i].ref_name, ref) == 0 &&
118+
strcmp(r->usages.items[i].enclosing_func_qn, scope_qn) == 0) {
119+
count++;
120+
}
121+
}
122+
return count;
123+
}
124+
125+
static int count_usages_named(CBMFileResult *r, const char *ref) {
126+
int count = 0;
127+
for (int i = 0; i < r->usages.count; i++) {
128+
if (r->usages.items[i].ref_name && strcmp(r->usages.items[i].ref_name, ref) == 0) {
129+
count++;
130+
}
131+
}
132+
return count;
133+
}
134+
94135
static int count_calls_named(CBMFileResult *r, const char *callee) {
95136
int count = 0;
96137
for (int i = 0; i < r->calls.count; i++) {
@@ -1457,6 +1498,95 @@ TEST(extract_elixir_guarded_def_head) {
14571498
PASS();
14581499
}
14591500

1501+
/* A guard hides the head from the scope walk and from the calls walk.
1502+
* compute_elixir_func_qn accepts only a `call` or an `identifier` as a def's
1503+
* first argument, so a guarded clause resolved no QN and opened NO function
1504+
* scope: every call in its body was attributed to the FILE node, the function
1505+
* reported no outgoing edges, and its callees gained an in-edge from the file
1506+
* instead of from their caller. A paren-less guarded clause (`def f when g`) is
1507+
* worse still: it has no inner call node at all, so the guard operator itself
1508+
* falls through to extract_callee_name's first-identifier last resort and mints
1509+
* a phantom CALLS edge naming the function being defined. */
1510+
TEST(extract_elixir_guarded_def_head_scope) {
1511+
CBMFileResult *r = extract("defmodule Guards do\n"
1512+
" def target(x), do: x\n"
1513+
" def other(x), do: x\n"
1514+
" def unguarded_caller(a), do: target(a)\n"
1515+
" def guarded_caller(a) when is_binary(a) do\n"
1516+
" target(a)\n"
1517+
" other(a)\n"
1518+
" end\n"
1519+
" def bare_guarded when true, do: target(:ok)\n"
1520+
"end\n",
1521+
CBM_LANG_ELIXIR, "t", "guard_scope.ex");
1522+
ASSERT_NOT_NULL(r);
1523+
ASSERT_FALSE(r->has_error);
1524+
1525+
/* A paren-less guarded head is a definition, never a call to itself. */
1526+
ASSERT_EQ(count_calls_named(r, "bare_guarded"), 0);
1527+
1528+
/* Both calls in the guarded body source to the guarded function, and the
1529+
* paren-less clause's body call to that clause. */
1530+
ASSERT_EQ(count_calls_from(r, "target", "t.guard_scope.guarded_caller"), 1);
1531+
ASSERT_EQ(count_calls_from(r, "other", "t.guard_scope.guarded_caller"), 1);
1532+
ASSERT_EQ(count_calls_from(r, "target", "t.guard_scope.bare_guarded"), 1);
1533+
1534+
/* Control on the path the fix does not touch: an unguarded clause still
1535+
* sources its body call to itself. This is NOT a leak detector -- the walk
1536+
* expires scopes by tree depth (pop_expired_scopes), so a sibling clause
1537+
* always unwinds the previous one before pushing its own and "pushed but
1538+
* never popped" is not a state this walk can reach. */
1539+
ASSERT_EQ(count_calls_from(r, "target", "t.guard_scope.unguarded_caller"), 1);
1540+
1541+
/* Nothing falls back to the file node. */
1542+
ASSERT_EQ(count_calls_from(r, "target", "t.guard_scope"), 0);
1543+
ASSERT_EQ(count_calls_from(r, "other", "t.guard_scope"), 0);
1544+
1545+
cbm_free_result(r);
1546+
PASS();
1547+
}
1548+
1549+
/* The half the calls and scope assertions above cannot see: the usages walk
1550+
* asks whether an identifier sits inside the def's SIGNATURE, and treats
1551+
* everything that does as part of the binding rather than as a reference. A
1552+
* guard makes that signature the whole `when` operator, which spans the guard
1553+
* expression too -- so `a` read by `is_binary(a)` was classified as its own
1554+
* binding and emitted no USAGE row at all. A guard is an expression, not a
1555+
* pattern: the parameters are bound by `guarded(a, b)` and every mention of one
1556+
* to its right is a read, exactly as the same mention in the body already is.
1557+
* Unwrapping the signature to the head restores those rows without admitting
1558+
* the binding sites themselves, which is why each name is asserted both within
1559+
* its scope and in total. */
1560+
TEST(extract_elixir_guarded_def_head_guard_usages) {
1561+
CBMFileResult *r = extract("defmodule GuardUsage do\n"
1562+
" def plain(x), do: x\n"
1563+
" def guarded(a, b) when is_binary(a) and byte_size(a) > 3 do\n"
1564+
" b\n"
1565+
" end\n"
1566+
"end\n",
1567+
CBM_LANG_ELIXIR, "t", "guard_usage.ex");
1568+
ASSERT_NOT_NULL(r);
1569+
ASSERT_FALSE(r->has_error);
1570+
1571+
/* Both guard reads of the parameter are usages of the guarded clause. */
1572+
ASSERT_EQ(count_usages_from(r, "a", "t.guard_usage.guarded"), 2);
1573+
/* ...and nothing else claims `a`: the `a` in `guarded(a, b)` binds it. */
1574+
ASSERT_EQ(count_usages_named(r, "a"), 2);
1575+
1576+
/* The body read is unchanged, and the head's `b` is still a binding --
1577+
* an unwrap that took the guard instead of the head would add both head
1578+
* parameters here. */
1579+
ASSERT_EQ(count_usages_from(r, "b", "t.guard_usage.guarded"), 1);
1580+
ASSERT_EQ(count_usages_named(r, "b"), 1);
1581+
1582+
/* Control: an unguarded clause was never affected either way. */
1583+
ASSERT_EQ(count_usages_from(r, "x", "t.guard_usage.plain"), 1);
1584+
ASSERT_EQ(count_usages_named(r, "x"), 1);
1585+
1586+
cbm_free_result(r);
1587+
PASS();
1588+
}
1589+
14601590
/* Every clause of an Elixir function is its own `def` call, and an Elixir QN
14611591
* carries neither module nor arity, so all clauses compute one qualified name
14621592
* and collide on a single graph node. cbm_gbuf_upsert_node ranks a same-QN
@@ -9039,6 +9169,8 @@ SUITE(extraction) {
90399169
RUN_TEST(extract_elixir_declaration_head_mints_no_reference_of_any_kind);
90409170
RUN_TEST(extract_elixir_real_recursion_survives_head_suppression);
90419171
RUN_TEST(elixir_call_string_argument);
9172+
RUN_TEST(extract_elixir_guarded_def_head_scope);
9173+
RUN_TEST(extract_elixir_guarded_def_head_guard_usages);
90429174
RUN_TEST(haskell_function);
90439175
RUN_TEST(ocaml_function);
90449176
RUN_TEST(erlang_function);

0 commit comments

Comments
 (0)