@@ -14,7 +14,6 @@ import (
14
14
"testing"
15
15
16
16
"golang.org/x/tools/go/expect"
17
- "golang.org/x/tools/go/loader"
18
17
"golang.org/x/tools/go/ssa"
19
18
)
20
19
@@ -93,22 +92,22 @@ func TestGenericBodies(t *testing.T) {
93
92
94
93
func From() {
95
94
type A [4]byte
96
- print(a[A]) //@ types("func(x p03 .A)")
95
+ print(a[A]) //@ types("func(x example.com .A)")
97
96
98
97
type B *[4]byte
99
- print(b[B]) //@ types("func(x p03 .B)")
98
+ print(b[B]) //@ types("func(x example.com .B)")
100
99
101
100
type C []byte
102
- print(c[C]) //@ types("func(x p03 .C)")
101
+ print(c[C]) //@ types("func(x example.com .C)")
103
102
104
103
type D string
105
- print(d[D]) //@ types("func(x p03 .D)")
104
+ print(d[D]) //@ types("func(x example.com .D)")
106
105
107
106
type E map[int]string
108
- print(e[E]) //@ types("func(x p03 .E)")
107
+ print(e[E]) //@ types("func(x example.com .E)")
109
108
110
109
type F chan string
111
- print(f[F]) //@ types("func(x p03 .F)")
110
+ print(f[F]) //@ types("func(x example.com .F)")
112
111
}
113
112
` ,
114
113
`
@@ -122,7 +121,7 @@ func TestGenericBodies(t *testing.T) {
122
121
123
122
func From() {
124
123
type F chan string
125
- print(f[string, F]) //@ types("func(x p05 .F)")
124
+ print(f[string, F]) //@ types("func(x example.com .F)")
126
125
}
127
126
` ,
128
127
`
@@ -152,8 +151,8 @@ func TestGenericBodies(t *testing.T) {
152
151
type F chan int
153
152
c := make(F)
154
153
quit := make(F)
155
- print(start[F], c, quit) //@ types("func(c p06. F, quit p06. F)", "p06. F", "p06 .F")
156
- print(fibonacci[F], c, quit) //@ types("func(c p06. F, quit p06. F)", "p06. F", "p06 .F")
154
+ print(start[F], c, quit) //@ types("func(c example.com. F, quit example.com. F)", "example.com. F", "example.com .F")
155
+ print(fibonacci[F], c, quit) //@ types("func(c example.com. F, quit example.com. F)", "example.com. F", "example.com .F")
157
156
}
158
157
` ,
159
158
`
@@ -165,7 +164,7 @@ func TestGenericBodies(t *testing.T) {
165
164
}
166
165
func From() {
167
166
type S struct{ x int; y string }
168
- print(f[S]) //@ types("func(i int) p07 .S")
167
+ print(f[S]) //@ types("func(i int) example.com .S")
169
168
}
170
169
` ,
171
170
`
@@ -186,7 +185,7 @@ func TestGenericBodies(t *testing.T) {
186
185
type H []int32
187
186
print(f[F](F{}, 0, 0)) //@ types("[]int8")
188
187
print(g[G](nil, 0, 0)) //@ types("[]int16")
189
- print(h[H](nil, 0, 0)) //@ types("p08 .H")
188
+ print(h[H](nil, 0, 0)) //@ types("example.com .H")
190
189
}
191
190
` ,
192
191
`
@@ -322,18 +321,18 @@ func TestGenericBodies(t *testing.T) {
322
321
type MyInterface interface{ foo() }
323
322
324
323
// ChangeType tests
325
- func ct0(x int) { v := MyInt(x); print(x, v) /*@ types(int, "p15 .MyInt")*/ }
324
+ func ct0(x int) { v := MyInt(x); print(x, v) /*@ types(int, "example.com .MyInt")*/ }
326
325
func ct1[T MyInt | Other, S int ](x S) { v := T(x); print(x, v) /*@ types(S, T)*/ }
327
326
func ct2[T int, S MyInt | int ](x S) { v := T(x); print(x, v) /*@ types(S, T)*/ }
328
327
func ct3[T MyInt | Other, S MyInt | int ](x S) { v := T(x) ; print(x, v) /*@ types(S, T)*/ }
329
328
330
329
// Convert tests
331
- func co0[T int | int8](x MyInt) { v := T(x); print(x, v) /*@ types("p15 .MyInt", T)*/}
332
- func co1[T int | int8](x T) { v := MyInt(x); print(x, v) /*@ types(T, "p15 .MyInt")*/ }
330
+ func co0[T int | int8](x MyInt) { v := T(x); print(x, v) /*@ types("example.com .MyInt", T)*/}
331
+ func co1[T int | int8](x T) { v := MyInt(x); print(x, v) /*@ types(T, "example.com .MyInt")*/ }
333
332
func co2[S, T int | int8](x T) { v := S(x); print(x, v) /*@ types(T, S)*/ }
334
333
335
334
// MakeInterface tests
336
- func mi0[T MyInterface](x T) { v := MyInterface(x); print(x, v) /*@ types(T, "p15 .MyInterface")*/ }
335
+ func mi0[T MyInterface](x T) { v := MyInterface(x); print(x, v) /*@ types(T, "example.com .MyInterface")*/ }
337
336
338
337
// NewConst tests
339
338
func nc0[T any]() { v := (*T)(nil); print(v) /*@ types("*T")*/}
@@ -427,9 +426,9 @@ func TestGenericBodies(t *testing.T) {
427
426
Marker()
428
427
}](v T) {
429
428
v.Marker()
430
- a := *(any(v).(*A)); print(a) /*@ types("p23 .A")*/
431
- b := *(any(v).(*B)); print(b) /*@ types("p23 .B")*/
432
- c := *(any(v).(*C)); print(c) /*@ types("p23 .C")*/
429
+ a := *(any(v).(*A)); print(a) /*@ types("example.com .A")*/
430
+ b := *(any(v).(*B)); print(b) /*@ types("example.com .B")*/
431
+ c := *(any(v).(*C)); print(c) /*@ types("example.com .C")*/
433
432
}
434
433
` ,
435
434
`
@@ -519,32 +518,14 @@ func TestGenericBodies(t *testing.T) {
519
518
contents := contents
520
519
pkgname := packageName (t , contents )
521
520
t .Run (pkgname , func (t * testing.T ) {
522
- // Parse
523
- conf := loader.Config {ParserMode : parser .ParseComments }
524
- f , err := conf .ParseFile ("file.go" , contents )
525
- if err != nil {
526
- t .Fatalf ("parse: %v" , err )
527
- }
528
- conf .CreateFromFiles (pkgname , f )
529
-
530
- // Load
531
- lprog , err := conf .Load ()
532
- if err != nil {
533
- t .Fatalf ("Load: %v" , err )
534
- }
521
+ info := ssa .LoadPackageFromSingleFile (t , contents , ssa .SanityCheckFunctions )
522
+ p := info .SPkg
523
+ prog := p .Prog
535
524
536
- // Create and build SSA
537
- prog := ssa .NewProgram (lprog .Fset , ssa .SanityCheckFunctions )
538
- for _ , info := range lprog .AllPackages {
539
- if info .TransitivelyErrorFree {
540
- prog .CreatePackage (info .Pkg , info .Files , & info .Info , info .Importable )
541
- }
542
- }
543
- p := prog .Package (lprog .Package (pkgname ).Pkg )
544
525
p .Build ()
545
526
546
527
// Collect all notes in f, i.e. comments starting with "//@ types".
547
- notes , err := expect .ExtractGo (prog .Fset , f )
528
+ notes , err := expect .ExtractGo (prog .Fset , info . File )
548
529
if err != nil {
549
530
t .Errorf ("expect.ExtractGo: %v" , err )
550
531
}
@@ -754,33 +735,13 @@ func TestInstructionString(t *testing.T) {
754
735
}
755
736
`
756
737
757
- // Parse
758
- conf := loader.Config {ParserMode : parser .ParseComments }
759
- const fname = "p.go"
760
- f , err := conf .ParseFile (fname , contents )
761
- if err != nil {
762
- t .Fatalf ("parse: %v" , err )
763
- }
764
- conf .CreateFromFiles ("p" , f )
765
-
766
- // Load
767
- lprog , err := conf .Load ()
768
- if err != nil {
769
- t .Fatalf ("Load: %v" , err )
770
- }
771
-
772
- // Create and build SSA
773
- prog := ssa .NewProgram (lprog .Fset , ssa .SanityCheckFunctions )
774
- for _ , info := range lprog .AllPackages {
775
- if info .TransitivelyErrorFree {
776
- prog .CreatePackage (info .Pkg , info .Files , & info .Info , info .Importable )
777
- }
778
- }
779
- p := prog .Package (lprog .Package ("p" ).Pkg )
738
+ info := ssa .LoadPackageFromSingleFile (t , contents , ssa .SanityCheckFunctions )
739
+ p := info .SPkg
740
+ prog := p .Prog
780
741
p .Build ()
781
742
782
743
// Collect all notes in f, i.e. comments starting with "//@ instr".
783
- notes , err := expect .ExtractGo (prog .Fset , f )
744
+ notes , err := expect .ExtractGo (prog .Fset , info . File )
784
745
if err != nil {
785
746
t .Errorf ("expect.ExtractGo: %v" , err )
786
747
}
0 commit comments