Skip to content

Commit 630f851

Browse files
committed
fix(elixir): extract a def head wrapped in a when guard
extract_elixir_func_def reads the def's first argument directly and accepts only a `call` or an `identifier` there. A guard makes that argument a binary_operator, so the extractor found no name and returned. Every guarded clause was dropped, and a function whose clauses ALL carry guards never reached the graph at all. The loss is silent: a missing definition is not an error anywhere in the pipeline. Guards are ordinary Elixir. Measured by indexing one 970-file Elixir lib tree with the binary built from this commit's parent and with this one, and diffing the `nodes` and `edges` rows out of the two SQLite stores: Function nodes 20,882 -> 21,941 new qualified names 1,059 (functions with no node at all before) lost qualified names 0 CALLS edges 55,851 -> 57,129 USAGE edges 27,418 -> 28,223 Every one of those 1,059 is a function whose every clause carries a guard: a clause without one already minted the node they collided on. An agent asking "who calls this" about any of them got an empty answer that reads exactly like dead code. The unwrap goes in the shared helper. The calls, unified and usages walks read the same node, and a private copy in one of them makes the rest disagree about what a guarded clause is. Two regressions come with the new node. Neither is minted here: both are pre-existing phantoms that had nowhere to land while guarded clauses were dropped, and now have a node. 1. A phantom CALLS edge onto the function being defined. The calls walk decides a def head is a declaration by comparing the visited node against the def's first argument, and a guard makes that argument the `when` operator, so the inner `name(args)` call no longer compares equal. cbm.c flags self-recursion by line containment, so the phantom lands inside the function's own span: self_recursive Function nodes 129 -> 1,342 ...with no self-call form in span 24 -> 1,241 (18.6% -> 92.5%) (measured by reading each flagged node's own source span and looking for any non-declaration line that names it as a call, a capture or a pipe target.) 2. A phantom WRITES edge onto it, from a paren-less guarded head. `def f when g` has no inner call at all, so the `when` operator reaches the read/write extractor and its first identifier is recorded as an assignment target. Reproduced on `def bare_guarded when true, do: :ok`: one CBMReadWrite row, var_name "bare_guarded", is_write true, scoped to the file's Module node (the graph asserting that a module mutates a function). Corpus WRITES 11,220 -> 11,302 (+82). Both become observable with this commit, and the guarded call-scope commit later in this stack closes them, by different mechanisms. The CALLS phantom goes because that commit admits the `when` operator to the definition-container check, so a paren-less head is recognised as a declaration. The WRITES phantom goes because that commit opens a function scope for the clause: the read/write row is still recorded, but its enclosing_func_qn becomes the guarded clause itself rather than the enclosing module, so resolve_rw_edges finds src->id == tgt->id and drops the self-edge. They are stated here because a stack that lands partially must not hide them. tests/test_extraction.c: extract_elixir_guarded_def_head covers every guarded shape (one guard, two guards, a `defp`, a paren-less `def bare when true`) and both operator-definition forms that must NOT be unwrapped (`def a + b`, `def c - d when is_integer(c)`, neither of which is extracted, as before). It fails on the current tree for each of the guarded names. Depends on the shared def-head helper commit. Signed-off-by: Chad <4307099+BobbieBarker@users.noreply.github.com>
1 parent a5a836c commit 630f851

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5435,6 +5435,16 @@ static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char
54355435
return;
54365436
}
54375437

