| 1 |
This test checks that hover reports the sizes of vars/types, |
| 2 |
and the offsets of struct fields. |
| 3 |
|
| 4 |
Notes: |
| 5 |
- this only works on the declaring identifier, not on refs. |
| 6 |
- the size of a type is undefined if it depends on type parameters. |
| 7 |
- the offset of a field is undefined if it or any preceding field |
| 8 |
has undefined size/alignment. |
| 9 |
- the test's size expectations assumes a 64-bit machine. |
| 10 |
- requires go1.22 because size information was inaccurate before. |
| 11 |
|
| 12 |
-- settings.json -- |
| 13 |
{"analyses": {"unusedfunc": false}} |
| 14 |
|
| 15 |
-- flags -- |
| 16 |
-skip_goarch=386,arm |
| 17 |
|
| 18 |
-- go.mod -- |
| 19 |
module example.com |
| 20 |
|
| 21 |
go 1.18 |
| 22 |
-- a.go -- |
| 23 |
package a |
| 24 |
|
| 25 |
type T struct { //@ hover("T", "T", T) |
| 26 |
a int //@ hover("a", "a", a) |
| 27 |
U U //@ hover("U", "U", U) |
| 28 |
y, z int //@ hover("y", "y", y), hover("z", "z", z) |
| 29 |
} |
| 30 |
|
| 31 |
type U struct { |
| 32 |
slice []string |
| 33 |
} |
| 34 |
|
| 35 |
type G[T any] struct { |
| 36 |
p T //@ hover("p", "p", p) |
| 37 |
q int //@ hover("q", "q", q) |
| 38 |
} |
| 39 |
|
| 40 |
var _ struct { |
| 41 |
Gint G[int] //@ hover("Gint", "Gint", Gint) |
| 42 |
Gstring G[string] //@ hover("Gstring", "Gstring", Gstring) |
| 43 |
} |
| 44 |
|
| 45 |
type wasteful struct { //@ hover("wasteful", "wasteful", wasteful) |
| 46 |
a bool |
| 47 |
b [2]string |
| 48 |
c bool |
| 49 |
} |
| 50 |
|
| 51 |
type sizeclass struct { //@ hover("sizeclass", "sizeclass", sizeclass) |
| 52 |
a [5]*int |
| 53 |
} |
| 54 |
|
| 55 |
-- @T -- |
| 56 |
```go |
| 57 |
type T struct { // size=48 (0x30) |
| 58 |
a int //@ hover("a", "a", a) |
| 59 |
U U //@ hover("U", "U", U) |
| 60 |
y, z int //@ hover("y", "y", y), hover("z", "z", z) |
| 61 |
} |
| 62 |
``` |
| 63 |
|
| 64 |
--- |
| 65 |
|
| 66 |
[`a.T` on pkg.go.dev](https://pkg.go.dev/example.com#T) |
| 67 |
-- @wasteful -- |
| 68 |
```go |
| 69 |
type wasteful struct { // size=48 (0x30) (29% wasted) |
| 70 |
a bool |
| 71 |
b [2]string |
| 72 |
c bool |
| 73 |
} |
| 74 |
``` |
| 75 |
-- @sizeclass -- |
| 76 |
```go |
| 77 |
type sizeclass struct { // size=40 (0x28), class=48 (0x30) |
| 78 |
a [5]*int |
| 79 |
} |
| 80 |
``` |
| 81 |
-- @a -- |
| 82 |
```go |
| 83 |
field a int // size=8, offset=0 |
| 84 |
``` |
| 85 |
|
| 86 |
--- |
| 87 |
|
| 88 |
@ hover("a", "a", a) |
| 89 |
-- @U -- |
| 90 |
```go |
| 91 |
field U U // size=24 (0x18), offset=8 |
| 92 |
``` |
| 93 |
|
| 94 |
--- |
| 95 |
|
| 96 |
@ hover("U", "U", U) |
| 97 |
|
| 98 |
|
| 99 |
--- |
| 100 |
|
| 101 |
[`(a.T).U` on pkg.go.dev](https://pkg.go.dev/example.com#T.U) |
| 102 |
-- @y -- |
| 103 |
```go |
| 104 |
field y int // size=8, offset=32 (0x20) |
| 105 |
``` |
| 106 |
|
| 107 |
--- |
| 108 |
|
| 109 |
@ hover("y", "y", y), hover("z", "z", z) |
| 110 |
-- @z -- |
| 111 |
```go |
| 112 |
field z int // size=8, offset=40 (0x28) |
| 113 |
``` |
| 114 |
|
| 115 |
--- |
| 116 |
|
| 117 |
@ hover("y", "y", y), hover("z", "z", z) |
| 118 |
-- @p -- |
| 119 |
```go |
| 120 |
field p T |
| 121 |
``` |
| 122 |
|
| 123 |
--- |
| 124 |
|
| 125 |
@ hover("p", "p", p) |
| 126 |
-- @q -- |
| 127 |
```go |
| 128 |
field q int // size=8 |
| 129 |
``` |
| 130 |
|
| 131 |
--- |
| 132 |
|
| 133 |
@ hover("q", "q", q) |
| 134 |
-- @Gint -- |
| 135 |
```go |
| 136 |
field Gint G[int] // size=16 (0x10), offset=0 |
| 137 |
``` |
| 138 |
|
| 139 |
--- |
| 140 |
|
| 141 |
@ hover("Gint", "Gint", Gint) |
| 142 |
-- @Gstring -- |
| 143 |
```go |
| 144 |
field Gstring G[string] // size=24 (0x18), offset=16 (0x10) |
| 145 |
``` |
| 146 |
|
| 147 |
--- |
| 148 |
|
| 149 |
@ hover("Gstring", "Gstring", Gstring) |