Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/ssa: test creating package after program has been built #521

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions go/ssa/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go/parser"
"go/token"
"go/types"
"golang.org/x/tools/go/packages"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -1609,3 +1610,65 @@ func TestBuildPackageGo120(t *testing.T) {
})
}
}

// TestCreateNewPackageAfterBuild tests creating a new package after the program has been built,
// and verifies the slices.Contains has instantiated correctly.
func TestCreateNewPackageAfterBuild(t *testing.T) {
content1 := `
package p
import "slices"
func main(){
ints := []int{1, 2, 3, 4, 5}
slices.Contains(ints, 1)
}`
content2 := `
package p2
import "slices"

func After(){
numbers := []float32{1, 2, 3, 4, 5}
slices.Contains(numbers, 1)
}`

name := parsePackageClause(t, content1)
fs := overlayFS(map[string][]byte{
"go.mod": goMod(name, -1),
"p.go": []byte(content1),
"sub/p2.go": []byte(content2),
})

pkgs := loadPackages(t, fs, "./...")

var p2 *packages.Package
// remove the package p2 so we can build the other packages first
for i, pkg := range pkgs {
if pkg.Name == "p2" {
p2 = pkg
pkgs = append(pkgs[:i], pkgs[i+1:]...)
}
}

if p2 == nil {
t.Fatal("cannot find package p2 in the loaded packages")
}

prog, _ := ssautil.Packages(pkgs, ssa.InstantiateGenerics)
prog.Build()

// create a package after the whole program has been built, and build the new created package
npkg := prog.CreatePackage(p2.Types, p2.Syntax, p2.TypesInfo, true)
npkg.Build()

var pkgSlices *ssa.Package
for _, pkg := range prog.AllPackages() {
if pkg.Pkg.Name() == "slices" {
pkgSlices = pkg
}
}

all := ssautil.AllFunctions(prog)
number := len(instancesOf(all, pkgSlices.Func("Contains")))
if number != 2 {
t.Errorf("slices.Contains should have 2 instances but got %d", number)
}
}