Skip to content

Commit f6fb3e0

Browse files
authored
Merge pull request #2371 from BobbieBarker/fix/elixir-extraction-defects
fix(elixir): guarded def heads, multi-clause spans, and typespecs walked as code
2 parents 3b46abe + a5c5b42 commit f6fb3e0

8 files changed

Lines changed: 1267 additions & 12 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,66 @@ static bool elixir_call_head_in(TSNode call, const char *source, const char *con
718718
call_node_text_in(ts_node_child(call, 0), source, heads);
719719
}
720720

721+
/* A definition's head declares a name; it is never a call to it. The head under
722+
* a guard is not the def's first argument -- `def f(x) when g` parses its whole
723+
* head as a `when` binary_operator -- so the plain node comparison below stopped
724+
* recognising it, and the inner `f(x)` was recorded as a call to `f`. Under the
725+
* widened span that phantom lands inside the function's own node, and cbm.c
726+
* decides self-recursion by line containment, so an ordinary two-clause guarded
727+
* function reported itself recursive. `recursive` is a queryable node property
728+
* and seeds the cycle detection in pass_complexity, so that is a load-bearing
729+
* signal, not a cosmetic one. The suppression therefore travels with the fold
730+
* rather than following it.
731+
*
732+
* The head comes from cbm_elixir_def_head_is (helpers.c) rather than a private
733+
* peel here, because three files read this same node and all three have to
734+
* agree on which node is the head, or a node one of them is treating as a
735+
* definition name is recorded by another as a reference to that very name:
736+
* - extract_defs.c, extract_elixir_func_def, unwraps it to NAME the
737+
* definition. It and this file disagreeing mints the phantom above.
738+
* - extract_usages.c, is_elixir_def_binding, treats an identifier inside that
739+
* head as a binding occurrence rather than a reference, which is what keeps
740+
* a head suppressed here from re-emerging as a USAGE edge: a suppression in
741+
* one extractor of the unified walk only REMOVES a phantom if the others
742+
* also decline the node. The two files ask different questions of the same
743+
* helper -- this one whether the node IS the head or a `when` wrapper above
744+
* it, that one whether the node sits INSIDE the head -- so there is one
745+
* definition of the head and they cannot drift apart. That containment
746+
* stops at the head rather than covering the whole `when` operator, which
747+
* is deliberate in the other direction: only the parameters being BOUND are
748+
* excluded, and a parameter READ in the guard is a reference, exactly as
749+
* the same read in the body is.
750+
* - extract_unified.c, compute_elixir_func_qn, resolves a def's QN to open
751+
* the function's call scope and reads the head through the same helper.
752+
* Handed the `when` operator instead it returns NULL, no scope is pushed,
753+
* and every call in a guarded body is sourced from the File rather than
754+
* from the enclosing Function. That failure is independent of the two above
755+
* because it decides edge SOURCE, not edge existence.
756+
*
757+
* Suppressing the head does not only delete edges; it moves metadata on edges
758+
* that SURVIVE, and that is worth stating because a caller reading an edge's
759+
* `line` will see a different number than before. `edges` is UNIQUE on
760+
* (source, target, type), so where the def-head phantom happened to be the
761+
* dedup survivor for a pair, deleting it promotes some other row for that same
762+
* pair and the surviving edge takes that row's `line`.
763+
*
764+
* Measured by indexing one 970-file Elixir lib tree with the binary built from
765+
* this commit's parent and with this one, and diffing the `edges` rows out of
766+
* the two SQLite stores keyed on (source QN, target QN, type): 2,436 edges go
767+
* and NONE is added -- 2,104 CALLS and 332 USAGE. Of the 119,696 edges that
768+
* survive, 1,054 record a different `line`.
769+
*
770+
* The typespec attributes -- `@spec f(t) :: u` and the @callback / @type family
771+
* -- put the declared name in a `call` node too, and are deliberately NOT
772+
* suppressed here. A def head can be, because extract_usages.c already treats a
773+
* def's first argument as a binding. Nothing there treats a typespec subject as
774+
* one, so declining it in this walk would not remove its phantom, it would
775+
* RELABEL it: handle_usages reaches the bare identifier handle_calls just
776+
* declined and mints a USAGE onto the same function, which under
777+
* UNIQUE(source, target, type) is a separate row that no longer collides with
778+
* the real call it used to hide behind, and which pass_importance then counts.
779+
* A typespec phantom has to be removed before any extractor sees it, by
780+
* skipping the whole subtree in the unified walk. */
721781
static bool elixir_call_is_definition_role(TSNode node, const char *source) {
722782
static const char *const structural_heads[] = {"def", "defp", "defmacro", "defmodule", NULL};
723783
static const char *const function_heads[] = {"def", "defp", "defmacro", NULL};
@@ -735,7 +795,7 @@ static bool elixir_call_is_definition_role(TSNode node, const char *source) {
735795
TSNode signature = ts_node_named_child_count(arguments) > 0
736796
? ts_node_named_child(arguments, 0)
737797
: arguments;
738-
return ts_node_eq(signature, node);
798+
return cbm_elixir_def_head_is(signature, node);
739799
}
740800
return false;
741801
}
@@ -757,7 +817,23 @@ static bool call_node_is_definition_container(CBMLanguage lang, TSNode node, con
757817
if (lang == CBM_LANG_AGDA && strcmp(kind, "expr") == 0) {
758818
return agda_expr_is_definition_role(node);
759819
}
760-
return lang == CBM_LANG_ELIXIR && strcmp(kind, "call") == 0 &&
820+
/* A guarded head is a `when` binary_operator, not a `call`, and Elixir's
821+
* call node types include binary_operator -- so the head reaches this walk
822+
* and must be able to answer that it is a definition. A paren-less clause
823+
* (`def f when g`) has no inner call at all, so the operator node itself
824+
* reaches extract_callee_name and, with no callee of its own, takes that
825+
* function's last resort -- the first identifier child -- minting a phantom
826+
* CALLS edge onto the very function being defined.
827+
*
828+
* That last resort overrides a deliberate NULL for every Elixir
829+
* binary_operator extract_scripting_callee declines (`=`, `<-`, `->`, `\\`,
830+
* `::`, `when`), so `def g(c \\ 2)` still emits a phantom `c` and
831+
* `e = target(d)` a phantom `e`. That is the pre-existing behaviour for
832+
* those operators and is untouched here: only a `when` operator is
833+
* admitted, so an operator definition's own head (`def a + b`) stays an
834+
* ordinary node, as it was before guards were handled at all. */
835+
return lang == CBM_LANG_ELIXIR &&
836+
(strcmp(kind, "call") == 0 || cbm_elixir_is_when_guard(node)) &&
761837
elixir_call_is_definition_role(node, source);
762838
}
763839

