Skip to content

Commit a5a836c

Browse files
committed
refactor(elixir): read a def head through its guard from one helper
`def name(args) when guard` does not parse as a call with a guard beside it. tree-sitter-elixir builds one `when` binary_operator for the whole head, and the `name(args)` call that names the function is that operator's left operand. Four walks read that same node, the def's first argument, and each asks a different question of it: the defs walk names the function from it; the unified walk opens the function's call scope from it; the usages walk bounds the head's bindings with it; the calls walk tells a definition head apart from an invocation by it. A private copy in one of them makes the rest disagree about what a guarded clause is. The disagreement is silent, and it mints an edge: a head one walk treats as a declaration and another does not is recorded as a call to the very function being defined. Two questions, so two exported entry points: cbm_elixir_def_head_unwrap_guard(first_arg) returns the head UNDER every guard, or the node unchanged when there is none. The three walks that need a name or a span take this. cbm_elixir_def_head_is(signature, node) is true for that head OR for any `when` operator above it. The calls walk takes this, because a guarded head reaches it by two routes and it sees only one node per visit: `def f(x) when g` arrives as the inner `f(x)` call, and `def f when g` has no inner call at all, so the `when` operator itself arrives. Recognising one route and not the other leaves the phantom in place for the other. Both test the operator TOKEN rather than the node kind. `def a + b` is a binary_operator head too, and peeling it would name the function after its own left parameter. This commit changes no extraction behaviour: nothing calls either helper yet. The walks are separate commits, each with its own measurement. Measured while writing the test, which corrects a claim worth not repeating: tree-sitter-elixir nests multiple guards RIGHT-associatively. `def f(x) when is_atom(x) when is_binary(x)` parses as `when(f(x), when(is_atom(x), is_binary(x)))`, so the head is the outer operator's left operand and one peel reaches it. The peel still runs to a fixed point (one extra token compare), so the helper's contract is "the head under every guard" even where one peel would do. In tests/test_extraction.c, the test elixir_def_head_is_covers_the_head_and_every_guard_above_it drives the helpers on the AST directly rather than through cbm_extract_file. It has to: the two routes into the calls walk are the pair a single-walk test cannot separate, because each walk only ever sees the node its own traversal reaches. It pins the unguarded case, both guarded shapes, the double guard, and that `def a + b` is returned unchanged with its left operand answering false. Signed-off-by: Chad <4307099+BobbieBarker@users.noreply.github.com>
1 parent 5df8b04 commit a5a836c

3 files changed

Lines changed: 271 additions & 0 deletions

File tree

‎internal/cbm/helpers.c‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,93 @@ const char *cbm_nix_qn_name(CBMArena *a, TSNode func_node, const char *source, c
11401140
return scope ? cbm_arena_sprintf(a, "%s.%s", scope, name) : name;
11411141
}
11421142

