@@ -34,13 +34,8 @@ func TestGenericBodies(t *testing.T) {
3434 if ! typeparams .Enabled {
3535 t .Skip ("TestGenericBodies requires type parameters" )
3636 }
37- for _ , test := range []struct {
38- pkg string // name of the package.
39- contents string // contents of the Go package.
40- }{
41- {
42- pkg : "p" ,
43- contents : `
37+ for _ , contents := range []string {
38+ `
4439 package p
4540
4641 func f(x int) {
@@ -50,31 +45,22 @@ func TestGenericBodies(t *testing.T) {
5045 print(x) //@ types(int)
5146 }
5247 ` ,
53- },
54- {
55- pkg : "q" ,
56- contents : `
48+ `
5749 package q
5850
5951 func f[T any](x T) {
6052 print(x) //@ types(T)
6153 }
6254 ` ,
63- },
64- {
65- pkg : "r" ,
66- contents : `
55+ `
6756 package r
6857
6958 func f[T ~int]() {
7059 var x T
7160 print(x) //@ types(T)
7261 }
7362 ` ,
74- },
75- {
76- pkg : "s" ,
77- contents : `
63+ `
7864 package s
7965
8066 func a[T ~[4]byte](x T) {
@@ -128,10 +114,7 @@ func TestGenericBodies(t *testing.T) {
128114 print(f[F]) //@ types("func(x s.F)")
129115 }
130116 ` ,
131- },
132- {
133- pkg : "t" ,
134- contents : `
117+ `
135118 package t
136119
137120 func f[S any, T ~chan S](x T) {
@@ -145,10 +128,7 @@ func TestGenericBodies(t *testing.T) {
145128 print(f[string, F]) //@ types("func(x t.F)")
146129 }
147130 ` ,
148- },
149- {
150- pkg : "u" ,
151- contents : `
131+ `
152132 package u
153133
154134 func fibonacci[T ~chan int](c, quit T) {
@@ -179,10 +159,7 @@ func TestGenericBodies(t *testing.T) {
179159 print(fibonacci[F], c, quit) //@ types("func(c u.F, quit u.F)", "u.F", "u.F")
180160 }
181161 ` ,
182- },
183- {
184- pkg : "v" ,
185- contents : `
162+ `
186163 package v
187164
188165 func f[T ~struct{ x int; y string }](i int) T {
@@ -194,10 +171,7 @@ func TestGenericBodies(t *testing.T) {
194171 print(f[S]) //@ types("func(i int) v.S")
195172 }
196173 ` ,
197- },
198- {
199- pkg : "w" ,
200- contents : `
174+ `
201175 package w
202176
203177 func f[T ~[4]int8](x T, l, h int) []int8 {
@@ -218,10 +192,7 @@ func TestGenericBodies(t *testing.T) {
218192 print(h[H](nil, 0, 0)) //@ types("w.H")
219193 }
220194 ` ,
221- },
222- {
223- pkg : "x" ,
224- contents : `
195+ `
225196 package x
226197
227198 func h[E any, T ~[]E](x T, l, h int) []E {
@@ -234,10 +205,7 @@ func TestGenericBodies(t *testing.T) {
234205 print(h[int32, H](nil, 0, 0)) //@ types("[]int32")
235206 }
236207 ` ,
237- },
238- {
239- pkg : "y" ,
240- contents : `
208+ `
241209 package y
242210
243211 // Test "make" builtin with different forms on core types and
@@ -267,10 +235,7 @@ func TestGenericBodies(t *testing.T) {
267235 j[I, J](6)
268236 }
269237 ` ,
270- },
271- {
272- pkg : "z" ,
273- contents : `
238+ `
274239 package z
275240
276241 func h[T ~[4]int](x T) {
@@ -287,10 +252,7 @@ func TestGenericBodies(t *testing.T) {
287252 panic(x)
288253 }
289254 ` ,
290- },
291- {
292- pkg : "a" ,
293- contents : `
255+ `
294256 package a
295257
296258 func f[E any, F ~func() E](x F) {
@@ -302,10 +264,7 @@ func TestGenericBodies(t *testing.T) {
302264 f[int, func() int](func() int { return 1 })
303265 }
304266 ` ,
305- },
306- {
307- pkg : "b" ,
308- contents : `
267+ `
309268 package b
310269
311270 func f[E any, M ~map[string]E](m M) {
@@ -317,10 +276,7 @@ func TestGenericBodies(t *testing.T) {
317276 f(O{"lorem": []int{0, 1, 2, 3}})
318277 }
319278 ` ,
320- },
321- {
322- pkg : "c" ,
323- contents : `
279+ `
324280 package c
325281
326282 func a[T interface{ []int64 | [5]int64 }](x T) int64 {
@@ -361,10 +317,7 @@ func TestGenericBodies(t *testing.T) {
361317 print(t, &t[0]) //@ types(T, "*int")
362318 }
363319 ` ,
364- },
365- {
366- pkg : "d" ,
367- contents : `
320+ `
368321 package d
369322
370323 type MyInt int
@@ -392,37 +345,25 @@ func TestGenericBodies(t *testing.T) {
392345 func sl0[T *[4]int | *[2]int](x []int) { v := T(x); print(x, v) /*@ types("[]int", T)*/ }
393346 func sl1[T *[4]int | *[2]int, S []int](x S) { v := T(x); print(x, v) /*@ types(S, T)*/ }
394347 ` ,
395- },
396- {
397- pkg : "e" ,
398- contents : `
348+ `
399349 package e
400350
401351 func c[T interface{ foo() string }](x T) {
402352 print(x, x.foo, x.foo()) /*@ types(T, "func() string", string)*/
403353 }
404354 ` ,
405- },
406- {
407- pkg : "f" ,
408- contents : `package f
355+ `package f
409356
410357 func eq[T comparable](t T, i interface{}) bool {
411358 return t == i
412359 }
413360 ` ,
414- },
415- {
416- // TODO(59983): investigate why writing g.c panics in (*FieldAddr).String.
417- pkg : "g" ,
418- contents : `package g
361+ // TODO(59983): investigate why writing g.c panics in (*FieldAddr).String.
362+ `package g
419363 type S struct{ f int }
420364 func c[P *S]() []P { return []P{{f: 1}} }
421365 ` ,
422- },
423- {
424- pkg : "h" ,
425- contents : `package h
366+ `package h
426367 func sign[bytes []byte | string](s bytes) (bool, bool) {
427368 neg := false
428369 if len(s) > 0 && (s[0] == '-' || s[0] == '+') {
@@ -431,10 +372,7 @@ func TestGenericBodies(t *testing.T) {
431372 }
432373 return !neg, len(s) > 0
433374 }` ,
434- },
435- {
436- pkg : "i" ,
437- contents : `package i
375+ `package i
438376 func digits[bytes []byte | string](s bytes) bool {
439377 for _, c := range []byte(s) {
440378 if c < '0' || '9' < c {
@@ -443,10 +381,7 @@ func TestGenericBodies(t *testing.T) {
443381 }
444382 return true
445383 }` ,
446- },
447- {
448- pkg : "j" ,
449- contents : `
384+ `
450385 package j
451386
452387 type E interface{}
@@ -458,10 +393,7 @@ func TestGenericBodies(t *testing.T) {
458393 return x
459394 }
460395 ` ,
461- },
462- {
463- pkg : "k" ,
464- contents : `
396+ `
465397 package k
466398
467399 func f[M any, PM *M](p PM) {
@@ -471,10 +403,7 @@ func TestGenericBodies(t *testing.T) {
471403 print(p) /*@ types(PM)*/
472404 }
473405 ` ,
474- },
475- {
476- pkg : "l" ,
477- contents : `
406+ `
478407 package l
479408
480409 type A struct{int}
@@ -497,18 +426,17 @@ func TestGenericBodies(t *testing.T) {
497426 b := *(any(v).(*B)); print(b) /*@ types("l.B")*/
498427 c := *(any(v).(*C)); print(c) /*@ types("l.C")*/
499428 }` ,
500- },
501429 } {
502- test := test
503- t .Run (test .pkg , func (t * testing.T ) {
430+ contents := contents
431+ pkgname := packageName (t , contents )
432+ t .Run (pkgname , func (t * testing.T ) {
504433 // Parse
505434 conf := loader.Config {ParserMode : parser .ParseComments }
506- fname := test .pkg + ".go"
507- f , err := conf .ParseFile (fname , test .contents )
435+ f , err := conf .ParseFile ("file.go" , contents )
508436 if err != nil {
509437 t .Fatalf ("parse: %v" , err )
510438 }
511- conf .CreateFromFiles (test . pkg , f )
439+ conf .CreateFromFiles (pkgname , f )
512440
513441 // Load
514442 lprog , err := conf .Load ()
@@ -523,7 +451,7 @@ func TestGenericBodies(t *testing.T) {
523451 prog .CreatePackage (info .Pkg , info .Files , & info .Info , info .Importable )
524452 }
525453 }
526- p := prog .Package (lprog .Package (test . pkg ).Pkg )
454+ p := prog .Package (lprog .Package (pkgname ).Pkg )
527455 p .Build ()
528456
529457 // Collect calls to the builtin print function.
@@ -717,3 +645,13 @@ func TestInstructionString(t *testing.T) {
717645 }
718646 }
719647}
648+
649+ // packageName is a test helper to extract the package name from a string
650+ // containing the content of a go file.
651+ func packageName (t testing.TB , content string ) string {
652+ f , err := parser .ParseFile (token .NewFileSet (), "" , content , parser .PackageClauseOnly )
653+ if err != nil {
654+ t .Fatalf ("parsing the file %q failed with error: %s" , content , err )
655+ }
656+ return f .Name .Name
657+ }
0 commit comments