Description
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:
// FileVersions maps a file to the file's Go version.
// If the file doesn't specify a version and Config.GoVersion is not
// given, the reported version is the zero version (Major, Minor = 0, 0).
FileVersions map[*token.File]Version
The Version
type will be defined as follows:
// A Version represents a released Go version.
type Version struct {
Major int
Minor int
}
The Version
type may also define/export the following methods for convenience:
// String returns a string representation of v in the form "goMajor.Minor".
func (v Version) String() string
// Equal reports whether v and u are the same version.
func (v Version) Equal(u Version) bool
// Before reports whether version v is before version u.
func (v Version) Before(u Version) bool
// After reports whether version v is after version u.
func (v Version) After(u Version) bool
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 in go/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.