Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Index ¶
Index provides scope analysis for variable movement.
It maps scopes to their corresponding AST nodes and provides methods to:
- Determine if a variable can safely be moved to a tighter scope
- Find the tightest "safe" scope that avoids breaking semantics
func (Index) Innermost ¶
Innermost finds the innermost scope containing a use, with special handling for case/select expressions.
For most positions, this returns the innermost scope from the type checker. However, when a variable is used in a case or select expression (between "case" and ":" tokens), it adjusts the scope to the parent.
func (Index) ParentScope ¶
ParentScope calculates parent, skips case scopes when the current scope is not in the body but the expression.
func (Index) ParentScopes ¶
ParentScopes yields a sequence of scopes from start up to (but not including) root.
type TargetScope ¶
type TargetScope struct {
Index
}
TargetScope determines where declarations can be safely moved. It extends ScopeIndex with target-specific scope safety analysis.
func NewTargetScope ¶
func NewTargetScope(scopes Index) TargetScope
NewTargetScope creates a new TargetScope instance.
func (TargetScope) FindSafeScope ¶
func (s TargetScope) FindSafeScope(declScope, minScope *types.Scope) *types.Scope
FindSafeScope traverses from minScope up to declScope in the scope hierarchy, returning the tightest "safe" scope where a variable can be moved.
"Safe" means the scope avoids moves that would change semantics:
- Loop bodies: Variables used in multiple iterations must stay outside the loop
- Function literals: Variables captured by closures must remain in the capturing scope
func (TargetScope) TargetNode ¶
func (s TargetScope) TargetNode(declScope, targetScope *types.Scope, maxPos token.Pos, onlyBlock bool) ast.Node
TargetNode finds a suitable node for moving a variable to a tighter scope.
Parameters:
- declScope: The scope where the variable is currently declared
- targetScope: The tightest scope containing all variable uses
- maxPos: Position we should not cross that blocks the move
- onlyBlock: If true, only consider block scopes (not init fields)
type UsageScope ¶
type UsageScope struct {
Index
}
UsageScope determines the usage scope of declared variables. It extends ScopeIndex with usage-specific scope analysis.
func NewUsageScope ¶
func NewUsageScope(scopes Index) UsageScope
NewUsageScope creates a new UsageScope instance.
func (UsageScope) CommonAncestor ¶
func (s UsageScope) CommonAncestor(declScope, currentScope, usageScope *types.Scope) *types.Scope
CommonAncestor finds the lowest common ancestor (LCA) of two scopes in the scope tree.
- declScope: The declaration scope (root of the subtree we're searching)
- currentScope: First scope (the current minimum scope)
- usageScope: Second scope (scope of the new use we're processing)