| 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 | // Test program for testing type assertions and extract instructions. |
| 10 | // The latter are tested here too since extract instruction comes |
| 11 | // naturally in type assertions. |
| 12 | |
| 13 | type I interface { |
| 14 | Foo() |
| 15 | } |
| 16 | |
| 17 | type J interface { |
| 18 | Foo() |
| 19 | Bar() |
| 20 | } |
| 21 | |
| 22 | type A struct { |
| 23 | c int |
| 24 | } |
| 25 | |
| 26 | func (a A) Foo() {} |
| 27 | func (a A) Bar() {} |
| 28 | |
| 29 | func Baz(i I) { |
| 30 | j, ok := i.(J) |
| 31 | if ok { |
| 32 | j.Foo() |
| 33 | } |
| 34 | |
| 35 | a := i.(*A) |
| 36 | a.Bar() |
| 37 | } |
| 38 | |
| 39 | // Relevant SSA: |
| 40 | // func Baz(i I): |
| 41 | // t0 = typeassert,ok i.(J) |
| 42 | // t1 = extract t0 #0 |
| 43 | // t2 = extract t0 #1 |
| 44 | // if t2 goto 1 else 2 |
| 45 | // 1: |
| 46 | // t3 = invoke t1.Foo() |
| 47 | // jump 2 |
| 48 | // 2: |
| 49 | // t4 = typeassert i.(*A) // no flow since t4 is of concrete type |
| 50 | // t5 = *t4 |
| 51 | // t6 = (A).Bar(t5) |
| 52 | // return |
| 53 | |
| 54 | // WANT: |
| 55 | // Local(i) -> Local(t0[0]) |
| 56 | // Local(t0[0]) -> Local(t1) |
| 57 |
Members