From 1f7dbdf01abbd56f7fb4ac2c61ea25ff673633af Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 27 Feb 2024 11:18:55 -0500 Subject: [PATCH] gopls/internal/cache: add debug assertions for bug report Updates golang/go#65960 Change-Id: I01a416a0cf9cf8e13195c0d9405008ded1a9c53a Reviewed-on: https://go-review.googlesource.com/c/tools/+/567416 Auto-Submit: Alan Donovan Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/cache/check.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/gopls/internal/cache/check.go b/gopls/internal/cache/check.go index 7b700ea9a75..50596e130ad 100644 --- a/gopls/internal/cache/check.go +++ b/gopls/internal/cache/check.go @@ -1823,6 +1823,12 @@ func typeErrorsToDiagnostics(pkg *syntaxPackage, errs []types.Error, linkTarget // report. continue } + + // Invariant: both start and end are IsValid. + if !end.IsValid() { + panic("end is invalid") + } + posn := safetoken.StartPosition(e.Fset, start) if !posn.IsValid() { // All valid positions produced by the type checker should described by @@ -1848,10 +1854,34 @@ func typeErrorsToDiagnostics(pkg *syntaxPackage, errs []types.Error, linkTarget } continue } - if !end.IsValid() || end == start { + + // debugging #65960 + // + // At this point, we know 'start' IsValid, and + // StartPosition(start) worked (with e.Fset). + // + // If the asserted condition is true, 'start' + // is also in range for pgf.Tok, which means + // the PosRange failure must be caused by 'end'. + if pgf.Tok != e.Fset.File(start) { + bug.Reportf("internal error: inconsistent token.Files for pos") + } + + if end == start { // Expand the end position to a more meaningful span. end = analysisinternal.TypeErrorEndPos(e.Fset, pgf.Src, start) + + // debugging #65960 + if _, err := safetoken.Offset(pgf.Tok, end); err != nil { + bug.Reportf("TypeErrorEndPos returned invalid end: %v", err) + } + } else { + // debugging #65960 + if _, err := safetoken.Offset(pgf.Tok, end); err != nil { + bug.Reportf("ReadGo116ErrorData returned invalid end: %v", err) + } } + rng, err := pgf.Mapper.PosRange(pgf.Tok, start, end) if err != nil { bug.Reportf("internal error: could not compute pos to range for %v: %v", e, err)