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/tools: add a function+analyzer to detect code that uses generics #48790
Comments
It might be valuable to answer both: P transitively uses generics, and P uses generics itself. But yeah if we need to choose exactly one to implement, it is P transitively uses generics. This can be done as a package Fact. |
Change https://golang.org/cl/355009 mentions this issue: |
I think it would be super useful if the analyzer could distinguish whether a package only "uses" generics or doesn't need to know about generics at all. For example consider three packages A, B, C, where A depends on B, and B depends on C. It would be great, if we could distinguish between these 3 cases, because it would allow us to experiment with generics in a more controlled way. I think this could also be realized as a Fact (but I might be wrong). WDYT? |
Hi @chressie -- in CL 355009 I distinguish five different 'generics' features, and report which are used directly vs transitively. This may answer some of your questions: we would see that A does not directly use any generics features, and that B only directly uses instantiation. However, I think there's a more subtle interpretation of your request: can we compute whether B 'exposes' any generics features. This more subtle interpretation is non-trivial to compute: see the objectpath package for an algorithm to find 'reachable' objects. It's even harder (and likely infeasible) to prove that the dynamic type of an interface can never be e.g. an instantiated type. I've added you as a reviewer of that CL. Would it meet your needs? |
Thanks for your quick reply! I'll comment on the CL. |
Add a new "usesgenerics" analyzer to report uses of features related to generic programming. This analyzer may be used to temporarily guard other analyzers until they may be updated to support generic features. Along the way, update the typeparams API to return the Info.Instances map, rather than provide indirect access, so that it can be iterated. Fixes golang/go#48790 Change-Id: Ia3555524beff6e19f0b9101582205e26e757c8da Reviewed-on: https://go-review.googlesource.com/c/tools/+/355009 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Tim King <taking@google.com>
As we work on updating tools to support generics, an emerging theme is that sometimes we need to be able to stage partial support for generics, as in "only run this analysis if the package doesn't use generics".
Discussing with @timothy-king and others, a logical way to achieve this is via an analyzer that exports a fact reporting whether a package uses generics. As a first cut, I think it makes sense to just report whether a package transitively imports any packages containing declarations with type parameters. I'm not sure there's much value in additional granularity.
The text was updated successfully, but these errors were encountered: