Skip to content

Commit f002efa

Browse files
committed
go/ssa: remove outdated doc in CreatePackage and add respective test cases
It also removes the deprecated loader package and uses packages and txtar to load the package for testing. Now all packages are created under a go module.
1 parent 91d4bdb commit f002efa

File tree

11 files changed

+435
-446
lines changed

11 files changed

+435
-446
lines changed

go/ssa/builder_generic_test.go

Lines changed: 26 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"testing"
1515

1616
"golang.org/x/tools/go/expect"
17-
"golang.org/x/tools/go/loader"
1817
"golang.org/x/tools/go/ssa"
1918
)
2019

@@ -93,22 +92,22 @@ func TestGenericBodies(t *testing.T) {
9392
9493
func From() {
9594
type A [4]byte
96-
print(a[A]) //@ types("func(x p03.A)")
95+
print(a[A]) //@ types("func(x example.com.A)")
9796
9897
type B *[4]byte
99-
print(b[B]) //@ types("func(x p03.B)")
98+
print(b[B]) //@ types("func(x example.com.B)")
10099
101100
type C []byte
102-
print(c[C]) //@ types("func(x p03.C)")
101+
print(c[C]) //@ types("func(x example.com.C)")
103102
104103
type D string
105-
print(d[D]) //@ types("func(x p03.D)")
104+
print(d[D]) //@ types("func(x example.com.D)")
106105
107106
type E map[int]string
108-
print(e[E]) //@ types("func(x p03.E)")
107+
print(e[E]) //@ types("func(x example.com.E)")
109108
110109
type F chan string
111-
print(f[F]) //@ types("func(x p03.F)")
110+
print(f[F]) //@ types("func(x example.com.F)")
112111
}
113112
`,
114113
`
@@ -122,7 +121,7 @@ func TestGenericBodies(t *testing.T) {
122121
123122
func From() {
124123
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)")
126125
}
127126
`,
128127
`
@@ -152,8 +151,8 @@ func TestGenericBodies(t *testing.T) {
152151
type F chan int
153152
c := make(F)
154153
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")
157156
}
158157
`,
159158
`
@@ -165,7 +164,7 @@ func TestGenericBodies(t *testing.T) {
165164
}
166165
func From() {
167166
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")
169168
}
170169
`,
171170
`
@@ -186,7 +185,7 @@ func TestGenericBodies(t *testing.T) {
186185
type H []int32
187186
print(f[F](F{}, 0, 0)) //@ types("[]int8")
188187
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")
190189
}
191190
`,
192191
`
@@ -322,18 +321,18 @@ func TestGenericBodies(t *testing.T) {
322321
type MyInterface interface{ foo() }
323322
324323
// 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")*/ }
326325
func ct1[T MyInt | Other, S int ](x S) { v := T(x); print(x, v) /*@ types(S, T)*/ }
327326
func ct2[T int, S MyInt | int ](x S) { v := T(x); print(x, v) /*@ types(S, T)*/ }
328327
func ct3[T MyInt | Other, S MyInt | int ](x S) { v := T(x) ; print(x, v) /*@ types(S, T)*/ }
329328
330329
// 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")*/ }
333332
func co2[S, T int | int8](x T) { v := S(x); print(x, v) /*@ types(T, S)*/ }
334333
335334
// 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")*/ }
337336
338337
// NewConst tests
339338
func nc0[T any]() { v := (*T)(nil); print(v) /*@ types("*T")*/}
@@ -427,9 +426,9 @@ func TestGenericBodies(t *testing.T) {
427426
Marker()
428427
}](v T) {
429428
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")*/
433432
}
434433
`,
435434
`
@@ -519,32 +518,14 @@ func TestGenericBodies(t *testing.T) {
519518
contents := contents
520519
pkgname := packageName(t, contents)
521520
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
535524

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)
544525
p.Build()
545526

546527
// 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)
548529
if err != nil {
549530
t.Errorf("expect.ExtractGo: %v", err)
550531
}
@@ -754,33 +735,13 @@ func TestInstructionString(t *testing.T) {
754735
}
755736
`
756737

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
780741
p.Build()
781742

782743
// 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)
784745
if err != nil {
785746
t.Errorf("expect.ExtractGo: %v", err)
786747
}

0 commit comments

Comments
 (0)