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

audit staticcheck for GODEBUG=gotypesalias=1 support #1523

Closed
adonovan opened this issue Apr 19, 2024 · 2 comments
Closed

audit staticcheck for GODEBUG=gotypesalias=1 support #1523

adonovan opened this issue Apr 19, 2024 · 2 comments

Comments

@adonovan
Copy link
Contributor

adonovan commented Apr 19, 2024

Go 1.22 introduced the types.Alias type, but did not start creating instances of them, unless GODEBUG=gotypesalias=1. Go 1.23 will start creating instances of them, unless GODEBUG=gotypesalias=0. The effective default value of GODEBUG is determined by the version of the go directive in the go.mod file of the application. It is inevitably an incompatible change.

Staticcheck will need to handle types.Alias values. In most cases, it is sufficient to ensure that every type switch and type assertion that tests for a particular types.Type removes any Alias constructors, either by calling Unalias or Underlying as the situation requires. (You may find this package useful during the transition.) In some cases, for example when a name or source location is required, the application can do a better job by not discarding aliases, but handling them with additional logic.

You may find it helpful to:

  • look at some of the CLs attached to the issues below to get a feel for what is involved;
  • run the analyzer in https://go.dev/cl/575057, which detects all the type switches and assertions that need to be audited;
  • set up an additional CI (e.g. GitHub Actions) build with GODEBUG=gotypesalias=1.

Let us know if we can help.

(In due course, aliases will support type parameters, like Named types. We expect this change will be straightforward since it is compatible.)

Related:

cc @findleyr

@adonovan adonovan added the needs-triage Newly filed issue that needs triage label Apr 19, 2024
@adonovan
Copy link
Contributor Author

(Updated; sorry about that. Feel free to delete your note to unsubscribe.)

@dominikh
Copy link
Owner

Thanks for the detailed description and resources. I'll get started on this as soon as possible.

@dominikh dominikh removed the needs-triage Newly filed issue that needs triage label Apr 20, 2024
dominikh added a commit that referenced this issue Apr 24, 2024
Go 1.22 introduced go/types.Alias, a node that explicitly encodes type
aliases. GODEBUG=gotypesalias=1 enables this behavior. In Go 1.23, that
setting will become the new default.

Update all code that compares types (via equality, string equality, or
type assertions) to account for aliases. Most checks don't require
changes, as they operate on underlying types. For the remainder, a mix
of types.Unalias and explicit handling of aliases is necessary.

Closes: gh-1523
gopherbot pushed a commit to golang/tools that referenced this issue Oct 29, 2024
Also: remove t.Skips related to its types.Alias support,
which is done (dominikh/go-tools#1523).

A follow-up change will revert the workaround for missing
buildir support for range-over-func (dominikh/go-tools#1494),
which is also done.

Change-Id: Ic3dff1e2d9616bc12d0ee2c98e81ca747491c0cc
Reviewed-on: https://go-review.googlesource.com/c/tools/+/586076
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: 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
None yet
Projects
None yet
Development

No branches or pull requests

2 participants