]> Cypherpunks.ru repositories - gostls13.git/blob - test/convert4.go
cmd/compile/internal/inline: score call sites exposed by inlines
[gostls13.git] / test / convert4.go
1 // run
2
3 // Copyright 2020 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 // Test conversion from slice to array pointer.
8
9 package main
10
11 func wantPanic(fn func(), s string) {
12         defer func() {
13                 err := recover()
14                 if err == nil {
15                         panic("expected panic")
16                 }
17                 if got := err.(error).Error(); got != s {
18                         panic("expected panic " + s + " got " + got)
19                 }
20         }()
21         fn()
22 }
23
24 func main() {
25         s := make([]byte, 8, 10)
26         for i := range s {
27                 s[i] = byte(i)
28         }
29         if p := (*[8]byte)(s); &p[0] != &s[0] {
30                 panic("*[8]byte conversion failed")
31         }
32         if [8]byte(s) != *(*[8]byte)(s) {
33                 panic("[8]byte conversion failed")
34         }
35         wantPanic(
36                 func() {
37                         _ = (*[9]byte)(s)
38                 },
39                 "runtime error: cannot convert slice with length 8 to array or pointer to array with length 9",
40         )
41         wantPanic(
42                 func() {
43                         _ = [9]byte(s)
44                 },
45                 "runtime error: cannot convert slice with length 8 to array or pointer to array with length 9",
46         )
47
48         var n []byte
49         if p := (*[0]byte)(n); p != nil {
50                 panic("nil slice converted to *[0]byte should be nil")
51         }
52         _ = [0]byte(n)
53
54         z := make([]byte, 0)
55         if p := (*[0]byte)(z); p == nil {
56                 panic("empty slice converted to *[0]byte should be non-nil")
57         }
58         _ = [0]byte(z)
59
60         var p *[]byte
61         wantPanic(
62                 func() {
63                         _ = [0]byte(*p) // evaluating *p should still panic
64                 },
65                 "runtime error: invalid memory address or nil pointer dereference",
66         )
67
68         // Test with named types
69         type Slice []int
70         type Int4 [4]int
71         type PInt4 *[4]int
72         ii := make(Slice, 4)
73         if p := (*Int4)(ii); &p[0] != &ii[0] {
74                 panic("*Int4 conversion failed")
75         }
76         if p := PInt4(ii); &p[0] != &ii[0] {
77                 panic("PInt4 conversion failed")
78         }
79 }
80
81 // test static variable conversion
82
83 var (
84         ss  = make([]string, 10)
85         s5  = (*[5]string)(ss)
86         s10 = (*[10]string)(ss)
87
88         ns  []string
89         ns0 = (*[0]string)(ns)
90
91         zs  = make([]string, 0)
92         zs0 = (*[0]string)(zs)
93 )
94
95 func init() {
96         if &ss[0] != &s5[0] {
97                 panic("s5 conversion failed")
98         }
99         if &ss[0] != &s10[0] {
100                 panic("s5 conversion failed")
101         }
102         if ns0 != nil {
103                 panic("ns0 should be nil")
104         }
105         if zs0 == nil {
106                 panic("zs0 should not be nil")
107         }
108 }