]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: accept string|[]byte-constrained 2nd argument in append
authorRobert Griesemer <gri@golang.org>
Fri, 7 Jan 2022 02:02:30 +0000 (18:02 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 7 Jan 2022 22:40:23 +0000 (22:40 +0000)
Similarly to what we do for the built-in function `copy`,
where we allow a string as 2nd argument to append, also
permit a type parameter constrained by string|[]byte.

While at it, change date in the manual.go2 test files so
that we don't need to constantly correct it when copying
a test case from that file into a proper test file.

Fixes #50281.

Change-Id: I23fed66736aa07bb3c481fe97313e828425ac448
Reviewed-on: https://go-review.googlesource.com/c/go/+/376214
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/builtins.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue50281.go2 [new file with mode: 0644]
src/cmd/compile/internal/types2/testdata/manual.go2
src/go/types/builtins.go
src/go/types/testdata/fixedbugs/issue50281.go2 [new file with mode: 0644]
src/go/types/testdata/manual.go2
test/typeparam/issue376214.go [new file with mode: 0644]

index fcf02a697591ac20864a70d98fe54e6d8a2acbff..cea4fd36318cd149fb5a6bbfe9923c3204f7aefa 100644 (file)
@@ -101,7 +101,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
                                if x.mode == invalid {
                                        return
                                }
-                               if allString(x.typ) {
+                               if t := structuralString(x.typ); t != nil && isString(t) {
                                        if check.Types != nil {
                                                sig := makeSig(S, S, x.typ)
                                                sig.variadic = true
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue50281.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue50281.go2
new file mode 100644 (file)
index 0000000..f333e81
--- /dev/null
@@ -0,0 +1,26 @@
+// Copyright 2022 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 p
+
+func _[S string | []byte](s S) {
+       var buf []byte
+       _ = append(buf, s...)
+}
+
+func _[S ~string | ~[]byte](s S) {
+       var buf []byte
+       _ = append(buf, s...)
+}
+
+// test case from issue
+
+type byteseq interface {
+       string | []byte
+}
+
+// This should allow to eliminate the two functions above.
+func AppendByteString[source byteseq](buf []byte, s source) []byte {
+       return append(buf, s[1:6]...)
+}
index efe13cf8bc56c170d2a232230521b79e995bdc1b..96d4ba67c225ffc0eea310a70f8c98edb6304f74 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2021 The Go Authors. All rights reserved.
+// Copyright 2022 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.
 
index 828220f257a934ad1d685d7622e5632a5195431a..35a2d1ae2e6799c82fabc7d1d750b8897a852685 100644 (file)
@@ -102,7 +102,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
                                if x.mode == invalid {
                                        return
                                }
-                               if allString(x.typ) {
+                               if t := structuralString(x.typ); t != nil && isString(t) {
                                        if check.Types != nil {
                                                sig := makeSig(S, S, x.typ)
                                                sig.variadic = true
diff --git a/src/go/types/testdata/fixedbugs/issue50281.go2 b/src/go/types/testdata/fixedbugs/issue50281.go2
new file mode 100644 (file)
index 0000000..f333e81
--- /dev/null
@@ -0,0 +1,26 @@
+// Copyright 2022 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 p
+
+func _[S string | []byte](s S) {
+       var buf []byte
+       _ = append(buf, s...)
+}
+
+func _[S ~string | ~[]byte](s S) {
+       var buf []byte
+       _ = append(buf, s...)
+}
+
+// test case from issue
+
+type byteseq interface {
+       string | []byte
+}
+
+// This should allow to eliminate the two functions above.
+func AppendByteString[source byteseq](buf []byte, s source) []byte {
+       return append(buf, s[1:6]...)
+}
index 25e6f22f94b7336cd60ae59e226f047856476e01..a7caee99032d3b3f95a077bccf1cacbdd54bc8a4 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2021 The Go Authors. All rights reserved.
+// Copyright 2022 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.
 
diff --git a/test/typeparam/issue376214.go b/test/typeparam/issue376214.go
new file mode 100644 (file)
index 0000000..8f94f41
--- /dev/null
@@ -0,0 +1,20 @@
+// run -gcflags=-G=3
+
+// Copyright 2022 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 add[S ~string | ~[]byte](buf *[]byte, s S) {
+       *buf = append(*buf, s...)
+}
+
+func main() {
+       var buf []byte
+       add(&buf, "foo")
+       add(&buf, []byte("bar"))
+       if string(buf) != "foobar" {
+               panic("got " + string(buf))
+       }
+}