| 1 | // Copyright 2021 The Go Authors. All rights reserved. |
|---|---|
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // go:build ignore |
| 6 | |
| 7 | package testdata |
| 8 | |
| 9 | type WrappedFunc struct { |
| 10 | F func() complex64 |
| 11 | } |
| 12 | |
| 13 | func callWrappedFunc(f WrappedFunc) { |
| 14 | f.F() |
| 15 | } |
| 16 | |
| 17 | func foo() complex64 { |
| 18 | println("foo") |
| 19 | return -1 |
| 20 | } |
| 21 | |
| 22 | func Foo(b bool) { |
| 23 | callWrappedFunc(WrappedFunc{foo}) |
| 24 | x := func() {} |
| 25 | y := func() {} |
| 26 | var a *func() |
| 27 | if b { |
| 28 | a = &x |
| 29 | } else { |
| 30 | a = &y |
| 31 | } |
| 32 | (*a)() |
| 33 | } |
| 34 | |
| 35 | // Relevant SSA: |
| 36 | // func Foo(b bool): |
| 37 | // t0 = local WrappedFunc (complit) |
| 38 | // t1 = &t0.F [#0] |
| 39 | // *t1 = foo |
| 40 | // t2 = *t0 |
| 41 | // t3 = callWrappedFunc(t2) |
| 42 | // t4 = new func() (x) |
| 43 | // *t4 = Foo$1 |
| 44 | // t5 = new func() (y) |
| 45 | // *t5 = Foo$2 |
| 46 | // if b goto 1 else 3 |
| 47 | // 1: |
| 48 | // jump 2 |
| 49 | // 2: |
| 50 | // t6 = phi [1: t4, 3: t5] #a |
| 51 | // t7 = *t6 |
| 52 | // t8 = t7() |
| 53 | // return |
| 54 | // 3: |
| 55 | // jump 2 |
| 56 | // |
| 57 | // func callWrappedFunc(f WrappedFunc): |
| 58 | // t0 = local WrappedFunc (f) |
| 59 | // *t0 = f |
| 60 | // t1 = &t0.F [#0] |
| 61 | // t2 = *t1 |
| 62 | // t3 = t2() |
| 63 | // return |
| 64 | |
| 65 | // WANT: |
| 66 | // callWrappedFunc: t2() -> foo |
| 67 | // Foo: callWrappedFunc(t2) -> callWrappedFunc; t7() -> Foo$1, Foo$2 |
| 68 |