1143+
/* ── Elixir def-head guards ─────────────────────────────────── */
1144+
1145+
/* The guard sits on the head, not beside it: `def name(args) when guard` parses
1146+
* as a single `when` binary_operator whose left operand is `name(args)`. The
1147+
* operator field is an exact anonymous token, so the check is the token type
1148+
* rather than the node kind — `def a + b` is a binary_operator head too, and
1149+
* unwrapping it would name the function after its left parameter. */
1150+
bool cbm_elixir_is_when_guard(TSNode node) {
1151+
if (ts_node_is_null(node) || strcmp(ts_node_type(node), "binary_operator") != 0) {
1152+
return false;
1153+
}
1154+
TSNode op = ts_node_child_by_field_name(node, TS_FIELD("operator"));
1155+
return !ts_node_is_null(op) && strcmp(ts_node_type(op), "when") == 0;
1156+
}
1157+
1158+
/* One `when` level off a def's first argument, or the node unchanged.
1159+
*
1160+
* There is deliberately no named-child fallback for a missing `left`. An infix
1161+
* operator cannot reduce without a left operand, so tree-sitter-elixir never
1162+
* builds a `when` binary_operator that lacks one: measured over every guarded
1163+
* shape in the suite plus a corpus of truncated heads (`def when true`,
1164+
* `def f(x) when`, `def f(x) when when is_x(x)`, `def when() when when`),
1165+
* `left` was present on all 66 unwraps, ERROR-recovered parses included, and
1166+
* every one had the left operand as named child 0. Where the left operand was
1167+
* genuinely absent the grammar produced no `when` operator at all -- it read
1168+
* `when` as the head identifier -- so the unwrap is never entered. A
1169+
* named-child fallback would therefore be dead on a well-formed tree and, on
1170+
* the hypothetical malformed one, would hand back named child 0 without
1171+
* knowing it is the left operand rather than the guard expression: naming the
1172+
* function after its own guard. Returning the node unchanged degrades to the
1173+
* pre-guard behaviour instead, which is the safe direction. */
1174+
static TSNode elixir_unwrap_one_guard(TSNode first_arg) {
1175+
if (!cbm_elixir_is_when_guard(first_arg)) {
1176+
return first_arg;
1177+
}
1178+
TSNode lhs = ts_node_child_by_field_name(first_arg, TS_FIELD("left"));
1179+
return ts_node_is_null(lhs) ? first_arg : lhs;
1180+
}
1181+
1182+
/* The head under every guard a def's first argument carries, or first_arg
1183+
* unchanged when it carries none. Elixir admits more than one guard on a
1184+
* clause -- `def f(x) when is_atom(x) when is_binary(x)` -- and
1185+
* tree-sitter-elixir nests those RIGHT-associatively, measured on that exact
1186+
* source: `when(f(x), when(is_atom(x), is_binary(x)))`. The declared head is
1187+
* therefore the outer operator's left operand and one peel reaches it. The
1188+
* peel still runs to a fixed point: the extra iteration is one token compare,
1189+
* and it is what makes the helper's contract "the head under EVERY guard"
1190+
* rather than "the head under the first one", which is the property the three
1191+
* walks rely on.
1192+
*
1193+
* Left wrapped, the defs walk drops the clause, the unified walk opens no
1194+
* function scope (so every call in a guarded body sources to the FILE node),
1195+
* the calls walk stops recognising the head and emits it as an invocation of
1196+
* the very function being defined, and the usages walk swallows every
1197+
* identifier in the guard as part of the binding. Four walks read this same
1198+
* argument, which is why the unwrap lives here rather than in any one of
1199+
* them. */
1200+
TSNode cbm_elixir_def_head_unwrap_guard(TSNode first_arg) {
1201+
for (;;) {
1202+
TSNode next = elixir_unwrap_one_guard(first_arg);
1203+
if (ts_node_eq(next, first_arg)) {
1204+
return first_arg;
1205+
}
1206+
first_arg = next;
1207+
}
1208+
}
1209+
1210+
/* True when `node` is the head a def declares, or any of the `when` guard
1211+
* operators wrapping it. The calls walk needs the whole chain, not just its
1212+
* ends: each member reaches that walk by its own route -- `def f(x) when g`
1213+
* arrives as the inner `f(x)` call, `def f when g` has no inner call at all and
1214+
* the operator itself falls through to extract_callee_name's first-identifier
1215+
* last resort -- and a member it fails to recognise as a declaration is emitted
1216+
* as an invocation of the function being defined. */
1217+
bool cbm_elixir_def_head_is(TSNode signature, TSNode node) {
1218+
for (;;) {
1219+
if (ts_node_eq(signature, node)) {
1220+
return true;
1221+
}
1222+
TSNode next = elixir_unwrap_one_guard(signature);
1223+
if (ts_node_eq(next, signature)) {
1224+
return false;
1225+
}
1226+
signature = next;
1227+
}
1228+
}
1229+
11431230
static const char *func_node_name(CBMArena *a, TSNode func_node, const char *source,
11441231
CBMLanguage lang) {
11451232
// Wolfram: set_delayed_top/set_top/set_delayed/set — LHS is apply(user_symbol("f"), ...)

‎internal/cbm/helpers.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ const char *cbm_nix_binding_scope_qn(CBMExtractCtx *ctx, TSNode node, const char
103103
// attrpath and an enclosing attrset compose into one qualified name.
104104
const char *cbm_nix_qn_name(CBMArena *a, TSNode func_node, const char *source, const char *name);
105105

106+
// ── Elixir def-head guards ──
107+
// `def f(x) when g` parses its WHOLE head as a `when` binary_operator, so the
108+
// `f(x)` call naming the function is the operator's left operand, and more than
109+
// one guard (`when a when b`) nests those operators right-associatively, so the
110+
// head stays the outermost operator's left operand in either shape. Four
111+
// walks read that same first argument — the defs walk names the function from
112+
// it, the unified walk opens the function's call scope from it, the calls walk
113+
// tells a head apart from an invocation by it, and the usages walk excludes the
114+
// head's identifiers from the usage set by it — so a private copy in one of
115+
// them makes the rest disagree about what a guarded clause is.
116+
// cbm_elixir_is_when_guard answers "is this node a guarded head"; the unwrap
117+
// returns the head under every guard, or the node unchanged when it carries
118+
// none; cbm_elixir_def_head_is answers whether a node is that head or one of
119+
// the guard operators above it, which is the question the calls walk asks.
120+
// All three test the operator token, so an operator definition (`def a + b`) is
121+
// left alone rather than mis-read as a guard.
122+
bool cbm_elixir_is_when_guard(TSNode node);
123+
TSNode cbm_elixir_def_head_unwrap_guard(TSNode first_arg);
124+
bool cbm_elixir_def_head_is(TSNode signature, TSNode node);
125+
106126
// Resolve a function/method definition node's NAME node across all ~130 grammars
107127
// (generic `name` field, arrow→declarator, C/C++ declarator chain, plus the many
108128
// per-language quirks: Fortran subroutine, SCSS mixin, SQL create_function, R,

‎tests/test_extraction.c‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "result_spill.h"
1717
#include "pipeline/pass_lsp_cross.h"
1818
#include "iris_export_xml.h"
19+
#include "helpers.h"
20+
#include "lang_specs.h"
1921

2022
/* ── Helpers ───────────────────────────────────────────────────── */
2123

@@ -1246,6 +1248,167 @@ TEST(elixir_function) {
12461248
PASS();
12471249
}
12481250

1251+
/* ── Elixir def-head guard helpers (internal/cbm/helpers.c) ─────────
1252+
*
1253+
* `def name(args) when guard` parses its WHOLE head as one `when`
1254+
* binary_operator whose left operand is the `name(args)` call, and Elixir
1255+
* admits more than one guard on a clause, which tree-sitter-elixir nests
1256+
* right-associatively. Four walks read that same first argument and each asks a
1257+
* different question of it: the defs and call-scope walks want the head UNDER
1258+
* every guard, the usages walk wants it so that a parameter READ by the guard
1259+
* stays outside the binding, and the calls walk wants to know whether the node
1260+
* it is visiting IS that head or one of the guard operators above it.
1261+
*
1262+
* That last question is why cbm_elixir_def_head_is exists beside the unwrap,
1263+
* and it is exercised here on the AST rather than through cbm_extract_file
1264+
* because the two routes it has to answer for are indistinguishable from
1265+
* outside: `def f(x) when g` reaches the calls walk as the inner `f(x)` call,
1266+
* which the unwrap reaches, while `def f when g` has no inner call at all and
1267+
* reaches it as the `when` operator itself, which the unwrap does not. A test
1268+
* that only drives one walk sees whichever of the two that walk happens to hit.
1269+
*
1270+
* This file's helpers change no extraction behaviour on their own; the walks
1271+
* that consume them are separate changes. */
1272+
1273+
enum { ELIXIR_HEAD_PROBE_MAX = 16, ELIXIR_HEAD_STACK_MAX = 256 };
1274+
1275+
static bool head_probe_text_is(const char *source, TSNode node, const char *want) {
1276+
if (ts_node_is_null(node)) {
1277+
return false;
1278+
}
1279+
uint32_t start = ts_node_start_byte(node);
1280+
uint32_t end = ts_node_end_byte(node);
1281+
size_t len = strlen(want);
1282+
return end >= start && (size_t)(end - start) == len && memcmp(source + start, want, len) == 0;
1283+
}
1284+
1285+
/* The first argument of every `def`/`defp` call under `root`, in source order.
1286+
* That argument is what all four walks read, so it is what the helpers take. */
1287+
typedef struct {
1288+
TSNode signature[ELIXIR_HEAD_PROBE_MAX];
1289+
int count;
1290+
} elixir_head_probe_t;
1291+
1292+
static void collect_elixir_def_signatures(TSNode root, const char *source,
1293+
elixir_head_probe_t *out) {
1294+
TSNode stack[ELIXIR_HEAD_STACK_MAX];
1295+
int top = 0;
1296+
stack[top++] = root;
1297+
while (top > 0) {
1298+
TSNode cur = stack[--top];
1299+
uint32_t cc = ts_node_child_count(cur);
1300+
if (cc > 1 && strcmp(ts_node_type(cur), "call") == 0 &&
1301+
(head_probe_text_is(source, ts_node_child(cur, 0), "def") ||
1302+
head_probe_text_is(source, ts_node_child(cur, 0), "defp")) &&
1303+
out->count < ELIXIR_HEAD_PROBE_MAX) {
1304+
TSNode args = ts_node_child(cur, 1);
1305+
if (!ts_node_is_null(args) && ts_node_child_count(args) > 0) {
1306+
out->signature[out->count++] = ts_node_child(args, 0);
1307+
}
1308+
}
1309+
for (int i = (int)cc - 1; i >= 0 && top < ELIXIR_HEAD_STACK_MAX; i--) {
1310+
stack[top++] = ts_node_child(cur, (uint32_t)i);
1311+
}
1312+
}
1313+
}
1314+
1315+
TEST(elixir_def_head_is_covers_the_head_and_every_guard_above_it) {
1316+
const char *source = "defmodule Heads do\n"
1317+
" def plain(x), do: x\n"
1318+
" def guarded(x) when is_binary(x), do: x\n"
1319+
" def bare when true, do: :ok\n"
1320+
" def twice(x) when is_atom(x) when is_binary(x), do: x\n"
1321+
" def a + b, do: {a, b}\n"
1322+
"end\n";
1323+
const TSLanguage *language = cbm_ts_language(CBM_LANG_ELIXIR);
1324+
ASSERT_NOT_NULL(language);
1325+
TSParser *parser = ts_parser_new();
1326+
ASSERT_NOT_NULL(parser);
1327+
ASSERT_TRUE(ts_parser_set_language(parser, language));
1328+
TSTree *tree = ts_parser_parse_string(parser, NULL, source, (uint32_t)strlen(source));
1329+
ASSERT_NOT_NULL(tree);
1330+
1331+
elixir_head_probe_t probe;
1332+
probe.count = 0;
1333+
collect_elixir_def_signatures(ts_tree_root_node(tree), source, &probe);
1334+
ASSERT_EQ(5, probe.count);
1335+
1336+
/* Unguarded: nothing to peel, and the head is the whole first argument.
1337+
* The name node inside it is NOT the head -- the calls walk compares the
1338+
* node it is visiting, and the `plain` identifier is one of those. */
1339+
TSNode plain = probe.signature[0];
1340+
ASSERT_FALSE(cbm_elixir_is_when_guard(plain));
1341+
ASSERT_TRUE(ts_node_eq(cbm_elixir_def_head_unwrap_guard(plain), plain));
1342+
ASSERT_TRUE(cbm_elixir_def_head_is(plain, plain));
1343+
ASSERT_FALSE(cbm_elixir_def_head_is(plain, ts_node_child(plain, 0)));
1344+
1345+
/* One guard: the first argument is the operator, the head is its left
1346+
* operand, and BOTH answer true -- the operator is the node the paren-less
1347+
* route hands the calls walk, the inner call is the node the peel route
1348+
* hands it. The guard expression answers false, or the walk would suppress
1349+
* a real call written inside a guard. */
1350+
TSNode guarded = probe.signature[1];
1351+
ASSERT_TRUE(cbm_elixir_is_when_guard(guarded));
1352+
TSNode guarded_head = cbm_elixir_def_head_unwrap_guard(guarded);
1353+
ASSERT_FALSE(ts_node_eq(guarded_head, guarded));
1354+
ASSERT_STR_EQ("call", ts_node_type(guarded_head));
1355+
ASSERT_TRUE(head_probe_text_is(source, guarded_head, "guarded(x)"));
1356+
ASSERT_TRUE(cbm_elixir_def_head_is(guarded, guarded));
1357+
ASSERT_TRUE(cbm_elixir_def_head_is(guarded, guarded_head));
1358+
TSNode guard_expr = ts_node_child_by_field_name(guarded, TS_FIELD("right"));
1359+
ASSERT_FALSE(ts_node_is_null(guard_expr));
1360+
ASSERT_TRUE(head_probe_text_is(source, guard_expr, "is_binary(x)"));
1361+
ASSERT_FALSE(cbm_elixir_def_head_is(guarded, guard_expr));
1362+
1363+
/* Paren-less: the head under the guard is a bare identifier, so there is no
1364+
* inner call for the peel route to produce. This is the shape that makes
1365+
* cbm_elixir_def_head_is necessary: the only node the calls walk ever sees
1366+
* for this clause is the operator. */
1367+
TSNode bare = probe.signature[2];
1368+
ASSERT_TRUE(cbm_elixir_is_when_guard(bare));
1369+
TSNode bare_head = cbm_elixir_def_head_unwrap_guard(bare);
1370+
ASSERT_STR_EQ("identifier", ts_node_type(bare_head));
1371+
ASSERT_TRUE(head_probe_text_is(source, bare_head, "bare"));
1372+
ASSERT_TRUE(cbm_elixir_def_head_is(bare, bare));
1373+
ASSERT_TRUE(cbm_elixir_def_head_is(bare, bare_head));
1374+
1375+
/* Two guards on one clause. tree-sitter-elixir nests them
1376+
* RIGHT-associatively -- `when(twice(x), when(is_atom(x), is_binary(x)))`,
1377+
* which is what this asserts -- so the head is still the outer operator's
1378+
* left operand and the peel reaches it in one step. The repeated peel is
1379+
* therefore a fixed point rather than a chain walk on this grammar; the
1380+
* nodes that must answer true are the operator and the head. */
1381+
TSNode twice = probe.signature[3];
1382+
ASSERT_TRUE(cbm_elixir_is_when_guard(twice));
1383+
TSNode second_guard = ts_node_child_by_field_name(twice, TS_FIELD("right"));
1384+
ASSERT_TRUE(cbm_elixir_is_when_guard(second_guard));
1385+
TSNode twice_head = cbm_elixir_def_head_unwrap_guard(twice);
1386+
ASSERT_TRUE(head_probe_text_is(source, twice_head, "twice(x)"));
1387+
ASSERT_TRUE(cbm_elixir_def_head_is(twice, twice));
1388+
ASSERT_TRUE(cbm_elixir_def_head_is(twice, twice_head));
1389+
/* The second guard is a `when` operator too, and it is NOT on the head
1390+
* chain: a walk that treated any `when` above the head as a declaration
1391+
* would suppress the calls written inside it. */
1392+
ASSERT_FALSE(cbm_elixir_def_head_is(twice, second_guard));
1393+
1394+
/* An operator definition's head is a binary_operator too, and unwrapping it
1395+
* would name the function after its own left parameter. Only the `when`
1396+
* token admits the peel, so `def a + b` is returned unchanged and its left
1397+
* operand is not a head. */
1398+
TSNode op_def = probe.signature[4];
1399+
ASSERT_STR_EQ("binary_operator", ts_node_type(op_def));
1400+
ASSERT_FALSE(cbm_elixir_is_when_guard(op_def));
1401+
ASSERT_TRUE(ts_node_eq(cbm_elixir_def_head_unwrap_guard(op_def), op_def));
1402+
ASSERT_TRUE(cbm_elixir_def_head_is(op_def, op_def));
1403+
TSNode op_lhs = ts_node_child_by_field_name(op_def, TS_FIELD("left"));
1404+
ASSERT_TRUE(head_probe_text_is(source, op_lhs, "a"));
1405+
ASSERT_FALSE(cbm_elixir_def_head_is(op_def, op_lhs));
1406+
1407+
ts_tree_delete(tree);
1408+
ts_parser_delete(parser);
1409+
PASS();
1410+
}
1411+
12491412
/* tree-sitter-elixir gives a call's arguments node no field name, so the
12501413
* generic `arguments` field lookup returns null and first_string_arg was never
12511414
* populated for any Elixir call — Phoenix route paths, service URLs and config
@@ -8487,6 +8650,7 @@ SUITE(extraction) {
84878650

84888651
/* Functional */
84898652
RUN_TEST(elixir_function);
8653+
RUN_TEST(elixir_def_head_is_covers_the_head_and_every_guard_above_it);
84908654
RUN_TEST(elixir_call_string_argument);
84918655
RUN_TEST(haskell_function);
84928656
RUN_TEST(ocaml_function);

0 commit comments

Comments
 (0)