[dev.typeparams] go/types: export the Config.GoVersion field

Export the types.Config.GoVersion field, so that users can specify a
language compatibility version for go/types to enforce.

Updates #46648

Change-Id: I9e00122925faf0006cfb08c3f2d022619d5d54d5
Reviewed-on: https://go-review.googlesource.com/c/go/+/334533
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Rob Findley 2021-07-14 14:19:36 -04:00 committed by Robert Findley
parent 5517053d17
commit 2b10d7ff0b
5 changed files with 6 additions and 13 deletions

View file

@ -103,12 +103,12 @@ type ImporterFrom interface {
// A Config specifies the configuration for type checking. // A Config specifies the configuration for type checking.
// The zero value for Config is a ready-to-use default configuration. // The zero value for Config is a ready-to-use default configuration.
type Config struct { type Config struct {
// goVersion describes the accepted Go language version. The string // GoVersion describes the accepted Go language version. The string
// must follow the format "go%d.%d" (e.g. "go1.12") or it must be // must follow the format "go%d.%d" (e.g. "go1.12") or it must be
// empty; an empty string indicates the latest language version. // empty; an empty string indicates the latest language version.
// If the format is invalid, invoking the type checker will cause a // If the format is invalid, invoking the type checker will cause a
// panic. // panic.
goVersion string GoVersion string
// If IgnoreFuncBodies is set, function bodies are not // If IgnoreFuncBodies is set, function bodies are not
// type-checked. // type-checked.

View file

@ -180,9 +180,9 @@ func NewChecker(conf *Config, fset *token.FileSet, pkg *Package, info *Info) *Ch
info = new(Info) info = new(Info)
} }
version, err := parseGoVersion(conf.goVersion) version, err := parseGoVersion(conf.GoVersion)
if err != nil { if err != nil {
panic(fmt.Sprintf("invalid Go version %q (%v)", conf.goVersion, err)) panic(fmt.Sprintf("invalid Go version %q (%v)", conf.GoVersion, err))
} }
return &Checker{ return &Checker{

View file

@ -244,7 +244,7 @@ func testFiles(t *testing.T, sizes Sizes, filenames []string, srcs [][]byte, man
// typecheck and collect typechecker errors // typecheck and collect typechecker errors
var conf Config var conf Config
conf.Sizes = sizes conf.Sizes = sizes
SetGoVersion(&conf, goVersion) conf.GoVersion = goVersion
// special case for importC.src // special case for importC.src
if len(filenames) == 1 { if len(filenames) == 1 {

View file

@ -140,8 +140,7 @@ func testTestDir(t *testing.T, path string, ignore ...string) {
// parse and type-check file // parse and type-check file
file, err := parser.ParseFile(fset, filename, nil, 0) file, err := parser.ParseFile(fset, filename, nil, 0)
if err == nil { if err == nil {
conf := Config{Importer: stdLibImporter} conf := Config{GoVersion: goVersion, Importer: stdLibImporter}
SetGoVersion(&conf, goVersion)
_, err = conf.Check(filename, fset, []*ast.File{file}, nil) _, err = conf.Check(filename, fset, []*ast.File{file}, nil)
} }

View file

@ -4,11 +4,5 @@
package types package types
// SetGoVersion sets the unexported goVersion field on config, so that tests
// which assert on behavior for older Go versions can set it.
func SetGoVersion(config *Config, goVersion string) {
config.goVersion = goVersion
}
// Debug is set if go/types is built with debug mode enabled. // Debug is set if go/types is built with debug mode enabled.
const Debug = debug const Debug = debug