From 2548a8b1f56ce0247793bdb13633b079987251ce Mon Sep 17 00:00:00 2001 From: Tim King Date: Sun, 24 Apr 2022 21:09:33 -0700 Subject: [PATCH] ssautil: Add unit tests that set ssa.InstantiateGenerics Updates golang/go#52463 Updates golang/go#48525 Change-Id: I07cac2c9ad8d08a96cd14d4c729b14cf224d7f2e Reviewed-on: https://go-review.googlesource.com/c/tools/+/402055 Run-TryBot: Tim King gopls-CI: kokoro TryBot-Result: Gopher Robot Auto-Submit: Tim King Reviewed-by: Zvonimir Pavlinovic --- go/ssa/ssautil/load_test.go | 58 ++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/go/ssa/ssautil/load_test.go b/go/ssa/ssautil/load_test.go index 9b7eba9b8f..f769be273b 100644 --- a/go/ssa/ssautil/load_test.go +++ b/go/ssa/ssautil/load_test.go @@ -40,18 +40,23 @@ func TestBuildPackage(t *testing.T) { t.Fatal(err) } - pkg := types.NewPackage("hello", "") - mode := ssa.InstantiateGenerics - ssapkg, _, err := ssautil.BuildPackage(&types.Config{Importer: importer.Default()}, fset, pkg, []*ast.File{f}, mode) - if err != nil { - t.Fatal(err) - } - if pkg.Name() != "main" { - t.Errorf("pkg.Name() = %s, want main", pkg.Name()) - } - if ssapkg.Func("main") == nil { - ssapkg.WriteTo(os.Stderr) - t.Errorf("ssapkg has no main function") + for _, mode := range []ssa.BuilderMode{ + ssa.SanityCheckFunctions, + ssa.InstantiateGenerics | ssa.SanityCheckFunctions, + } { + pkg := types.NewPackage("hello", "") + ssapkg, _, err := ssautil.BuildPackage(&types.Config{Importer: importer.Default()}, fset, pkg, []*ast.File{f}, mode) + if err != nil { + t.Fatal(err) + } + if pkg.Name() != "main" { + t.Errorf("pkg.Name() = %s, want main", pkg.Name()) + } + if ssapkg.Func("main") == nil { + ssapkg.WriteTo(os.Stderr) + t.Errorf("ssapkg has no main function") + } + } } @@ -67,19 +72,23 @@ func TestPackages(t *testing.T) { t.Fatal("there were errors") } - prog, pkgs := ssautil.Packages(initial, ssa.InstantiateGenerics) - bytesNewBuffer := pkgs[0].Func("NewBuffer") - bytesNewBuffer.Pkg.Build() + for _, mode := range []ssa.BuilderMode{ + ssa.SanityCheckFunctions, + ssa.SanityCheckFunctions | ssa.InstantiateGenerics, + } { + prog, pkgs := ssautil.Packages(initial, mode) + bytesNewBuffer := pkgs[0].Func("NewBuffer") + bytesNewBuffer.Pkg.Build() - // We'll dump the SSA of bytes.NewBuffer because it is small and stable. - out := new(bytes.Buffer) - bytesNewBuffer.WriteTo(out) + // We'll dump the SSA of bytes.NewBuffer because it is small and stable. + out := new(bytes.Buffer) + bytesNewBuffer.WriteTo(out) - // For determinism, sanitize the location. - location := prog.Fset.Position(bytesNewBuffer.Pos()).String() - got := strings.Replace(out.String(), location, "$GOROOT/src/bytes/buffer.go:1", -1) + // For determinism, sanitize the location. + location := prog.Fset.Position(bytesNewBuffer.Pos()).String() + got := strings.Replace(out.String(), location, "$GOROOT/src/bytes/buffer.go:1", -1) - want := ` + want := ` # Name: bytes.NewBuffer # Package: bytes # Location: $GOROOT/src/bytes/buffer.go:1 @@ -91,8 +100,9 @@ func NewBuffer(buf []byte) *Buffer: return t0 `[1:] - if got != want { - t.Errorf("bytes.NewBuffer SSA = <<%s>>, want <<%s>>", got, want) + if got != want { + t.Errorf("bytes.NewBuffer SSA = <<%s>>, want <<%s>>", got, want) + } } }