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

x/tools: add a function+analyzer to detect code that uses generics #48790

Closed
findleyr opened this issue Oct 5, 2021 · 5 comments
Closed

x/tools: add a function+analyzer to detect code that uses generics #48790

findleyr opened this issue Oct 5, 2021 · 5 comments
Labels
FeatureRequest FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@findleyr
Copy link
Contributor

findleyr commented Oct 5, 2021

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.

@findleyr findleyr self-assigned this Oct 5, 2021
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Oct 5, 2021
@gopherbot gopherbot added this to the Unreleased milestone Oct 5, 2021
@timothy-king
Copy link
Contributor

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.

@gopherbot
Copy link

Change https://golang.org/cl/355009 mentions this issue: go/analysis/passes/usesgenerics: a new analysis to detect generic code

@chressie
Copy link
Contributor

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.
C defines a generic type or function.
B may or may not instantiate a type from C, but doesn't expose (export) any generic types (or functions).
Then A would "just" use B without knowing having to know anything about generics.

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?

@findleyr
Copy link
Contributor Author

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?

@chressie
Copy link
Contributor

Thanks for your quick reply! I'll comment on the CL.

CarlosRosuero pushed a commit to CarlosRosuero/typeparams that referenced this issue Jun 13, 2023
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>
@golang golang locked and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants