scope

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Name

func Name(node ast.Node) string

Name returns a human-readable name for the scope type.

Types

type Index

type Index map[*types.Scope]ast.Node

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 NewIndex

func NewIndex(info *types.Info) Index

NewIndex creates a scope analyzer from the type checker's scope map.

func (Index) Innermost

func (s Index) Innermost(declScope *types.Scope, pos token.Pos) *types.Scope

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

func (s Index) ParentScope(scope *types.Scope) *types.Scope

ParentScope calculates parent, skips case scopes when the current scope is not in the body but the expression.

func (Index) ParentScopes

func (s Index) ParentScopes(root, start *types.Scope) iter.Seq2[*types.Scope, struct{}]

ParentScopes yields a sequence of scopes from start up to (but not including) root.

func (Index) Shadowing added in v0.0.5

func (s Index) Shadowing(inner *types.Var) (outer *types.Var)

Shadowing looks for a shadowed variable in parent scopes. It doesn't cross-function scopes.

Parameters:

  • inner: The variable that may be shadowing another

Returns:

  • outer: The outer variable being shadowed (nil if none is found)

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)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL