aboutsummaryrefslogtreecommitdiff
path: root/test/typeparam
diff options
context:
space:
mode:
authorDan Scales <danscales@google.com>2021-08-12 18:39:24 -0700
committerDan Scales <danscales@google.com>2021-08-31 19:07:50 +0000
commit891470fbf767a09ac2b00cc58dbaf9ee2a67b09e (patch)
tree41cc7adf70196198b262d520a277015ed40bd8a1 /test/typeparam
parent46121306d3ceb97a8bb2acb328d68426ad3aa805 (diff)
downloadgo-891470fbf767a09ac2b00cc58dbaf9ee2a67b09e.tar.xz
cmd/compile: fix handling of Defn field during stenciling
When the Defn field of a name node is not an ONAME (for a closure variable), then it points to a body node of the same function/closure. Therefore, we should not attempt to substitute it at the time we are substituting the local variables. Instead, we remember a mapping from the Defn node to the nodes that reference it, and update the Defn fields of the copied name nodes at the time that we create the new copy of the Defn node. Added some comments to the Defn field of ir.Name. Moved the Defn (and Outer code, for consistency) from namelist() to localvar(), since Defn needs to updated for all local variables, not just those in a closure. Fixed case where .Defn was not being set properly in noder2 for type switches. Fixed another case where the Defn field had to be updated during transformSelect() because the Defn node was being completely changed to a new node. Fixed some spacing in typeswitch2.go Fixes #47676 Fixes #48016 Change-Id: Iae70dd76575f4a647c1db79e1eba9bbe44bfc226 Reviewed-on: https://go-review.googlesource.com/c/go/+/346290 Trust: Dan Scales <danscales@google.com> Run-TryBot: Dan Scales <danscales@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'test/typeparam')
-rw-r--r--test/typeparam/issue47676.go23
-rw-r--r--test/typeparam/issue48016.go35
-rw-r--r--test/typeparam/typeswitch2.go4
3 files changed, 60 insertions, 2 deletions
diff --git a/test/typeparam/issue47676.go b/test/typeparam/issue47676.go
new file mode 100644
index 0000000000..1b01624ce0
--- /dev/null
+++ b/test/typeparam/issue47676.go
@@ -0,0 +1,23 @@
+// 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.
+
+package main
+
+func main() {
+ d := diff([]int{}, func(int) string {
+ return "foo"
+ })
+ d()
+}
+
+func diff[T any](previous []T, uniqueKey func(T) string) func() {
+ return func() {
+ newJSON := map[string]T{}
+ for _, prev := range previous {
+ delete(newJSON, uniqueKey(prev))
+ }
+ }
+}
diff --git a/test/typeparam/issue48016.go b/test/typeparam/issue48016.go
new file mode 100644
index 0000000000..582751e884
--- /dev/null
+++ b/test/typeparam/issue48016.go
@@ -0,0 +1,35 @@
+// 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.
+
+package main
+
+import (
+ "fmt"
+ "strconv"
+)
+
+func test1[T any](fn func(T) int, v T) int {
+ fn1 := func() int {
+ var i interface{} = v
+ val := fn(i.(T))
+ return val
+ }
+ return fn1()
+}
+
+func main() {
+ want := 123
+ got := test1(func(s string) int {
+ r, err := strconv.Atoi(s)
+ if err != nil {
+ return 0
+ }
+ return r
+ }, "123")
+ if got != want {
+ panic(fmt.Sprintf("got %f, want %f", got, want))
+ }
+}
diff --git a/test/typeparam/typeswitch2.go b/test/typeparam/typeswitch2.go
index 913c56321c..0e434e1383 100644
--- a/test/typeparam/typeswitch2.go
+++ b/test/typeparam/typeswitch2.go
@@ -16,7 +16,7 @@ func f[T any](i interface{}) {
println("int", x)
case int32, int16:
println("int32/int16", reflect.ValueOf(x).Int())
- case struct { a, b T }:
+ case struct{ a, b T }:
println("struct{T,T}", x.a, x.b)
default:
println("other", reflect.ValueOf(x).Int())
@@ -26,6 +26,6 @@ func main() {
f[float64](float64(6))
f[float64](int(7))
f[float64](int32(8))
- f[float64](struct{a, b float64}{a:1, b:2})
+ f[float64](struct{ a, b float64 }{a: 1, b: 2})
f[float64](int8(9))
}