Background: Since go1.16 (https://github.com/golang/go/issues/42290), go/types has internally recorded three positions (start, pos, and end) for type errors. However, only the Pos field is exposed. The x/tools repo has long resorted to unsavory hacks such as reading the non-exported fields, and using heuristics on the source text (!) to guess the end position.

Proposal: We propose to publish the Start and End fields.

package types

type Error struct {
    Fset       *token.FileSet // file set for interpretation of Pos
    // Invariant: Start <= Pos < End || Start == Pos == End.
    Pos        token.Pos      // error position
+   Start, End token.Pos      // start, end of containing expression
    Msg        string         // error message
    Soft       bool           // if set, error is "soft"
}

@gri @findleyr

Comment From: gabyhelp

Related Issues

Related Code Changes

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

Comment From: gopherbot

Change https://go.dev/cl/650217 mentions this issue: gopls/internal/analysis: simplify type-error analyzers with Cursor