Debian Codegraph @

internal/astutil/free/free.go

raw symbols SCIP
1 // Copyright 2025 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package free defines utilities for computing the free variables of
6 // a syntax tree without type information. This is inherently
7 // heuristic because of the T{f: x} ambiguity, in which f may or may
8 // not be a lexical reference depending on whether T is a struct type.
9 package freescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/
10
11 import (
12 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
13 "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/"
14 )
15
16 // Copied, with considerable changes, from go/parser/resolver.go
17 // at af53bd2c03.
18
19 // Names computes an approximation to the set of free names of the AST
20 // at node n based solely on syntax.
21 //
22 // In the absence of composite literals, the set of free names is exact. Composite
23 // literals introduce an ambiguity that can only be resolved with type information:
24 // whether F is a field name or a value in `T{F: ...}`.
25 // If includeComplitIdents is true, this function conservatively assumes
26 // T is not a struct type, so freeishNames overapproximates: the resulting
27 // set may contain spurious entries that are not free lexical references
28 // but are references to struct fields.
29 // If includeComplitIdents is false, this function assumes that T *is*
30 // a struct type, so freeishNames underapproximates: the resulting set
31 // may omit names that are free lexical references.
32 //
33 // TODO(adonovan): includeComplitIdents is a crude hammer: the caller
34 // may have partial or heuristic information about whether a given T
35 // is struct type. Replace includeComplitIdents with a hook to query
36 // the caller.
37 //
38 // The code is based on go/parser.resolveFile, but heavily simplified. Crucial
39 // differences are:
40 // - Instead of resolving names to their objects, this function merely records
41 // whether they are free.
42 // - Labels are ignored: they do not refer to values.
43 // - This is never called on ImportSpecs, so the function panics if it sees one.
44 func Namesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/Names().(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, includeComplitIdents bool) map[string]bool {
45 v := &freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#{
46 freescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#free.: make(map[string]bool),
47 includeComplitIdentsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#includeComplitIdents.: includeComplitIdents,
48 }
49 // Begin with a scope, even though n might not be a form that establishes a scope.
50 // For example, n might be:
51 // x := ...
52 // Then we need to add the first x to some scope.
53 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
54 astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Walkscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Walk().(v, n)
55 v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
56 if v.scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#scope. != nil {
57 panic("unbalanced scopes")
58 }
59 return v.freescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#free.
60 }
61
62 // A freeVisitor holds state for a free-name analysis.
63 type freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor# struct {
64 scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#scope. *scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope# // the current innermost scope
65 freescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#free. map[string]bool // free names seen so far
66 includeComplitIdentsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#includeComplitIdents. bool // include identifier key in composite literals
67 }
68
69 // scope contains all the names defined in a lexical scope.
70 // It is like ast.Scope, but without deprecation warnings.
71 type scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope# struct {
72 namesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope#names. map[string]bool
73 outerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope#outer. *scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope#
74 }
75
76 func (s *scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope#) definedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope#defined().(name string) bool {
77 for ; s != nil; s = s.outerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope#outer. {
78 if s.namesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope#names.[name] {
79 return true
80 }
81 }
82 return false
83 }
84
85 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) Visitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#Visit().(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Visitorscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Visitor# {
86 switch n := n.(type) {
87
88 // Expressions.
89 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#:
90 v.usescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#use().(n)
91
92 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#:
93 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
94 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
95 v.walkFuncTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkFuncType().(nil, n.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#Type.)
96 v.walkBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkBody().(n.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#Body.)
97
98 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#:
99 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#X.)
100 // Skip n.Sel: it cannot be free.
101
102 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.StructTypescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/StructType#:
103 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
104 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
105 v.walkFieldListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkFieldList().(n.Fieldsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/StructType#Fields.)
106
107 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncTypescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#:
108 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
109 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
110 v.walkFuncTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkFuncType().(nil, n)
111
112 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CompositeLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#:
113 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Type.)
114 for _, e := range n.Eltsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CompositeLit#Elts. {
115 if kv, _ := e.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.KeyValueExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#); kv != nil {
116 if ident, _ := kv.Keyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#Key..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#); ident != nil {
117 // It is not possible from syntax alone to know whether
118 // an identifier used as a composite literal key is
119 // a struct field (if n.Type is a struct) or a value
120 // (if n.Type is a map, slice or array).
121 if v.includeComplitIdentsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#includeComplitIdents. {
122 // Over-approximate by treating both cases as potentially
123 // free names.
124 v.usescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#use().(ident)
125 } else {
126 // Under-approximate by ignoring potentially free names.
127 }
128 } else {
129 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(kv.Keyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#Key.)
130 }
131 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(kv.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/KeyValueExpr#Value.)
132 } else {
133 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(e)
134 }
135 }
136
137 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.InterfaceTypescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/InterfaceType#:
138 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
139 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
140 v.walkFieldListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkFieldList().(n.Methodsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/InterfaceType#Methods.)
141
142 // Statements
143 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.AssignStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#:
144 walkSlicescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/walkSlice().(v, n.Rhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Rhs.)
145 if n.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Tok. == tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.DEFINEscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/DEFINE. {
146 v.shortVarDeclscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#shortVarDecl().(n.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.)
147 } else {
148 walkSlicescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/walkSlice().(v, n.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.)
149 }
150
151 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.LabeledStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/LabeledStmt#:
152 // Ignore labels.
153 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/LabeledStmt#Stmt.)
154
155 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BranchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BranchStmt#:
156 // Ignore labels.
157
158 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BlockStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#:
159 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
160 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
161 walkSlicescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/walkSlice().(v, n.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List.)
162
163 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IfStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#:
164 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
165 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
166 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Init.)
167 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Cond.)
168 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Body.)
169 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Elsescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Else.)
170
171 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CaseClausescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CaseClause#:
172 walkSlicescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/walkSlice().(v, n.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CaseClause#List.)
173 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
174 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
175 walkSlicescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/walkSlice().(v, n.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CaseClause#Body.)
176
177 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#:
178 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
179 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
180 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#Init.)
181 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Tagscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#Tag.)
182 v.walkBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkBody().(n.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#Body.)
183
184 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.TypeSwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#:
185 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
186 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
187 if n.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#Init. != nil {
188 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#Init.)
189 }
190 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Assignscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#Assign.)
191 // We can use walkBody here because we don't track label scopes.
192 v.walkBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkBody().(n.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#Body.)
193
194 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CommClausescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommClause#:
195 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
196 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
197 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Commscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommClause#Comm.)
198 walkSlicescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/walkSlice().(v, n.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommClause#Body.)
199
200 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectStmt#:
201 v.walkBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkBody().(n.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectStmt#Body.)
202
203 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ForStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#:
204 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
205 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
206 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Init.)
207 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Cond.)
208 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Postscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Post.)
209 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Body.)
210
211 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.RangeStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#:
212 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
213 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
214 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#X.)
215 var lhs []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#
216 if n.Keyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Key. != nil {
217 lhs = append(lhs, n.Keyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Key.)
218 }
219 if n.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Value. != nil {
220 lhs = append(lhs, n.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Value.)
221 }
222 if len(lhs) > 0 {
223 if n.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Tok. == tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.DEFINEscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/DEFINE. {
224 v.shortVarDeclscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#shortVarDecl().(lhs)
225 } else {
226 walkSlicescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/walkSlice().(v, lhs)
227 }
228 }
229 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Body.)
230
231 // Declarations
232 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.GenDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#:
233 switch n.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Tok. {
234 case tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.CONSTscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/CONST., tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.VARscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/VAR.:
235 for _, spec := range n.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. {
236 spec := spec.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ValueSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#)
237 walkSlicescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/walkSlice().(v, spec.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Values.)
238 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(spec.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Type.)
239 v.declarescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declare().(spec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names....)
240 }
241
242 case tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.TYPEscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/TYPE.:
243 for _, spec := range n.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. {
244 spec := spec.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.TypeSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#)
245 // Go spec: The scope of a type identifier declared inside a
246 // function begins at the identifier in the TypeSpec and ends
247 // at the end of the innermost containing block.
248 v.declarescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declare().(spec.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#Name.)
249 if spec.TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#TypeParams. != nil {
250 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
251 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
252 v.walkTypeParamsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkTypeParams().(spec.TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#TypeParams.)
253 }
254 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(spec.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#Type.)
255 }
256
257 case tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.IMPORTscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/IMPORT.:
258 panic("encountered import declaration in free analysis")
259 }
260
261 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#:
262 if n.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Recv. == nil && n.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. != "init" { // package-level function
263 v.declarescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declare().(n.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name.)
264 }
265 v.openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().()
266 defer v.closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().()
267 v.walkTypeParamsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkTypeParams().(n.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#TypeParams.)
268 v.walkFuncTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkFuncType().(n.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Recv., n.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type.)
269 v.walkBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkBody().(n.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Body.)
270
271 default:
272 return v
273 }
274
275 return nil
276 }
277
278 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) openScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#openScope().() {
279 v.scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#scope. = &scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope#{map[string]bool{}, v.scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#scope.}
280 }
281
282 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) closeScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#closeScope().() {
283 v.scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#scope. = v.scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#scope..outerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope#outer.
284 }
285
286 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) {
287 if n != nil {
288 astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Walkscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Walk().(v, n)
289 }
290 }
291
292 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) walkFuncTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkFuncType().(recv *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FieldListscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#, typ *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncTypescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#) {
293 // First use field types...
294 v.walkRecvFieldTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkRecvFieldType().(recv)
295 v.walkFieldTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkFieldTypes().(typ.Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params.)
296 v.walkFieldTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkFieldTypes().(typ.Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results.)
297
298 // ...then declare field names.
299 v.declareFieldNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declareFieldNames().(recv)
300 v.declareFieldNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declareFieldNames().(typ.Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params.)
301 v.declareFieldNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declareFieldNames().(typ.Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results.)
302 }
303
304 // A receiver field is not like a param or result field because
305 // "func (recv R[T]) method()" uses R but declares T.
306 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) walkRecvFieldTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkRecvFieldType().(list *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FieldListscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#) {
307 if list == nil {
308 return
309 }
310 for _, f := range list.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List. { // valid => len=1
311 typ := f.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Type.
312 if ptr, ok := typ.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.StarExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/StarExpr#); ok {
313 typ = ptr.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/StarExpr#X.
314 }
315
316 // Analyze receiver type as Base[Index, ...]
317 var (
318 base astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#
319 indices []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#
320 )
321 switch typ := typ.(type) {
322 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IndexExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IndexExpr#: // B[T]
323 base, indices = typ.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IndexExpr#X., []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#{typ.Indexscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IndexExpr#Index.}
324 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IndexListExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IndexListExpr#: // B[K, V]
325 base, indices = typ.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IndexListExpr#X., typ.Indicesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IndexListExpr#Indices.
326 default: // B
327 base = typ
328 }
329 for _, expr := range indices {
330 if id, ok := expr.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#); ok {
331 v.declarescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declare().(id)
332 }
333 }
334 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(base)
335 }
336 }
337
338 // walkTypeParams is like walkFieldList, but declares type parameters eagerly so
339 // that they may be resolved in the constraint expressions held in the field
340 // Type.
341 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) walkTypeParamsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkTypeParams().(list *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FieldListscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#) {
342 v.declareFieldNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declareFieldNames().(list)
343 v.walkFieldTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkFieldTypes().(list) // constraints
344 }
345
346 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) walkBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkBody().(body *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BlockStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#) {
347 if body == nil {
348 return
349 }
350 walkSlicescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/walkSlice().(v, body.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List.)
351 }
352
353 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) walkFieldListscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkFieldList().(list *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FieldListscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#) {
354 if list == nil {
355 return
356 }
357 v.walkFieldTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkFieldTypes().(list) // .Type may contain references
358 v.declareFieldNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declareFieldNames().(list) // .Names declares names
359 }
360
361 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) shortVarDeclscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#shortVarDecl().(lhs []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) {
362 // Go spec: A short variable declaration may redeclare variables provided
363 // they were originally declared in the same block with the same type, and
364 // at least one of the non-blank variables is new.
365 //
366 // However, it doesn't matter to free analysis whether a variable is declared
367 // fresh or redeclared.
368 for _, x := range lhs {
369 // In a well-formed program each expr must be an identifier,
370 // but be forgiving.
371 if id, ok := x.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#); ok {
372 v.declarescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declare().(id)
373 }
374 }
375 }
376
377 func walkSlicescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/walkSlice().[S ~[]Escip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/E#, E astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#](r *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#, list S) {
378 for _, e := range list {
379 r.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(e)
380 }
381 }
382
383 // walkFieldTypes resolves the types of the walkFieldTypes in list.
384 // The companion method declareFieldList declares the names of the walkFieldTypes.
385 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) walkFieldTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walkFieldTypes().(list *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FieldListscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#) {
386 if list != nil {
387 for _, f := range list.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List. {
388 v.walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#walk().(f.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Type.)
389 }
390 }
391 }
392
393 // declareFieldNames declares the names of the fields in list.
394 // (Names in a FieldList always establish new bindings.)
395 // The companion method resolveFieldList resolves the types of the fields.
396 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) declareFieldNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declareFieldNames().(list *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FieldListscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#) {
397 if list != nil {
398 for _, f := range list.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List. {
399 v.declarescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declare().(f.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Names....)
400 }
401 }
402 }
403
404 // use marks ident as free if it is not in scope.
405 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) usescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#use().(ident *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) {
406 if s := ident.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.; s != "_" && !v.scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#scope..definedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope#defined().(s) {
407 v.freescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#free.[s] = true
408 }
409 }
410
411 // declare adds each non-blank ident to the current scope.
412 func (v *freeVisitorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#) declarescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#declare().(idents ...*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) {
413 for _, id := range idents {
414 if id.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. != "_" {
415 v.scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/freeVisitor#scope..namesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil/free`/scope#names.[id.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.] = true
416 }
417 }
418 }