From d4cc65f0b2ff86a8ea502e02a79168a16b992872 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Fri, 20 Aug 2021 12:29:25 -0700 Subject: [PATCH] gopls/internal/regtest/codelens: avoid compiler bug Because of how the compiler internally represents variable references, it sometimes gets position information incorrectly for them. The codelens test hits this issue because for var x string fmt.Println(x) the diagnostic about "x" escaping to heap (which actually refers to the implicit conversion to "interface{}" type) should be rightly reported at the "x" identifier within the call arguments list. However, due to the aforementioned compiler bug, historically we reported the diagnostic at the "(" token instead. In -G=3 mode, the compiler (correctly) report the diagnostic at "x" instead; but with GOEXPERIMENT=unified, the compiler intentionally matches the original -G=0 behavior and continues reporting at "(" instead. This CL avoids the issue entirely by changing the line to "fmt.Println(42)", which avoids the issue because the compiler always correctly prints the diagnostic then at "42". Change-Id: I22d4c756ae801489f3f16c440e4339bc9b115fb0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/343875 Trust: Matthew Dempsky Run-TryBot: Matthew Dempsky gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Heschi Kreinick --- gopls/internal/regtest/codelens/codelens_test.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/gopls/internal/regtest/codelens/codelens_test.go b/gopls/internal/regtest/codelens/codelens_test.go index d89b8e0bd8..ad35a29911 100644 --- a/gopls/internal/regtest/codelens/codelens_test.go +++ b/gopls/internal/regtest/codelens/codelens_test.go @@ -303,8 +303,7 @@ package main import "fmt" func main() { - var x string - fmt.Println(x) + fmt.Println(42) } ` WithOptions( @@ -320,7 +319,7 @@ func main() { d := &protocol.PublishDiagnosticsParams{} env.Await( OnceMet( - DiagnosticAt("main.go", 6, 12), + DiagnosticAt("main.go", 5, 13), ReadDiagnostics("main.go", d), ), ) @@ -330,12 +329,12 @@ func main() { if d.Severity != protocol.SeverityInformation { t.Fatalf("unexpected diagnostic severity %v, wanted Information", d.Severity) } - if strings.Contains(d.Message, "x escapes") { + if strings.Contains(d.Message, "42 escapes") { found = true } } if !found { - t.Fatalf(`expected to find diagnostic with message "escape(x escapes to heap)", found none`) + t.Fatalf(`expected to find diagnostic with message "escape(42 escapes to heap)", found none`) } // Editing a buffer should cause gc_details diagnostics to disappear, since @@ -346,7 +345,7 @@ func main() { // Saving a buffer should re-format back to the original state, and // re-enable the gc_details diagnostics. env.SaveBuffer("main.go") - env.Await(DiagnosticAt("main.go", 6, 12)) + env.Await(DiagnosticAt("main.go", 5, 13)) // Toggle the GC details code lens again so now it should be off. env.ExecuteCodeLensCommand("main.go", command.GCDetails)