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

go/types: should the 'newer Go version' error be reported to Config.Error? #66525

Open
timothy-king opened this issue Mar 25, 2024 · 4 comments
Assignees
Labels
Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@timothy-king
Copy link
Contributor

Since go1.21, (*go/types.Checker).Files() has returned an error when a package has a GoVersion (specified in types.Config.GoVersion) that is newer than the go/types library it is compiled against. Error in question is:

	if check.version.after(version{1, goversion.Version}) {
		return fmt.Errorf("package requires newer Go version %v", check.version)
	}

x/tools/go/packages was assuming the only error not reported to types.Config.Error was types.errBadCgo. Example downstream issue: #65590.

This issue is to decide whether this error should be reported to the types.Config.Error field, and if so how?

@griesemer @adonovan

@timothy-king timothy-king added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Tools This label describes issues relating to any tools in the x/tools repository. labels Mar 25, 2024
@timothy-king timothy-king added this to the Go1.23 milestone Mar 25, 2024
@gopherbot
Copy link

Change https://go.dev/cl/574255 mentions this issue: go/packages: report type errors unconditionally

@timothy-king
Copy link
Contributor Author

One option that came up in code review is to report the error as a types.Error on the first package symbol and to proceed as if the GoVersion=="". This would not fail-fast and best effort types would work better for IDEs like gopls.

OTOH it is not clear whether it is acceptable to do a best effort typing when we know the package is a future GoVersion.

A minor complicating wrinkle is when all files are pinned to versions less than the version of go/types. Should there be an error on this package at all?

@adonovan
Copy link
Member

This issue is to decide whether this error should be reported to the types.Config.Error field, and if so how?

Yes, all errors should be reported through Config.Error, and the error returned by Checker.Files should merely be the first of them. We should re-establish the invariant that a non-zero number of calls to Error is equivalent to the package being ill-typed. The two early error returns in Checker.Files both violate this invariant. The FakeImportC one was rarely exercised, but the version check (downgraded from a panic by https://go.dev/cl/507975) is much more important. Both these errors should be reported (e.g. against the first package declaration if no other location is appropriate), but should not abort type-checking of the package. We should document clearly in the code that early returns are not allowed.

Clients of go/types make widespread assumptions that the type-checker does the best it can with the syntax, and that well-formed declarations are annotated with types even if the package contains some type errors. For example, it is common to evaluate info.Defs[decl].(*types.Func) where decl is an *ast.FuncDecl, without needing a check.

I can send you a CL for the go/types change if you like.

@gopherbot
Copy link

Change https://go.dev/cl/574495 mentions this issue: go/types: don't fail fast on Go version errors

@adonovan adonovan self-assigned this Mar 26, 2024
@adonovan adonovan removed the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Mar 26, 2024
gopherbot pushed a commit to golang/tools that referenced this issue Apr 2, 2024
Ensure that when types.Checker.Files returns an error some
error is always reported and the package is illTyped.

Adds an additional hint to recompile the tool when types returns
an error "package requires newer Go version 1.22" or similar.

Updates golang/go#65608
Updates golang/go#66525

Change-Id: I131ee3e668815f88d16a18c6e92f002220284a03
Reviewed-on: https://go-review.googlesource.com/c/tools/+/574255
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
gopherbot pushed a commit that referenced this issue Apr 3, 2024
Many tools (especially in the IDE) rely on type information
being computed even for packages that have some type errors.
Previously, there were two early (error) exits in checkFiles
that violated this invariant, one related to FakeImportC
and one related to a too-new Config.GoVersion.
(The FakeImportC one is rarely encountered in practice,
but the GoVersion one, which was recently downgraded from
a panic by CL 507975, was a source of crashes
due to incomplete type information.)

This change moves both of those errors out of checkFiles
so that they report localized errors and don't obstruct
type checking. A test exercises the errors, and that
type annotations are produced.

Also, we restructure and document checkFiles to make clear
that it is never supposed to stop early.

Updates #66525

Change-Id: I9c6210e30bbf619f32a21157f17864b09cfb5cf2
Reviewed-on: https://go-review.googlesource.com/c/go/+/574495
Reviewed-by: Tim King <taking@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
philip-peterson pushed a commit to philip-peterson/go that referenced this issue Apr 12, 2024
The type checker produces an error if the Go version is too new. When
compiled with Go 1.21, this error is silently dropped on the floor and
the type checked package is empty, due to golang/go#golang#66525.

Guard against this very problematic failure mode by filtering out Go
versions that are too new. We should also produce a diagnostic, but that
is more complicated and covered by golang#61673.

Also: fix a bug where sandbox cleanup would fail due to being run with a
non-local toolchain.

Fixes golang#66677

Change-Id: Ia66f17c195382c9c55cf0ef883e898553ce950e3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/576678
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants