| 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_test |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "go/ast" |
| 10 | "go/parser" |
| 11 | "go/token" |
| 12 | "maps" |
| 13 | "slices" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | |
| 17 | "golang.org/x/tools/internal/astutil/free" |
| 18 | ) |
| 19 | |
| 20 | func TestNames(t *testing.T) { |
| 21 | elems := func(m map[string]bool) string { |
| 22 | return strings.Join(slices.Sorted(maps.Keys(m)), " ") |
| 23 | } |
| 24 | |
| 25 | type testcase struct { |
| 26 | code string // file content (without package decl) |
| 27 | want string // space-separated list of expected free names of first decl |
| 28 | } |
| 29 | |
| 30 | for _, tc := range []struct { |
| 31 | includeComplitIdents bool |
| 32 | cases []testcase |
| 33 | }{ |
| 34 | {true, []testcase{ |
| 35 | { |
| 36 | `var _ = x`, |
| 37 | "x", |
| 38 | }, |
| 39 | { |
| 40 | `var _ = x.y.z`, |
| 41 | "x", |
| 42 | }, |
| 43 | { |
| 44 | `var _ = T{a: 1, b: 2, c.d: e}`, |
| 45 | "a b c e T", |
| 46 | }, |
| 47 | { |
| 48 | `var _ = f(x)`, |
| 49 | "f x", |
| 50 | }, |
| 51 | { |
| 52 | `var _ = f.m(x)`, |
| 53 | "f x", |
| 54 | }, |
| 55 | { |
| 56 | `var _ = func(x int) int { return x + y }`, |
| 57 | "int y", |
| 58 | }, |
| 59 | { |
| 60 | `func _() { x = func(x int) int { return 2*x }() }`, |
| 61 | "int x", |
| 62 | }, |
| 63 | { |
| 64 | `var _ = func(x int) (y int) { return x + y }`, |
| 65 | "int", |
| 66 | }, |
| 67 | { |
| 68 | `var _ = struct{a **int; b map[int][]bool}`, |
| 69 | "bool int", |
| 70 | }, |
| 71 | { |
| 72 | `var _ = struct{f int}{f: 0}`, |
| 73 | "f int", |
| 74 | }, |
| 75 | { |
| 76 | `var _ = interface{m1(int) bool; m2(x int) (y bool)}`, |
| 77 | "bool int", |
| 78 | }, |
| 79 | { |
| 80 | `func _() { x := 1; x++ }`, |
| 81 | "", |
| 82 | }, |
| 83 | { |
| 84 | `func _() { x = 1 }`, |
| 85 | "x", |
| 86 | }, |
| 87 | { |
| 88 | `func _() { _ = 1 }`, |
| 89 | "", |
| 90 | }, |
| 91 | { |
| 92 | `func _() { x, y := 1, 2; x = y + z }`, |
| 93 | "z", |
| 94 | }, |
| 95 | { |
| 96 | `func _() { x, y := y, x; x = y + z }`, |
| 97 | "x y z", |
| 98 | }, |
| 99 | { |
| 100 | `func _() { a, b := 0, 0; b, c := 0, 0; print(a, b, c, d) }`, |
| 101 | "d print", |
| 102 | }, |
| 103 | { |
| 104 | `func _() { label: x++ }`, |
| 105 | "x", |
| 106 | }, |
| 107 | { |
| 108 | `func _() { if x == y {x} }`, |
| 109 | "x y", |
| 110 | }, |
| 111 | { |
| 112 | `func _() { if x := 1; x == y {x} }`, |
| 113 | "y", |
| 114 | }, |
| 115 | { |
| 116 | `func _() { if x := 1; x == y {x} else {z} }`, |
| 117 | "y z", |
| 118 | }, |
| 119 | { |
| 120 | `func _() { switch x { case 1: x; case y: z } }`, |
| 121 | "x y z", |
| 122 | }, |
| 123 | { |
| 124 | `func _() { switch x := 1; x { case 1: x; case y: z } }`, |
| 125 | "y z", |
| 126 | }, |
| 127 | { |
| 128 | `func _() { switch x.(type) { case int: x; case []int: y } }`, |
| 129 | "int x y", |
| 130 | }, |
| 131 | { |
| 132 | `func _() { switch x := 1; x.(type) { case int: x; case []int: y } }`, |
| 133 | "int y", |
| 134 | }, |
| 135 | { |
| 136 | `func _() { switch y := x.(type) { case int: x; case []int: y } }`, |
| 137 | "int x", |
| 138 | }, |
| 139 | { |
| 140 | `func _() { select { case c <- 1: x; case x := <-c: 2; default: y} }`, |
| 141 | "c x y", |
| 142 | }, |
| 143 | { |
| 144 | `func _() { for i := 0; i < 9; i++ { c <- j } }`, |
| 145 | "c j", |
| 146 | }, |
| 147 | { |
| 148 | `func _() { for i = 0; i < 9; i++ { c <- j } }`, |
| 149 | "c i j", |
| 150 | }, |
| 151 | { |
| 152 | `func _() { for i := range 9 { c <- j } }`, |
| 153 | "c j", |
| 154 | }, |
| 155 | { |
| 156 | `func _() { for i = range 9 { c <- j } }`, |
| 157 | "c i j", |
| 158 | }, |
| 159 | { |
| 160 | `func _() { for _, e := range []int{1, 2, x} {e} }`, |
| 161 | "int x", |
| 162 | }, |
| 163 | { |
| 164 | `func _() { var x, y int; f(x, y) }`, |
| 165 | "f int", |
| 166 | }, |
| 167 | { |
| 168 | `func _() { {var x, y int}; f(x, y) }`, |
| 169 | "f int x y", |
| 170 | }, |
| 171 | { |
| 172 | `func _() { const x = 1; { const y = iota; return x, y } }`, |
| 173 | "iota", |
| 174 | }, |
| 175 | { |
| 176 | `func _() { type t int; t(0) }`, |
| 177 | "int", |
| 178 | }, |
| 179 | { |
| 180 | `func _() { type t[T ~int] struct { t T }; x = t{t: 1}.t }`, // field t shadowed by type decl |
| 181 | "int x", |
| 182 | }, |
| 183 | { |
| 184 | `func _() { type t[S ~[]E, E any] S }`, |
| 185 | "any", |
| 186 | }, |
| 187 | { |
| 188 | `var a [unsafe.Sizeof(func(x int) { x + y })]int`, |
| 189 | "int unsafe y", |
| 190 | }, |
| 191 | { |
| 192 | `func f(x int) (int y)`, |
| 193 | "int y", |
| 194 | }, |
| 195 | { |
| 196 | `func f(int x) (y int)`, |
| 197 | "int x", |
| 198 | }, |
| 199 | { |
| 200 | `func f[T int](int [k]T) *T`, |
| 201 | "int k", |
| 202 | }, |
| 203 | { |
| 204 | `func f[T *T]([k]T)`, |
| 205 | "k", |
| 206 | }, |
| 207 | { |
| 208 | `func (recv T) method(x int) { print(recv) }`, |
| 209 | "T int print", |
| 210 | }, |
| 211 | { |
| 212 | `func (recv R[P]) method(x int) { var _ P }`, |
| 213 | "R int", |
| 214 | }, |
| 215 | { |
| 216 | `func (recv R[P, P2]) method(x int) { var ( _ P; _ P2 ) }`, |
| 217 | "R int", |
| 218 | }, |
| 219 | { |
| 220 | `func init() { print(init) }`, |
| 221 | "print init", |
| 222 | }, |
| 223 | }}, |
| 224 | { |
| 225 | false, |
| 226 | []testcase{ |
| 227 | { |
| 228 | `func _() { x }`, |
| 229 | "x", |
| 230 | }, |
| 231 | { |
| 232 | `func _() { x.y.z }`, |
| 233 | "x", |
| 234 | }, |
| 235 | { |
| 236 | `func _() { T{a: 1, b: 2, c.d: e} }`, |
| 237 | "c e T", // omit a and b |
| 238 | }, |
| 239 | { |
| 240 | `func _() { type t[T ~int] struct { t T }; x = t{t: 1}.t }`, // field t shadowed by type decl |
| 241 | "int x", |
| 242 | }, |
| 243 | }, |
| 244 | }, |
| 245 | } { |
| 246 | t.Run(fmt.Sprintf("includeComplitIdents=%t", tc.includeComplitIdents), func(t *testing.T) { |
| 247 | for _, test := range tc.cases { |
| 248 | _, f := mustParse(t, "free.go", `package p; `+test.code) |
| 249 | decl := f.Decls[0] |
| 250 | want := map[string]bool{} |
| 251 | for name := range strings.FieldsSeq(test.want) { |
| 252 | want[name] = true |
| 253 | } |
| 254 | |
| 255 | got := free.Names(decl, tc.includeComplitIdents) |
| 256 | |
| 257 | if !maps.Equal(got, want) { |
| 258 | t.Errorf("\ncode %s\ngot %v\nwant %v", test.code, elems(got), elems(want)) |
| 259 | } |
| 260 | } |
| 261 | }) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | func TestFreeNames_scope(t *testing.T) { |
| 266 | // Verify that inputs that don't start a scope don't crash. |
| 267 | _, f := mustParse(t, "free.go", `package p; func _() { x := 1; _ = x }`) |
| 268 | // Select the short var decl, not the entire function body. |
| 269 | n := f.Decls[0].(*ast.FuncDecl).Body.List[0] |
| 270 | _ = free.Names(n, false) |
| 271 | } |
| 272 | |
| 273 | func mustParse(t *testing.T, filename string, content any) (*token.FileSet, *ast.File) { |
| 274 | fset := token.NewFileSet() |
| 275 | f, err := parser.ParseFile(fset, filename, content, parser.ParseComments|parser.SkipObjectResolution) |
| 276 | if err != nil { |
| 277 | t.Fatalf("ParseFile: %v\ncode: %s", err, content) |
| 278 | } |
| 279 | return fset, f |
| 280 | } |