Skip to content

Commit c5706c3

Browse files
committed
Add (failing) test for slice-to-array-pointer conversion added in Go 1.17
1 parent d919bd4 commit c5706c3

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

tests/slice_to_array_ptr_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package tests
2+
3+
import "testing"
4+
5+
func TestSliceToArrayPointerConversion(t *testing.T) {
6+
// https://tip.golang.org/ref/spec#Conversions_from_slice_to_array_pointer
7+
s := make([]byte, 2, 4)
8+
s0 := (*[0]byte)(s)
9+
if s0 == nil {
10+
t.Error("s0 should not be nil")
11+
}
12+
s2 := (*[2]byte)(s)
13+
if &s2[0] != &s[0] {
14+
t.Error("&s2[0] should match &s[0]")
15+
}
16+
r := func() (r interface{}) {
17+
defer func() {
18+
r = recover()
19+
}()
20+
s4 := (*[4]byte)(s)
21+
_ = s4
22+
return nil
23+
}()
24+
if r == nil {
25+
t.Error("out-of-bounds conversion of s should panic")
26+
}
27+
28+
(*s2)[0] = 'x'
29+
if s[0] != 'x' {
30+
t.Errorf("s[0] should be changed")
31+
}
32+
33+
var q []string
34+
q0 := (*[0]string)(q)
35+
if q0 != nil {
36+
t.Error("t0 should be nil")
37+
}
38+
r = func() (r interface{}) {
39+
defer func() {
40+
r = recover()
41+
}()
42+
q1 := (*[1]string)(q)
43+
_ = q1
44+
return nil
45+
}
46+
if r == nil {
47+
t.Error("out-of-bounds conversion of q should panic")
48+
}
49+
50+
u := make([]byte, 0)
51+
u0 := (*[0]byte)(u)
52+
if u0 == nil {
53+
t.Error("u0 should not be nil")
54+
}
55+
}

0 commit comments

Comments
 (0)