| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package misc |
| 6 |
|
| 7 |
import ( |
| 8 |
"runtime" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"golang.org/x/tools/gopls/internal/protocol" |
| 12 |
"golang.org/x/tools/gopls/internal/server" |
| 13 |
"golang.org/x/tools/gopls/internal/settings" |
| 14 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
func TestCompilerOptDetails(t *testing.T) { |
| 19 |
if runtime.GOOS == "android" { |
| 20 |
t.Skipf("the compiler optimization details code action doesn't work on Android") |
| 21 |
} |
| 22 |
|
| 23 |
const mod = ` |
| 24 |
-- go.mod -- |
| 25 |
module mod.com |
| 26 |
|
| 27 |
go 1.18 |
| 28 |
|
| 29 |
-- main.go -- |
| 30 |
package main |
| 31 |
|
| 32 |
import "fmt" |
| 33 |
|
| 34 |
func main() { |
| 35 |
fmt.Println(42) |
| 36 |
} |
| 37 |
` |
| 38 |
Run(t, mod, func(t *testing.T, env *Env) { |
| 39 |
env.OpenFile("main.go") |
| 40 |
actions := env.CodeActionForFile("main.go", nil) |
| 41 |
|
| 42 |
|
| 43 |
docAction, err := CodeActionByKind(actions, settings.GoToggleCompilerOptDetails) |
| 44 |
if err != nil { |
| 45 |
t.Fatal(err) |
| 46 |
} |
| 47 |
|
| 48 |
params := &protocol.ExecuteCommandParams{ |
| 49 |
Command: docAction.Command.Command, |
| 50 |
Arguments: docAction.Command.Arguments, |
| 51 |
} |
| 52 |
env.ExecuteCommand(params, nil) |
| 53 |
|
| 54 |
env.OnceMet( |
| 55 |
CompletedWork(server.DiagnosticWorkTitle(server.FromToggleCompilerOptDetails), 1, true), |
| 56 |
Diagnostics( |
| 57 |
ForFile("main.go"), |
| 58 |
AtPosition("main.go", 5, 13), |
| 59 |
WithMessage("42 escapes"), |
| 60 |
WithSeverityTags("optimizer details", protocol.SeverityInformation, nil), |
| 61 |
), |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
env.SetBufferContent("main.go", ` |
| 67 |
package main |
| 68 |
func main() { _ = f } |
| 69 |
func f(x int) *int { return &x }`) |
| 70 |
env.AfterChange(Diagnostics( |
| 71 |
ForFile("main.go"), |
| 72 |
WithMessage("x escapes"), |
| 73 |
WithSeverityTags("optimizer details", protocol.SeverityInformation, nil), |
| 74 |
)) |
| 75 |
|
| 76 |
|
| 77 |
env.ExecuteCommand(params, nil) |
| 78 |
env.OnceMet( |
| 79 |
CompletedWork(server.DiagnosticWorkTitle(server.FromToggleCompilerOptDetails), 2, true), |
| 80 |
NoDiagnostics(ForFile("main.go")), |
| 81 |
) |
| 82 |
}) |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
func TestCompilerOptDetails_perDirectory(t *testing.T) { |
| 88 |
if runtime.GOOS == "android" { |
| 89 |
t.Skipf("the compiler optimization details code action doesn't work on Android") |
| 90 |
} |
| 91 |
|
| 92 |
const mod = ` |
| 93 |
-- go.mod -- |
| 94 |
module mod.com |
| 95 |
go 1.18 |
| 96 |
|
| 97 |
-- a/a.go -- |
| 98 |
package a |
| 99 |
|
| 100 |
func F(x int) any { return &x } |
| 101 |
|
| 102 |
-- a/a_test.go -- |
| 103 |
package a |
| 104 |
|
| 105 |
func G(x int) any { return &x } |
| 106 |
|
| 107 |
-- a/a_x_test.go -- |
| 108 |
package a_test |
| 109 |
|
| 110 |
func H(x int) any { return &x } |
| 111 |
` |
| 112 |
|
| 113 |
Run(t, mod, func(t *testing.T, env *Env) { |
| 114 |
|
| 115 |
|
| 116 |
toggle := func(filename, wantTitle string) { |
| 117 |
env.OpenFile(filename) |
| 118 |
actions := env.CodeActionForFile(filename, nil) |
| 119 |
|
| 120 |
docAction, err := CodeActionByKind(actions, settings.GoToggleCompilerOptDetails) |
| 121 |
if err != nil { |
| 122 |
t.Fatal(err) |
| 123 |
} |
| 124 |
if docAction.Title != wantTitle { |
| 125 |
t.Errorf("CodeAction.Title = %q, want %q", docAction.Title, wantTitle) |
| 126 |
} |
| 127 |
params := &protocol.ExecuteCommandParams{ |
| 128 |
Command: docAction.Command.Command, |
| 129 |
Arguments: docAction.Command.Arguments, |
| 130 |
} |
| 131 |
env.ExecuteCommand(params, nil) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
toggle("a/a.go", `Show compiler optimization details for "a"`) |
| 137 |
env.OnceMet( |
| 138 |
CompletedWork(server.DiagnosticWorkTitle(server.FromToggleCompilerOptDetails), 1, true), |
| 139 |
Diagnostics( |
| 140 |
ForFile("a/a.go"), |
| 141 |
AtPosition("a/a.go", 2, 7), |
| 142 |
WithMessage("x escapes to heap"), |
| 143 |
WithSeverityTags("optimizer details", protocol.SeverityInformation, nil), |
| 144 |
), |
| 145 |
Diagnostics( |
| 146 |
ForFile("a/a_test.go"), |
| 147 |
AtPosition("a/a_test.go", 2, 7), |
| 148 |
WithMessage("x escapes to heap"), |
| 149 |
WithSeverityTags("optimizer details", protocol.SeverityInformation, nil), |
| 150 |
), |
| 151 |
Diagnostics( |
| 152 |
ForFile("a/a_x_test.go"), |
| 153 |
AtPosition("a/a_x_test.go", 2, 7), |
| 154 |
WithMessage("x escapes to heap"), |
| 155 |
WithSeverityTags("optimizer details", protocol.SeverityInformation, nil), |
| 156 |
), |
| 157 |
) |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
toggle("a/a_test.go", `Hide compiler optimization details for "a"`) |
| 162 |
env.OnceMet( |
| 163 |
CompletedWork(server.DiagnosticWorkTitle(server.FromToggleCompilerOptDetails), 2, true), |
| 164 |
NoDiagnostics(ForFile("a/a.go")), |
| 165 |
NoDiagnostics(ForFile("a/a_test.go")), |
| 166 |
NoDiagnostics(ForFile("a/a_x_test.go")), |
| 167 |
) |
| 168 |
}) |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
func TestCompilerOptDetails_config(t *testing.T) { |
| 174 |
if runtime.GOOS == "android" { |
| 175 |
t.Skipf("the compiler optimization details code action doesn't work on Android") |
| 176 |
} |
| 177 |
|
| 178 |
const mod = ` |
| 179 |
-- go.mod -- |
| 180 |
module mod.com |
| 181 |
go 1.18 |
| 182 |
|
| 183 |
-- a/a.go -- |
| 184 |
package a |
| 185 |
|
| 186 |
func F(x int) any { return &x } // escape(x escapes to heap) |
| 187 |
func G() { defer func(){} () } // cannotInlineFunction(unhandled op DEFER) |
| 188 |
` |
| 189 |
|
| 190 |
for _, escape := range []bool{true, false} { |
| 191 |
WithOptions( |
| 192 |
Settings{"annotations": map[string]any{"inline": true, "escape": escape}}, |
| 193 |
).Run(t, mod, func(t *testing.T, env *Env) { |
| 194 |
env.OpenFile("a/a.go") |
| 195 |
actions := env.CodeActionForFile("a/a.go", nil) |
| 196 |
|
| 197 |
docAction, err := CodeActionByKind(actions, settings.GoToggleCompilerOptDetails) |
| 198 |
if err != nil { |
| 199 |
t.Fatal(err) |
| 200 |
} |
| 201 |
params := &protocol.ExecuteCommandParams{ |
| 202 |
Command: docAction.Command.Command, |
| 203 |
Arguments: docAction.Command.Arguments, |
| 204 |
} |
| 205 |
env.ExecuteCommand(params, nil) |
| 206 |
|
| 207 |
env.OnceMet( |
| 208 |
CompletedWork(server.DiagnosticWorkTitle(server.FromToggleCompilerOptDetails), 1, true), |
| 209 |
cond(escape, Diagnostics, NoDiagnostics)( |
| 210 |
ForFile("a/a.go"), |
| 211 |
AtPosition("a/a.go", 2, 7), |
| 212 |
WithMessage("x escapes to heap"), |
| 213 |
WithSeverityTags("optimizer details", protocol.SeverityInformation, nil), |
| 214 |
), |
| 215 |
Diagnostics( |
| 216 |
ForFile("a/a.go"), |
| 217 |
AtPosition("a/a.go", 3, 5), |
| 218 |
WithMessage("cannotInlineFunction(unhandled op DEFER)"), |
| 219 |
WithSeverityTags("optimizer details", protocol.SeverityInformation, nil), |
| 220 |
), |
| 221 |
) |
| 222 |
}) |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
func cond[T any](cond bool, x, y T) T { |
| 227 |
if cond { |
| 228 |
return x |
| 229 |
} else { |
| 230 |
return y |
| 231 |
} |
| 232 |
} |