aboutsummaryrefslogtreecommitdiff
path: root/test/typeparam/boundmethod.go
diff options
context:
space:
mode:
authorDan Scales <danscales@google.com>2021-07-02 17:51:20 -0700
committerDan Scales <danscales@google.com>2021-07-12 16:09:57 +0000
commit1c783dc1480e8dec8bd4e76b744238607ea527f0 (patch)
tree286159b0c651880cc88eac4c0447022b740b7b1d /test/typeparam/boundmethod.go
parent0dcab98fd829e845a83fed996025f96b8b1165b1 (diff)
downloadgo-1c783dc1480e8dec8bd4e76b744238607ea527f0.tar.gz
go-1c783dc1480e8dec8bd4e76b744238607ea527f0.zip
[dev.typeparams] Add optional sub-dict entry for typeparam bound calls
In the case that a generic function/method f does a method call on a type param allowed by its bound, an instantiation of f may do a direct method call of a concrete type or a method call defined on a generic type, depending on whether the passed type in a concrete type or an instantiated type with the appropriate method defined. See the test case boundmethod.go added to this change. In order to keep the dictionary format the same for all instantiations of a generic function/method, I decided to have an optional sub-dictionary entry for "bounds" calls. At the point that we are creating the actual dictionary, we can then fill in the needed sub-dictionary, if the type arg is an instantiated type, or a zeroed dictionary entry, if type arg is not instantiated and the method will be on a concrete type. In order to implement this, I now fill in n.Selection for "bounds" method calls in generic functions as well. Also, I need to calculate n.Selection correctly during import for the case where it is now set - method calls on generic types, and bounds calls on typeparams. With this change, the dictionaries/sub-dictionaries are correct for absdiff.go. The new test boundmethod.go illustrates the case where the bound sub-dict entry is not used for a dictionary for stringify[myint], but is used for a dictionary for stringify[StringInt[myint]]. Change-Id: Ie2bcb971b7019a9f1da68c97eb03da2333327457 Reviewed-on: https://go-review.googlesource.com/c/go/+/333456 Run-TryBot: Dan Scales <danscales@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Trust: Dan Scales <danscales@google.com>
Diffstat (limited to 'test/typeparam/boundmethod.go')
-rw-r--r--test/typeparam/boundmethod.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/test/typeparam/boundmethod.go b/test/typeparam/boundmethod.go
new file mode 100644
index 0000000000..c150f9d85a
--- /dev/null
+++ b/test/typeparam/boundmethod.go
@@ -0,0 +1,60 @@
+// run -gcflags=-G=3
+
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This test illustrates how a type bound method (String below) can be implemented
+// either by a concrete type (myint below) or a instantiated generic type
+// (StringInt[myint] below).
+
+package main
+
+import (
+ "fmt"
+ "reflect"
+ "strconv"
+)
+
+type myint int
+
+//go:noinline
+func (m myint) String() string {
+ return strconv.Itoa(int(m))
+}
+
+type Stringer interface {
+ String() string
+}
+
+func stringify[T Stringer](s []T) (ret []string) {
+ for _, v := range s {
+ ret = append(ret, v.String())
+ }
+ return ret
+}
+
+type StringInt[T any] T
+
+//go:noinline
+func (m StringInt[T]) String() string {
+ return "aa"
+}
+
+func main() {
+ x := []myint{myint(1), myint(2), myint(3)}
+
+ got := stringify(x)
+ want := []string{"1", "2", "3"}
+ if !reflect.DeepEqual(got, want) {
+ panic(fmt.Sprintf("got %s, want %s", got, want))
+ }
+
+ x2 := []StringInt[myint]{StringInt[myint](1), StringInt[myint](2), StringInt[myint](3)}
+
+ got2 := stringify(x2)
+ want2 := []string{"aa", "aa", "aa"}
+ if !reflect.DeepEqual(got2, want2) {
+ panic(fmt.Sprintf("got %s, want %s", got2, want2))
+ }
+}