Adding special case for root node for flatten()

This commit is contained in:
Neko Box Coder
2026-01-31 21:11:27 +00:00
parent 7ef8ca476d
commit fda43aff15

View File

@@ -483,7 +483,20 @@ func (n *Node) Unsplit() bool {
// flattens the tree by removing unnecessary intermediate parents that have only one child
// and handles the side effect of it
func (n *Node) flatten() {
if n.parent == nil || len(n.children) != 1 {
if len(n.children) != 1 {
return
}
// Special case for root node
if n.parent == nil {
*n = *n.children[0]
n.parent = nil
for _, c := range n.children {
c.parent = n
}
if len(n.children) == 0 {
n.Kind = STUndef
}
return
}