| 1 |
-- go.mod -- |
| 2 |
module example.com |
| 3 |
go 1.18 |
| 4 |
|
| 5 |
-- iface.go -- |
| 6 |
package main |
| 7 |
|
| 8 |
// Test of interface calls. |
| 9 |
|
| 10 |
func use(interface{}) |
| 11 |
|
| 12 |
type A byte // instantiated but not a reflect type |
| 13 |
|
| 14 |
func (A) f() {} // called directly |
| 15 |
func (A) F() {} // unreachable |
| 16 |
|
| 17 |
type B int // a reflect type |
| 18 |
|
| 19 |
func (*B) f() {} // reachable via interface invoke |
| 20 |
func (*B) F() {} // reachable: exported method of reflect type |
| 21 |
|
| 22 |
type B2 int // a reflect type, and *B2 also |
| 23 |
|
| 24 |
func (B2) f() {} // reachable via interface invoke |
| 25 |
func (B2) g() {} // reachable: exported method of reflect type |
| 26 |
|
| 27 |
type C string // not instantiated |
| 28 |
|
| 29 |
func (C) f() {} // unreachable |
| 30 |
func (C) F() {} // unreachable |
| 31 |
|
| 32 |
type D uint // instantiated only in dead code |
| 33 |
|
| 34 |
func (D) f() {} // unreachable |
| 35 |
func (D) F() {} // unreachable |
| 36 |
|
| 37 |
func main() { |
| 38 |
A(0).f() |
| 39 |
|
| 40 |
use(new(B)) |
| 41 |
use(B2(0)) |
| 42 |
|
| 43 |
var i interface { |
| 44 |
f() |
| 45 |
} |
| 46 |
i.f() // calls (*B).f, (*B2).f and (B2.f) |
| 47 |
|
| 48 |
live() |
| 49 |
} |
| 50 |
|
| 51 |
func live() { |
| 52 |
var j interface { |
| 53 |
f() |
| 54 |
g() |
| 55 |
} |
| 56 |
j.f() // calls (B2).f and (*B2).f but not (*B).f (no g method). |
| 57 |
} |
| 58 |
|
| 59 |
func dead() { |
| 60 |
use(D(0)) |
| 61 |
} |
| 62 |
|
| 63 |
// WANT: |
| 64 |
// |
| 65 |
// edge live --dynamic method call--> (*B2).f |
| 66 |
// edge live --dynamic method call--> (B2).f |
| 67 |
// edge main --dynamic method call--> (*B).f |
| 68 |
// edge main --dynamic method call--> (*B2).f |
| 69 |
// edge main --dynamic method call--> (B2).f |
| 70 |
// |
| 71 |
// reachable (A).f |
| 72 |
// !reachable (A).F |
| 73 |
// reachable (*B).f |
| 74 |
// reachable (*B).F |
| 75 |
// reachable (B2).f |
| 76 |
// !reachable (B2).g |
| 77 |
// reachable (*B2).f |
| 78 |
// !reachable (*B2).g |
| 79 |
// !reachable (C).f |
| 80 |
// !reachable (C).F |
| 81 |
// !reachable (D).f |
| 82 |
// !reachable (D).F |
| 83 |
// reachable main |
| 84 |
// reachable live |
| 85 |
// reachable use |
| 86 |
// !reachable dead |
| 87 |
// |
| 88 |
// !rtype A |
| 89 |
// rtype *B |
| 90 |
// rtype *B2 |
| 91 |
// rtype B |
| 92 |
// rtype B2 |
| 93 |
// !rtype C |
| 94 |
// !rtype D |