aboutsummaryrefslogtreecommitdiff
path: root/test/typeparam/mapimp.dir
diff options
context:
space:
mode:
authorDan Scales <danscales@google.com>2021-05-10 10:17:51 -0700
committerDan Scales <danscales@google.com>2021-05-21 17:14:19 +0000
commitb1a398cf0f04ac911be204d3c0ee001a94621683 (patch)
tree46dc3881562f151d261423b87b60f19ef51c816f /test/typeparam/mapimp.dir
parentccbfbb1c3327fffe88dd6b6da550f4a0cd37db6e (diff)
downloadgo-b1a398cf0f04ac911be204d3c0ee001a94621683.tar.gz
go-b1a398cf0f04ac911be204d3c0ee001a94621683.zip
[dev.typeparams] cmd/compile: add import/export of calls to builtin functions
For generic functions, we have to leave the builtins in OCALL form, rather than transform to specific ops, since we don't know the exact types involved. Allow export/import of builtins in OCALL form. Added new export/import test mapimp.go. Change-Id: I571f8eeaa13b4f69389dbdb9afb6cc61924b9bf2 Reviewed-on: https://go-review.googlesource.com/c/go/+/321750 Trust: Dan Scales <danscales@google.com> Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'test/typeparam/mapimp.dir')
-rw-r--r--test/typeparam/mapimp.dir/a.go15
-rw-r--r--test/typeparam/mapimp.dir/main.go28
2 files changed, 43 insertions, 0 deletions
diff --git a/test/typeparam/mapimp.dir/a.go b/test/typeparam/mapimp.dir/a.go
new file mode 100644
index 0000000000..6835e214b8
--- /dev/null
+++ b/test/typeparam/mapimp.dir/a.go
@@ -0,0 +1,15 @@
+// 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 a
+
+// Map calls the function f on every element of the slice s,
+// returning a new slice of the results.
+func Mapper[F, T any](s []F, f func(F) T) []T {
+ r := make([]T, len(s))
+ for i, v := range s {
+ r[i] = f(v)
+ }
+ return r
+}
diff --git a/test/typeparam/mapimp.dir/main.go b/test/typeparam/mapimp.dir/main.go
new file mode 100644
index 0000000000..4d4a4d9eb0
--- /dev/null
+++ b/test/typeparam/mapimp.dir/main.go
@@ -0,0 +1,28 @@
+// 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 (
+ "a"
+ "fmt"
+ "reflect"
+ "strconv"
+)
+
+func main() {
+ got := a.Mapper([]int{1, 2, 3}, strconv.Itoa)
+ want := []string{"1", "2", "3"}
+ if !reflect.DeepEqual(got, want) {
+ panic(fmt.Sprintf("got %s, want %s", got, want))
+ }
+
+ fgot := a.Mapper([]float64{2.5, 2.3, 3.5}, func(f float64) string {
+ return strconv.FormatFloat(f, 'f', -1, 64)
+ })
+ fwant := []string{"2.5", "2.3", "3.5"}
+ if !reflect.DeepEqual(fgot, fwant) {
+ panic(fmt.Sprintf("got %s, want %s", fgot, fwant))
+ }
+}