GoPLS Viewer

Home|gopls/go/callgraph/vta/testdata/src/type_assertions.go
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
7package 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
13type I interface {
14    Foo()
15}
16
17type J interface {
18    Foo()
19    Bar()
20}
21
22type A struct {
23    c int
24}
25
26func (a AFoo() {}
27func (a ABar() {}
28
29func Baz(i I) {
30    jok := 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
MembersX
A.c
Members
X