aboutsummaryrefslogtreecommitdiff
path: root/go/ssa/builder_test.go
diff options
context:
space:
mode:
authorTim King <taking@google.com>2022-04-01 17:37:52 -0700
committerTim King <taking@google.com>2022-04-08 13:27:25 +0000
commit7dd9f20ebac47dca3f1f54189ae40f1e9a1bd1e9 (patch)
treea73f5e133d91787d5715760dba8cedfa20ee96ea /go/ssa/builder_test.go
parentee2bc8be3a1239911a5b56f69078ca50a1c3ed5a (diff)
downloadgolang-x-tools-7dd9f20ebac47dca3f1f54189ae40f1e9a1bd1e9.tar.gz
go/ssa: Adds datastructures for function instantiation.
Adds [unexported] fields to Function for Origin, TypeParams, and TypeArguments. Populates TypeParameters for package level functions and methods. Adds datastructures for creating function instantiations. Tracking unique instantiations on Program. Adds map for canonicalizing lists of types. Updates golang/go#48525 Change-Id: I9cb01f2ed24a9cacf3a515444d0cc0474333e417 Reviewed-on: https://go-review.googlesource.com/c/tools/+/397714 Reviewed-by: Robert Findley <rfindley@google.com> Trust: Tim King <taking@google.com> Run-TryBot: Tim King <taking@google.com> gopls-CI: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'go/ssa/builder_test.go')
-rw-r--r--go/ssa/builder_test.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/go/ssa/builder_test.go b/go/ssa/builder_test.go
index c45f930b3..3990eff29 100644
--- a/go/ssa/builder_test.go
+++ b/go/ssa/builder_test.go
@@ -20,6 +20,7 @@ import (
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
+ "golang.org/x/tools/internal/typeparams"
)
func isEmpty(f *ssa.Function) bool { return f.Blocks == nil }
@@ -498,3 +499,65 @@ func h(error)
t.Errorf("expected a single Phi (for the range index), got %d", phis)
}
}
+
+// TestGenericDecls ensures that *unused* generic types, methods and functions
+// signatures can be built.
+//
+// TODO(taking): Add calls from non-generic functions to instantiations of generic functions.
+// TODO(taking): Add globals with types that are instantiations of generic functions.
+func TestGenericDecls(t *testing.T) {
+ if !typeparams.Enabled {
+ t.Skip("TestGenericDecls only works with type parameters enabled.")
+ }
+ const input = `
+package p
+
+import "unsafe"
+
+type Pointer[T any] struct {
+ v unsafe.Pointer
+}
+
+func (x *Pointer[T]) Load() *T {
+ return (*T)(LoadPointer(&x.v))
+}
+
+func Load[T any](x *Pointer[T]) *T {
+ return x.Load()
+}
+
+func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
+`
+ // The SSA members for this package should look something like this:
+ // func LoadPointer func(addr *unsafe.Pointer) (val unsafe.Pointer)
+ // type Pointer struct{v unsafe.Pointer}
+ // method (*Pointer[T any]) Load() *T
+ // func init func()
+ // var init$guard bool
+
+ // Parse
+ var conf loader.Config
+ f, err := conf.ParseFile("<input>", input)
+ if err != nil {
+ t.Fatalf("parse: %v", err)
+ }
+ conf.CreateFromFiles("p", f)
+
+ // Load
+ lprog, err := conf.Load()
+ if err != nil {
+ t.Fatalf("Load: %v", err)
+ }
+
+ // Create and build SSA
+ prog := ssautil.CreateProgram(lprog, 0)
+ p := prog.Package(lprog.Package("p").Pkg)
+ p.Build()
+
+ if load := p.Func("Load"); typeparams.ForSignature(load.Signature).Len() != 1 {
+ t.Errorf("expected a single type param T for Load got %q", load.Signature)
+ }
+ if ptr := p.Type("Pointer"); typeparams.ForNamed(ptr.Type().(*types.Named)).Len() != 1 {
+ t.Errorf("expected a single type param T for Pointer got %q", ptr.Type())
+ }
+}