From 8ba91691647756357cce2b7fa3dcf4051528cfc4 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 9 Sep 2024 17:39:08 -0400 Subject: [PATCH] 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 Reviewed-by: Robert Findley --- gopls/internal/golang/highlight.go | 5 +++++ .../test/marker/testdata/highlight/issue68918.txt | 9 +++++++++ 2 files changed, 14 insertions(+) create mode 100644 gopls/internal/test/marker/testdata/highlight/issue68918.txt diff --git a/gopls/internal/golang/highlight.go b/gopls/internal/golang/highlight.go index 5ad0a61d0..f53e73f30 100644 --- a/gopls/internal/golang/highlight.go +++ b/gopls/internal/golang/highlight.go @@ -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() } diff --git a/gopls/internal/test/marker/testdata/highlight/issue68918.txt b/gopls/internal/test/marker/testdata/highlight/issue68918.txt new file mode 100644 index 000000000..ff2afc18f --- /dev/null +++ b/gopls/internal/test/marker/testdata/highlight/issue68918.txt @@ -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)