| 1 | // Copyright 2024 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 I interface { |
| 10 | Foo() |
| 11 | } |
| 12 | |
| 13 | type A struct{} |
| 14 | |
| 15 | func (a A) Foo() {} |
| 16 | |
| 17 | type B struct{} |
| 18 | |
| 19 | func (b B) Foo() {} |
| 20 | |
| 21 | type C struct{} |
| 22 | |
| 23 | func (c C) Foo() {} // Test that this is not called. |
| 24 | |
| 25 | type iset []I |
| 26 | |
| 27 | func (i iset) All() func(func(I) bool) { |
| 28 | return func(yield func(I) bool) { |
| 29 | for _, v := range i { |
| 30 | if !yield(v) { |
| 31 | return |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | var x = iset([]I{A{}, B{}}) |
| 38 | |
| 39 | func X() { |
| 40 | for i := range x.All() { |
| 41 | i.Foo() |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func Y() I { |
| 46 | for i := range x.All() { |
| 47 | return i |
| 48 | } |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | func Bar() { |
| 53 | X() |
| 54 | y := Y() |
| 55 | y.Foo() |
| 56 | } |
| 57 | |
| 58 | // Relevant SSA: |
| 59 | //func X$1(I) bool: |
| 60 | // t0 = *jump$1 |
| 61 | // t1 = t0 == 0:int |
| 62 | // if t1 goto 1 else 2 |
| 63 | //1: |
| 64 | // *jump$1 = -1:int |
| 65 | // t2 = invoke arg0.Foo() |
| 66 | // *jump$1 = 0:int |
| 67 | // return true:bool |
| 68 | //2: |
| 69 | // t3 = make interface{} <- string ("yield function ca...":string) interface{} |
| 70 | // panic t3 |
| 71 | // |
| 72 | //func All$1(yield func(I) bool): |
| 73 | // t0 = *i |
| 74 | // t1 = len(t0) |
| 75 | // jump 1 |
| 76 | //1: |
| 77 | // t2 = phi [0: -1:int, 2: t3] #rangeindex |
| 78 | // t3 = t2 + 1:int |
| 79 | // t4 = t3 < t1 |
| 80 | // if t4 goto 2 else 3 |
| 81 | //2: |
| 82 | // t5 = &t0[t3] |
| 83 | // t6 = *t5 |
| 84 | // t7 = yield(t6) |
| 85 | // if t7 goto 1 else 4 |
| 86 | // |
| 87 | //func Bar(): |
| 88 | // t0 = X() |
| 89 | // t1 = Y() |
| 90 | // t2 = invoke t1.Foo() |
| 91 | // return |
| 92 | |
| 93 | // WANT: |
| 94 | // Bar: X() -> X; Y() -> Y; invoke t1.Foo() -> A.Foo, B.Foo |
| 95 | // X$1: invoke arg0.Foo() -> A.Foo, B.Foo |
| 96 | // All$1: yield(t6) -> X$1, Y$1 |