5438+
// `def name(args) when guard` parses the whole head as a `when`
5439+
// binary_operator, so the name lives on its left operand rather than
5440+
// directly under the call. Without unwrapping it, every guarded clause is
5441+
// dropped, and a function whose clauses ALL carry guards never appears in
5442+
// the graph at all -- silently, since a missing definition is not an error.
5443+
// The unwrap lives in helpers.c because it peels only `when`: an operator
5444+
// definition (`def a + b`) is a binary_operator head too, and unwrapping
5445+
// that one would name the function after its own left parameter.
5446+
first_arg = cbm_elixir_def_head_unwrap_guard(first_arg);
5447+
54385448
const char *fk = ts_node_type(first_arg);
54395449
char *name = NULL;
54405450
if (strcmp(fk, "call") == 0 && ts_node_child_count(first_arg) > 0) {

‎tests/test_extraction.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,54 @@ TEST(elixir_def_head_is_covers_the_head_and_every_guard_above_it) {
14091409
PASS();
14101410
}
14111411

1412+
/* `def name(args) when guard` parses its whole head as a `when`
1413+
* binary_operator, so the name sits on the operator's left operand rather than
1414+
* directly under the call. The def extractor accepted only `call` and
1415+
* `identifier` there, so every guarded clause was dropped -- and a function
1416+
* whose clauses ALL carry guards never reached the graph at all, silently.
1417+
* Guards are ordinary Elixir: on one real 2.9k-function codebase this hid 216
1418+
* of 270 all-guarded functions, and every call edge touching them. */
1419+
TEST(extract_elixir_guarded_def_head) {
1420+
CBMFileResult *r = extract("defmodule Guarded do\n"
1421+
" def plain(x), do: x\n"
1422+
" def guarded(x) when is_binary(x), do: x\n"
1423+
" def guarded(x) when is_list(x), do: x\n"
1424+
" def multi(x, y) when is_binary(x) and is_integer(y), do: {x, y}\n"
1425+
" defp priv_guarded(x) when is_atom(x), do: x\n"
1426+
" def bare when true, do: :ok\n"
1427+
" def a + b, do: {a, b}\n"
1428+
" def c - d when is_integer(c), do: {c, d}\n"
1429+
" def multi_guard(x) when is_atom(x) when is_binary(x), do: x\n"
1430+
"end\n",
1431+
CBM_LANG_ELIXIR, "t", "guarded.ex");
1432+
ASSERT_NOT_NULL(r);
1433+
ASSERT_FALSE(r->has_error);
1434+
ASSERT(has_def(r, "Function", "plain"));
1435+
ASSERT(has_def(r, "Function", "guarded"));
1436+
ASSERT(has_def(r, "Function", "multi"));
1437+
ASSERT(has_def(r, "Function", "priv_guarded"));
1438+
ASSERT(has_def(r, "Function", "bare"));
1439+
1440+
/* Elixir admits more than one guard on a clause. tree-sitter-elixir nests
1441+
* those `when` operators right-associatively, so the declared head stays
1442+
* the outermost operator's left operand -- one node either way, and the
1443+
* peel must not stop short of it or return the guard. */
1444+
ASSERT(has_def(r, "Function", "multi_guard"));
1445+
ASSERT_EQ(count_defs_named(r, "Function", "multi_guard"), 1);
1446+
1447+
/* An operator definition is a binary_operator head too, but it is not a
1448+
* guard: unwrapping it names the function after its own LEFT PARAMETER.
1449+
* `def a + b` must not mint a function called `a`, nor `def c - d when ...`
1450+
* one called `c` -- the guard's left operand is itself the `c - d`
1451+
* operator, which names no function either. Neither operator form is
1452+
* extracted at all, which is what this grammar did before guards were
1453+
* handled; only the `when` token admits the unwrap. */
1454+
ASSERT_EQ(count_defs_named(r, "Function", "a"), 0);
1455+
ASSERT_EQ(count_defs_named(r, "Function", "c"), 0);
1456+
cbm_free_result(r);
1457+
PASS();
1458+
}
1459+
14121460
/* tree-sitter-elixir gives a call's arguments node no field name, so the
14131461
* generic `arguments` field lookup returns null and first_string_arg was never
14141462
* populated for any Elixir call — Phoenix route paths, service URLs and config
@@ -8651,6 +8699,7 @@ SUITE(extraction) {
86518699
/* Functional */
86528700
RUN_TEST(elixir_function);
86538701
RUN_TEST(elixir_def_head_is_covers_the_head_and_every_guard_above_it);
8702+
RUN_TEST(extract_elixir_guarded_def_head);
86548703
RUN_TEST(elixir_call_string_argument);
86558704
RUN_TEST(haskell_function);
86568705
RUN_TEST(ocaml_function);

0 commit comments

Comments
 (0)