Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: go/types: publish Error.{Start,End} fields #71803

Open
adonovan opened this issue Feb 17, 2025 · 2 comments
Open

proposal: go/types: publish Error.{Start,End} fields #71803

adonovan opened this issue Feb 17, 2025 · 2 comments
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Feb 17, 2025

Background: Since go1.16 (#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

@adonovan adonovan added this to the Go1.25 milestone Feb 17, 2025
@adonovan adonovan moved this to Incoming in Proposals Feb 17, 2025
@adonovan adonovan changed the title go/types: add TypeError.End go/types: add Error.End Feb 17, 2025
@seankhliao seankhliao changed the title go/types: add Error.End proposa: go/types: add Error.End Feb 17, 2025
@seankhliao seankhliao changed the title proposa: go/types: add Error.End proposal: go/types: add Error.End Feb 17, 2025
@adonovan adonovan changed the title proposal: go/types: add Error.End proposal: go/types: publish Error.{Start,End} fields Feb 17, 2025
@gabyhelp
Copy link

Related Issues

Related Code Changes

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

@gabyhelp gabyhelp added the LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool label Feb 17, 2025
@gopherbot
Copy link
Contributor

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

gopherbot pushed a commit to golang/tools that referenced this issue Feb 18, 2025
This CL makes a number of simplifications to the type-error
analyzers:

- Nodes are found using Cursor.FindPos from the error position,
  which is very fast;
- Error position information is read from types.Error instead
  of formatting the ast.File (!) then invoking the dubious
  heuristics of analysisinternal.TypeErrorEndPos, which scans
  the text (!!) assuming well-formattedness (!!!).
- plus various minor cleanups.
- rename typesinternal.ReadGo116ErrorData to ErrorCodeStartEnd.

Updates golang/go#65966
Updates golang/go#71803

Change-Id: Ie4561144040b001b957ef6a3c3631328297d5001
Reviewed-on: https://go-review.googlesource.com/c/tools/+/650217
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Commit-Queue: Alan Donovan <adonovan@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Projects
Status: Incoming
Development

No branches or pull requests

4 participants