diff options
author | Dan Scales <danscales@google.com> | 2021-02-21 10:54:38 -0800 |
---|---|---|
committer | Dan Scales <danscales@google.com> | 2021-02-26 18:57:20 +0000 |
commit | d8e33d558e2c5fcd7f9092790780e68adbac0f1b (patch) | |
tree | 91b9f8086bc66fab3348a058d8c53f6efda99496 /test/typeparam/settable.go | |
parent | 19f96e73bf655764b57424cc9e00657f364ffb89 (diff) | |
download | go-d8e33d558e2c5fcd7f9092790780e68adbac0f1b.tar.gz go-d8e33d558e2c5fcd7f9092790780e68adbac0f1b.zip |
cmd/compile: deal with closures in generic functions and instantiated function values
- Deal with closures in generic functions by fixing the stenciling code
- Deal with instantiated function values (instantiated generic
functions that are not immediately called) during stenciling. This
requires changing the OFUNCINST node to an ONAME node for the
appropriately instantiated function. We do this in a second pass,
since this is uncommon, but requires editing the tree at multiple
levels.
- Check global assignments (as well as functions) for generic function
instantiations.
- Fix a bug in (*subst).typ where a generic type in a generic function
may definitely not use all the type args of the function, so we need
to translate the rparams of the type based on the tparams/targs of
the function.
- Added new test combine.go that tests out closures in generic
functions and instantiated function values.
- Added one new variant to the settable test.
- Enabling inlining functions with closures for -G=3. (For now, set
Ntype on closures in -G=3 mode to keep compatibility with later parts
of compiler, and allow inlining of functions with closures.)
Change-Id: Iea63d5704c322e42e2f750a83adc8b44f911d4ec
Reviewed-on: https://go-review.googlesource.com/c/go/+/296269
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dan Scales <danscales@google.com>
Diffstat (limited to 'test/typeparam/settable.go')
-rw-r--r-- | test/typeparam/settable.go | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/test/typeparam/settable.go b/test/typeparam/settable.go index 3bd141f784..7532953a77 100644 --- a/test/typeparam/settable.go +++ b/test/typeparam/settable.go @@ -11,7 +11,24 @@ import ( "strconv" ) -func fromStrings3[T any](s []string, set func(*T, string)) []T { +type Setter[B any] interface { + Set(string) + type *B +} + +func fromStrings1[T any, PT Setter[T]](s []string) []T { + result := make([]T, len(s)) + for i, v := range s { + // The type of &result[i] is *T which is in the type list + // of Setter, so we can convert it to PT. + p := PT(&result[i]) + // PT has a Set method. + p.Set(v) + } + return result +} + +func fromStrings2[T any](s []string, set func(*T, string)) []T { results := make([]T, len(s)) for i, v := range s { set(&results[i], v) @@ -30,8 +47,12 @@ func (p *Settable) Set(s string) { } func main() { - s := fromStrings3([]string{"1"}, - func(p *Settable, s string) { p.Set(s) }) + s := fromStrings1[Settable, *Settable]([]string{"1"}) + if len(s) != 1 || s[0] != 1 { + panic(fmt.Sprintf("got %v, want %v", s, []int{1})) + } + + s = fromStrings2([]string{"1"}, func(p *Settable, s string) { p.Set(s) }) if len(s) != 1 || s[0] != 1 { panic(fmt.Sprintf("got %v, want %v", s, []int{1})) } |