]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile: add import/export of calls to builtin functions
authorDan Scales <danscales@google.com>
Mon, 10 May 2021 17:17:51 +0000 (10:17 -0700)
committerDan Scales <danscales@google.com>
Fri, 21 May 2021 17:14:19 +0000 (17:14 +0000)
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>
src/cmd/compile/internal/typecheck/iexport.go
src/cmd/compile/internal/typecheck/iimport.go
test/typeparam/mapimp.dir/a.go [new file with mode: 0644]
test/typeparam/mapimp.dir/main.go [new file with mode: 0644]
test/typeparam/mapimp.go [new file with mode: 0644]

index d125dadd883d03802a3a34469ae8c054bd9f6b42..802a8c3839bbb7304b69ae90832c980e8026dd9e 100644 (file)
@@ -1579,6 +1579,16 @@ func (w *exportWriter) expr(n ir.Node) {
                // We don't need a type here, as the type will be provided at the
                // declaration of n.
                w.op(ir.ONAME)
+
+               // This handles the case where we haven't yet transformed a call
+               // to a builtin, so we must write out the builtin as a name in the
+               // builtin package.
+               isBuiltin := n.BuiltinOp != ir.OXXX
+               w.bool(isBuiltin)
+               if isBuiltin {
+                       w.string(n.Sym().Name)
+                       break
+               }
                w.localName(n)
 
        // case OPACK, ONONAME:
index 3b725a226c46953eede35a39188e66daf4f0919d..39b5ab09da1f4cf81647397d260e105a98fe43f4 100644 (file)
@@ -1184,6 +1184,10 @@ func (r *importReader) node() ir.Node {
                return n
 
        case ir.ONAME:
+               isBuiltin := r.bool()
+               if isBuiltin {
+                       return types.BuiltinPkg.Lookup(r.string()).Def.(*ir.Name)
+               }
                return r.localName()
 
        // case OPACK, ONONAME:
diff --git a/test/typeparam/mapimp.dir/a.go b/test/typeparam/mapimp.dir/a.go
new file mode 100644 (file)
index 0000000..6835e21
--- /dev/null
@@ -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 (file)
index 0000000..4d4a4d9
--- /dev/null
@@ -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))
+       }
+}
diff --git a/test/typeparam/mapimp.go b/test/typeparam/mapimp.go
new file mode 100644 (file)
index 0000000..76930e5
--- /dev/null
@@ -0,0 +1,7 @@
+// rundir -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 ignored