Skip to content

Commit f41d27e

Browse files
committed
gopls/internal/cache: avoid panic when the primary diagnostic is broken
If the primary diagnostic has invalid position information (due to a bug in the parser or AST fix logic), gopls may panics when linking primary and related information. Avoid this panic. Fixes golang/go#66731 Change-Id: Ie2f95d158a1c93d00603a7ce4d38d8097bd8cb08 Reviewed-on: https://go-review.googlesource.com/c/tools/+/577259 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent c0ab013 commit f41d27e

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

gopls/internal/cache/check.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -1968,7 +1968,10 @@ func typeErrorsToDiagnostics(pkg *syntaxPackage, errs []types.Error, linkTarget
19681968
// This is because go/types assumes that errors are read top-down, such as
19691969
// in the cycle error "A refers to...". The structure of the secondary
19701970
// error set likely only makes sense for the primary error.
1971-
if i > 0 {
1971+
//
1972+
// NOTE: len(diags) == 0 if the primary diagnostic has invalid positions.
1973+
// See also golang/go#66731.
1974+
if i > 0 && len(diags) > 0 {
19721975
primary := diags[0]
19731976
primary.Related = append(primary.Related, protocol.DiagnosticRelatedInformation{
19741977
Location: protocol.Location{URI: diag.URI, Range: diag.Range},

0 commit comments

Comments
 (0)