Source file
test/inline.go
1
2
3
4
5
6
7
8
9
10 package foo
11
12 import (
13 "errors"
14 "runtime"
15 "unsafe"
16 )
17
18 func add2(p *byte, n uintptr) *byte {
19 return (*byte)(add1(unsafe.Pointer(p), n))
20 }
21
22 func add1(p unsafe.Pointer, x uintptr) unsafe.Pointer {
23 return unsafe.Pointer(uintptr(p) + x)
24 }
25
26 func f(x *byte) *byte {
27 return add2(x, 1)
28 }
29
30
31 func g(x int) int {
32 return x + 1
33 }
34
35 func h(x int) int {
36 return x + 2
37 }
38
39 func i(x int) int {
40 const y = 2
41 return x + y
42 }
43
44 func j(x int) int {
45 switch {
46 case x > 0:
47 return x + 2
48 default:
49 return x + 1
50 }
51 }
52
53 func f2() int {
54 tmp1 := h
55 tmp2 := tmp1
56 return tmp2(0)
57 }
58
59 var abc = errors.New("abc")
60
61 var somethingWrong error
62
63
64 func l(x, y int) (int, int, error) {
65 e := func(err error) (int, int, error) {
66 return 0, 0, err
67 }
68 if x == y {
69 e(somethingWrong)
70 } else {
71 f := e
72 f(nil)
73 }
74 return y, x, nil
75 }
76
77
78 func m() int {
79 foo := func() int { return 1 }
80 x := foo()
81 foo = func() int { return 2 }
82 return x + foo()
83 }
84
85
86 func n() int {
87 foo := func() int { return 1 }
88 bar := &foo
89 x := (*bar)() + foo()
90 return x
91 }
92
93
94 func o() int {
95 foo := func() int { return 1 }
96 func(x int) {
97 if x > 10 {
98 foo = func() int { return 2 }
99 }
100 }(11)
101 return foo()
102 }
103
104 func p() int {
105 return func() int { return 42 }()
106 }
107
108 func q(x int) int {
109 foo := func() int { return x * 2 }
110 return foo()
111 }
112
113 func s0(x int) int {
114 foo := func() {
115 x = x + 1
116 }
117 foo()
118 return x
119 }
120
121 func s1(x int) int {
122 foo := func() int {
123 return x
124 }
125 x = x + 1
126 return foo()
127 }
128
129 func switchBreak(x, y int) int {
130 var n int
131 switch x {
132 case 0:
133 n = 1
134 Done:
135 switch y {
136 case 0:
137 n += 10
138 break Done
139 }
140 n = 2
141 }
142 return n
143 }
144
145 func switchType(x interface{}) int {
146 switch x.(type) {
147 case int:
148 return x.(int)
149 default:
150 return 0
151 }
152 }
153
154
155
156 func switchConst1(p func(string)) {
157 const c = 1
158 switch c {
159 case 0:
160 p("zero")
161 case 1:
162 p("one")
163 case 2:
164 p("two")
165 default:
166 p("other")
167 }
168 }
169
170 func switchConst2() string {
171 switch runtime.GOOS {
172 case "linux":
173 return "Leenooks"
174 case "windows":
175 return "Windoze"
176 case "darwin":
177 return "MackBone"
178 case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100":
179 return "Numbers"
180 default:
181 return "oh nose!"
182 }
183 }
184 func switchConst3() string {
185 switch runtime.GOOS {
186 case "Linux":
187 panic("Linux")
188 case "Windows":
189 panic("Windows")
190 case "Darwin":
191 panic("Darwin")
192 case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100":
193 panic("Numbers")
194 default:
195 return "oh nose!"
196 }
197 }
198
199 func inlineRangeIntoMe(data []int) {
200 rangeFunc(data, 12)
201 }
202
203 func rangeFunc(xs []int, b int) int {
204 for i, x := range xs {
205 if x == b {
206 return i
207 }
208 }
209 return -1
210 }
211
212 type T struct{}
213
214 func (T) meth(int, int) {}
215
216 func k() (T, int, int) { return T{}, 0, 0 }
217
218 func f3() {
219 T.meth(k())
220
221 }
222
223 func small1() {
224 runtime.GC()
225 }
226 func small2() int {
227 return runtime.GOMAXPROCS(0)
228 }
229 func small3(t T) {
230 t.meth2(3, 5)
231 }
232 func small4(t T) {
233 t.meth2(runtime.GOMAXPROCS(0), 5)
234 }
235 func (T) meth2(int, int) {
236 runtime.GC()
237 runtime.GC()
238 }
239
240
241 func ee() {
242 ff(100)
243 }
244
245 func ff(x int) {
246 if x < 0 {
247 return
248 }
249 gg(x - 1)
250 }
251 func gg(x int) {
252 hh(x - 1)
253 }
254 func hh(x int) {
255 ff(x - 1)
256 }
257
258
259 func for1(fn func() bool) {
260 for {
261 if fn() {
262 break
263 } else {
264 continue
265 }
266 }
267 }
268
269 func for2(fn func() bool) {
270 Loop:
271 for {
272 if fn() {
273 break Loop
274 } else {
275 continue Loop
276 }
277 }
278 }
279
280
281 type T1 struct{}
282
283 func (a T1) meth(val int) int {
284 return val + 5
285 }
286
287 func getMeth(t1 T1) func(int) int {
288 return t1.meth
289
290 }
291
292 func ii() {
293 var t1 T1
294 f := getMeth(t1)
295 _ = f(3)
296 }
297
298
299
300 func gd1(int) {
301 defer gd1(gd2())
302 defer gd3()()
303 go gd1(gd2())
304 go gd3()()
305 }
306
307 func gd2() int {
308 return 1
309 }
310
311 func gd3() func() {
312 return ii
313 }
314
315
316 func EncodeQuad(d []uint32, x [6]float32) {
317 _ = d[:6]
318 d[0] = float32bits(x[0])
319 d[1] = float32bits(x[1])
320 d[2] = float32bits(x[2])
321 d[3] = float32bits(x[3])
322 d[4] = float32bits(x[4])
323 d[5] = float32bits(x[5])
324 }
325
326
327
328 func float32bits(f float32) uint32 {
329 return *(*uint32)(unsafe.Pointer(&f))
330 }
331
332
333 func Conv(v uint64) uint64 {
334 return conv2(conv2(conv2(v)))
335 }
336 func conv2(v uint64) uint64 {
337 return conv1(conv1(conv1(conv1(v))))
338 }
339 func conv1(v uint64) uint64 {
340 return uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(v)))))))))))
341 }
342
343 func select1(x, y chan bool) int {
344 select {
345 case <-x:
346 return 1
347 case <-y:
348 return 2
349 }
350 }
351
352 func select2(x, y chan bool) {
353 loop:
354 select {
355 case <-x:
356 break loop
357 case <-y:
358 }
359 }
360
361 func inlineSelect2(x, y chan bool) {
362 loop:
363 for i := 0; i < 5; i++ {
364 if i == 3 {
365 break loop
366 }
367 select2(x, y)
368 }
369 }
370
View as plain text