‎internal/cbm/extract_defs.c‎

Lines changed: 148 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9714,8 +9714,119 @@ static TSNode elixir_call_args(TSNode node) {
97149714
return args;
97159715
}
97169716

9717-
// Handle Elixir def/defp/defmacro — extract function definition.
9718-
static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char *macro) {
9717+
// Fold one more clause of an Elixir function into the def already pushed for
9718+
// the previous clause. Every clause is its own `def` call, and an Elixir QN
9719+
// carries neither module nor arity, so all of them compute one qualified name
9720+
// and collide on a single graph node. cbm_gbuf_upsert_node breaks a same-QN
9721+
// collision by keeping the LARGEST start_line, so the survivor was the last
9722+
// clause in the file and get_code_snippet returned that clause as the whole
9723+
// function: `def admin?(%__MODULE__{role: :admin}), do: true` /
9724+
// `def admin?(%__MODULE__{}), do: false` read back as a predicate that always
9725+
// returns false. One node per function is right, so widen its span instead.
9726+
//
9727+
// Two bounds keep the widened span honest, and a clause folds only if it clears
9728+
// both:
9729+
// - Same module body. `scope` is the do_block the previous clause was written
9730+
// in, so a nested `defmodule` cannot merge with its parent: the QN carries
9731+
// no module, so `Outer.run` and `Outer.Inner.run` compute the same name and
9732+
// are array-adjacent when Inner is declared just above Outer's own clause.
9733+
// - Same group. Only the immediately preceding EXTRACTED definition is a
9734+
// candidate, so a def of another name between two same-named defs ends the
9735+
// group. That is a bound this code imposes, not a guarantee Elixir gives:
9736+
// the compiler only WARNS ("clauses with the same name and arity should be
9737+
// grouped together"), it warns per name AND arity while this QN is
9738+
// arity-free, and non-contiguous clauses do compile. Split clauses
9739+
// therefore keep separate nodes, which is the pre-existing behaviour.
9740+
// What the group bound does NOT exclude is a non-definition construct
9741+
// between two clauses: `@doc` / `@spec` are unary_operators, `use` /
9742+
// `alias` / `describe` are ordinary calls, and a `defimpl` block's inner
9743+
// defs are not extracted -- none of them push a def, so anything of that
9744+
// kind written between two clauses of one function ends up inside the
9745+
// widened span, and that is not rare. Measured by indexing one 970-file
9746+
// Elixir lib tree with the binary built from this commit's parent and with
9747+
// this one and diffing the `nodes` rows out of the two SQLite stores: 6,996
9748+
// Function nodes widen their span, none is added, none is lost and none
9749+
// narrows. Reading each widened span for a module-body construct written at
9750+
// the module's own two-space indent, which no clause body reaches, 169 of
9751+
// the 6,996 cover such a line. Counting each node once per kind, 123 cover
9752+
// a typespec attribute (@spec/@type/@typep/@opaque/@callback/
9753+
// @macrocallback), 100 an @doc or @typedoc, 46 a bare comment line, 14 an
9754+
// @impl, and none a `use`, `alias`, `import`, `require` or a module-level
9755+
// `quote`. Swallowed typespecs are the largest attribute kind, not an
9756+
// absent one.
9757+
//
9758+
// The widened span also admits a phantom call that a narrower span kept out,
9759+
// which is why the head suppression in extract_calls.c travels with this fold
9760+
// rather than after it. cbm.c flags self-recursion by finding the innermost
9761+
// Function whose [start_line, end_line] contains a recorded call whose short
9762+
// name matches the function's own, so any pre-existing phantom between the
9763+
// first and the last clause becomes a self-edge as soon as the span covers it.
9764+
// That cost is real and is measured rather than asserted away. Same corpus,
9765+
// reading each flagged node's own source span for any line that names it as a
9766+
// call, a capture or a pipe target, counting a one-line clause's body after
9767+
// `, do:` as such a line: self_recursive Function nodes go 129 -> 430, and the
9768+
// ones carrying no self-call form anywhere in their span go 6 -> 48. The lines
9769+
// responsible are the typespec heads the widened span now covers -- `@spec
9770+
// f(t) :: u` puts the declared name in a `call` node, and the unified walk
9771+
// reads it as code -- a separate defect with a separate fix, not something this
9772+
// fold can close.
9773+
//
9774+
// Skipping the typespec subtrees, later in this stack, closes that half: the
9775+
// same method then reports 389 self_recursive Function nodes and 8 with no
9776+
// self-call form, against 129 and 6 on the base. All 8 are the pre-existing
9777+
// phantom a local variable sharing the function's name mints (`defp slug(value)
9778+
// do slug = ...`), 6 of them already present on the base; the other 2 are
9779+
// guarded clauses that only have a node of their own to be flagged on because
9780+
// of this stack.
9781+
//
9782+
// What the fold and the head suppression together remove is 2,436 edges, none
9783+
// of them added back: see elixir_call_is_definition_role in extract_calls.c.
9784+
//
9785+
// A clause whose macro differs (`def` foo/1 beside `defp` foo/2) still folds,
9786+
// because the arity-free QN already puts both on one node; is_exported is then
9787+
// the OR over the folded clauses, so a name any clause exports stays exported.
9788+
// That OR changes a recorded flag, which the node diff confirms and which is
9789+
// worth stating rather than leaving to be discovered: on the same corpus
9790+
// exactly 6 Function nodes change is_exported, all false -> true and none the
9791+
// other way. Each is a `def` clause written above a `defp` clause of the same
9792+
// name, where the pre-patch last-clause-wins record reported a publicly
9793+
// callable name as unexported. Verified against the source in every one: the
9794+
// new value is the correct one.
9795+
// Returns true when the clause folded.
9796+
static bool fold_elixir_clause(CBMExtractCtx *ctx, const char *qn, TSNode scope, TSNode node,
9797+
uint32_t end_line, bool is_exported) {
9798+
if (!qn || ctx->result->defs.count <= 0 || ts_node_is_null(scope)) {
9799+
return false;
9800+
}
9801+
TSNode here = ts_node_parent(node);
9802+
if (ts_node_is_null(here) || !ts_node_eq(here, scope)) {
9803+
return false;
9804+
}
9805+
CBMDefinition *prev = &ctx->result->defs.items[ctx->result->defs.count - 1];
9806+
if (!prev->label || strcmp(prev->label, "Function") != 0 || !prev->qualified_name ||
9807+
strcmp(prev->qualified_name, qn) != 0) {
9808+
return false;
9809+
}
9810+
/* Only end_line moves. prev->start_line is already the minimum, so this
9811+
* takes no start_line argument: a clause folds only into the def pushed
9812+
* for the immediately preceding extracted clause of the same do_block, and
9813+
* extract_elixir_call pushes a do_block's children onto its stack in
9814+
* reverse index order so they pop in source order. A `start_line <
9815+
* prev->start_line` guard here would be unreachable; if that traversal
9816+
* ever stops being source-ordered, this is the line that has to change
9817+
* with it. */
9818+
if (end_line > prev->end_line) {
9819+
prev->end_line = end_line;
9820+
}
9821+
prev->is_exported = prev->is_exported || is_exported;
9822+
return true;
9823+
}
9824+
9825+
// Handle Elixir def/defp/defmacro — extract function definition. `scope` is the
9826+
// module body the previous clause was extracted from, and is updated to this
9827+
// def's own module body; see fold_elixir_clause for what it bounds.
9828+
static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char *macro,
9829+
TSNode *scope) {
97199830
CBMArena *a = ctx->arena;
97209831
TSNode args = elixir_call_args(node);
97219832
if (ts_node_is_null(args)) {
@@ -9727,6 +9838,16 @@ static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char
97279838
return;
97289839
}
97299840

9841+
// `def name(args) when guard` parses the whole head as a `when`
9842+
// binary_operator, so the name lives on its left operand rather than
9843+
// directly under the call. Without unwrapping it, every guarded clause is
9844+
// dropped, and a function whose clauses ALL carry guards never appears in
9845+
// the graph at all -- silently, since a missing definition is not an error.
9846+
// The unwrap lives in helpers.c because it peels only `when`: an operator
9847+
// definition (`def a + b`) is a binary_operator head too, and unwrapping
9848+
// that one would name the function after its own left parameter.
9849+
first_arg = cbm_elixir_def_head_unwrap_guard(first_arg);
9850+
97309851
const char *fk = ts_node_type(first_arg);
97319852
char *name = NULL;
97329853
if (strcmp(fk, "call") == 0 && ts_node_child_count(first_arg) > 0) {
@@ -9738,15 +9859,25 @@ static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char
97389859
return;
97399860
}
97409861

9862+
const char *qn = cbm_fqn_compute(a, ctx->project, ctx->rel_path, name);
9863+
uint32_t start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;
9864+
uint32_t end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;
9865+
bool is_exported = (strcmp(macro, "def") == 0 || strcmp(macro, "defmacro") == 0);
9866+
bool folded = fold_elixir_clause(ctx, qn, *scope, node, end_line, is_exported);
9867+
*scope = ts_node_parent(node);
9868+
if (folded) {
9869+
return;
9870+
}
9871+
97419872
CBMDefinition def;
97429873
memset(&def, 0, sizeof(def));
97439874
def.name = name;
9744-
def.qualified_name = cbm_fqn_compute(a, ctx->project, ctx->rel_path, name);
9875+
def.qualified_name = qn;
97459876
def.label = "Function";
97469877
def.file_path = ctx->rel_path;
9747-
def.start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;
9748-
def.end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;
9749-
def.is_exported = (strcmp(macro, "def") == 0 || strcmp(macro, "defmacro") == 0);
9878+
def.start_line = start_line;
9879+
def.end_line = end_line;
9880+
def.is_exported = is_exported;
97509881
cbm_defs_push(&ctx->result->defs, a, def);
97519882
}
97529883

