| 1 | // Copyright 2026 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 ssa_test |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "go/ast" |
| 10 | "go/importer" |
| 11 | "go/parser" |
| 12 | "go/token" |
| 13 | "go/types" |
| 14 | "testing" |
| 15 | |
| 16 | "golang.org/x/tools/go/ssa" |
| 17 | "golang.org/x/tools/internal/testenv" |
| 18 | ) |
| 19 | |
| 20 | // generic |
| 21 | const g = ` |
| 22 | package p |
| 23 | |
| 24 | type G[P any] struct { |
| 25 | x P |
| 26 | } |
| 27 | |
| 28 | func (g G[P]) M[Q any](q Q) (P, Q) { |
| 29 | return g.x, q |
| 30 | } |
| 31 | |
| 32 | func (g *G[P]) N[Q any](q Q) (P, Q) { |
| 33 | return g.x, q |
| 34 | } |
| 35 | |
| 36 | func f() { |
| 37 | g := G[int]{ x: 42 } |
| 38 | %s |
| 39 | } |
| 40 | ` |
| 41 | |
| 42 | // non-generic |
| 43 | const n = ` |
| 44 | package p |
| 45 | |
| 46 | type N struct{} |
| 47 | |
| 48 | func (N) M[P any](p P) P { |
| 49 | return p |
| 50 | } |
| 51 | |
| 52 | func (*N) N[P any](p P) P { |
| 53 | return p |
| 54 | } |
| 55 | |
| 56 | func f() { |
| 57 | n := N{} |
| 58 | %s |
| 59 | } |
| 60 | ` |
| 61 | |
| 62 | // TestGenericMethods ensures that generic methods are properly instantiated. |
| 63 | func TestGenericMethods(t *testing.T) { |
| 64 | testenv.NeedsGoCommand1Point(t, 27) |
| 65 | |
| 66 | // TODO(mark): include implicit type arguments for direct callees? |
| 67 | // TODO(mark): ensure tpars == targs? |
| 68 | testCalls := []struct { |
| 69 | prog string |
| 70 | stmts string |
| 71 | callee string |
| 72 | tparams int // number of type parameters |
| 73 | targs int // number of type arguments |
| 74 | }{ |
| 75 | // value receivers on generic types |
| 76 | { |
| 77 | prog: g, |
| 78 | stmts: "g.M[bool](true)", |
| 79 | callee: "(p.G[int]).M[bool]", |
| 80 | tparams: 2, |
| 81 | targs: 2, |
| 82 | }, |
| 83 | { |
| 84 | // same as previous but with inferred type arguments |
| 85 | prog: g, |
| 86 | stmts: "g.M(true)", |
| 87 | callee: "(p.G[int]).M[bool]", |
| 88 | tparams: 2, |
| 89 | targs: 2, |
| 90 | }, |
| 91 | { |
| 92 | prog: g, |
| 93 | stmts: "f := g.M[bool]; f(true)", |
| 94 | callee: "(p.G[int]).M[bool]$bound", |
| 95 | tparams: 0, // not propagated |
| 96 | targs: 1, // receiver eagerly specialized |
| 97 | }, |
| 98 | { |
| 99 | // same as previous but with inferred type arguments |
| 100 | prog: g, |
| 101 | stmts: "var f func(bool) (int, bool); f = g.M; f(true)", |
| 102 | callee: "(p.G[int]).M[bool]$bound", |
| 103 | tparams: 0, |
| 104 | targs: 1, |
| 105 | }, |
| 106 | { |
| 107 | prog: g, |
| 108 | stmts: "f := G[int].M[bool]; f(g, true)", |
| 109 | callee: "(p.G[int]).M[bool]$thunk", |
| 110 | tparams: 0, // not propagated |
| 111 | targs: 1, // receiver eagerly specialized |
| 112 | }, |
| 113 | { |
| 114 | // same as previous but with inferred type arguments |
| 115 | prog: g, |
| 116 | stmts: "var f func(G[int], bool) (int, bool); f = (G[int]).M; f(g, true)", |
| 117 | callee: "(p.G[int]).M[bool]$thunk", |
| 118 | tparams: 0, |
| 119 | targs: 1, |
| 120 | }, |
| 121 | // pointer receivers on generic types |
| 122 | { |
| 123 | prog: g, |
| 124 | stmts: "g.N[bool](true)", |
| 125 | callee: "(*p.G[int]).N[bool]", |
| 126 | tparams: 2, |
| 127 | targs: 2, |
| 128 | }, |
| 129 | { |
| 130 | // same as previous but with inferred type arguments |
| 131 | prog: g, |
| 132 | stmts: "g.N(true)", |
| 133 | callee: "(*p.G[int]).N[bool]", |
| 134 | tparams: 2, |
| 135 | targs: 2, |
| 136 | }, |
| 137 | { |
| 138 | prog: g, |
| 139 | stmts: "f := g.N[bool]; f(true)", |
| 140 | callee: "(*p.G[int]).N[bool]$bound", |
| 141 | tparams: 0, // not propagated |
| 142 | targs: 1, // receiver eagerly specialized |
| 143 | }, |
| 144 | { |
| 145 | // same as previous but with inferred type arguments |
| 146 | prog: g, |
| 147 | stmts: "var f func(bool) (int, bool); f = g.N; f(true)", |
| 148 | callee: "(*p.G[int]).N[bool]$bound", |
| 149 | tparams: 0, |
| 150 | targs: 1, |
| 151 | }, |
| 152 | { |
| 153 | prog: g, |
| 154 | stmts: "f := (*G[int]).N[bool]; f(&g, true)", |
| 155 | callee: "(*p.G[int]).N[bool]$thunk", |
| 156 | tparams: 0, // not propagated |
| 157 | targs: 1, // receiver eagerly specialized |
| 158 | }, |
| 159 | { |
| 160 | // same as previous but with inferred type arguments |
| 161 | prog: g, |
| 162 | stmts: "var f func(*G[int], bool) (int, bool); f = (*G[int]).N[bool]; f(&g, true)", |
| 163 | callee: "(*p.G[int]).N[bool]$thunk", |
| 164 | tparams: 0, |
| 165 | targs: 1, |
| 166 | }, |
| 167 | // value receivers on non-generic types |
| 168 | { |
| 169 | prog: n, |
| 170 | stmts: "n.M[bool](true)", |
| 171 | callee: "(p.N).M[bool]", |
| 172 | tparams: 1, |
| 173 | targs: 1, |
| 174 | }, |
| 175 | { |
| 176 | // same as previous but with inferred type arguments |
| 177 | prog: n, |
| 178 | stmts: "n.M(true)", |
| 179 | callee: "(p.N).M[bool]", |
| 180 | tparams: 1, |
| 181 | targs: 1, |
| 182 | }, |
| 183 | { |
| 184 | prog: n, |
| 185 | stmts: "f := n.M[bool]; f(true)", |
| 186 | callee: "(p.N).M[bool]$bound", |
| 187 | tparams: 0, // not propagated |
| 188 | targs: 1, |
| 189 | }, |
| 190 | { |
| 191 | // same as previous but with inferred type arguments |
| 192 | prog: n, |
| 193 | stmts: "var f func(bool) bool; f = n.M; f(true)", |
| 194 | callee: "(p.N).M[bool]$bound", |
| 195 | tparams: 0, |
| 196 | targs: 1, |
| 197 | }, |
| 198 | { |
| 199 | prog: n, |
| 200 | stmts: "f := N.M[bool]; f(n, true)", |
| 201 | callee: "(p.N).M[bool]$thunk", |
| 202 | tparams: 0, // not propagated |
| 203 | targs: 1, |
| 204 | }, |
| 205 | { |
| 206 | // same as previous but with inferred type arguments |
| 207 | prog: n, |
| 208 | stmts: "var f func(N, bool) bool; f = N.M; f(n, true)", |
| 209 | callee: "(p.N).M[bool]$thunk", |
| 210 | tparams: 0, |
| 211 | targs: 1, |
| 212 | }, |
| 213 | // pointer receivers on non-generic types |
| 214 | { |
| 215 | prog: n, |
| 216 | stmts: "n.N[bool](true)", |
| 217 | callee: "(*p.N).N[bool]", |
| 218 | tparams: 1, |
| 219 | targs: 1, |
| 220 | }, |
| 221 | { |
| 222 | // same as previous but with inferred type arguments |
| 223 | prog: n, |
| 224 | stmts: "n.N(true)", |
| 225 | callee: "(*p.N).N[bool]", |
| 226 | tparams: 1, |
| 227 | targs: 1, |
| 228 | }, |
| 229 | { |
| 230 | prog: n, |
| 231 | stmts: "f := n.N[bool]; f(true)", |
| 232 | callee: "(*p.N).N[bool]$bound", |
| 233 | tparams: 0, // not propagated |
| 234 | targs: 1, |
| 235 | }, |
| 236 | { |
| 237 | // same as previous but with inferred type arguments |
| 238 | prog: n, |
| 239 | stmts: "var f func(bool) bool; f = n.N[bool]; f(true)", |
| 240 | callee: "(*p.N).N[bool]$bound", |
| 241 | tparams: 0, |
| 242 | targs: 1, |
| 243 | }, |
| 244 | { |
| 245 | prog: n, |
| 246 | stmts: "f := (*N).N[bool]; f(&n, true)", |
| 247 | callee: "(*p.N).N[bool]$thunk", |
| 248 | tparams: 0, // not propagated |
| 249 | targs: 1, |
| 250 | }, |
| 251 | { |
| 252 | // same as previous but with inferred type arguments |
| 253 | prog: n, |
| 254 | stmts: "var f func(*N, bool) bool; f = (*N).N; f(&n, true)", |
| 255 | callee: "(*p.N).N[bool]$thunk", |
| 256 | tparams: 0, |
| 257 | targs: 1, |
| 258 | }, |
| 259 | // bounds of instantiated signatures which only differ by receiver pointerness |
| 260 | { |
| 261 | prog: g, |
| 262 | stmts: "f := g.M[bool]; _ = g.N[bool]; f(true)", |
| 263 | callee: "(p.G[int]).M[bool]$bound", |
| 264 | tparams: 0, // not propagated |
| 265 | targs: 1, // receiver eagerly specialized |
| 266 | }, |
| 267 | { |
| 268 | prog: g, |
| 269 | stmts: "_ = g.M[bool]; f := g.N[bool]; f(true)", |
| 270 | callee: "(*p.G[int]).N[bool]$bound", |
| 271 | tparams: 0, // not propagated |
| 272 | targs: 1, // receiver eagerly specialized |
| 273 | }, |
| 274 | { |
| 275 | prog: n, |
| 276 | stmts: "f := n.M[bool]; _ = n.N[bool]; f(true)", |
| 277 | callee: "(p.N).M[bool]$bound", |
| 278 | tparams: 0, // not propagated |
| 279 | targs: 1, |
| 280 | }, |
| 281 | { |
| 282 | prog: n, |
| 283 | stmts: "_ = n.M[bool]; f := n.N[bool]; f(true)", |
| 284 | callee: "(*p.N).N[bool]$bound", |
| 285 | tparams: 0, // not propagated |
| 286 | targs: 1, |
| 287 | }, |
| 288 | // thunks of instantiated signatures which only differ by receiver pointerness |
| 289 | { |
| 290 | prog: g, |
| 291 | stmts: "f := G[int].M[bool]; _ = (*G[int]).N[bool]; f(g, true)", |
| 292 | callee: "(p.G[int]).M[bool]$thunk", |
| 293 | tparams: 0, // not propagated |
| 294 | targs: 1, // receiver eagerly specialized |
| 295 | }, |
| 296 | { |
| 297 | prog: g, |
| 298 | stmts: "_ = G[int].M[bool]; f := (*G[int]).N[bool]; f(&g, true)", |
| 299 | callee: "(*p.G[int]).N[bool]$thunk", |
| 300 | tparams: 0, // not propagated |
| 301 | targs: 1, // receiver eagerly specialized |
| 302 | }, |
| 303 | { |
| 304 | prog: n, |
| 305 | stmts: "f := N.M[bool]; _ = (*N).N[bool]; f(n, true)", |
| 306 | callee: "(p.N).M[bool]$thunk", |
| 307 | tparams: 0, // not propagated |
| 308 | targs: 1, |
| 309 | }, |
| 310 | { |
| 311 | prog: n, |
| 312 | stmts: "_ = N.M[bool]; f := (*N).N[bool]; f(&n, true)", |
| 313 | callee: "(*p.N).N[bool]$thunk", |
| 314 | tparams: 0, |
| 315 | targs: 1, |
| 316 | }, |
| 317 | } |
| 318 | |
| 319 | for _, want := range testCalls { |
| 320 | prog := fmt.Sprintf(want.prog, want.stmts) |
| 321 | calls := getCalls(t, build(t, prog)) |
| 322 | |
| 323 | if len(calls) != 1 { |
| 324 | t.Fatalf("too many direct calls for %s: got %d, want 1", prog, len(calls)) |
| 325 | } |
| 326 | |
| 327 | got := calls[0] |
| 328 | if got.callee != want.callee { |
| 329 | t.Errorf("wrong callee for %s: got %s, want %s", prog, got.callee, want.callee) |
| 330 | } |
| 331 | if got.tparams != want.tparams { |
| 332 | t.Errorf("wrong number of tparams for %s: got %d, want %d", prog, got.tparams, want.tparams) |
| 333 | } |
| 334 | if got.targs != want.targs { |
| 335 | t.Errorf("wrong number of targs for %s: got %d, want %d", prog, got.targs, want.targs) |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | testBuilds := []string{ |
| 340 | // instantiate same signature with value / pointer receivers |
| 341 | fmt.Sprintf(g, "_ = g.M[bool]; _ = g.N[bool]"), |
| 342 | fmt.Sprintf(n, "_ = n.M[bool]; _ = n.N[bool]"), |
| 343 | } |
| 344 | |
| 345 | for _, prog := range testBuilds { |
| 346 | build(t, prog) |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | type call struct { |
| 351 | callee string |
| 352 | tparams int |
| 353 | targs int |
| 354 | } |
| 355 | |
| 356 | func build(t *testing.T, src string) *ssa.Package { |
| 357 | fset := token.NewFileSet() |
| 358 | f, err := parser.ParseFile(fset, "p.go", src, 0) |
| 359 | if err != nil { |
| 360 | t.Fatal(err) |
| 361 | } |
| 362 | |
| 363 | conf := types.Config{Importer: importer.Default()} |
| 364 | info := &types.Info{ |
| 365 | Types: make(map[ast.Expr]types.TypeAndValue), |
| 366 | Defs: make(map[*ast.Ident]types.Object), |
| 367 | Uses: make(map[*ast.Ident]types.Object), |
| 368 | Selections: make(map[*ast.SelectorExpr]*types.Selection), |
| 369 | Instances: make(map[*ast.Ident]types.Instance), |
| 370 | } |
| 371 | pkg, err := conf.Check("p", fset, []*ast.File{f}, info) |
| 372 | if err != nil { |
| 373 | t.Fatal(err) |
| 374 | } |
| 375 | |
| 376 | prog := ssa.NewProgram(fset, ssa.SanityCheckFunctions|ssa.InstantiateGenerics) |
| 377 | p := prog.CreatePackage(pkg, []*ast.File{f}, info, true) |
| 378 | prog.Build() |
| 379 | |
| 380 | return p |
| 381 | } |
| 382 | |
| 383 | func getCalls(t *testing.T, p *ssa.Package) []*call { |
| 384 | fun := p.Func("f") |
| 385 | if fun == nil { |
| 386 | t.Fatal("f not found") |
| 387 | } |
| 388 | |
| 389 | var calls []*call |
| 390 | for _, block := range fun.Blocks { |
| 391 | for _, instr := range block.Instrs { |
| 392 | if c, ok := instr.(*ssa.Call); ok { |
| 393 | fn := c.Call.StaticCallee() |
| 394 | calls = append(calls, &call{ |
| 395 | callee: fn.String(), |
| 396 | tparams: fn.TypeParams().Len(), |
| 397 | targs: len(fn.TypeArgs()), |
| 398 | }) |
| 399 | } |
| 400 | // don't care about the extra call for MakeClosure |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return calls |
| 405 | } |