}
// A Checker maintains the state of the type checker.
-// It must be created with NewChecker.
+// It must be created with [NewChecker].
type Checker struct {
// package information
// (initialized by NewChecker, valid for the life-time of checker)
fset *token.FileSet
pkg *Package
*Info
- version version // accepted language version
- versionErr error // version error, delayed from NewChecker
- nextID uint64 // unique Id for type parameters (first valid Id is 1)
- objMap map[Object]*declInfo // maps package-level objects and (non-interface) methods to declaration info
- impMap map[importKey]*Package // maps (import path, source directory) to (complete or fake) package
- valids instanceLookup // valid *Named (incl. instantiated) types per the validType check
+ version version // accepted language version
+ posVers map[token.Pos]version // maps file start positions to versions (may be nil)
+ nextID uint64 // unique Id for type parameters (first valid Id is 1)
+ objMap map[Object]*declInfo // maps package-level objects and (non-interface) methods to declaration info
+ impMap map[importKey]*Package // maps (import path, source directory) to (complete or fake) package
+ valids instanceLookup // valid *Named (incl. instantiated) types per the validType check
// pkgPathMap maps package names to the set of distinct import paths we've
// seen for that name, anywhere in the import graph. It is used for
// (initialized by Files, valid only for the duration of check.Files;
// maps and lists are allocated on demand)
files []*ast.File // package files
- posVers map[*token.File]version // Pos -> Go version mapping
imports []*PkgName // list of imported packages
dotImportMap map[dotImportKey]*PkgName // maps dot-imported objects to the package they were dot-imported through
recvTParamMap map[*ast.Ident]*TypeParam // maps blank receiver type parameters to their type
// isBrokenAlias reports whether alias doesn't have a determined type yet.
func (check *Checker) isBrokenAlias(alias *TypeName) bool {
- return alias.typ == Typ[Invalid] && check.brokenAliases[alias]
+ return !isValid(alias.typ) && check.brokenAliases[alias]
}
func (check *Checker) rememberUntyped(e ast.Expr, lhs bool, mode operandMode, typ *Basic, val constant.Value) {
check.cleaners = append(check.cleaners, c)
}
-// NewChecker returns a new Checker instance for a given package.
-// Package files may be added incrementally via checker.Files.
+// NewChecker returns a new [Checker] instance for a given package.
+// [Package] files may be added incrementally via checker.Files.
func NewChecker(conf *Config, fset *token.FileSet, pkg *Package, info *Info) *Checker {
// make sure we have a configuration
if conf == nil {
info = new(Info)
}
- version, versionErr := parseGoVersion(conf.GoVersion)
- if pkg != nil {
- pkg.goVersion = conf.GoVersion
- }
+ // Note: clients may call NewChecker with the Unsafe package, which is
+ // globally shared and must not be mutated. Therefore NewChecker must not
+ // mutate *pkg.
+ //
+ // (previously, pkg.goVersion was mutated here: go.dev/issue/61212)
return &Checker{
- conf: conf,
- ctxt: conf.Context,
- fset: fset,
- pkg: pkg,
- Info: info,
- version: version,
- versionErr: versionErr,
- objMap: make(map[Object]*declInfo),
- impMap: make(map[importKey]*Package),
+ conf: conf,
+ ctxt: conf.Context,
+ fset: fset,
+ pkg: pkg,
+ Info: info,
+ objMap: make(map[Object]*declInfo),
+ impMap: make(map[importKey]*Package),
}
}
}
}
+ // collect file versions
for _, file := range check.files {
- v, _ := parseGoVersion(file.GoVersion)
- if v.major > 0 {
+ check.recordFileVersion(file, check.conf.GoVersion)
+ if v, _ := parseGoVersion(file.GoVersion); v.major > 0 {
if v.equal(check.version) {
continue
}
// If there is no check.version, then we don't really know what Go version to apply.
// Legacy tools may do this, and they historically have accepted everything.
// Preserve that behavior by ignoring //go:build constraints entirely in that case.
- if (v.before(check.version) && check.version.before(version{1, 21})) || check.version.equal(version{0, 0}) {
+ if (v.before(check.version) && check.version.before(go1_21)) || check.version.equal(go0_0) {
continue
}
if check.posVers == nil {
- check.posVers = make(map[*token.File]version)
+ check.posVers = make(map[token.Pos]version)
}
- check.posVers[check.fset.File(file.FileStart)] = v
+ check.posVers[file.FileStart] = v
+ check.recordFileVersion(file, file.GoVersion) // overwrite package version
}
}
}
-// A posVers records that the file starting at pos declares the Go version vers.
-type posVers struct {
- pos token.Pos
- vers version
-}
-
// A bailout panic is used for early termination.
type bailout struct{}
var errBadCgo = errors.New("cannot use FakeImportC and go115UsesCgo together")
func (check *Checker) checkFiles(files []*ast.File) (err error) {
- if check.versionErr != nil {
- return check.versionErr
+ if check.pkg == Unsafe {
+ // Defensive handling for Unsafe, which cannot be type checked, and must
+ // not be mutated. See https://go.dev/issue/61212 for an example of where
+ // Unsafe is passed to NewChecker.
+ return nil
+ }
+
+ // Note: parseGoVersion and the subsequent checks should happen once,
+ // when we create a new Checker, not for each batch of files.
+ // We can't change it at this point because NewChecker doesn't
+ // return an error.
+ check.version, err = parseGoVersion(check.conf.GoVersion)
+ if err != nil {
+ return err
}
if check.version.after(version{1, goversion.Version}) {
return fmt.Errorf("package requires newer Go version %v", check.version)
check.monomorph()
}
+ check.pkg.goVersion = check.conf.GoVersion
check.pkg.complete = true
// no longer needed - release memory
assert(val != nil)
// We check allBasic(typ, IsConstType) here as constant expressions may be
// recorded as type parameters.
- assert(typ == Typ[Invalid] || allBasic(typ, IsConstType))
+ assert(!isValid(typ) || allBasic(typ, IsConstType))
}
if m := check.Types; m != nil {
m[x] = TypeAndValue{mode, typ, val}
m[node] = scope
}
}
+
+func (check *Checker) recordFileVersion(file *ast.File, version string) {
+ if m := check.FileVersions; m != nil {
+ m[file] = version
+ }
+}