@@ -9783,6 +9914,10 @@ static TSNode emit_elixir_module_class(CBMExtractCtx *ctx, TSNode cur) {
97839914
// without recursion between extract_elixir_call ↔ extract_elixir_module_def.
97849915
static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec) {
97859916
(void)spec;
9917+
/* Module body the last extracted clause was written in; see
9918+
* fold_elixir_clause. Null until the first def, so nothing folds into a
9919+
* def left over from a previous top-level call node. */
9920+
TSNode def_scope = {0};
97869921
TSNodeStack stack;
97879922
ts_nstack_init(&stack, ctx, CBM_SZ_64);
97889923
ts_nstack_push(&stack, node);
@@ -9805,12 +9940,17 @@ static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
98059940

98069941
if (strcmp(macro, "def") == 0 || strcmp(macro, "defp") == 0 ||
98079942
strcmp(macro, "defmacro") == 0) {
9808-
extract_elixir_func_def(ctx, cur, macro);
9943+
extract_elixir_func_def(ctx, cur, macro, &def_scope);
98099944
} else if (strcmp(macro, "if") == 0 || strcmp(macro, "unless") == 0) {
98109945
/* A compile-time if/unless in a module body defines functions
98119946
* under a condition: its do and else branches hold variants of the
98129947
* same definitions (graph_buffer.c, "Definition variants"). The
9813-
* grammar nests the else_block inside the do_block. */
9948+
* grammar nests the else_block inside the do_block.
9949+
* The clause fold composes with this: fold_elixir_clause only
9950+
* folds a clause into one written in the same block, and each
9951+
* branch is its own block, so the clauses of one branch fold into
9952+
* one definition while the branches stay separate definitions that
9953+
* the graph buffer merges as variants of one identity. */
98149954
TSNode branch = cbm_find_child_by_kind(cur, "do_block");
98159955
for (int pass = 0; pass < 2 && !ts_node_is_null(branch); pass++) {
98169956
uint32_t bc = ts_node_child_count(branch);

0 commit comments

Comments
 (0)