| 1 |
// Copyright 2022 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 ir_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"go/parser" |
| 11 |
"go/token" |
| 12 |
"reflect" |
| 13 |
"sort" |
| 14 |
"testing" |
| 15 |
|
| 16 |
//lint:ignore SA1019 Migration away from go/loader TBD |
| 17 |
"golang.org/x/tools/go/loader" |
| 18 |
"honnef.co/go/tools/go/ir" |
| 19 |
"honnef.co/go/tools/internal/xtools-internal/expect" |
| 20 |
) |
| 21 |
|
| 22 |
// TestGenericBodies tests that bodies of generic functions and methods containing |
| 23 |
// different constructs can be built in BuilderMode(0). |
| 24 |
// |
| 25 |
// Each test specifies the contents of package containing a single go file. |
| 26 |
// Each call print(arg0, arg1, ...) to the builtin print function |
| 27 |
// in ir is correlated a comment at the end of the line of the form: |
| 28 |
// |
| 29 |
// //@ types(a, b, c) |
| 30 |
// |
| 31 |
// where a, b and c are the types of the arguments to the print call |
| 32 |
// serialized using go/types.Type.String(). |
| 33 |
// See x/tools/internal/expect for details on the syntax. |
| 34 |
func TestGenericBodies(t *testing.T) { |
| 35 |
for _, content := range []string{ |
| 36 |
` |
| 37 |
package p00 |
| 38 |
|
| 39 |
func f(x int) { |
| 40 |
var i interface{} |
| 41 |
print(i, 0) //@ types("interface{}", int) |
| 42 |
print() //@ types() |
| 43 |
print(x) //@ types(int) |
| 44 |
} |
| 45 |
`, |
| 46 |
` |
| 47 |
package p01 |
| 48 |
|
| 49 |
func f[T any](x T) { |
| 50 |
print(x) //@ types(T) |
| 51 |
} |
| 52 |
`, |
| 53 |
` |
| 54 |
package p02 |
| 55 |
|
| 56 |
func f[T ~int]() { |
| 57 |
var x T |
| 58 |
print(x) //@ types(T) |
| 59 |
} |
| 60 |
`, |
| 61 |
` |
| 62 |
package p03 |
| 63 |
|
| 64 |
func a[T ~[4]byte](x T) { |
| 65 |
for k, v := range x { |
| 66 |
print(x, k, v) //@ types(T, int, byte) |
| 67 |
} |
| 68 |
} |
| 69 |
func b[T ~*[4]byte](x T) { |
| 70 |
for k, v := range x { |
| 71 |
print(x, k, v) //@ types(T, int, byte) |
| 72 |
} |
| 73 |
} |
| 74 |
func c[T ~[]byte](x T) { |
| 75 |
for k, v := range x { |
| 76 |
print(x, k, v) //@ types(T, int, byte) |
| 77 |
} |
| 78 |
} |
| 79 |
func d[T ~string](x T) { |
| 80 |
for k, v := range x { |
| 81 |
print(x, k, v) //@ types(T, int, rune) |
| 82 |
} |
| 83 |
} |
| 84 |
func e[T ~map[int]string](x T) { |
| 85 |
for k, v := range x { |
| 86 |
print(x, k, v) //@ types(T, int, string) |
| 87 |
} |
| 88 |
} |
| 89 |
func f[T ~chan string](x T) { |
| 90 |
for v := range x { |
| 91 |
print(x, v) //@ types(T, string) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func From() { |
| 96 |
type A [4]byte |
| 97 |
print(a[A]) //@ types("func(x p03.A)") |
| 98 |
|
| 99 |
type B *[4]byte |
| 100 |
print(b[B]) //@ types("func(x p03.B)") |
| 101 |
|
| 102 |
type C []byte |
| 103 |
print(c[C]) //@ types("func(x p03.C)") |
| 104 |
|
| 105 |
type D string |
| 106 |
print(d[D]) //@ types("func(x p03.D)") |
| 107 |
|
| 108 |
type E map[int]string |
| 109 |
print(e[E]) //@ types("func(x p03.E)") |
| 110 |
|
| 111 |
type F chan string |
| 112 |
print(f[F]) //@ types("func(x p03.F)") |
| 113 |
} |
| 114 |
`, |
| 115 |
` |
| 116 |
package p05 |
| 117 |
|
| 118 |
func f[S any, T ~chan S](x T) { |
| 119 |
for v := range x { |
| 120 |
print(x, v) //@ types(T, S) |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
func From() { |
| 125 |
type F chan string |
| 126 |
print(f[string, F]) //@ types("func(x p05.F)") |
| 127 |
} |
| 128 |
`, |
| 129 |
` |
| 130 |
package p06 |
| 131 |
|
| 132 |
func fibonacci[T ~chan int](c, quit T) { |
| 133 |
x, y := 0, 1 |
| 134 |
for { |
| 135 |
select { |
| 136 |
case c <- x: |
| 137 |
x, y = y, x+y |
| 138 |
case <-quit: |
| 139 |
print(c, quit, x, y) //@ types(T, T, int, int) |
| 140 |
return |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
func start[T ~chan int](c, quit T) { |
| 145 |
go func() { |
| 146 |
for i := 0; i < 10; i++ { |
| 147 |
print(<-c) //@ types(int) |
| 148 |
} |
| 149 |
quit <- 0 |
| 150 |
}() |
| 151 |
} |
| 152 |
func From() { |
| 153 |
type F chan int |
| 154 |
c := make(F) |
| 155 |
quit := make(F) |
| 156 |
print(start[F], c, quit) //@ types("func(c p06.F, quit p06.F)", "p06.F", "p06.F") |
| 157 |
print(fibonacci[F], c, quit) //@ types("func(c p06.F, quit p06.F)", "p06.F", "p06.F") |
| 158 |
} |
| 159 |
`, |
| 160 |
` |
| 161 |
package p07 |
| 162 |
|
| 163 |
func f[T ~struct{ x int; y string }](i int) T { |
| 164 |
u := []T{ T{0, "lorem"}, T{1, "ipsum"}} |
| 165 |
return u[i] |
| 166 |
} |
| 167 |
func From() { |
| 168 |
type S struct{ x int; y string } |
| 169 |
print(f[S]) //@ types("func(i int) p07.S") |
| 170 |
} |
| 171 |
`, |
| 172 |
` |
| 173 |
package p08 |
| 174 |
|
| 175 |
func f[T ~[4]int8](x T, l, h int) []int8 { |
| 176 |
return x[l:h] |
| 177 |
} |
| 178 |
func g[T ~*[4]int16](x T, l, h int) []int16 { |
| 179 |
return x[l:h] |
| 180 |
} |
| 181 |
func h[T ~[]int32](x T, l, h int) T { |
| 182 |
return x[l:h] |
| 183 |
} |
| 184 |
func From() { |
| 185 |
type F [4]int8 |
| 186 |
type G *[4]int16 |
| 187 |
type H []int32 |
| 188 |
print(f[F](F{}, 0, 0)) //@ types("[]int8") |
| 189 |
print(g[G](nil, 0, 0)) //@ types("[]int16") |
| 190 |
print(h[H](nil, 0, 0)) //@ types("p08.H") |
| 191 |
} |
| 192 |
`, |
| 193 |
` |
| 194 |
package p09 |
| 195 |
|
| 196 |
func h[E any, T ~[]E](x T, l, h int) []E { |
| 197 |
s := x[l:h] |
| 198 |
print(s) //@ types("T") |
| 199 |
return s |
| 200 |
} |
| 201 |
func From() { |
| 202 |
type H []int32 |
| 203 |
print(h[int32, H](nil, 0, 0)) //@ types("[]int32") |
| 204 |
} |
| 205 |
`, |
| 206 |
` |
| 207 |
package p10 |
| 208 |
|
| 209 |
// Test "make" builtin with different forms on core types and |
| 210 |
// when capacities are constants or variable. |
| 211 |
func h[E any, T ~[]E](m, n int) { |
| 212 |
print(make(T, 3)) //@ types(T) |
| 213 |
print(make(T, 3, 5)) //@ types(T) |
| 214 |
print(make(T, m)) //@ types(T) |
| 215 |
print(make(T, m, n)) //@ types(T) |
| 216 |
} |
| 217 |
func i[K comparable, E any, T ~map[K]E](m int) { |
| 218 |
print(make(T)) //@ types(T) |
| 219 |
print(make(T, 5)) //@ types(T) |
| 220 |
print(make(T, m)) //@ types(T) |
| 221 |
} |
| 222 |
func j[E any, T ~chan E](m int) { |
| 223 |
print(make(T)) //@ types(T) |
| 224 |
print(make(T, 6)) //@ types(T) |
| 225 |
print(make(T, m)) //@ types(T) |
| 226 |
} |
| 227 |
func From() { |
| 228 |
type H []int32 |
| 229 |
h[int32, H](3, 4) |
| 230 |
type I map[int8]H |
| 231 |
i[int8, H, I](5) |
| 232 |
type J chan I |
| 233 |
j[I, J](6) |
| 234 |
} |
| 235 |
`, |
| 236 |
` |
| 237 |
package p11 |
| 238 |
|
| 239 |
func h[T ~[4]int](x T) { |
| 240 |
print(len(x), cap(x)) //@ types(int, int) |
| 241 |
} |
| 242 |
func i[T ~[4]byte | []int | ~chan uint8](x T) { |
| 243 |
print(len(x), cap(x)) //@ types(int, int) |
| 244 |
} |
| 245 |
func j[T ~[4]int | any | map[string]int]() { |
| 246 |
print(new(T)) //@ types("*T") |
| 247 |
} |
| 248 |
func k[T ~[4]int | any | map[string]int](x T) { |
| 249 |
print(x) //@ types(T) |
| 250 |
panic(x) |
| 251 |
} |
| 252 |
`, |
| 253 |
` |
| 254 |
package p12 |
| 255 |
|
| 256 |
func f[E any, F ~func() E](x F) { |
| 257 |
print(x, x()) //@ types(F, E) |
| 258 |
} |
| 259 |
func From() { |
| 260 |
type T func() int |
| 261 |
f[int, T](func() int { return 0 }) |
| 262 |
f[int, func() int](func() int { return 1 }) |
| 263 |
} |
| 264 |
`, |
| 265 |
` |
| 266 |
package p13 |
| 267 |
|
| 268 |
func f[E any, M ~map[string]E](m M) { |
| 269 |
y, ok := m["lorem"] |
| 270 |
print(m, y, ok) //@ types(M, E, bool) |
| 271 |
} |
| 272 |
func From() { |
| 273 |
type O map[string][]int |
| 274 |
f(O{"lorem": []int{0, 1, 2, 3}}) |
| 275 |
} |
| 276 |
`, |
| 277 |
` |
| 278 |
package p14 |
| 279 |
|
| 280 |
func a[T interface{ []int64 | [5]int64 }](x T) int64 { |
| 281 |
print(x, x[2], x[3]) //@ types(T, int64, int64) |
| 282 |
x[2] = 5 |
| 283 |
return x[3] |
| 284 |
} |
| 285 |
func b[T interface{ []byte | string }](x T) byte { |
| 286 |
print(x, x[3]) //@ types(T, byte) |
| 287 |
return x[3] |
| 288 |
} |
| 289 |
func c[T interface{ []byte }](x T) byte { |
| 290 |
print(x, x[2], x[3]) //@ types(T, byte, byte) |
| 291 |
x[2] = 'b' |
| 292 |
return x[3] |
| 293 |
} |
| 294 |
func d[T interface{ map[int]int64 }](x T) int64 { |
| 295 |
print(x, x[2], x[3]) //@ types(T, int64, int64) |
| 296 |
x[2] = 43 |
| 297 |
return x[3] |
| 298 |
} |
| 299 |
func e[T ~string](t T) { |
| 300 |
print(t, t[0]) //@ types(T, uint8) |
| 301 |
} |
| 302 |
func f[T ~string|[]byte](t T) { |
| 303 |
print(t, t[0]) //@ types(T, uint8) |
| 304 |
} |
| 305 |
func g[T []byte](t T) { |
| 306 |
print(t, t[0]) //@ types(T, byte) |
| 307 |
} |
| 308 |
func h[T ~[4]int|[]int](t T) { |
| 309 |
print(t, t[0]) //@ types(T, int) |
| 310 |
} |
| 311 |
func i[T ~[4]int|*[4]int|[]int](t T) { |
| 312 |
print(t, t[0]) //@ types(T, int) |
| 313 |
} |
| 314 |
func j[T ~[4]int|*[4]int|[]int](t T) { |
| 315 |
print(t, &t[0]) //@ types(T, "*int") |
| 316 |
} |
| 317 |
`, |
| 318 |
` |
| 319 |
package p15 |
| 320 |
|
| 321 |
type MyInt int |
| 322 |
type Other int |
| 323 |
type MyInterface interface{ foo() } |
| 324 |
|
| 325 |
// ChangeType tests |
| 326 |
func ct0(x int) { v := MyInt(x); print(x, v) /*@ types(int, "p15.MyInt")*/ } |
| 327 |
func ct1[T MyInt | Other, S int ](x S) { v := T(x); print(x, v) /*@ types(S, T)*/ } |
| 328 |
func ct2[T int, S MyInt | int ](x S) { v := T(x); print(x, v) /*@ types(S, T)*/ } |
| 329 |
func ct3[T MyInt | Other, S MyInt | int ](x S) { v := T(x) ; print(x, v) /*@ types(S, T)*/ } |
| 330 |
|
| 331 |
// Convert tests |
| 332 |
func co0[T int | int8](x MyInt) { v := T(x); print(x, v) /*@ types("p15.MyInt", T)*/} |
| 333 |
func co1[T int | int8](x T) { v := MyInt(x); print(x, v) /*@ types(T, "p15.MyInt")*/ } |
| 334 |
func co2[S, T int | int8](x T) { v := S(x); print(x, v) /*@ types(T, S)*/ } |
| 335 |
|
| 336 |
// MakeInterface tests |
| 337 |
func mi0[T MyInterface](x T) { v := MyInterface(x); print(x, v) /*@ types(T, "p15.MyInterface")*/ } |
| 338 |
|
| 339 |
// NewConst tests |
| 340 |
func nc0[T any]() { v := (*T)(nil); print(v) /*@ types("*T")*/} |
| 341 |
|
| 342 |
// SliceToArrayPointer |
| 343 |
func sl0[T *[4]int | *[2]int](x []int) { v := T(x); print(x, v) /*@ types("[]int", T)*/ } |
| 344 |
func sl1[T *[4]int | *[2]int, S []int](x S) { v := T(x); print(x, v) /*@ types(S, T)*/ } |
| 345 |
`, |
| 346 |
` |
| 347 |
package p16 |
| 348 |
|
| 349 |
func c[T interface{ foo() string }](x T) { |
| 350 |
print(x, x.foo, x.foo()) /*@ types(T, "func() string", string)*/ |
| 351 |
} |
| 352 |
`, |
| 353 |
` |
| 354 |
package p17 |
| 355 |
|
| 356 |
func eq[T comparable](t T, i interface{}) bool { |
| 357 |
return t == i |
| 358 |
} |
| 359 |
`, |
| 360 |
// TODO(59983): investigate why writing g.c panics in (*FieldAddr).String. |
| 361 |
` |
| 362 |
package p18 |
| 363 |
|
| 364 |
type S struct{ f int } |
| 365 |
func c[P *S]() []P { return []P{{f: 1}} } |
| 366 |
`, |
| 367 |
` |
| 368 |
package p19 |
| 369 |
|
| 370 |
func sign[bytes []byte | string](s bytes) (bool, bool) { |
| 371 |
neg := false |
| 372 |
if len(s) > 0 && (s[0] == '-' || s[0] == '+') { |
| 373 |
neg = s[0] == '-' |
| 374 |
s = s[1:] |
| 375 |
} |
| 376 |
return !neg, len(s) > 0 |
| 377 |
} |
| 378 |
`, |
| 379 |
`package p20 |
| 380 |
|
| 381 |
func digits[bytes []byte | string](s bytes) bool { |
| 382 |
for _, c := range []byte(s) { |
| 383 |
if c < '0' || '9' < c { |
| 384 |
return false |
| 385 |
} |
| 386 |
} |
| 387 |
return true |
| 388 |
} |
| 389 |
`, |
| 390 |
` |
| 391 |
package p21 |
| 392 |
|
| 393 |
type E interface{} |
| 394 |
|
| 395 |
func Foo[T E, PT interface{ *T }]() T { |
| 396 |
pt := PT(new(T)) |
| 397 |
x := *pt |
| 398 |
print(x) /*@ types(T)*/ |
| 399 |
return x |
| 400 |
} |
| 401 |
`, |
| 402 |
` |
| 403 |
package p22 |
| 404 |
|
| 405 |
func f[M any, PM *M](p PM) { |
| 406 |
var m M |
| 407 |
*p = m |
| 408 |
print(m) /*@ types(M)*/ |
| 409 |
print(p) /*@ types(PM)*/ |
| 410 |
} |
| 411 |
`, |
| 412 |
` |
| 413 |
package p23 |
| 414 |
|
| 415 |
type A struct{int} |
| 416 |
func (*A) Marker() {} |
| 417 |
|
| 418 |
type B struct{string} |
| 419 |
func (*B) Marker() {} |
| 420 |
|
| 421 |
type C struct{float32} |
| 422 |
func (*C) Marker() {} |
| 423 |
|
| 424 |
func process[T interface { |
| 425 |
*A |
| 426 |
*B |
| 427 |
*C |
| 428 |
Marker() |
| 429 |
}](v T) { |
| 430 |
v.Marker() |
| 431 |
a := *(any(v).(*A)); print(a) /*@ types("p23.A")*/ |
| 432 |
b := *(any(v).(*B)); print(b) /*@ types("p23.B")*/ |
| 433 |
c := *(any(v).(*C)); print(c) /*@ types("p23.C")*/ |
| 434 |
} |
| 435 |
`, |
| 436 |
` |
| 437 |
package p24 |
| 438 |
|
| 439 |
func a[T any](f func() [4]T) { |
| 440 |
x := len(f()) |
| 441 |
print(x) /*@ types("int")*/ |
| 442 |
} |
| 443 |
|
| 444 |
func b[T [4]any](f func() T) { |
| 445 |
x := len(f()) |
| 446 |
print(x) /*@ types("int")*/ |
| 447 |
} |
| 448 |
|
| 449 |
func c[T any](f func() *[4]T) { |
| 450 |
x := len(f()) |
| 451 |
print(x) /*@ types("int")*/ |
| 452 |
} |
| 453 |
|
| 454 |
func d[T *[4]any](f func() T) { |
| 455 |
x := len(f()) |
| 456 |
print(x) /*@ types("int")*/ |
| 457 |
} |
| 458 |
`, |
| 459 |
` |
| 460 |
package p25 |
| 461 |
|
| 462 |
func a[T any]() { |
| 463 |
var f func() [4]T |
| 464 |
for i, v := range f() { |
| 465 |
print(i, v) /*@ types("int", "T")*/ |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
func b[T [4]any](f func() T) { |
| 470 |
for i, v := range f() { |
| 471 |
print(i, v) /*@ types("int", "any")*/ |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
func c[T any](f func() *[4]T) { |
| 476 |
for i, v := range f() { |
| 477 |
print(i, v) /*@ types("int", "T")*/ |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
func d[T *[4]any](f func() T) { |
| 482 |
for i, v := range f() { |
| 483 |
print(i, v) /*@ types("int", "any")*/ |
| 484 |
} |
| 485 |
} |
| 486 |
`, |
| 487 |
` |
| 488 |
package issue64324 |
| 489 |
|
| 490 |
type bar[T any] interface { |
| 491 |
Bar(int) T |
| 492 |
} |
| 493 |
type foo[T any] interface { |
| 494 |
bar[[]T] |
| 495 |
*T |
| 496 |
} |
| 497 |
func Foo[T any, F foo[T]](d int) { |
| 498 |
m := new(T) |
| 499 |
f := F(m) |
| 500 |
print(f.Bar(d)) /*@ types("[]T")*/ |
| 501 |
} |
| 502 |
`, ` |
| 503 |
package issue64324b |
| 504 |
|
| 505 |
type bar[T any] interface { |
| 506 |
Bar(int) T |
| 507 |
} |
| 508 |
type baz[T any] interface { |
| 509 |
bar[*int] |
| 510 |
*int |
| 511 |
} |
| 512 |
|
| 513 |
func Baz[I baz[string]](d int) { |
| 514 |
m := new(int) |
| 515 |
f := I(m) |
| 516 |
print(f.Bar(d)) /*@ types("*int")*/ |
| 517 |
} |
| 518 |
`, |
| 519 |
} { |
| 520 |
pkgname := parsePackageClause(t, content) |
| 521 |
t.Run(pkgname, func(t *testing.T) { |
| 522 |
t.Parallel() |
| 523 |
ssapkg, ppkg := buildPackage(t, content, ir.SanityCheckFunctions) |
| 524 |
fset := ssapkg.Prog.Fset |
| 525 |
file := ppkg.Syntax[0] |
| 526 |
|
| 527 |
// Collect all notes in f, i.e. comments starting with "//@ types". |
| 528 |
notes, err := expect.ExtractGo(fset.File(file.Pos()), file) |
| 529 |
if err != nil { |
| 530 |
t.Errorf("expect.ExtractGo: %v", err) |
| 531 |
} |
| 532 |
|
| 533 |
// Collect calls to the builtin print function. |
| 534 |
fns := make(map[*ir.Function]bool) |
| 535 |
for _, mem := range ssapkg.Members { |
| 536 |
if fn, ok := mem.(*ir.Function); ok { |
| 537 |
fns[fn] = true |
| 538 |
} |
| 539 |
} |
| 540 |
probes := callsTo(fns, "print") |
| 541 |
expectations := matchNotes(fset, notes, probes) |
| 542 |
|
| 543 |
for call := range probes { |
| 544 |
if expectations[call] == nil { |
| 545 |
t.Errorf("Unmatched call: %v", call) |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
// Check each expectation. |
| 550 |
for call, note := range expectations { |
| 551 |
var args []string |
| 552 |
for _, a := range call.Args { |
| 553 |
args = append(args, a.Type().String()) |
| 554 |
} |
| 555 |
if got, want := fmt.Sprint(args), fmt.Sprint(note.Args); got != want { |
| 556 |
t.Errorf("Arguments to print() were expected to be %q. got %q", want, got) |
| 557 |
logFunction(t, probes[call]) |
| 558 |
} |
| 559 |
} |
| 560 |
}) |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
// callsTo finds all calls to an SSA value named fname, |
| 565 |
// and returns a map from each call site to its enclosing function. |
| 566 |
func callsTo(fns map[*ir.Function]bool, fname string) map[*ir.CallCommon]*ir.Function { |
| 567 |
callsites := make(map[*ir.CallCommon]*ir.Function) |
| 568 |
for fn := range fns { |
| 569 |
for _, bb := range fn.Blocks { |
| 570 |
for _, i := range bb.Instrs { |
| 571 |
if i, ok := i.(ir.CallInstruction); ok { |
| 572 |
call := i.Common() |
| 573 |
if call.Value.Name() == fname { |
| 574 |
callsites[call] = fn |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
return callsites |
| 581 |
} |
| 582 |
|
| 583 |
// matchNotes returns a mapping from call sites (found by callsTo) |
| 584 |
// to the first "//@ note" comment on the same line. |
| 585 |
func matchNotes(fset *token.FileSet, notes []*expect.Note, calls map[*ir.CallCommon]*ir.Function) map[*ir.CallCommon]*expect.Note { |
| 586 |
// Matches each probe with a note that has the same line. |
| 587 |
sameLine := func(x, y token.Pos) bool { |
| 588 |
xp := fset.Position(x) |
| 589 |
yp := fset.Position(y) |
| 590 |
return xp.Filename == yp.Filename && xp.Line == yp.Line |
| 591 |
} |
| 592 |
expectations := make(map[*ir.CallCommon]*expect.Note) |
| 593 |
for call := range calls { |
| 594 |
for _, note := range notes { |
| 595 |
if sameLine(call.Pos(), note.Pos) { |
| 596 |
expectations[call] = note |
| 597 |
break // first match is good enough. |
| 598 |
} |
| 599 |
} |
| 600 |
} |
| 601 |
return expectations |
| 602 |
} |
| 603 |
|
| 604 |
// TestInstructionString tests serializing instructions via Instruction.String(). |
| 605 |
func TestInstructionString(t *testing.T) { |
| 606 |
// Tests (ir.Instruction).String(). Instructions are from a single go file. |
| 607 |
// The Instructions tested are those that match a comment of the form: |
| 608 |
// |
| 609 |
// //@ instrs(f, kind, strs...) |
| 610 |
// |
| 611 |
// where f is the name of the function, kind is the type of the instructions matched |
| 612 |
// within the function, and tests that the String() value for all of the instructions |
| 613 |
// matched of String() is strs (in some order). |
| 614 |
// See x/tools/go/expect for details on the syntax. |
| 615 |
|
| 616 |
const contents = ` |
| 617 |
package p |
| 618 |
|
| 619 |
//@ instrs("f0", "*ir.TypeAssert") |
| 620 |
//@ instrs("f0", "*ir.Call", "print(nil:interface{}, 0:int)") |
| 621 |
func f0(x int) { // non-generic smoke test. |
| 622 |
var i interface{} |
| 623 |
print(i, 0) |
| 624 |
} |
| 625 |
|
| 626 |
//@ instrs("f1", "*ir.CompositeValue", "{x: \"lorem\":string}") |
| 627 |
func f1[T ~struct{ x string }]() T { |
| 628 |
u := T{"lorem"} |
| 629 |
return u |
| 630 |
} |
| 631 |
|
| 632 |
//@ instrs("f1b", "*ir.Alloc", "new T (complit)") |
| 633 |
func f1b[T ~struct{ x string }]() *T { |
| 634 |
u := &T{"lorem"} |
| 635 |
return u |
| 636 |
} |
| 637 |
|
| 638 |
//@ instrs("f2", "*ir.TypeAssert", "typeassert t1.(interface{})") |
| 639 |
//@ instrs("f2", "*ir.Call", "invoke x.foo()") |
| 640 |
func f2[T interface{ foo() string }](x T) { |
| 641 |
_ = x.foo |
| 642 |
_ = x.foo() |
| 643 |
} |
| 644 |
|
| 645 |
//@ instrs("f3", "*ir.TypeAssert", "typeassert t1.(interface{})") |
| 646 |
//@ instrs("f3", "*ir.Call", "invoke x.foo()") |
| 647 |
func f3[T interface{ foo() string; comparable }](x T) { |
| 648 |
_ = x.foo |
| 649 |
_ = x.foo() |
| 650 |
} |
| 651 |
|
| 652 |
//@ instrs("f4", "*ir.BinOp", "t3 + 1:int", "t4 < 4:int") |
| 653 |
//@ instrs("f4", "*ir.Call", "f()", "print(t4, t7)") |
| 654 |
func f4[T [4]string](f func() T) { |
| 655 |
for i, v := range f() { |
| 656 |
print(i, v) |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
//@ instrs("f5", "*ir.Call", "nil:func()()") |
| 661 |
func f5() { |
| 662 |
var f func() |
| 663 |
f() |
| 664 |
} |
| 665 |
|
| 666 |
type S struct{ f int } |
| 667 |
|
| 668 |
//@ instrs("f6", "*ir.Alloc", "new [1]P (slicelit)", "new S (complit)") |
| 669 |
func f6[P *S]() []P { return []P{{f: 1}} } |
| 670 |
|
| 671 |
//@ instrs("f8", "*ir.Alloc", "new [1]P (slicelit)", "new struct{f T} (complit)") |
| 672 |
func f8[T any, P *struct{f T}](x T) []P { return []P{{f: x}} } |
| 673 |
|
| 674 |
//@ instrs("f9", "*ir.Alloc", "new [1]PS (slicelit)", "new S (complit)") |
| 675 |
func f9[T any, S struct{f T}, PS *S](x T) { |
| 676 |
_ = []PS{{f: x}} |
| 677 |
} |
| 678 |
|
| 679 |
//@ instrs("f10", "*ir.CompositeValue", "{x: 4:int, y: 0:int}") |
| 680 |
func f10[T ~struct{ x, y int }]() T { |
| 681 |
var u T |
| 682 |
u = T{x: 4} |
| 683 |
return u |
| 684 |
} |
| 685 |
|
| 686 |
//@ instrs("f12", "*ir.Alloc", "new struct{f T} (complit)") |
| 687 |
//@ instrs("f12", "*ir.MakeMap", "make map[P]bool 1:int") |
| 688 |
func f12[T any, P *struct{f T}](x T) map[P]bool { return map[P]bool{{}: true} } |
| 689 |
|
| 690 |
//@ instrs("f13", "*ir.IndexAddr", "&v[0:int]") |
| 691 |
//@ instrs("f13", "*ir.Store", "*t1 = 7:int", "*v = *new(A):A") |
| 692 |
func f13[A [3]int, PA *A](v PA) { |
| 693 |
*v = A{7} |
| 694 |
} |
| 695 |
|
| 696 |
//@ instrs("f14", "*ir.Call", "invoke t2.Set(0:int)") |
| 697 |
func f14[T any, PT interface { |
| 698 |
Set(int) |
| 699 |
*T |
| 700 |
}]() { |
| 701 |
var t T |
| 702 |
p := PT(&t) |
| 703 |
p.Set(0) |
| 704 |
} |
| 705 |
|
| 706 |
// t0 = new T (t) |
| 707 |
// t1 = changetype PT <- *T (t0) |
| 708 |
// t2 = changetype interface{Set(int); *T} <- PT (t1) |
| 709 |
// t3 = make closure (interface{Set(int); *T}).Set$bound [t2] |
| 710 |
// |
| 711 |
//@ instrs("f15", "*ir.MakeClosure", "make closure (interface{Set(int); *T}).Set$bound [t3]") |
| 712 |
func f15[T any, PT interface { |
| 713 |
Set(int) |
| 714 |
*T |
| 715 |
}]() func(int) { |
| 716 |
var t T |
| 717 |
p := PT(&t) |
| 718 |
return p.Set |
| 719 |
} |
| 720 |
` |
| 721 |
|
| 722 |
// Parse |
| 723 |
conf := loader.Config{ParserMode: parser.ParseComments} |
| 724 |
const fname = "p.go" |
| 725 |
f, err := conf.ParseFile(fname, contents) |
| 726 |
if err != nil { |
| 727 |
t.Fatalf("parse: %v", err) |
| 728 |
} |
| 729 |
conf.CreateFromFiles("p", f) |
| 730 |
|
| 731 |
// Load |
| 732 |
lprog, err := conf.Load() |
| 733 |
if err != nil { |
| 734 |
t.Fatalf("Load: %v", err) |
| 735 |
} |
| 736 |
|
| 737 |
// Create and build SSA |
| 738 |
prog := ir.NewProgram(lprog.Fset, ir.SanityCheckFunctions) |
| 739 |
for _, info := range lprog.AllPackages { |
| 740 |
if info.TransitivelyErrorFree { |
| 741 |
prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable) |
| 742 |
} |
| 743 |
} |
| 744 |
p := prog.Package(lprog.Package("p").Pkg) |
| 745 |
p.Build() |
| 746 |
|
| 747 |
// Collect all notes in f, i.e. comments starting with "//@ instr". |
| 748 |
notes, err := expect.ExtractGo(prog.Fset.File(f.Pos()), f) |
| 749 |
if err != nil { |
| 750 |
t.Errorf("expect.ExtractGo: %v", err) |
| 751 |
} |
| 752 |
|
| 753 |
// Expectation is a {function, type string} -> {want, matches} |
| 754 |
// where matches is all Instructions.String() that match the key. |
| 755 |
// Each expectation is that some permutation of matches is wants. |
| 756 |
type expKey struct { |
| 757 |
function string |
| 758 |
kind string |
| 759 |
} |
| 760 |
type expValue struct { |
| 761 |
wants []string |
| 762 |
matches []string |
| 763 |
} |
| 764 |
expectations := make(map[expKey]*expValue) |
| 765 |
for _, note := range notes { |
| 766 |
if note.Name == "instrs" { |
| 767 |
if len(note.Args) < 2 { |
| 768 |
t.Error("Had @instrs annotation without at least 2 arguments") |
| 769 |
continue |
| 770 |
} |
| 771 |
fn, kind := fmt.Sprint(note.Args[0]), fmt.Sprint(note.Args[1]) |
| 772 |
var wants []string |
| 773 |
for _, arg := range note.Args[2:] { |
| 774 |
wants = append(wants, fmt.Sprint(arg)) |
| 775 |
} |
| 776 |
expectations[expKey{fn, kind}] = &expValue{wants, nil} |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
// Collect all Instructions that match the expectations. |
| 781 |
for _, mem := range p.Members { |
| 782 |
if fn, ok := mem.(*ir.Function); ok { |
| 783 |
for _, bb := range fn.Blocks { |
| 784 |
for _, i := range bb.Instrs { |
| 785 |
kind := fmt.Sprintf("%T", i) |
| 786 |
if e := expectations[expKey{fn.Name(), kind}]; e != nil { |
| 787 |
e.matches = append(e.matches, i.String()) |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
// Check each expectation. |
| 795 |
for key, value := range expectations { |
| 796 |
fn, ok := p.Members[key.function].(*ir.Function) |
| 797 |
if !ok { |
| 798 |
t.Errorf("Expectation on %s does not match a member in %s", key.function, p.Pkg.Name()) |
| 799 |
} |
| 800 |
got, want := value.matches, value.wants |
| 801 |
sort.Strings(got) |
| 802 |
sort.Strings(want) |
| 803 |
if !reflect.DeepEqual(want, got) { |
| 804 |
t.Errorf("Within %s wanted instructions of kind %s: %q. got %q", key.function, key.kind, want, got) |
| 805 |
logFunction(t, fn) |
| 806 |
} |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
func logFunction(t testing.TB, fn *ir.Function) { |
| 811 |
// TODO: Consider adding a ir.Function.GoString() so this can be logged to t via '%#v'. |
| 812 |
var buf bytes.Buffer |
| 813 |
ir.WriteFunction(&buf, fn) |
| 814 |
t.Log(buf.String()) |
| 815 |
} |