@@ -24,7 +24,6 @@ import (
24
24
25
25
"golang.org/x/sync/errgroup"
26
26
"golang.org/x/tools/go/analysis/analysistest"
27
- "golang.org/x/tools/go/buildutil"
28
27
"golang.org/x/tools/go/expect"
29
28
"golang.org/x/tools/go/loader"
30
29
"golang.org/x/tools/go/packages"
@@ -767,30 +766,28 @@ func sliceMax(s []int) []int { return s[a():b():c()] }
767
766
768
767
// TestGenericFunctionSelector ensures generic functions from other packages can be selected.
769
768
func TestGenericFunctionSelector (t * testing.T ) {
770
- pkgs := map [string ]map [string ]string {
771
- "main" : {"m.go" : `package main; import "a"; func main() { a.F[int](); a.G[int,string](); a.H(0) }` },
772
- "a" : {"a.go" : `package a; func F[T any](){}; func G[S, T any](){}; func H[T any](a T){} ` },
773
- }
769
+ fsys := overlayFS (map [string ][]byte {
770
+ "go.mod" : goMod ("example.com" , - 1 ),
771
+ "main.go" : []byte (`package main; import "example.com/a"; func main() { a.F[int](); a.G[int,string](); a.H(0) }` ),
772
+ "a/a.go" : []byte (`package a; func F[T any](){}; func G[S, T any](){}; func H[T any](a T){} ` ),
773
+ })
774
774
775
775
for _ , mode := range []ssa.BuilderMode {
776
776
ssa .SanityCheckFunctions ,
777
777
ssa .SanityCheckFunctions | ssa .InstantiateGenerics ,
778
778
} {
779
- conf := loader.Config {
780
- Build : buildutil .FakeContext (pkgs ),
781
- }
782
- conf .Import ("main" )
783
779
784
- lprog , err := conf . Load ()
785
- if err != nil {
786
- t .Errorf ( "Load failed: %s " , err )
780
+ pkgs := loadPackages ( t , fsys , "example.com" ) // package main
781
+ if len ( pkgs ) != 1 {
782
+ t .Fatalf ( "Expected 1 root package but got %d " , len ( pkgs ) )
787
783
}
788
- if lprog == nil {
789
- t .Fatalf ("Load returned nil *Program" )
784
+ prog , _ := ssautil .Packages (pkgs , mode )
785
+ p := prog .Package (pkgs [0 ].Types )
786
+ p .Build ()
787
+
788
+ if p .Pkg .Name () != "main" {
789
+ t .Fatalf ("Expected the second package is main but got %s" , p .Pkg .Name ())
790
790
}
791
- // Create and build SSA
792
- prog := ssautil .CreateProgram (lprog , mode )
793
- p := prog .Package (lprog .Package ("main" ).Pkg )
794
791
p .Build ()
795
792
796
793
var callees []string // callees of the CallInstruction.String() in main().
@@ -807,7 +804,7 @@ func TestGenericFunctionSelector(t *testing.T) {
807
804
}
808
805
sort .Strings (callees ) // ignore the order in the code.
809
806
810
- want := "[a.F[int] a.G[int string] a.H[int]]"
807
+ want := "[example.com/ a.F[int] example.com/ a.G[int string] example.com/ a.H[int]]"
811
808
if got := fmt .Sprint (callees ); got != want {
812
809
t .Errorf ("Expected main() to contain calls %v. got %v" , want , got )
813
810
}
0 commit comments