-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: Export Info.FileVersions for access to file-specific version information #62605
Comments
cc: @findleyr |
I seem to understand that this information is already available via the Is this new API about parsing the version so that it can be used and compared more easily? That would remind me of #62039, so I wonder if we could deduplicate. Or is it about accessing the version information when one loads |
@mvdan You are correct that clients can find out about the respective Go version already, but that would involve duplicating parsing the string and various decisions. For instance, the The type checker needs to make all these decisions already internally. By exporting the data via |
Ah, so go/types would "flatten" this information beyond just parsing it out of the go/ast string - that makes sense then. It might still make sense to reuse the logic from #62039 if possible, or somehow merge the proposals. That API is still in flux, but it would be a bit weird for the |
Maybe the Version should be a string to match |
@timothy-king A string doesn't lend itself well to before/after comparisons. |
I think the key of the map should be I think there's meandering history that led us to |
This proposal has been added to the active column of the proposals project |
We should probably do something to fit better with #62039. Maybe that proposal should become a new go/version package, and then this package can just use a plain string and direct people to the go/version package for operations. If we do that, the only addition in this proposal is the FileVersions map added to Info, of type |
Change https://go.dev/cl/532580 mentions this issue: |
Change https://go.dev/cl/533056 mentions this issue: |
Updated proposal, per internal discussion and comments here and here. We propose to export the following additional map from // FileVersions maps a file to the file's Go version string.
// If the file doesn't specify a version and Config.GoVersion is not
// given, the reported version is the empty string.
FileVersions map[*ast.File]string The version string can be compared against with functionality as suggested here.
|
Change https://go.dev/cl/533975 mentions this issue: |
Change https://go.dev/cl/534018 mentions this issue: |
By analogy, didn't we regret not having declaring identifiers in I don't have a strong opinion either way, but it seems simpler to populate every file in the |
@findleyr Agreed. Removed the open question. |
For #62605. Change-Id: I6e9032eb92db758bf359e7cc9c4cedc1e0fb2309 Reviewed-on: https://go-review.googlesource.com/c/go/+/534018 Auto-Submit: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
FWIW, I think *token.File would be easier for users in practice than *ast.File. With the go/* APIs, you almost always need the *token.FileSet handy anyway, so given any token.Pos, you can readily lookup the *token.File. And token.Pos is available through the go/types public APIs, as well as at any ast.Node within a file (including *ast.File itself). By comparison, nothing points back up to the *ast.File, and that's not information users currently need to explicitly track for any other parts of the API. So users may need to restructure their code more to ensure its availability. (E.g., discussing with @griesemer right now, we're leaning towards leaving types2 keyed by *syntax.PosBase instead of changing it to *syntax.File, because the former aligns more naturally with how the unified IR writer is currently structured.) |
@mdempsky *token.File is problematic for a couple reasons:
This is why we originally switched to CC @adonovan, with whom I was discussing this earlier, and who agreed at the time that |
I'm not worried about increased liveness, because in practice one nearly always wants the token, ast, and types information for a complete package until all of them become garbage together. @mdempsky makes a good point that it's easier to find the token.File from an arbitrary node. I was concerned earlier that line directives make things confusing, but they affect only the apparent filename and line, not the token.File instance, associated with the ast.File. So I'm ok with using token.File. |
I agree it may be more convenient, but it still feels off to me, since I think of go/types as deriving from the abstract syntax (which holds The one exception to my mental model is the fact that I am also not entirely convinced by the convenience argument. I think that one often already has the No problem if I'm overruled. |
After prototyping using goversions for I agree that it seems odd to get information from |
Russ suggested that we keep the representation opaque and provide an accessor function, allowing the representation to evolve as needed:
That's not consistent with the other fields of Info which are public, but in hindsight I do wish we had encapsulated them as well, so perhaps it's not too late to start now. It does leave the question of how the user initializes the Info to indicate that they want file version info, but perhaps this particular mapping is so tiny that it's fine to gather it unconditionally. |
After in-person discussions with @adonovan and @findleyr, we concluded that the API change should be essentially as proposed a few days ago (comment): // FileVersions maps a file to the file's Go version string.
// If the file doesn't specify a version the reported string
// is Config.GoVersion.
FileVersions map[*ast.File]string The version string can be compared against with functionality as suggested in #62039 (comment). Discussion: |
Note that the existing implementation in types2 always set go/src/cmd/compile/internal/types2/check.go Line 289 in aa05674
|
For golang#62605. Change-Id: I6e9032eb92db758bf359e7cc9c4cedc1e0fb2309 Reviewed-on: https://go-review.googlesource.com/c/go/+/534018 Auto-Submit: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
Have all remaining concerns about this proposal been addressed? A new map will be added to
|
Change https://go.dev/cl/539016 mentions this issue: |
Based on the discussion above, this proposal seems like a likely accept. A new map will be added to
|
Change https://go.dev/cl/540056 mentions this issue: |
For #62605. Change-Id: Icf1a8332e4b60d77607716b55893ea2f39ae2f10 Reviewed-on: https://go-review.googlesource.com/c/go/+/540056 Run-TryBot: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
Change https://go.dev/cl/541296 mentions this issue: |
This CL changes the FileVersions map to map to version strings rather than Version structs, for use with the new go/versions package. Adjust the cmd/dist bootstrap package list to include go/version. Adjust the compiler's noder to work with the new API. For #62605. For #63974. Change-Id: I191a7015ba3fb61c646e9f9d3c3dbafc9653ccb5 Reviewed-on: https://go-review.googlesource.com/c/go/+/541296 Reviewed-by: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
No change in consensus, so accepted. 🎉 A new map will be added to
|
Adds a new versions package to provide x/tools a way to deal with new GoVersion() and FileVersions API from go/types and the new go/version standard library. This provides a stable API until 1.26. Updates golang/go#63374 Updates golang/go#62605 Change-Id: I4de54df00ea0f4363c0383cbdc917186277bfd0a Reviewed-on: https://go-review.googlesource.com/c/tools/+/533056 Run-TryBot: Tim King <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
Change https://go.dev/cl/541736 mentions this issue: |
Updates golang/go#62605 Change-Id: I127b57f4eb5b6d2521dde7f139048987614e1904 Reviewed-on: https://go-review.googlesource.com/c/tools/+/533975 Reviewed-by: Robert Findley <[email protected]> Run-TryBot: Tim King <[email protected]> TryBot-Result: Gopher Robot <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
Make sure the FileVersions map is populated in test runs. For #62605. Change-Id: I06585b5110a4a98b577edb8e03a4981b2484a5a4 Reviewed-on: https://go-review.googlesource.com/c/go/+/541736 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
A number of CLs are already landed, including the new API. Is there anything else needed to be done for this? Or we can close this issue? Thanks. |
Uses the (*types.Info).FileVersion to disable the loopclosure checker when in an *ast.File that uses GoVersion >= 1.22. Updates golang/go#62605 Updates golang/go#63888 Change-Id: I2ebe974bc2ee2323eafb0f02d455ab76b3b9268d Reviewed-on: https://go-review.googlesource.com/c/tools/+/539016 Run-TryBot: Tim King <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
Change https://go.dev/cl/546836 mentions this issue: |
For #62605. Change-Id: I3c06b835c874c1be5aa5293e3906bdd06c021d87 Reviewed-on: https://go-review.googlesource.com/c/go/+/546836 Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Bypass: Robert Griesemer <[email protected]>
For golang#62605. Change-Id: I3c06b835c874c1be5aa5293e3906bdd06c021d87 Reviewed-on: https://go-review.googlesource.com/c/go/+/546836 Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Bypass: Robert Griesemer <[email protected]>
This proposal has been updated. See this comment for the most recent proposed changes.
Background
Tools (such as the compiler) need access to per-file version information as provided in the processed Go source such that they (the tools) can do correct semantic analysis. For instance, this information is needed for correct scoping rules for the loop variable scoping change (#60078). The type checker (
go/types
) can collect this information during type-checking and provide it to clients.Proposal
We propose to export the following additional data structures from
go/types
:We add a new map to the
go/types.Info
struct which will be populated if present:The
Version
type will be defined as follows:The
Version
type may also define/export the following methods for convenience:These methods are not necessary (they are trivial to implement if needed), but unexported versions of them exist in the type checker and there are no strong reasons to withhold them from being exported.
Implementation
This proposal has been implemented in
types2
for use by the compiler and the code is present in unexported form ingo/types
, see CL 515135. If there are no objections to the proposed API we can simply export the functionality. If there are viable alternative proposals, we can adjust the existing implementation as needed.The text was updated successfully, but these errors were encountered: