Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.0.1-2020.1.5 / ir / testdata / objlookup.go
1 //+build ignore
2
3 package main
4
5 // This file is the input to TestObjValueLookup in source_test.go,
6 // which ensures that each occurrence of an ident defining or
7 // referring to a func, var or const object can be mapped to its
8 // corresponding IR Value.
9 //
10 // For every reference to a var object, we use annotations in comments
11 // to denote both the expected IR Value kind, and whether to expect
12 // its value (x) or its address (&x).
13 //
14 // For const and func objects, the results don't vary by reference and
15 // are always values not addresses, so no annotations are needed.  The
16 // declaration is enough.
17
18 import (
19         "fmt"
20         "os"
21 )
22
23 type J int
24
25 func (*J) method() {}
26
27 const globalConst = 0
28
29 var globalVar int //@ ir(globalVar,"&Global")
30
31 func globalFunc() {}
32
33 type I interface {
34         interfaceMethod()
35 }
36
37 type S struct {
38         x int //@ ir(x,"nil")
39 }
40
41 func main() {
42         print(globalVar) //@ ir(globalVar,"Load")
43         globalVar = 1    //@ ir(globalVar,"Const")
44
45         var v0 int = 1 //@ ir(v0,"Const") // simple local value spec
46         if v0 > 0 {    //@ ir(v0,"Const")
47                 v0 = 2 //@ ir(v0,"Const")
48         }
49         print(v0) //@ ir(v0,"Phi")
50
51         // v1 is captured and thus implicitly address-taken.
52         var v1 int = 1         //@ ir(v1,"Const")
53         v1 = 2                 //@ ir(v1,"Const")
54         fmt.Println(v1)        //@ ir(v1,"Load") // load
55         f := func(param int) { //@ ir(f,"MakeClosure"), ir(param,"Parameter")
56                 if y := 1; y > 0 { //@ ir(y,"Const")
57                         print(v1, param) //@ ir(v1,"Load") /*load*/, ir(param,"Sigma")
58                 }
59                 param = 2      //@ ir(param,"Const")
60                 println(param) //@ ir(param,"Const")
61         }
62
63         f(0) //@ ir(f,"MakeClosure")
64
65         var v2 int //@ ir(v2,"Const") // implicitly zero-initialized local value spec
66         print(v2)  //@ ir(v2,"Const")
67
68         m := make(map[string]int) //@ ir(m,"MakeMap")
69
70         // Local value spec with multi-valued RHS:
71         var v3, v4 = m[""] //@ ir(v3,"Extract"), ir(v4,"Extract"), ir(m,"MakeMap")
72         print(v3)          //@ ir(v3,"Extract")
73         print(v4)          //@ ir(v4,"Extract")
74
75         v3++    //@ ir(v3,"BinOp") // assign with op
76         v3 += 2 //@ ir(v3,"BinOp") // assign with op
77
78         v5, v6 := false, "" //@ ir(v5,"Const"), ir(v6,"Const") // defining assignment
79         print(v5)           //@ ir(v5,"Const")
80         print(v6)           //@ ir(v6,"Const")
81
82         var v7 S    //@ ir(v7,"&Alloc")
83         v7.x = 1    //@ ir(v7,"&Alloc"), ir(x,"&FieldAddr")
84         print(v7.x) //@ ir(v7,"&Alloc"), ir(x,"&FieldAddr")
85
86         var v8 [1]int //@ ir(v8,"&Alloc")
87         v8[0] = 0     //@ ir(v8,"&Alloc")
88         print(v8[:])  //@ ir(v8,"&Alloc")
89         _ = v8[0]     //@ ir(v8,"&Alloc")
90         _ = v8[:][0]  //@ ir(v8,"&Alloc")
91         v8ptr := &v8  //@ ir(v8ptr,"Alloc"), ir(v8,"&Alloc")
92         _ = v8ptr[0]  //@ ir(v8ptr,"Alloc")
93         _ = *v8ptr    //@ ir(v8ptr,"Alloc")
94
95         v8a := make([]int, 1) //@ ir(v8a,"Slice")
96         v8a[0] = 0            //@ ir(v8a,"Slice")
97         print(v8a[:])         //@ ir(v8a,"Slice")
98
99         v9 := S{} //@ ir(v9,"&Alloc")
100
101         v10 := &v9 //@ ir(v10,"Alloc"), ir(v9,"&Alloc")
102         _ = v10    //@ ir(v10,"Alloc")
103
104         var v11 *J = nil //@ ir(v11,"Const")
105         v11.method()     //@ ir(v11,"Const")
106
107         var v12 J    //@ ir(v12,"&Alloc")
108         v12.method() //@ ir(v12,"&Alloc") // implicitly address-taken
109
110         // NB, in the following, 'method' resolves to the *types.Func
111         // of (*J).method, so it doesn't help us locate the specific
112         // ir.Values here: a bound-method closure and a promotion
113         // wrapper.
114         _ = v11.method            //@ ir(v11,"Const")
115         _ = (*struct{ J }).method //@ ir(J,"nil")
116
117         // These vars are not optimised away.
118         if false {
119                 v13 := 0     //@ ir(v13,"Const")
120                 println(v13) //@ ir(v13,"Const")
121         }
122
123         switch x := 1; x { //@ ir(x,"Const")
124         case v0: //@ ir(v0,"Phi")
125         }
126
127         for k, v := range m { //@ ir(k,"Extract"), ir(v,"Extract"), ir(m,"MakeMap")
128                 _ = k //@ ir(k,"Extract")
129                 v++   //@ ir(v,"BinOp")
130         }
131
132         if y := 0; y > 1 { //@ ir(y,"Const"), ir(y,"Const")
133         }
134
135         var i interface{}      //@ ir(i,"Const") // nil interface
136         i = 1                  //@ ir(i,"MakeInterface")
137         switch i := i.(type) { //@ ir(i,"MakeInterface"), ir(i,"MakeInterface")
138         case int:
139                 println(i) //@ ir(i,"Extract")
140         }
141
142         ch := make(chan int) //@ ir(ch,"MakeChan")
143         select {
144         case x := <-ch: //@ ir(x,"Recv") /*receive*/, ir(ch,"MakeChan")
145                 _ = x //@ ir(x,"Recv")
146         }
147
148         // .Op is an inter-package FieldVal-selection.
149         var err os.PathError //@ ir(err,"&Alloc")
150         _ = err.Op           //@ ir(err,"&Alloc"), ir(Op,"&FieldAddr")
151         _ = &err.Op          //@ ir(err,"&Alloc"), ir(Op,"&FieldAddr")
152
153         // Exercise corner-cases of lvalues vs rvalues.
154         // (Guessing IsAddr from the 'pointerness' won't cut it here.)
155         type N *N
156         var n N    //@ ir(n,"Const")
157         n1 := n    //@ ir(n1,"Const"), ir(n,"Const")
158         n2 := &n1  //@ ir(n2,"Alloc"), ir(n1,"&Alloc")
159         n3 := *n2  //@ ir(n3,"Load"), ir(n2,"Alloc")
160         n4 := **n3 //@ ir(n4,"Load"), ir(n3,"Load")
161         _ = n4     //@ ir(n4,"Load")
162 }