-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
x/exp/typeparams: a new module with a transitional API for tools #50447
Comments
This proposal has been added to the active column of the proposals project |
It may be worth emphasizing that without this package [or something similar] it is challenging to develop tools that deal with the
A way to avoid the vendoring from
We may need a permanent home for the hard to implement utilities like this. |
Indeed. Right now tip.golang.org/pkg/constraints/?m=old is failing for precisely this reason. |
It is disallowed to have any x repo, nor the main repo, depend on x/exp. |
SGTM. It is better for us to maintain two copies than have every tool to maintain its own copy... |
I think this should eventually be a part of |
Change https://golang.org/cl/377834 mentions this issue: |
Out of curiosity, would this be a v0 or a v1+ module? "tag a final version and delete it" makes me think that we may not need to reach a v1.0.0. |
We have no v1 in x repos yet (working on it) so I don't think we'd start with this one. |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
This CL begins adding a guide for the new APIs introduced with Go 1.18 to support writing tools that understand generic Go code. For now I've added a summary of the new APIs, an initial example, and some discussion of the typeparams package. Subsequent CLs will add more examples, and polish. Updates golang/go#50447 Change-Id: I4ed8d7a2f43e748374d14f3f515673d69ab2d5a0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/377834 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> Trust: Dominik Honnef <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Suzy Mueller <[email protected]>
Change https://golang.org/cl/380554 mentions this issue: |
With instantiated types, method objects are no longer unique: they may be instantiations of methods with generic receiver. However, some use-cases require finding the canonical method representing the method in the source. For these use-cases, provide an OriginMethod helper. For golang/go#50447 Change-Id: I6f8af3fb5c5eeefb11f8f3bdba54cd6692ca389f Reviewed-on: https://go-review.googlesource.com/c/tools/+/380554 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Tim King <[email protected]>
This was blocked by beta2 and some outstanding concerns about predicates on generic types (#50887), with both of those hopefully resolved, I'll work on implementing this tomorrow. I'll put together a CL for the new module, and leave it open for at least a week to invite comments. |
Change https://golang.org/cl/383375 mentions this issue: |
Hi! Please add comments on https://golang.org/cl/383375 if you have opinions on the API for this new module. Even naming suggestions are appreciated. I am pretty content with the current API, though one likely upcoming change is the name |
Add a new module for use by tool authors that need operate on generic code, while still building at older Go versions. This module API proxies the Go 1.18 API for type parameters, providing stubs at earlier Go versions. It also contains some common helpers to supplement the go/types API. For golang/go#50447 Change-Id: I97feb834eb2da646620f8377904520b511871915 Reviewed-on: https://go-review.googlesource.com/c/exp/+/383375 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> Trust: Dominik Honnef <[email protected]> Reviewed-by: Ian Cottrell <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
I've submitted the initial CL for this module: https://pkg.go.dev/golang.org/x/exp/typeparams I didn't get much feedback on the CL, so decided to add a caveat that the API is still experimental. Please try to use it in your projects, and let me know if you have opinions. I will remove the caveat in around a week, at which point we will not break the API (though we may still deprecate things). Meanwhile, I am lagging behind on documentation, unfortunately. With 1.18 issues more or less resolved at this point, it is my highest priority. |
Change https://go.dev/cl/388274 mentions this issue: |
Add a rough draft of a guide to the new go/ast and go/types APIs introduced with Go 1.18, including explanation of how the x/exp/typeparams package helps bridge gaps. For golang/go#50447 Change-Id: I2a5f9a5f0801a71466a4faa3e42efca75b3c7d3c Reviewed-on: https://go-review.googlesource.com/c/exp/+/388274 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
Change https://go.dev/cl/391674 mentions this issue: |
Background
With Go 1.18, we are adding a large number of new APIs in
go/*
packages such asgo/ast
andgo/types
to represent the new languages features introduced with generics (see proposals #47781 and #47916). Those proposals describe the new types and functions, but do not comprehensively discuss their semantics nor offer guidance on how to update existing tools. Recently, we've updated many tools inx/tools
, such asgopls
andvet
analyzers, and it would be great to share what we learned from that work with the larger community, to help others update their tools.There were two notably difficult aspects of updating our tools to support generics:
printf
analyzer to report a diagnostic if any members of the type set to not match the corresponding printf verb.go/types
does not yet expose an API for this, because at the time we weren't sure what the correct interface for a type set should be.Our solutions to both of those problems are contained in the
x/tools/internal/typeparams
package. This package exposes a transitional API, which at Go 1.18 is just a proxy for the correspondinggo/*
APIs, and at earlier Go versions is a stub. For example, it exports atypeparams.Union
type, which is an alias forgo/types.Union
at Go 1.18 but a placeholder at Go 1.17. Furthermore thetypeparams.StructuralTerms
function computes the 'structural' terms of a type parameter, i.e. the normalized list of structural restrictions, after reducing unions and intersections.Proposal
I propose that we to add a new module to
x/exp
,golang.org/x/exp/typeparams
, which contains the functionality currently exposed byx/tools/internal/typeparams
(modulo improved documentation and other superficial improvements). By being publicly importable, this module would provide the same functionality to other tools asx/tools/internal/typeparams
has providedx/tools
. Furthermore, this module could serve as a home (or entrypoint) for more detailed documentation on how to update tools to support generics. As an initial pass, it could include guidance in its package documentation or in an associatedREADME
.I considered instead suggesting that tools simply copy
x/tools/internal/typeparams
, but particularly with thetypeparams.StructuralTerms
API it is possible that we will release bug-fixes, so we should make updating easy.As for why a new module, and why
x/exp
, consider the following:Putting this code in a new module in
x/exp
identifies it as experimental/transient, and means that we won't break builds depending onx/tools
for other reasons, if/when it is deleted.Caveats
If we add
x/exp/typeparams
, it makes sense to use it to replace the existing usage ofx/tools/internal/typeparams
. In that case,x/exp/typeparams
would not only be required byx/tools
, it would be vendored intocmd
by way ofcmd/vet
.It might not be a good precedent to include code fromSince this is disallowed, we'll have to keep usingx/exp
in the go distribution, though I'm not aware of a practical problem with this. We could alternatively create a new submodule ofx/tools
.x/tools/internal/typeparams
, and sync it withx/exp
via a script.Aside: suggestions for improved APIs are certainly welcome, but I'd like to keep the initial focus of the discussion here about whether or not to create such a module, and where to put it. If this proposal is accepted, we can shift discussion to the details.
The text was updated successfully, but these errors were encountered: