Skip to content

Commit

Permalink
gopls/internal/golang: Highlight: work around go/types bug
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
adonovan committed Sep 9, 2024
1 parent bfc94c9 commit 8ba9169
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions gopls/internal/golang/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
9 changes: 9 additions & 0 deletions gopls/internal/test/marker/testdata/highlight/issue68918.txt
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 8ba9169

Please sign in to comment.