Repository navigation
Commit 198e211
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
725 | 725 | | |
726 | 726 | | |
727 | 727 | | |
728 | | - | |
729 | | - | |
730 | | - | |
731 | | - | |
732 | | - | |
733 | | - | |
734 | | - | |
735 | | - | |
736 | | - | |
737 | | - | |
738 | | - | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
739 | 746 | | |
740 | 747 | | |
741 | 748 | | |
| |||
800 | 807 | | |
801 | 808 | | |
802 | 809 | | |
803 | | - | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
804 | 827 | | |
805 | 828 | | |
806 | 829 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
602 | 602 | | |
603 | 603 | | |
604 | 604 | | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
605 | 610 | | |
606 | 611 | | |
607 | 612 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
617 | 617 | | |
618 | 618 | | |
619 | 619 | | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
620 | 625 | | |
621 | 626 | | |
622 | 627 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
94 | 135 | | |
95 | 136 | | |
96 | 137 | | |
| |||
1457 | 1498 | | |
1458 | 1499 | | |
1459 | 1500 | | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
| 1537 | + | |
| 1538 | + | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
| 1569 | + | |
| 1570 | + | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
| 1574 | + | |
| 1575 | + | |
| 1576 | + | |
| 1577 | + | |
| 1578 | + | |
| 1579 | + | |
| 1580 | + | |
| 1581 | + | |
| 1582 | + | |
| 1583 | + | |
| 1584 | + | |
| 1585 | + | |
| 1586 | + | |
| 1587 | + | |
| 1588 | + | |
| 1589 | + | |
1460 | 1590 | | |
1461 | 1591 | | |
1462 | 1592 | | |
| |||
9039 | 9169 | | |
9040 | 9170 | | |
9041 | 9171 | | |
| 9172 | + | |
| 9173 | + | |
9042 | 9174 | | |
9043 | 9175 | | |
9044 | 9176 | | |
| |||
0 commit comments