Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/chart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2751,7 +2751,9 @@ grammar's tables and its precedence: no Earley item gains a fact.
refuse, the set of nodes reachable from the root, and each reachable
node's kept alternatives in order. Nodes are matched by what they derive
(symbol, rule position and span), never by index, so a renumbered forest
compares equal and a node unreachable from the root is not compared. The
compares equal and a node unreachable from the root is not compared. Two
distinct reachable nodes sharing a key are a difference
(`TestForestDiffDuplicateKey`), never one overwriting the other. The
V1 side (`V1Forest`, `V1ToyForest`) is the old pipeline: the recognizer
`V1Recognizer` builds, which is only ever the unjudged one, then `Build`,
`LibraryOperators` (for a source) and `Constrain`. The candidate
Expand Down
23 changes: 18 additions & 5 deletions internal/chart/forest/equality.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ func ForestDiff(cand, v1 *Forest) string {
case v1 == nil:
return "the candidate accepts, V1 refuses"
}
c, v := reachableAlts(cand), reachableAlts(v1)
c, cdup := reachableAlts(cand)
v, vdup := reachableAlts(v1)
if cdup != nil {
return fmt.Sprintf("node %+v is reachable twice in the candidate", *cdup)
}
if vdup != nil {
return fmt.Sprintf("node %+v is reachable twice in V1", *vdup)
}
keys := slices.SortedFunc(maps.Keys(c), cmpNodeKey)
for _, k := range slices.SortedFunc(maps.Keys(v), cmpNodeKey) {
if _, ok := c[k]; !ok {
Expand All @@ -59,8 +66,9 @@ func ForestDiff(cand, v1 *Forest) string {
return ""
}

// reachableAlts is each node reachable from f's root, by key, with its kept alternatives in order.
func reachableAlts(f *Forest) map[NodeKey][]altKey {
// reachableAlts is each node reachable from f's root, by key, with its kept alternatives in order, and the first
// key, in cmpNodeKey order, that two distinct reachable nodes share (nil if none): a map by key would keep only one.
func reachableAlts(f *Forest) (map[NodeKey][]altKey, *NodeKey) {
key := func(n int32) NodeKey {
if n == 0 {
return noNode
Expand All @@ -69,6 +77,7 @@ func reachableAlts(f *Forest) map[NodeKey][]altKey {
return NodeKey{nd.Sym, nd.Rule, nd.Dot, nd.From, nd.To}
}
out := map[NodeKey][]altKey{}
var dup *NodeKey
seen := make([]bool, len(f.Nodes))
work := []int32{f.Root}
for len(work) > 0 {
Expand All @@ -88,9 +97,13 @@ func reachableAlts(f *Forest) map[NodeKey][]altKey {
}
}
}
out[key(n)] = alts
k := key(n)
if _, ok := out[k]; ok && (dup == nil || cmpNodeKey(k, *dup) < 0) {
dup = &k
}
out[k] = alts
}
return out
return out, dup
}

func cmpNodeKey(x, y NodeKey) int {
Expand Down
22 changes: 22 additions & 0 deletions internal/chart/forest/equality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,25 @@ func TestForestEqualityUnionShortcut(t *testing.T) {
t.Errorf("%q: %s", src, d)
}
}

// TestForestDiffDuplicateKey: two distinct reachable nodes sharing a NodeKey are reported as a difference naming the
// key, whichever side holds them, rather than the second silently overwriting the first in the comparison.
func TestForestDiffDuplicateKey(t *testing.T) {
// The root's one alternative has two children, nodes 2 and 3, both symbol 7 over 0..1.
dup := &Forest{
Root: 1,
Nodes: []Node{{}, {Sym: 5, From: 0, To: 2, Alt: 1}, {Sym: 7, From: 0, To: 1}, {Sym: 7, From: 0, To: 1}},
Alts: []Alt{{}, {Rule: 1, Left: 2, Right: 3}},
}
clean := dup.Copy()
clean.Alts[1].Right = 2
want := fmt.Sprintf("%+v", NodeKey{Sym: 7, Rule: 0, Dot: 0, From: 0, To: 1})
for name, pair := range map[string][2]*Forest{"candidate": {dup, clean}, "v1": {clean, dup}} {
if d := ForestDiff(pair[0], pair[1]); !strings.Contains(d, "twice") || !strings.Contains(d, want) {
t.Errorf("%s holds the duplicate: ForestDiff = %q, want a report naming %s", name, d, want)
}
}
if d := ForestDiff(clean, clean.Copy()); d != "" {
t.Errorf("no duplicate: ForestDiff = %q, want none", d)
}
}
Loading