From fc70354354e63fa88b8cba95d89e3115bdd717dd Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Tue, 27 Feb 2024 14:55:19 +0000 Subject: [PATCH] gopls/internal/test: add test for NPE in control flow highlighting Add a test for the fix in golang/go#65952: a nil pointer exception when highlighting a return value in a function returning no results. Also, merge tests related to control flow highlighting, since it is convenient to be able to run them together, and since there is nontrivial overhead to tiny tests. Updates golang/go#65952 Change-Id: Ibf8c7c6f0f4feed6dc7a283736bc038600a0bf04 Reviewed-on: https://go-review.googlesource.com/c/tools/+/567256 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Findley --- .../marker/testdata/highlight/controlflow.txt | 71 +++++++++++++++++++ .../marker/testdata/highlight/issue60589.txt | 30 -------- .../marker/testdata/highlight/issue65516.txt | 7 -- 3 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 gopls/internal/test/marker/testdata/highlight/controlflow.txt delete mode 100644 gopls/internal/test/marker/testdata/highlight/issue60589.txt delete mode 100644 gopls/internal/test/marker/testdata/highlight/issue65516.txt diff --git a/gopls/internal/test/marker/testdata/highlight/controlflow.txt b/gopls/internal/test/marker/testdata/highlight/controlflow.txt new file mode 100644 index 00000000000..25cc9394a47 --- /dev/null +++ b/gopls/internal/test/marker/testdata/highlight/controlflow.txt @@ -0,0 +1,71 @@ +This test verifies document highlighting for control flow. + +-- go.mod -- +module mod.com + +go 1.18 + +-- p.go -- +package p + +-- issue60589.go -- +package p + +// This test verifies that control flow lighlighting correctly +// accounts for multi-name result parameters. +// In golang/go#60589, it did not. + +func _() (foo int, bar, baz string) { //@ loc(func, "func"), loc(foo, "foo"), loc(fooint, "foo int"), loc(int, "int"), loc(bar, "bar"), loc(beforebaz, " baz"), loc(baz, "baz"), loc(barbazstring, "bar, baz string"), loc(beforestring, re`() string`), loc(string, "string") + return 0, "1", "2" //@ loc(return, `return 0, "1", "2"`), loc(l0, "0"), loc(l1, `"1"`), loc(l2, `"2"`) +} + +// Assertions, expressed here to avoid clutter above. +// Note that when the cursor is over the field type, there is some +// (likely harmless) redundancy. + +//@ highlight(func, func, return) +//@ highlight(foo, foo, l0) +//@ highlight(int, fooint, int, l0) +//@ highlight(bar, bar, l1) +//@ highlight(beforebaz) +//@ highlight(baz, baz, l2) +//@ highlight(beforestring, baz, l2) +//@ highlight(string, barbazstring, string, l1, l2) +//@ highlight(l0, foo, l0) +//@ highlight(l1, bar, l1) +//@ highlight(l2, baz, l2) + +// Check that duplicate result names do not cause +// inaccurate highlighting. + +func _() (x, x int32) { //@ loc(x1, re`\((x)`), loc(x2, re`(x) int`), diag(x1, re"redeclared"), diag(x2, re"redeclared") + return 1, 2 //@ loc(one, "1"), loc(two, "2") +} + +//@ highlight(one, one, x1) +//@ highlight(two, two, x2) +//@ highlight(x1, x1, one) +//@ highlight(x2, x2, two) + +-- issue65516.go -- +package p + +// This test checks that gopls doesn't crash while highlighting +// functions with no body (golang/go#65516). + +func Foo() (int, string) //@highlight("int", "int"), highlight("func", "func") + +-- issue65952.go -- +package p + +// This test checks that gopls doesn't crash while highlighting +// return values in functions with no results. + +func _() { + return 0 //@highlight("0", "0"), diag("0", re"too many return") +} + +func _() () { + // TODO(golang/go#65966): fix the triplicate diagnostics here. + return 0 //@highlight("0", "0"), diag("0", re"too many return"), diag("0", re"too many return"), diag("0", re"too many return") +} diff --git a/gopls/internal/test/marker/testdata/highlight/issue60589.txt b/gopls/internal/test/marker/testdata/highlight/issue60589.txt deleted file mode 100644 index afa4335a9c6..00000000000 --- a/gopls/internal/test/marker/testdata/highlight/issue60589.txt +++ /dev/null @@ -1,30 +0,0 @@ -This test verifies that control flow lighlighting correctly accounts for -multi-name result parameters. In golang/go#60589, it did not. - --- go.mod -- -module mod.com - -go 1.18 - --- p.go -- -package p - -func _() (foo int, bar, baz string) { //@ loc(func, "func"), loc(foo, "foo"), loc(fooint, "foo int"), loc(int, "int"), loc(bar, "bar"), loc(beforebaz, " baz"), loc(baz, "baz"), loc(barbazstring, "bar, baz string"), loc(beforestring, re`() string`), loc(string, "string") - return 0, "1", "2" //@ loc(return, `return 0, "1", "2"`), loc(l0, "0"), loc(l1, `"1"`), loc(l2, `"2"`) -} - -// Assertions, expressed here to avoid clutter above. -// Note that when the cursor is over the field type, there is some -// (likely harmless) redundancy. - -//@ highlight(func, func, return) -//@ highlight(foo, foo, l0) -//@ highlight(int, fooint, int, l0) -//@ highlight(bar, bar, l1) -//@ highlight(beforebaz) -//@ highlight(baz, baz, l2) -//@ highlight(beforestring, baz, l2) -//@ highlight(string, barbazstring, string, l1, l2) -//@ highlight(l0, foo, l0) -//@ highlight(l1, bar, l1) -//@ highlight(l2, baz, l2) diff --git a/gopls/internal/test/marker/testdata/highlight/issue65516.txt b/gopls/internal/test/marker/testdata/highlight/issue65516.txt deleted file mode 100644 index 3fc3be27416..00000000000 --- a/gopls/internal/test/marker/testdata/highlight/issue65516.txt +++ /dev/null @@ -1,7 +0,0 @@ -This test checks that gopls doesn't crash while highlighting functions with no -body (golang/go#65516). - --- p.go -- -package p - -func Foo() (int, string) //@highlight("int", "int"), highlight("func", "func")