go/types,types2: allocate the used* maps in initFiles

As described in the associated comment, we need to reallocate usedVars
and usedPkgNames in initFiles, as they are nilled out at the end of
Checker.Files, which may be called multiple times.

Fixes #72122

Change-Id: I9f6eb86e072d9d43a8720f6a5e86d827de6006a9
Reviewed-on: https://go-review.googlesource.com/c/go/+/655437
Auto-Submit: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Rob Findley 2025-03-07 18:13:51 +00:00 committed by Gopher Robot
parent d43c0f80d8
commit fe9b292b11
2 changed files with 14 additions and 0 deletions

View File

@ -315,6 +315,13 @@ func (check *Checker) initFiles(files []*syntax.File) {
check.objPath = nil
check.cleaners = nil
// We must initialize usedVars and usedPkgNames both here and in NewChecker,
// because initFiles is not called in the CheckExpr or Eval codepaths, yet we
// want to free this memory at the end of Files ('used' predicates are
// only needed in the context of a given file).
check.usedVars = make(map[*Var]bool)
check.usedPkgNames = make(map[*PkgName]bool)
// determine package name and collect valid files
pkg := check.pkg
for _, file := range files {

View File

@ -339,6 +339,13 @@ func (check *Checker) initFiles(files []*ast.File) {
check.objPath = nil
check.cleaners = nil
// We must initialize usedVars and usedPkgNames both here and in NewChecker,
// because initFiles is not called in the CheckExpr or Eval codepaths, yet we
// want to free this memory at the end of Files ('used' predicates are
// only needed in the context of a given file).
check.usedVars = make(map[*Var]bool)
check.usedPkgNames = make(map[*PkgName]bool)
// determine package name and collect valid files
pkg := check.pkg
for _, file := range files {