proposal: go/ast: add PreorderStack, a wrapper around ast.Inspect that maintains a stack #73319
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Proposal
Tools
This label describes issues relating to any tools in the x/tools repository.
Milestone
Background: It is common when using ast.Inspect to need to record a stack of nodes. The easiest way to do this is to append to a slice when n is non-nil (an "enter" event) and to slice it when n is nil (a "leave" event):
This pattern appears dozens of times throughout x/tools. However, it is subtly wrong: if the "...use n..." portion of the code executes
return false
, traversal of the subtree n is skipped, and in that case the user's function is never called with a corresponding nil to indicate the "leaving n" event; the stack gets misaligned. We make this mistake nearly every time we use a stack and "return false" in the same traversal.Proposal: We propose to provide a wrapper function around ast.Inspect that constructs the stack (correctly), passing it to the user's callback.
The text was updated successfully, but these errors were encountered: