Commit 1731f45c39 for golang

commit 1731f45c39139f9ac35f3da51da7114f94502c87
Author: Alan Donovan <adonovan@google.com>
Date:   Tue Nov 17 15:44:46 2026 -0500

    cmd/go/internal/checks/vetfilesexperiment: housekeeping check

    This CL adds an analyzer to ensure that functions implementing
    GOEXPERIMENT=vetfiles are never referenced from the other files
    of the go command except when enclosed in an 'if Experiment.VetFiles'
    guard. Of course, the check only runs when GOEXPERIMENT=vetfiles
    is enabled, but we plan set up a CI build soon.

    For #82464

    Change-Id: I639a90c3a70295073dc3c9cd845534da206eadee
    Reviewed-on: https://go-review.googlesource.com/c/go/+/864830
    Reviewed-by: t hepudds <thepudds1460@gmail.com>
    Reviewed-by: PJ Malloy <pjmalloy@google.com>
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

diff --git a/src/cmd/go/go.vet b/src/cmd/go/go.vet
new file mode 100644
index 0000000000..64d00f8de6
--- /dev/null
+++ b/src/cmd/go/go.vet
@@ -0,0 +1,6 @@
+// This file enables the housekeeping analyzers that go test runs on
+// the packages of the go command (see go.dev/issue/82464).
+
+// Ensure the code of GOEXPERIMENT=vetfiles is reachable from the rest
+// of the go command only through "if cfg.Experiment.VetFiles" guards.
+enable ./... cmd/go/internal/checks/vetfilesexperiment.Analyzer
diff --git a/src/cmd/go/internal/checks/vetfilesexperiment/vetfilesexperiment.go b/src/cmd/go/internal/checks/vetfilesexperiment/vetfilesexperiment.go
new file mode 100644
index 0000000000..626d8a3773
--- /dev/null
+++ b/src/cmd/go/internal/checks/vetfilesexperiment/vetfilesexperiment.go
@@ -0,0 +1,136 @@
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package vetfilesexperiment defines an internal "housekeeping"
+// analyzer for the go command that ensures the implementation
+// of GOEXPERIMENT=vetfiles (go.dev/issue/82464) lives in its
+// own files and that every reference from another file to a
+// function or method declared in those files is guarded by an
+// "if cfg.Experiment.VetFiles { ... }" check.
+// This helps prevents inadvertent changes to the production
+// behavior of "go test" during the experiment.
+//
+// Only functions and methods are checked, not types, variables, or
+// fields: an existing struct may need a field of a type declared by
+// the experiment, and no if statement can enclose a declaration.
+// (The check is imperfect: a method of the experiment may also be
+// called by dynamic dispatch through an interface.)
+package vetfilesexperiment
+
+import (
+	"go/analysis"
+	"go/ast"
+	"go/token"
+	"go/types"
+	"path/filepath"
+	"strings"
+)
+
+var Analyzer = &analysis.Analyzer{
+	Name: "vetfilesexperiment",
+	Doc:  "check that references to the functions of an experiment are guarded by the experiment",
+	Run:  run,
+}
+
+func run(pass *analysis.Pass) (any, error) {
+	var stack []ast.Node
+	for _, file := range pass.Files {
+		filename := pass.Fset.File(file.FileStart).Name()
+		if isExperimentFile(pass.Pkg, filename) ||
+			strings.HasSuffix(filename, "_test.go") {
+			continue
+		}
+		ast.Inspect(file, func(n ast.Node) bool {
+			if n == nil {
+				stack = stack[:len(stack)-1] // pop
+				return true
+			}
+			stack = append(stack, n) // push
+			if id, ok := n.(*ast.Ident); ok {
+				if obj, ok := pass.TypesInfo.Uses[id].(*types.Func); ok && obj.Pos().IsValid() {
+					filename := pass.Fset.Position(obj.Pos()).Filename
+					if isExperimentFile(obj.Pkg(), filename) && !guarded(stack) {
+						pass.Reportf(id.Pos(), "reference to %s in file %s not guarded by 'if cfg.Experiment.VetFiles'",
+							obj.Name(), filepath.Base(filename))
+					}
+				}
+			}
+			return true
+		})
+	}
+	return nil, nil
+}
+
+// isExperimentFile reports whether the file of the specified name, in
+// the specified package, belongs to the experiment.
+func isExperimentFile(pkg *types.Package, filename string) bool {
+	path := pkg.Path()
+	if !strings.HasPrefix(path, "cmd/go/") {
+		return false // (equivalent to "./..." scope in cmd/go/go.vet)
+	}
+
+	// For reference, this is the (current) exhaustive list,
+	// but we encode the logic using patterns to reduce drift:
+	//
+	// cmd/go/internal/load/analysis.go
+	// cmd/go/internal/load/vetfile.go
+	// cmd/go/internal/modload/vetfile.go
+	// cmd/go/internal/test/analysis.go
+	// cmd/go/internal/vetfile/vetfile.go
+	// cmd/go/internal/vetfile/vetfile_test.go
+	// cmd/go/internal/work/analysis.go
+	// cmd/go/internal/work/analysis_profile.go
+
+	if path == "cmd/go/internal/vetfile" {
+		return true // entire package
+	}
+	base := filepath.Base(filename)
+	return base == "analysis.go" ||
+		base == "vetfile.go" ||
+		strings.HasPrefix(base, "analysis_") && !strings.HasSuffix(base, "_test.go")
+}
+
+// guarded reports whether the innermost node of the stack (a
+// reference) is within the body of an if statement guarded by the
+// experiment, or to the right of the experiment in the condition of
+// such a statement.
+func guarded(stack []ast.Node) bool {
+	ref := stack[len(stack)-1]
+	for i := len(stack) - 2; i >= 0; i-- {
+		if stmt, ok := stack[i].(*ast.IfStmt); ok {
+			if guard := experimentConjunct(stmt.Cond); guard != nil {
+				if within(ref, stmt.Body) ||
+					within(ref, stmt.Cond) && ref.Pos() >= guard.End() {
+					return true
+				}
+			}
+		}
+	}
+	return false
+}
+
+// experimentConjunct returns the operand of the && chain cond that
+// tests the experiment (cfg.Experiment.VetFiles), or nil if none does.
+func experimentConjunct(cond ast.Expr) ast.Expr {
+	switch cond := ast.Unparen(cond).(type) {
+	case *ast.BinaryExpr:
+		if cond.Op == token.LAND {
+			if x := experimentConjunct(cond.X); x != nil {
+				return x
+			}
+			return experimentConjunct(cond.Y)
+		}
+	case *ast.SelectorExpr:
+		if cond.Sel.Name == "VetFiles" {
+			if x, ok := ast.Unparen(cond.X).(*ast.SelectorExpr); ok && x.Sel.Name == "Experiment" {
+				return cond
+			}
+		}
+	}
+	return nil
+}
+
+func within(n, outer ast.Node) bool {
+	return outer.Pos() <= n.Pos() && n.End() <= outer.End()
+}