go/ast: add File.GoVersion

For #57001, compilers and others tools will need to understand that
a different Go version can be used in different files in a program,
according to the //go:build lines in those files.

This CL adds a GoVersion string field to ast.File, to allow exposing this
per-file Go version information.

For #59033.

Change-Id: I3931ea86c237983d152964f48dce498bcb1f06aa
Reviewed-on: https://go-review.googlesource.com/c/go/+/476276
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>
This commit is contained in:
Russ Cox 2023-03-13 17:12:40 -04:00 committed by Gopher Robot
parent 830f54b70e
commit 3974029671
4 changed files with 5 additions and 2 deletions

View file

@ -1,2 +1,3 @@
pkg go/ast, type File struct, GoVersion string #59033
pkg go/build/constraint, func GoVersion(Expr) string #59033

View file

@ -1046,6 +1046,7 @@ type File struct {
Imports []*ImportSpec // imports in this file
Unresolved []*Ident // unresolved identifiers in this file
Comments []*CommentGroup // list of all comments in the source file
GoVersion string // minimum Go version required by //go:build or // +build directives
}
// Pos returns the position of the package declaration.

View file

@ -136,7 +136,8 @@ func main() {
// 57 . Unresolved: []*ast.Ident (len = 1) {
// 58 . . 0: *(obj @ 29)
// 59 . }
// 60 }
// 60 . GoVersion: ""
// 61 }
}
// This example illustrates how to remove a variable declaration

View file

@ -491,5 +491,5 @@ func MergePackageFiles(pkg *Package, mode MergeMode) *File {
}
// TODO(gri) need to compute unresolved identifiers!
return &File{doc, pos, NewIdent(pkg.Name), decls, minPos, maxPos, pkg.Scope, imports, nil, comments}
return &File{doc, pos, NewIdent(pkg.Name), decls, minPos, maxPos, pkg.Scope, imports, nil, comments, ""}
}