GoPLS Viewer

Home|gopls/go/ssa/interp/testdata/complit.go
1package main
2
3// Tests of composite literals.
4
5import "fmt"
6
7// Map literals.
8// TODO(adonovan): we can no longer print maps
9// until the interpreter supports (reflect.Value).MapRange.
10func _() {
11    type M map[int]int
12    m1 := []*M{{11}, &M{22}}
13    want := "map[1:1] map[2:2]"
14    if got := fmt.Sprint(*m1[0], *m1[1]); got != want {
15        panic(got)
16    }
17    m2 := []M{{11}, M{22}}
18    if got := fmt.Sprint(m2[0], m2[1]); got != want {
19        panic(got)
20    }
21}
22
23// Nonliteral keys in composite literal.
24func init() {
25    const zero int = 1
26    var v = []int{1 + zero42}
27    if x := fmt.Sprint(v); x != "[0 0 42]" {
28        panic(x)
29    }
30}
31
32// Test for in-place initialization.
33func init() {
34    // struct
35    type S struct {
36        ab int
37    }
38    s := S{12}
39    s = S{b3}
40    if s.a != 0 {
41        panic("s.a != 0")
42    }
43    if s.b != 3 {
44        panic("s.b != 3")
45    }
46    s = S{}
47    if s.a != 0 {
48        panic("s.a != 0")
49    }
50    if s.b != 0 {
51        panic("s.b != 0")
52    }
53
54    // array
55    type A [4]int
56    a := A{2468}
57    a = A{1624}
58    if a[0] != 0 {
59        panic("a[0] != 0")
60    }
61    if a[1] != 6 {
62        panic("a[1] != 6")
63    }
64    if a[2] != 4 {
65        panic("a[2] != 4")
66    }
67    if a[3] != 0 {
68        panic("a[3] != 0")
69    }
70    a = A{}
71    if a[0] != 0 {
72        panic("a[0] != 0")
73    }
74    if a[1] != 0 {
75        panic("a[1] != 0")
76    }
77    if a[2] != 0 {
78        panic("a[2] != 0")
79    }
80    if a[3] != 0 {
81        panic("a[3] != 0")
82    }
83}
84
85// Regression test for https://golang.org/issue/10127:
86// composite literal clobbers destination before reading from it.
87func init() {
88    // map
89    {
90        type M map[string]int
91        m := M{"x"1"y"2}
92        m = M{"x"m["y"], "y"m["x"]}
93        if m["x"] != 2 || m["y"] != 1 {
94            panic(fmt.Sprint(m))
95        }
96
97        n := M{"x"3}
98        mn = M{"x"n["x"]}, M{"x"m["x"]} // parallel assignment
99        if got := fmt.Sprint(m["x"], n["x"]); got != "3 2" {
100            panic(got)
101        }
102    }
103
104    // struct
105    {
106        type T struct{ xyz int }
107        t := T{x1y2z3}
108
109        t = T{xt.yyt.zzt.x// all fields
110        if got := fmt.Sprint(t); got != "{2 3 1}" {
111            panic(got)
112        }
113
114        t = T{xt.yyt.z + 3// not all fields
115        if got := fmt.Sprint(t); got != "{3 4 0}" {
116            panic(got)
117        }
118
119        u := T{x5y6z7}
120        tu = T{xu.x}, T{xt.x// parallel assignment
121        if got := fmt.Sprint(tu); got != "{5 0 0} {3 0 0}" {
122            panic(got)
123        }
124    }
125
126    // array
127    {
128        a := [3]int{011223}
129
130        a = [3]int{0a[1], 1a[2], 2a[0]} //  all elements
131        if got := fmt.Sprint(a); got != "[2 3 1]" {
132            panic(got)
133        }
134
135        a = [3]int{0a[1], 1a[2] + 3//  not all elements
136        if got := fmt.Sprint(a); got != "[3 4 0]" {
137            panic(got)
138        }
139
140        b := [3]int{051627}
141        ab = [3]int{0b[0]}, [3]int{0a[0]} // parallel assignment
142        if got := fmt.Sprint(ab); got != "[5 0 0] [3 0 0]" {
143            panic(got)
144        }
145    }
146
147    // slice
148    {
149        s := []int{011223}
150
151        s = []int{0s[1], 1s[2], 2s[0]} //  all elements
152        if got := fmt.Sprint(s); got != "[2 3 1]" {
153            panic(got)
154        }
155
156        s = []int{0s[1], 1s[2] + 3//  not all elements
157        if got := fmt.Sprint(s); got != "[3 4]" {
158            panic(got)
159        }
160
161        t := []int{051627}
162        st = []int{0t[0]}, []int{0s[0]} // parallel assignment
163        if got := fmt.Sprint(st); got != "[5] [3]" {
164            panic(got)
165        }
166    }
167}
168
169// Regression test for https://golang.org/issue/13341:
170// within a map literal, if a key expression is a composite literal,
171// Go 1.5 allows its type to be omitted.  An & operation may be implied.
172func init() {
173    type S struct{ x int }
174    // same as map[*S]bool{&S{x: 1}: true}
175    m := map[*S]bool{{x1}: true}
176    for s := range m {
177        if s.x != 1 {
178            panic(s// wrong key
179        }
180        return
181    }
182    panic("map is empty")
183}
184
185func main() {
186}
187
MembersX
init.BlockStmt.a
init.BlockStmt.BlockStmt.a
init.BlockStmt.RangeStmt_3411.s
_
_.m2
init.BlockStmt.BlockStmt.T.x
init.BlockStmt.BlockStmt.T.y
init.BlockStmt.BlockStmt.T.z
init
init.x
init.BlockStmt.BlockStmt.M
init.BlockStmt.S.b
init.BlockStmt.A
init.BlockStmt.BlockStmt.n
init.BlockStmt.BlockStmt.s
init.BlockStmt.S.x
_.got
init.BlockStmt.BlockStmt.got
_.M
_.m1
init.BlockStmt.S
init.BlockStmt.s
init.zero
init.BlockStmt.BlockStmt.T
init.BlockStmt.BlockStmt.t
init.BlockStmt.BlockStmt.b
init.BlockStmt.m
_.want
init.BlockStmt.S.a
init.BlockStmt.BlockStmt.m
init.BlockStmt.BlockStmt.u
Members
X