gopls/internal/golang: Highlight: work around go/types bug

go/types sometimes fails to annotate type information onto
nested composite literals when there is a type error (#69092).
This CL adds a workaround to one particularly vulnerable place
in gopls that crashes when this happens. (There are potentially
many others.)

+ test

Fixes golang/go#68918

Change-Id: I73e8e1dd8eb8965bde44d8ee3672a50ac362af52
Reviewed-on: https://go-review.googlesource.com/c/tools/+/612042
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Alan Donovan 2024-09-09 17:39:08 -04:00
Родитель bfc94c967a
Коммит 8ba9169164
2 изменённых файлов: 14 добавлений и 0 удалений

Просмотреть файл

@ -558,6 +558,11 @@ func highlightIdentifier(id *ast.Ident, file *ast.File, info *types.Info, result
highlightWriteInExpr(n.Chan)
case *ast.CompositeLit:
t := info.TypeOf(n)
// Every expression should have a type;
// work around https://github.com/golang/go/issues/69092.
if t == nil {
t = types.Typ[types.Invalid]
}
if ptr, ok := t.Underlying().(*types.Pointer); ok {
t = ptr.Elem()
}

9
gopls/internal/test/marker/testdata/highlight/issue68918.txt поставляемый Normal file
Просмотреть файл

@ -0,0 +1,9 @@
Regression test for https://github.com/golang/go/issues/68918:
crash due to missing type information in CompositeLit.
-- a.go --
package a
var _ = T{{ x }} //@hiloc(x, "x", text), diag("T", re"undefined"), diag("{ ", re"missing type")
//@highlight(x, x)