| 1 | package describe // @describe pkgdecl "describe" |
|---|---|
| 2 | |
| 3 | // Tests of 'describe' query. |
| 4 | // See go.tools/guru/guru_test.go for explanation. |
| 5 | // See describe.golden for expected query results. |
| 6 | |
| 7 | // TODO(adonovan): more coverage of the (extensive) logic. |
| 8 | |
| 9 | import ( |
| 10 | "lib" |
| 11 | "nosuchpkg" // @describe badimport1 "nosuchpkg" |
| 12 | nosuchpkg2 "nosuchpkg" // @describe badimport2 "nosuchpkg2" |
| 13 | // The unsafe package changed in Go 1.17 with the addition of |
| 14 | // unsafe.Add and unsafe.Slice. While we still support older versions |
| 15 | // of Go, the test case below cannot be enabled. |
| 16 | // _ "unsafe" // @describe unsafe "unsafe" |
| 17 | ) |
| 18 | |
| 19 | var _ nosuchpkg.T |
| 20 | var _ nosuchpkg2.T |
| 21 | |
| 22 | type cake float64 // @describe type-ref-builtin "float64" |
| 23 | |
| 24 | const c = iota // @describe const-ref-iota "iota" |
| 25 | |
| 26 | const pi = 3.141 // @describe const-def-pi "pi" |
| 27 | const pie = cake(pi) // @describe const-def-pie "pie" |
| 28 | const _ = pi // @describe const-ref-pi "pi" |
| 29 | |
| 30 | var global = new(string) // NB: ssa.Global is indirect, i.e. **string |
| 31 | |
| 32 | func main() { // @describe func-def-main "main" |
| 33 | // func objects |
| 34 | _ = main // @describe func-ref-main "main" |
| 35 | _ = (*C).f // @describe func-ref-*C.f "..C..f" |
| 36 | _ = D.f // @describe func-ref-D.f "D.f" |
| 37 | _ = I.f // @describe func-ref-I.f "I.f" |
| 38 | var d D // @describe type-D "D" |
| 39 | var i I // @describe type-I "I" |
| 40 | _ = d.f // @describe func-ref-d.f "d.f" |
| 41 | _ = i.f // @describe func-ref-i.f "i.f" |
| 42 | var slice []D // @describe slice-of-D "slice" |
| 43 | |
| 44 | var dptr *D // @describe ptr-with-nonptr-methods "dptr" |
| 45 | _ = dptr |
| 46 | |
| 47 | // var objects |
| 48 | anon := func() { |
| 49 | _ = d // @describe ref-lexical-d "d" |
| 50 | } |
| 51 | _ = anon // @describe ref-anon "anon" |
| 52 | _ = global // @describe ref-global "global" |
| 53 | |
| 54 | // SSA affords some local flow sensitivity. |
| 55 | var a, b int |
| 56 | var x = &a // @describe var-def-x-1 "x" |
| 57 | _ = x // @describe var-ref-x-1 "x" |
| 58 | x = &b // @describe var-def-x-2 "x" |
| 59 | _ = x // @describe var-ref-x-2 "x" |
| 60 | |
| 61 | i = new(C) // @describe var-ref-i-C "i" |
| 62 | if i != nil { |
| 63 | i = D{} // @describe var-ref-i-D "i" |
| 64 | } |
| 65 | print(i) // @describe var-ref-i "\\bi\\b" |
| 66 | |
| 67 | // const objects |
| 68 | const localpi = 3.141 // @describe const-local-pi "localpi" |
| 69 | const localpie = cake(pi) // @describe const-local-pie "localpie" |
| 70 | const _ = localpi // @describe const-ref-localpi "localpi" |
| 71 | |
| 72 | // type objects |
| 73 | type T int // @describe type-def-T "T" |
| 74 | var three T = 3 // @describe type-ref-T "T" |
| 75 | _ = three |
| 76 | |
| 77 | print(1 + 2*3) // @describe const-expr " 2.3" |
| 78 | print(real(1+2i) - 3) // @describe const-expr2 "real.*3" |
| 79 | |
| 80 | m := map[string]*int{"a": &a} |
| 81 | mapval, _ := m["a"] // @describe map-lookup,ok "m..a.." |
| 82 | _ = mapval // @describe mapval "mapval" |
| 83 | _ = m // @describe m "m" |
| 84 | |
| 85 | defer main() // @describe defer-stmt "defer" |
| 86 | go main() // @describe go-stmt "go" |
| 87 | |
| 88 | panic(3) // @describe builtin-ref-panic "panic" |
| 89 | |
| 90 | var a2 int // @describe var-decl-stmt "var a2 int" |
| 91 | _ = a2 |
| 92 | var _ int // @describe var-decl-stmt2 "var _ int" |
| 93 | var _ int // @describe var-def-blank "_" |
| 94 | |
| 95 | var _ lib.Outer // @describe lib-outer "Outer" |
| 96 | |
| 97 | var mmm map[C]D // @describe var-map-of-C-D "mmm" |
| 98 | |
| 99 | d := newD().ThirdField // @describe field-access "ThirdField" |
| 100 | |
| 101 | astCopy := ast |
| 102 | unknown() // @describe call-unknown "\\(" |
| 103 | } |
| 104 | |
| 105 | type I interface { // @describe def-iface-I "I" |
| 106 | f() // @describe def-imethod-I.f "f" |
| 107 | } |
| 108 | |
| 109 | type C int |
| 110 | type D struct { |
| 111 | Field int |
| 112 | AnotherField string |
| 113 | ThirdField C |
| 114 | } |
| 115 | |
| 116 | func (c *C) f() {} |
| 117 | func (d D) f() {} |
| 118 | |
| 119 | func newD() D { return D{} } |
| 120 |
Members