Source file src/cmd/internal/testdir/testdir_test.go

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package testdir_test runs tests in the GOROOT/test directory.
     6  package testdir_test
     7  
     8  import (
     9  	"bytes"
    10  	"encoding/json"
    11  	"errors"
    12  	"flag"
    13  	"fmt"
    14  	"go/build"
    15  	"go/build/constraint"
    16  	"hash/fnv"
    17  	"internal/testenv"
    18  	"io"
    19  	"io/fs"
    20  	"log"
    21  	"os"
    22  	"os/exec"
    23  	"path"
    24  	"path/filepath"
    25  	"regexp"
    26  	"runtime"
    27  	"slices"
    28  	"sort"
    29  	"strconv"
    30  	"strings"
    31  	"sync"
    32  	"testing"
    33  	"time"
    34  	"unicode"
    35  )
    36  
    37  var (
    38  	allCodegen     = flag.Bool("all_codegen", defaultAllCodeGen(), "run all goos/goarch for codegen")
    39  	runSkips       = flag.Bool("run_skips", false, "run skipped tests (ignore skip and build tags)")
    40  	linkshared     = flag.Bool("linkshared", false, "")
    41  	updateErrors   = flag.Bool("update_errors", false, "update error messages in test file based on compiler output")
    42  	runoutputLimit = flag.Int("l", defaultRunOutputLimit(), "number of parallel runoutput tests to run")
    43  	force          = flag.Bool("f", false, "ignore expected-failure test lists")
    44  	target         = flag.String("target", "", "cross-compile tests for `goos/goarch`")
    45  
    46  	shard  = flag.Int("shard", 0, "shard index to run. Only applicable if -shards is non-zero.")
    47  	shards = flag.Int("shards", 0, "number of shards. If 0, all tests are run. This is used by the continuous build.")
    48  )
    49  
    50  // defaultAllCodeGen returns the default value of the -all_codegen
    51  // flag. By default, we prefer to be fast (returning false), except on
    52  // the linux-amd64 builder that's already very fast, so we get more
    53  // test coverage on trybots. See https://go.dev/issue/34297.
    54  func defaultAllCodeGen() bool {
    55  	return os.Getenv("GO_BUILDER_NAME") == "linux-amd64"
    56  }
    57  
    58  var (
    59  	// Package-scoped variables that are initialized at the start of Test.
    60  	goTool       string
    61  	goos         string // Target GOOS
    62  	goarch       string // Target GOARCH
    63  	cgoEnabled   bool
    64  	goExperiment string
    65  	goDebug      string
    66  	tmpDir       string
    67  
    68  	// dirs are the directories to look for *.go files in.
    69  	// TODO(bradfitz): just use all directories?
    70  	dirs = []string{".", "ken", "chan", "interface", "internal/runtime/sys", "syntax", "dwarf", "fixedbugs", "codegen", "abi", "typeparam", "typeparam/mdempsky", "arenas"}
    71  )
    72  
    73  // Test is the main entrypoint that runs tests in the GOROOT/test directory.
    74  //
    75  // Each .go file test case in GOROOT/test is registered as a subtest with
    76  // a full name like "Test/fixedbugs/bug000.go" ('/'-separated relative path).
    77  func Test(t *testing.T) {
    78  	if *target != "" {
    79  		// When -target is set, propagate it to GOOS/GOARCH in our environment
    80  		// so that all commands run with the target GOOS/GOARCH.
    81  		//
    82  		// We do this before even calling "go env", because GOOS/GOARCH can
    83  		// affect other settings we get from go env (notably CGO_ENABLED).
    84  		goos, goarch, ok := strings.Cut(*target, "/")
    85  		if !ok {
    86  			t.Fatalf("bad -target flag %q, expected goos/goarch", *target)
    87  		}
    88  		t.Setenv("GOOS", goos)
    89  		t.Setenv("GOARCH", goarch)
    90  	}
    91  
    92  	goTool = testenv.GoToolPath(t)
    93  	cmd := exec.Command(goTool, "env", "-json")
    94  	stdout, err := cmd.StdoutPipe()
    95  	if err != nil {
    96  		t.Fatal("StdoutPipe:", err)
    97  	}
    98  	if err := cmd.Start(); err != nil {
    99  		t.Fatal("Start:", err)
   100  	}
   101  	var env struct {
   102  		GOOS         string
   103  		GOARCH       string
   104  		GOEXPERIMENT string
   105  		GODEBUG      string
   106  		CGO_ENABLED  string
   107  	}
   108  	if err := json.NewDecoder(stdout).Decode(&env); err != nil {
   109  		t.Fatal("Decode:", err)
   110  	}
   111  	if err := cmd.Wait(); err != nil {
   112  		t.Fatal("Wait:", err)
   113  	}
   114  	goos = env.GOOS
   115  	goarch = env.GOARCH
   116  	cgoEnabled, _ = strconv.ParseBool(env.CGO_ENABLED)
   117  	goExperiment = env.GOEXPERIMENT
   118  	goDebug = env.GODEBUG
   119  	tmpDir = t.TempDir()
   120  
   121  	common := testCommon{
   122  		gorootTestDir: filepath.Join(testenv.GOROOT(t), "test"),
   123  		runoutputGate: make(chan bool, *runoutputLimit),
   124  	}
   125  
   126  	// cmd/distpack deletes GOROOT/test, so skip the test if it isn't present.
   127  	// cmd/distpack also requires GOROOT/VERSION to exist, so use that to
   128  	// suppress false-positive skips.
   129  	if _, err := os.Stat(common.gorootTestDir); os.IsNotExist(err) {
   130  		if _, err := os.Stat(filepath.Join(testenv.GOROOT(t), "VERSION")); err == nil {
   131  			t.Skipf("skipping: GOROOT/test not present")
   132  		}
   133  	}
   134  
   135  	for _, dir := range dirs {
   136  		for _, goFile := range goFiles(t, dir) {
   137  			test := test{testCommon: common, dir: dir, goFile: goFile}
   138  			t.Run(path.Join(dir, goFile), func(t *testing.T) {
   139  				t.Parallel()
   140  				test.T = t
   141  				testError := test.run()
   142  				wantError := test.expectFail() && !*force
   143  				if testError != nil {
   144  					if wantError {
   145  						t.Log(testError.Error() + " (expected)")
   146  					} else {
   147  						t.Fatal(testError)
   148  					}
   149  				} else if wantError {
   150  					t.Fatal("unexpected success")
   151  				}
   152  			})
   153  		}
   154  	}
   155  }
   156  
   157  func shardMatch(name string) bool {
   158  	if *shards <= 1 {
   159  		return true
   160  	}
   161  	h := fnv.New32()
   162  	io.WriteString(h, name)
   163  	return int(h.Sum32()%uint32(*shards)) == *shard
   164  }
   165  
   166  func goFiles(t *testing.T, dir string) []string {
   167  	files, err := os.ReadDir(filepath.Join(testenv.GOROOT(t), "test", dir))
   168  	if err != nil {
   169  		t.Fatal(err)
   170  	}
   171  	names := []string{}
   172  	for _, file := range files {
   173  		name := file.Name()
   174  		if !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") && shardMatch(name) {
   175  			names = append(names, name)
   176  		}
   177  	}
   178  	return names
   179  }
   180  
   181  type runCmd func(...string) ([]byte, error)
   182  
   183  func compileFile(runcmd runCmd, longname string, flags []string) (out []byte, err error) {
   184  	cmd := []string{goTool, "tool", "compile", "-e", "-p=p", "-importcfg=" + stdlibImportcfgFile()}
   185  	cmd = append(cmd, flags...)
   186  	if *linkshared {
   187  		cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
   188  	}
   189  	cmd = append(cmd, longname)
   190  	return runcmd(cmd...)
   191  }
   192  
   193  func compileInDir(runcmd runCmd, dir string, flags []string, importcfg string, pkgname string, names ...string) (out []byte, err error) {
   194  	if importcfg == "" {
   195  		importcfg = stdlibImportcfgFile()
   196  	}
   197  	cmd := []string{goTool, "tool", "compile", "-e", "-D", "test", "-importcfg=" + importcfg}
   198  	if pkgname == "main" {
   199  		cmd = append(cmd, "-p=main")
   200  	} else {
   201  		pkgname = path.Join("test", strings.TrimSuffix(names[0], ".go"))
   202  		cmd = append(cmd, "-o", pkgname+".a", "-p", pkgname)
   203  	}
   204  	cmd = append(cmd, flags...)
   205  	if *linkshared {
   206  		cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
   207  	}
   208  	for _, name := range names {
   209  		cmd = append(cmd, filepath.Join(dir, name))
   210  	}
   211  	return runcmd(cmd...)
   212  }
   213  
   214  var stdlibImportcfg = sync.OnceValue(func() string {
   215  	cmd := exec.Command(goTool, "list", "-export", "-f", "{{if .Export}}packagefile {{.ImportPath}}={{.Export}}{{end}}", "std")
   216  	cmd.Env = append(os.Environ(), "GOENV=off", "GOFLAGS=")
   217  	output, err := cmd.Output()
   218  	if err, ok := err.(*exec.ExitError); ok && len(err.Stderr) != 0 {
   219  		log.Fatalf("'go list' failed: %v: %s", err, err.Stderr)
   220  	}
   221  	if err != nil {
   222  		log.Fatalf("'go list' failed: %v", err)
   223  	}
   224  	return string(output)
   225  })
   226  
   227  var stdlibImportcfgFile = sync.OnceValue(func() string {
   228  	filename := filepath.Join(tmpDir, "importcfg")
   229  	err := os.WriteFile(filename, []byte(stdlibImportcfg()), 0644)
   230  	if err != nil {
   231  		log.Fatal(err)
   232  	}
   233  	return filename
   234  })
   235  
   236  // linkFile links infile with the given importcfg and ldflags, writes to outfile.
   237  // infile can be the name of an object file or a go source file.
   238  func linkFile(runcmd runCmd, outfile, infile string, importcfg string, ldflags []string) (err error) {
   239  	if importcfg == "" {
   240  		importcfg = stdlibImportcfgFile()
   241  	}
   242  	if strings.HasSuffix(infile, ".go") {
   243  		infile = infile[:len(infile)-3] + ".o"
   244  	}
   245  	cmd := []string{goTool, "tool", "link", "-s", "-w", "-buildid=test", "-o", outfile, "-importcfg=" + importcfg}
   246  	if *linkshared {
   247  		cmd = append(cmd, "-linkshared", "-installsuffix=dynlink")
   248  	}
   249  	if ldflags != nil {
   250  		cmd = append(cmd, ldflags...)
   251  	}
   252  	cmd = append(cmd, infile)
   253  	_, err = runcmd(cmd...)
   254  	return
   255  }
   256  
   257  type testCommon struct {
   258  	// gorootTestDir is the GOROOT/test directory path.
   259  	gorootTestDir string
   260  
   261  	// runoutputGate controls the max number of runoutput tests
   262  	// executed in parallel as they can each consume a lot of memory.
   263  	runoutputGate chan bool
   264  }
   265  
   266  // test is a single test case in the GOROOT/test directory.
   267  type test struct {
   268  	testCommon
   269  	*testing.T
   270  	// dir and goFile identify the test case.
   271  	// For example, "fixedbugs", "bug000.go".
   272  	dir, goFile string
   273  }
   274  
   275  // expectFail reports whether the (overall) test recipe is
   276  // expected to fail under the current build+test configuration.
   277  func (t test) expectFail() bool {
   278  	failureSets := []map[string]bool{types2Failures}
   279  
   280  	// Note: gccgo supports more 32-bit architectures than this, but
   281  	// hopefully the 32-bit failures are fixed before this matters.
   282  	switch goarch {
   283  	case "386", "arm", "mips", "mipsle":
   284  		failureSets = append(failureSets, types2Failures32Bit)
   285  	}
   286  
   287  	testName := path.Join(t.dir, t.goFile) // Test name is '/'-separated.
   288  
   289  	for _, set := range failureSets {
   290  		if set[testName] {
   291  			return true
   292  		}
   293  	}
   294  	return false
   295  }
   296  
   297  func (t test) goFileName() string {
   298  	return filepath.Join(t.dir, t.goFile)
   299  }
   300  
   301  func (t test) goDirName() string {
   302  	return filepath.Join(t.dir, strings.ReplaceAll(t.goFile, ".go", ".dir"))
   303  }
   304  
   305  // goDirFiles returns .go files in dir.
   306  func goDirFiles(dir string) (filter []fs.DirEntry, _ error) {
   307  	files, err := os.ReadDir(dir)
   308  	if err != nil {
   309  		return nil, err
   310  	}
   311  	for _, goFile := range files {
   312  		if filepath.Ext(goFile.Name()) == ".go" {
   313  			filter = append(filter, goFile)
   314  		}
   315  	}
   316  	return filter, nil
   317  }
   318  
   319  var packageRE = regexp.MustCompile(`(?m)^package ([\p{Lu}\p{Ll}\w]+)`)
   320  
   321  func getPackageNameFromSource(fn string) (string, error) {
   322  	data, err := os.ReadFile(fn)
   323  	if err != nil {
   324  		return "", err
   325  	}
   326  	pkgname := packageRE.FindStringSubmatch(string(data))
   327  	if pkgname == nil {
   328  		return "", fmt.Errorf("cannot find package name in %s", fn)
   329  	}
   330  	return pkgname[1], nil
   331  }
   332  
   333  // goDirPkg represents a Go package in some directory.
   334  type goDirPkg struct {
   335  	name  string
   336  	files []string
   337  }
   338  
   339  // goDirPackages returns distinct Go packages in dir.
   340  // If singlefilepkgs is set, each file is considered a separate package
   341  // even if the package names are the same.
   342  func goDirPackages(t *testing.T, dir string, singlefilepkgs bool) []*goDirPkg {
   343  	files, err := goDirFiles(dir)
   344  	if err != nil {
   345  		t.Fatal(err)
   346  	}
   347  	var pkgs []*goDirPkg
   348  	m := make(map[string]*goDirPkg)
   349  	for _, file := range files {
   350  		name := file.Name()
   351  		pkgname, err := getPackageNameFromSource(filepath.Join(dir, name))
   352  		if err != nil {
   353  			t.Fatal(err)
   354  		}
   355  		p, ok := m[pkgname]
   356  		if singlefilepkgs || !ok {
   357  			p = &goDirPkg{name: pkgname}
   358  			pkgs = append(pkgs, p)
   359  			m[pkgname] = p
   360  		}
   361  		p.files = append(p.files, name)
   362  	}
   363  	return pkgs
   364  }
   365  
   366  type context struct {
   367  	GOOS       string
   368  	GOARCH     string
   369  	cgoEnabled bool
   370  	noOptEnv   bool
   371  }
   372  
   373  // shouldTest looks for build tags in a source file and returns
   374  // whether the file should be used according to the tags.
   375  func shouldTest(src string, goos, goarch string) (ok bool, whyNot string) {
   376  	if *runSkips {
   377  		return true, ""
   378  	}
   379  	for _, line := range strings.Split(src, "\n") {
   380  		if strings.HasPrefix(line, "package ") {
   381  			break
   382  		}
   383  
   384  		if expr, err := constraint.Parse(line); err == nil {
   385  			gcFlags := os.Getenv("GO_GCFLAGS")
   386  			ctxt := &context{
   387  				GOOS:       goos,
   388  				GOARCH:     goarch,
   389  				cgoEnabled: cgoEnabled,
   390  				noOptEnv:   strings.Contains(gcFlags, "-N") || strings.Contains(gcFlags, "-l"),
   391  			}
   392  
   393  			if !expr.Eval(ctxt.match) {
   394  				return false, line
   395  			}
   396  		}
   397  	}
   398  	return true, ""
   399  }
   400  
   401  func (ctxt *context) match(name string) bool {
   402  	if name == "" {
   403  		return false
   404  	}
   405  
   406  	// Tags must be letters, digits, underscores or dots.
   407  	// Unlike in Go identifiers, all digits are fine (e.g., "386").
   408  	for _, c := range name {
   409  		if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
   410  			return false
   411  		}
   412  	}
   413  
   414  	if slices.Contains(build.Default.ReleaseTags, name) {
   415  		return true
   416  	}
   417  
   418  	if strings.HasPrefix(name, "goexperiment.") {
   419  		return slices.Contains(build.Default.ToolTags, name)
   420  	}
   421  
   422  	if name == "cgo" && ctxt.cgoEnabled {
   423  		return true
   424  	}
   425  
   426  	if name == ctxt.GOOS || name == ctxt.GOARCH || name == "gc" {
   427  		return true
   428  	}
   429  
   430  	if ctxt.noOptEnv && name == "gcflags_noopt" {
   431  		return true
   432  	}
   433  
   434  	if name == "test_run" {
   435  		return true
   436  	}
   437  
   438  	return false
   439  }
   440  
   441  // goGcflags returns the -gcflags argument to use with go build / go run.
   442  // This must match the flags used for building the standard library,
   443  // or else the commands will rebuild any needed packages (like runtime)
   444  // over and over.
   445  func (test) goGcflags() string {
   446  	return "-gcflags=all=" + os.Getenv("GO_GCFLAGS")
   447  }
   448  
   449  func (test) goGcflagsIsEmpty() bool {
   450  	return "" == os.Getenv("GO_GCFLAGS")
   451  }
   452  
   453  var errTimeout = errors.New("command exceeded time limit")
   454  
   455  // run runs the test case.
   456  //
   457  // When there is a problem, run uses t.Fatal to signify that it's an unskippable
   458  // infrastructure error (such as failing to read an input file or the test recipe
   459  // being malformed), or it returns a non-nil error to signify a test case error.
   460  //
   461  // t.Error isn't used here to give the caller the opportunity to decide whether
   462  // the test case failing is expected before promoting it to a real test failure.
   463  // See expectFail and -f flag.
   464  func (t test) run() error {
   465  	srcBytes, err := os.ReadFile(filepath.Join(t.gorootTestDir, t.goFileName()))
   466  	if err != nil {
   467  		t.Fatal("reading test case .go file:", err)
   468  	} else if bytes.HasPrefix(srcBytes, []byte{'\n'}) {
   469  		t.Fatal(".go file source starts with a newline")
   470  	}
   471  	src := string(srcBytes)
   472  
   473  	// Execution recipe is contained in a comment in
   474  	// the first non-empty line that is not a build constraint.
   475  	var action string
   476  	for actionSrc := src; action == "" && actionSrc != ""; {
   477  		var line string
   478  		line, actionSrc, _ = strings.Cut(actionSrc, "\n")
   479  		if constraint.IsGoBuild(line) || constraint.IsPlusBuild(line) {
   480  			continue
   481  		}
   482  		action = strings.TrimSpace(strings.TrimPrefix(line, "//"))
   483  	}
   484  	if action == "" {
   485  		t.Fatalf("execution recipe not found in GOROOT/test/%s", t.goFileName())
   486  	}
   487  
   488  	// Check for build constraints only up to the actual code.
   489  	header, _, ok := strings.Cut(src, "\npackage")
   490  	if !ok {
   491  		header = action // some files are intentionally malformed
   492  	}
   493  	if ok, why := shouldTest(header, goos, goarch); !ok {
   494  		t.Skip(why)
   495  	}
   496  
   497  	var args, flags, runenv []string
   498  	var tim int
   499  	wantError := false
   500  	wantAuto := false
   501  	singlefilepkgs := false
   502  	f, err := splitQuoted(action)
   503  	if err != nil {
   504  		t.Fatal("invalid test recipe:", err)
   505  	}
   506  	if len(f) > 0 {
   507  		action = f[0]
   508  		args = f[1:]
   509  	}
   510  
   511  	// TODO: Clean up/simplify this switch statement.
   512  	switch action {
   513  	case "compile", "compiledir", "build", "builddir", "buildrundir", "run", "buildrun", "runoutput", "rundir", "runindir", "asmcheck":
   514  		// nothing to do
   515  	case "errorcheckandrundir":
   516  		wantError = false // should be no error if also will run
   517  	case "errorcheckwithauto":
   518  		action = "errorcheck"
   519  		wantAuto = true
   520  		wantError = true
   521  	case "errorcheck", "errorcheckdir", "errorcheckoutput":
   522  		wantError = true
   523  	case "skip":
   524  		if *runSkips {
   525  			break
   526  		}
   527  		t.Skip("skip")
   528  	default:
   529  		t.Fatalf("unknown pattern: %q", action)
   530  	}
   531  
   532  	goexp := goExperiment
   533  	godebug := goDebug
   534  	gomodvers := ""
   535  
   536  	// collect flags
   537  	for len(args) > 0 && strings.HasPrefix(args[0], "-") {
   538  		switch args[0] {
   539  		case "-1":
   540  			wantError = true
   541  		case "-0":
   542  			wantError = false
   543  		case "-s":
   544  			singlefilepkgs = true
   545  		case "-t": // timeout in seconds
   546  			args = args[1:]
   547  			var err error
   548  			tim, err = strconv.Atoi(args[0])
   549  			if err != nil {
   550  				t.Fatalf("need number of seconds for -t timeout, got %s instead", args[0])
   551  			}
   552  			if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
   553  				timeoutScale, err := strconv.Atoi(s)
   554  				if err != nil {
   555  					t.Fatalf("failed to parse $GO_TEST_TIMEOUT_SCALE = %q as integer: %v", s, err)
   556  				}
   557  				tim *= timeoutScale
   558  			}
   559  		case "-goexperiment": // set GOEXPERIMENT environment
   560  			args = args[1:]
   561  			if goexp != "" {
   562  				goexp += ","
   563  			}
   564  			goexp += args[0]
   565  			runenv = append(runenv, "GOEXPERIMENT="+goexp)
   566  
   567  		case "-godebug": // set GODEBUG environment
   568  			args = args[1:]
   569  			if godebug != "" {
   570  				godebug += ","
   571  			}
   572  			godebug += args[0]
   573  			runenv = append(runenv, "GODEBUG="+godebug)
   574  
   575  		case "-gomodversion": // set the GoVersion in generated go.mod files (just runindir ATM)
   576  			args = args[1:]
   577  			gomodvers = args[0]
   578  
   579  		default:
   580  			flags = append(flags, args[0])
   581  		}
   582  		args = args[1:]
   583  	}
   584  	if action == "errorcheck" {
   585  		found := false
   586  		for i, f := range flags {
   587  			if strings.HasPrefix(f, "-d=") {
   588  				flags[i] = f + ",ssa/check/on"
   589  				found = true
   590  				break
   591  			}
   592  		}
   593  		if !found {
   594  			flags = append(flags, "-d=ssa/check/on")
   595  		}
   596  	}
   597  
   598  	tempDir := t.TempDir()
   599  	err = os.Mkdir(filepath.Join(tempDir, "test"), 0755)
   600  	if err != nil {
   601  		t.Fatal(err)
   602  	}
   603  
   604  	err = os.WriteFile(filepath.Join(tempDir, t.goFile), srcBytes, 0644)
   605  	if err != nil {
   606  		t.Fatal(err)
   607  	}
   608  
   609  	var (
   610  		runInDir        = tempDir
   611  		tempDirIsGOPATH = false
   612  	)
   613  	runcmd := func(args ...string) ([]byte, error) {
   614  		cmd := exec.Command(args[0], args[1:]...)
   615  		var buf bytes.Buffer
   616  		cmd.Stdout = &buf
   617  		cmd.Stderr = &buf
   618  		cmd.Env = append(os.Environ(), "GOENV=off", "GOFLAGS=")
   619  		if runInDir != "" {
   620  			cmd.Dir = runInDir
   621  			// Set PWD to match Dir to speed up os.Getwd in the child process.
   622  			cmd.Env = append(cmd.Env, "PWD="+cmd.Dir)
   623  		} else {
   624  			// Default to running in the GOROOT/test directory.
   625  			cmd.Dir = t.gorootTestDir
   626  			// Set PWD to match Dir to speed up os.Getwd in the child process.
   627  			cmd.Env = append(cmd.Env, "PWD="+cmd.Dir)
   628  		}
   629  		if tempDirIsGOPATH {
   630  			cmd.Env = append(cmd.Env, "GOPATH="+tempDir)
   631  		}
   632  		cmd.Env = append(cmd.Env, "STDLIB_IMPORTCFG="+stdlibImportcfgFile())
   633  		cmd.Env = append(cmd.Env, runenv...)
   634  
   635  		var err error
   636  
   637  		if tim != 0 {
   638  			err = cmd.Start()
   639  			// This command-timeout code adapted from cmd/go/test.go
   640  			// Note: the Go command uses a more sophisticated timeout
   641  			// strategy, first sending SIGQUIT (if appropriate for the
   642  			// OS in question) to try to trigger a stack trace, then
   643  			// finally much later SIGKILL. If timeouts prove to be a
   644  			// common problem here, it would be worth porting over
   645  			// that code as well. See https://do.dev/issue/50973
   646  			// for more discussion.
   647  			if err == nil {
   648  				tick := time.NewTimer(time.Duration(tim) * time.Second)
   649  				done := make(chan error)
   650  				go func() {
   651  					done <- cmd.Wait()
   652  				}()
   653  				select {
   654  				case err = <-done:
   655  					// ok
   656  				case <-tick.C:
   657  					cmd.Process.Signal(os.Interrupt)
   658  					time.Sleep(1 * time.Second)
   659  					cmd.Process.Kill()
   660  					<-done
   661  					err = errTimeout
   662  				}
   663  				tick.Stop()
   664  			}
   665  		} else {
   666  			err = cmd.Run()
   667  		}
   668  		if err != nil && err != errTimeout {
   669  			err = fmt.Errorf("%s\n%s", err, buf.Bytes())
   670  		}
   671  		return buf.Bytes(), err
   672  	}
   673  
   674  	importcfg := func(pkgs []*goDirPkg) string {
   675  		cfg := stdlibImportcfg()
   676  		for _, pkg := range pkgs {
   677  			pkgpath := path.Join("test", strings.TrimSuffix(pkg.files[0], ".go"))
   678  			cfg += "\npackagefile " + pkgpath + "=" + filepath.Join(tempDir, pkgpath+".a")
   679  		}
   680  		filename := filepath.Join(tempDir, "importcfg")
   681  		err := os.WriteFile(filename, []byte(cfg), 0644)
   682  		if err != nil {
   683  			t.Fatal(err)
   684  		}
   685  		return filename
   686  	}
   687  
   688  	long := filepath.Join(t.gorootTestDir, t.goFileName())
   689  	switch action {
   690  	default:
   691  		t.Fatalf("unimplemented action %q", action)
   692  		panic("unreachable")
   693  
   694  	case "asmcheck":
   695  		// Compile Go file and match the generated assembly
   696  		// against a set of regexps in comments.
   697  		ops := t.wantedAsmOpcodes(long)
   698  		self := runtime.GOOS + "/" + runtime.GOARCH
   699  		for _, env := range ops.Envs() {
   700  			// Only run checks relevant to the current GOOS/GOARCH,
   701  			// to avoid triggering a cross-compile of the runtime.
   702  			if string(env) != self && !strings.HasPrefix(string(env), self+"/") && !*allCodegen {
   703  				continue
   704  			}
   705  			// -S=2 forces outermost line numbers when disassembling inlined code.
   706  			cmdline := []string{"build", "-gcflags", "-S=2"}
   707  
   708  			// Append flags, but don't override -gcflags=-S=2; add to it instead.
   709  			for i := 0; i < len(flags); i++ {
   710  				flag := flags[i]
   711  				switch {
   712  				case strings.HasPrefix(flag, "-gcflags="):
   713  					cmdline[2] += " " + strings.TrimPrefix(flag, "-gcflags=")
   714  				case strings.HasPrefix(flag, "--gcflags="):
   715  					cmdline[2] += " " + strings.TrimPrefix(flag, "--gcflags=")
   716  				case flag == "-gcflags", flag == "--gcflags":
   717  					i++
   718  					if i < len(flags) {
   719  						cmdline[2] += " " + flags[i]
   720  					}
   721  				default:
   722  					cmdline = append(cmdline, flag)
   723  				}
   724  			}
   725  
   726  			cmdline = append(cmdline, long)
   727  			cmd := exec.Command(goTool, cmdline...)
   728  			cmd.Env = append(os.Environ(), env.Environ()...)
   729  			if len(flags) > 0 && flags[0] == "-race" {
   730  				cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
   731  			}
   732  
   733  			var buf bytes.Buffer
   734  			cmd.Stdout, cmd.Stderr = &buf, &buf
   735  			if err := cmd.Run(); err != nil {
   736  				t.Log(env, "\n", cmd.Stderr)
   737  				return err
   738  			}
   739  
   740  			err := t.asmCheck(buf.String(), long, env, ops[env])
   741  			if err != nil {
   742  				return err
   743  			}
   744  		}
   745  		return nil
   746  
   747  	case "errorcheck":
   748  		// Compile Go file.
   749  		// Fail if wantError is true and compilation was successful and vice versa.
   750  		// Match errors produced by gc against errors in comments.
   751  		// TODO(gri) remove need for -C (disable printing of columns in error messages)
   752  		cmdline := []string{goTool, "tool", "compile", "-p=p", "-d=panic", "-C", "-e", "-importcfg=" + stdlibImportcfgFile(), "-o", "a.o"}
   753  		// No need to add -dynlink even if linkshared if we're just checking for errors...
   754  		cmdline = append(cmdline, flags...)
   755  		cmdline = append(cmdline, long)
   756  		out, err := runcmd(cmdline...)
   757  		if wantError {
   758  			if err == nil {
   759  				return fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
   760  			}
   761  			if err == errTimeout {
   762  				return fmt.Errorf("compilation timed out")
   763  			}
   764  		} else {
   765  			if err != nil {
   766  				return err
   767  			}
   768  		}
   769  		if *updateErrors {
   770  			t.updateErrors(string(out), long)
   771  		}
   772  		return t.errorCheck(string(out), wantAuto, long, t.goFile)
   773  
   774  	case "compile":
   775  		// Compile Go file.
   776  		_, err := compileFile(runcmd, long, flags)
   777  		return err
   778  
   779  	case "compiledir":
   780  		// Compile all files in the directory as packages in lexicographic order.
   781  		longdir := filepath.Join(t.gorootTestDir, t.goDirName())
   782  		pkgs := goDirPackages(t.T, longdir, singlefilepkgs)
   783  		importcfgfile := importcfg(pkgs)
   784  
   785  		for _, pkg := range pkgs {
   786  			_, err := compileInDir(runcmd, longdir, flags, importcfgfile, pkg.name, pkg.files...)
   787  			if err != nil {
   788  				return err
   789  			}
   790  		}
   791  		return nil
   792  
   793  	case "errorcheckdir", "errorcheckandrundir":
   794  		flags = append(flags, "-d=panic")
   795  		// Compile and errorCheck all files in the directory as packages in lexicographic order.
   796  		// If errorcheckdir and wantError, compilation of the last package must fail.
   797  		// If errorcheckandrundir and wantError, compilation of the package prior the last must fail.
   798  		longdir := filepath.Join(t.gorootTestDir, t.goDirName())
   799  		pkgs := goDirPackages(t.T, longdir, singlefilepkgs)
   800  		errPkg := len(pkgs) - 1
   801  		if wantError && action == "errorcheckandrundir" {
   802  			// The last pkg should compiled successfully and will be run in next case.
   803  			// Preceding pkg must return an error from compileInDir.
   804  			errPkg--
   805  		}
   806  		importcfgfile := importcfg(pkgs)
   807  		for i, pkg := range pkgs {
   808  			out, err := compileInDir(runcmd, longdir, flags, importcfgfile, pkg.name, pkg.files...)
   809  			if i == errPkg {
   810  				if wantError && err == nil {
   811  					return fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
   812  				} else if !wantError && err != nil {
   813  					return err
   814  				}
   815  			} else if err != nil {
   816  				return err
   817  			}
   818  			var fullshort []string
   819  			for _, name := range pkg.files {
   820  				fullshort = append(fullshort, filepath.Join(longdir, name), name)
   821  			}
   822  			err = t.errorCheck(string(out), wantAuto, fullshort...)
   823  			if err != nil {
   824  				return err
   825  			}
   826  		}
   827  		if action == "errorcheckdir" {
   828  			return nil
   829  		}
   830  		fallthrough
   831  
   832  	case "rundir":
   833  		// Compile all files in the directory as packages in lexicographic order.
   834  		// In case of errorcheckandrundir, ignore failed compilation of the package before the last.
   835  		// Link as if the last file is the main package, run it.
   836  		// Verify the expected output.
   837  		longdir := filepath.Join(t.gorootTestDir, t.goDirName())
   838  		pkgs := goDirPackages(t.T, longdir, singlefilepkgs)
   839  		// Split flags into gcflags and ldflags
   840  		ldflags := []string{}
   841  		for i, fl := range flags {
   842  			if fl == "-ldflags" {
   843  				ldflags = flags[i+1:]
   844  				flags = flags[0:i]
   845  				break
   846  			}
   847  		}
   848  
   849  		importcfgfile := importcfg(pkgs)
   850  
   851  		for i, pkg := range pkgs {
   852  			_, err := compileInDir(runcmd, longdir, flags, importcfgfile, pkg.name, pkg.files...)
   853  			// Allow this package compilation fail based on conditions below;
   854  			// its errors were checked in previous case.
   855  			if err != nil && !(wantError && action == "errorcheckandrundir" && i == len(pkgs)-2) {
   856  				return err
   857  			}
   858  
   859  			if i == len(pkgs)-1 {
   860  				err = linkFile(runcmd, "a.exe", pkg.files[0], importcfgfile, ldflags)
   861  				if err != nil {
   862  					return err
   863  				}
   864  				var cmd []string
   865  				cmd = append(cmd, findExecCmd()...)
   866  				cmd = append(cmd, filepath.Join(tempDir, "a.exe"))
   867  				cmd = append(cmd, args...)
   868  				out, err := runcmd(cmd...)
   869  				if err != nil {
   870  					return err
   871  				}
   872  				t.checkExpectedOutput(out)
   873  			}
   874  		}
   875  		return nil
   876  
   877  	case "runindir":
   878  		// Make a shallow copy of t.goDirName() in its own module and GOPATH, and
   879  		// run "go run ." in it. The module path (and hence import path prefix) of
   880  		// the copy is equal to the basename of the source directory.
   881  		//
   882  		// It's used when test a requires a full 'go build' in order to compile
   883  		// the sources, such as when importing multiple packages (issue29612.dir)
   884  		// or compiling a package containing assembly files (see issue15609.dir),
   885  		// but still needs to be run to verify the expected output.
   886  		tempDirIsGOPATH = true
   887  		srcDir := filepath.Join(t.gorootTestDir, t.goDirName())
   888  		modName := filepath.Base(srcDir)
   889  		gopathSrcDir := filepath.Join(tempDir, "src", modName)
   890  		runInDir = gopathSrcDir
   891  
   892  		if err := overlayDir(gopathSrcDir, srcDir); err != nil {
   893  			t.Fatal(err)
   894  		}
   895  
   896  		modVersion := gomodvers
   897  		if modVersion == "" {
   898  			modVersion = "1.14"
   899  		}
   900  		modFile := fmt.Sprintf("module %s\ngo %s\n", modName, modVersion)
   901  		if err := os.WriteFile(filepath.Join(gopathSrcDir, "go.mod"), []byte(modFile), 0666); err != nil {
   902  			t.Fatal(err)
   903  		}
   904  
   905  		cmd := []string{goTool, "run", t.goGcflags()}
   906  		if *linkshared {
   907  			cmd = append(cmd, "-linkshared")
   908  		}
   909  		cmd = append(cmd, flags...)
   910  		cmd = append(cmd, ".")
   911  		out, err := runcmd(cmd...)
   912  		if err != nil {
   913  			return err
   914  		}
   915  		return t.checkExpectedOutput(out)
   916  
   917  	case "build":
   918  		// Build Go file.
   919  		cmd := []string{goTool, "build", t.goGcflags()}
   920  		cmd = append(cmd, flags...)
   921  		cmd = append(cmd, "-o", "a.exe", long)
   922  		_, err := runcmd(cmd...)
   923  		return err
   924  
   925  	case "builddir", "buildrundir":
   926  		// Build an executable from all the .go and .s files in a subdirectory.
   927  		// Run it and verify its output in the buildrundir case.
   928  		longdir := filepath.Join(t.gorootTestDir, t.goDirName())
   929  		files, err := os.ReadDir(longdir)
   930  		if err != nil {
   931  			t.Fatal(err)
   932  		}
   933  		var gos []string
   934  		var asms []string
   935  		for _, file := range files {
   936  			switch filepath.Ext(file.Name()) {
   937  			case ".go":
   938  				gos = append(gos, filepath.Join(longdir, file.Name()))
   939  			case ".s":
   940  				asms = append(asms, filepath.Join(longdir, file.Name()))
   941  			}
   942  		}
   943  		if len(asms) > 0 {
   944  			emptyHdrFile := filepath.Join(tempDir, "go_asm.h")
   945  			if err := os.WriteFile(emptyHdrFile, nil, 0666); err != nil {
   946  				t.Fatalf("write empty go_asm.h: %v", err)
   947  			}
   948  			cmd := []string{goTool, "tool", "asm", "-p=main", "-gensymabis", "-o", "symabis"}
   949  			cmd = append(cmd, asms...)
   950  			_, err = runcmd(cmd...)
   951  			if err != nil {
   952  				return err
   953  			}
   954  		}
   955  		var objs []string
   956  		cmd := []string{goTool, "tool", "compile", "-p=main", "-e", "-D", ".", "-importcfg=" + stdlibImportcfgFile(), "-o", "go.o"}
   957  		if len(asms) > 0 {
   958  			cmd = append(cmd, "-asmhdr", "go_asm.h", "-symabis", "symabis")
   959  		}
   960  		cmd = append(cmd, gos...)
   961  		_, err = runcmd(cmd...)
   962  		if err != nil {
   963  			return err
   964  		}
   965  		objs = append(objs, "go.o")
   966  		if len(asms) > 0 {
   967  			cmd = []string{goTool, "tool", "asm", "-p=main", "-e", "-I", ".", "-o", "asm.o"}
   968  			cmd = append(cmd, asms...)
   969  			_, err = runcmd(cmd...)
   970  			if err != nil {
   971  				return err
   972  			}
   973  			objs = append(objs, "asm.o")
   974  		}
   975  		cmd = []string{goTool, "tool", "pack", "c", "all.a"}
   976  		cmd = append(cmd, objs...)
   977  		_, err = runcmd(cmd...)
   978  		if err != nil {
   979  			return err
   980  		}
   981  		err = linkFile(runcmd, "a.exe", "all.a", stdlibImportcfgFile(), nil)
   982  		if err != nil {
   983  			return err
   984  		}
   985  
   986  		if action == "builddir" {
   987  			return nil
   988  		}
   989  		cmd = append(findExecCmd(), filepath.Join(tempDir, "a.exe"))
   990  		out, err := runcmd(cmd...)
   991  		if err != nil {
   992  			return err
   993  		}
   994  		return t.checkExpectedOutput(out)
   995  
   996  	case "buildrun":
   997  		// Build an executable from Go file, then run it, verify its output.
   998  		// Useful for timeout tests where failure mode is infinite loop.
   999  		// TODO: not supported on NaCl
  1000  		cmd := []string{goTool, "build", t.goGcflags(), "-o", "a.exe"}
  1001  		if *linkshared {
  1002  			cmd = append(cmd, "-linkshared")
  1003  		}
  1004  		longDirGoFile := filepath.Join(filepath.Join(t.gorootTestDir, t.dir), t.goFile)
  1005  		cmd = append(cmd, flags...)
  1006  		cmd = append(cmd, longDirGoFile)
  1007  		_, err := runcmd(cmd...)
  1008  		if err != nil {
  1009  			return err
  1010  		}
  1011  		cmd = []string{"./a.exe"}
  1012  		out, err := runcmd(append(cmd, args...)...)
  1013  		if err != nil {
  1014  			return err
  1015  		}
  1016  
  1017  		return t.checkExpectedOutput(out)
  1018  
  1019  	case "run":
  1020  		// Run Go file if no special go command flags are provided;
  1021  		// otherwise build an executable and run it.
  1022  		// Verify the output.
  1023  		runInDir = ""
  1024  		var out []byte
  1025  		var err error
  1026  		if len(flags)+len(args) == 0 && t.goGcflagsIsEmpty() && !*linkshared && goarch == runtime.GOARCH && goos == runtime.GOOS && goexp == goExperiment && godebug == goDebug {
  1027  			// If we're not using special go command flags,
  1028  			// skip all the go command machinery.
  1029  			// This avoids any time the go command would
  1030  			// spend checking whether, for example, the installed
  1031  			// package runtime is up to date.
  1032  			// Because we run lots of trivial test programs,
  1033  			// the time adds up.
  1034  			pkg := filepath.Join(tempDir, "pkg.a")
  1035  			if _, err := runcmd(goTool, "tool", "compile", "-p=main", "-importcfg="+stdlibImportcfgFile(), "-o", pkg, t.goFileName()); err != nil {
  1036  				return err
  1037  			}
  1038  			exe := filepath.Join(tempDir, "test.exe")
  1039  			if err := linkFile(runcmd, exe, pkg, stdlibImportcfgFile(), nil); err != nil {
  1040  				return err
  1041  			}
  1042  			out, err = runcmd(append([]string{exe}, args...)...)
  1043  		} else {
  1044  			cmd := []string{goTool, "run", t.goGcflags()}
  1045  			if *linkshared {
  1046  				cmd = append(cmd, "-linkshared")
  1047  			}
  1048  			cmd = append(cmd, flags...)
  1049  			cmd = append(cmd, t.goFileName())
  1050  			out, err = runcmd(append(cmd, args...)...)
  1051  		}
  1052  		if err != nil {
  1053  			return err
  1054  		}
  1055  		return t.checkExpectedOutput(out)
  1056  
  1057  	case "runoutput":
  1058  		// Run Go file and write its output into temporary Go file.
  1059  		// Run generated Go file and verify its output.
  1060  		t.runoutputGate <- true
  1061  		defer func() {
  1062  			<-t.runoutputGate
  1063  		}()
  1064  		runInDir = ""
  1065  		cmd := []string{goTool, "run", t.goGcflags()}
  1066  		if *linkshared {
  1067  			cmd = append(cmd, "-linkshared")
  1068  		}
  1069  		cmd = append(cmd, t.goFileName())
  1070  		out, err := runcmd(append(cmd, args...)...)
  1071  		if err != nil {
  1072  			return err
  1073  		}
  1074  		tfile := filepath.Join(tempDir, "tmp__.go")
  1075  		if err := os.WriteFile(tfile, out, 0666); err != nil {
  1076  			t.Fatalf("write tempfile: %v", err)
  1077  		}
  1078  		cmd = []string{goTool, "run", t.goGcflags()}
  1079  		if *linkshared {
  1080  			cmd = append(cmd, "-linkshared")
  1081  		}
  1082  		cmd = append(cmd, tfile)
  1083  		out, err = runcmd(cmd...)
  1084  		if err != nil {
  1085  			return err
  1086  		}
  1087  		return t.checkExpectedOutput(out)
  1088  
  1089  	case "errorcheckoutput":
  1090  		// Run Go file and write its output into temporary Go file.
  1091  		// Compile and errorCheck generated Go file.
  1092  		runInDir = ""
  1093  		cmd := []string{goTool, "run", t.goGcflags()}
  1094  		if *linkshared {
  1095  			cmd = append(cmd, "-linkshared")
  1096  		}
  1097  		cmd = append(cmd, t.goFileName())
  1098  		out, err := runcmd(append(cmd, args...)...)
  1099  		if err != nil {
  1100  			return err
  1101  		}
  1102  		tfile := filepath.Join(tempDir, "tmp__.go")
  1103  		err = os.WriteFile(tfile, out, 0666)
  1104  		if err != nil {
  1105  			t.Fatalf("write tempfile: %v", err)
  1106  		}
  1107  		cmdline := []string{goTool, "tool", "compile", "-importcfg=" + stdlibImportcfgFile(), "-p=p", "-d=panic", "-e", "-o", "a.o"}
  1108  		cmdline = append(cmdline, flags...)
  1109  		cmdline = append(cmdline, tfile)
  1110  		out, err = runcmd(cmdline...)
  1111  		if wantError {
  1112  			if err == nil {
  1113  				return fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
  1114  			}
  1115  		} else {
  1116  			if err != nil {
  1117  				return err
  1118  			}
  1119  		}
  1120  		return t.errorCheck(string(out), false, tfile, "tmp__.go")
  1121  	}
  1122  }
  1123  
  1124  var findExecCmd = sync.OnceValue(func() (execCmd []string) {
  1125  	if goos == runtime.GOOS && goarch == runtime.GOARCH {
  1126  		return nil
  1127  	}
  1128  	if path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch)); err == nil {
  1129  		execCmd = []string{path}
  1130  	}
  1131  	return execCmd
  1132  })
  1133  
  1134  // checkExpectedOutput compares the output from compiling and/or running with the contents
  1135  // of the corresponding reference output file, if any (replace ".go" with ".out").
  1136  // If they don't match, fail with an informative message.
  1137  func (t test) checkExpectedOutput(gotBytes []byte) error {
  1138  	got := string(gotBytes)
  1139  	filename := filepath.Join(t.dir, t.goFile)
  1140  	filename = filename[:len(filename)-len(".go")]
  1141  	filename += ".out"
  1142  	b, err := os.ReadFile(filepath.Join(t.gorootTestDir, filename))
  1143  	if errors.Is(err, fs.ErrNotExist) {
  1144  		// File is allowed to be missing, in which case output should be empty.
  1145  		b = nil
  1146  	} else if err != nil {
  1147  		return err
  1148  	}
  1149  	got = strings.ReplaceAll(got, "\r\n", "\n")
  1150  	if got != string(b) {
  1151  		if err == nil {
  1152  			return fmt.Errorf("output does not match expected in %s. Instead saw\n%s", filename, got)
  1153  		} else {
  1154  			return fmt.Errorf("output should be empty when (optional) expected-output file %s is not present. Instead saw\n%s", filename, got)
  1155  		}
  1156  	}
  1157  	return nil
  1158  }
  1159  
  1160  func splitOutput(out string, wantAuto bool) []string {
  1161  	// gc error messages continue onto additional lines with leading tabs.
  1162  	// Split the output at the beginning of each line that doesn't begin with a tab.
  1163  	// <autogenerated> lines are impossible to match so those are filtered out.
  1164  	var res []string
  1165  	for _, line := range strings.Split(out, "\n") {
  1166  		if strings.HasSuffix(line, "\r") { // remove '\r', output by compiler on windows
  1167  			line = line[:len(line)-1]
  1168  		}
  1169  		if strings.HasPrefix(line, "\t") {
  1170  			res[len(res)-1] += "\n" + line
  1171  		} else if strings.HasPrefix(line, "go tool") || strings.HasPrefix(line, "#") || !wantAuto && strings.HasPrefix(line, "<autogenerated>") {
  1172  			continue
  1173  		} else if strings.TrimSpace(line) != "" {
  1174  			res = append(res, line)
  1175  		}
  1176  	}
  1177  	return res
  1178  }
  1179  
  1180  // errorCheck matches errors in outStr against comments in source files.
  1181  // For each line of the source files which should generate an error,
  1182  // there should be a comment of the form // ERROR "regexp".
  1183  // If outStr has an error for a line which has no such comment,
  1184  // this function will report an error.
  1185  // Likewise if outStr does not have an error for a line which has a comment,
  1186  // or if the error message does not match the <regexp>.
  1187  // The <regexp> syntax is Perl but it's best to stick to egrep.
  1188  //
  1189  // Sources files are supplied as fullshort slice.
  1190  // It consists of pairs: full path to source file and its base name.
  1191  func (t test) errorCheck(outStr string, wantAuto bool, fullshort ...string) (err error) {
  1192  	defer func() {
  1193  		if testing.Verbose() && err != nil {
  1194  			t.Logf("gc output:\n%s", outStr)
  1195  		}
  1196  	}()
  1197  	var errs []error
  1198  	out := splitOutput(outStr, wantAuto)
  1199  
  1200  	// Cut directory name.
  1201  	for i := range out {
  1202  		for j := 0; j < len(fullshort); j += 2 {
  1203  			full, short := fullshort[j], fullshort[j+1]
  1204  			out[i] = replacePrefix(out[i], full, short)
  1205  		}
  1206  	}
  1207  
  1208  	var want []wantedError
  1209  	for j := 0; j < len(fullshort); j += 2 {
  1210  		full, short := fullshort[j], fullshort[j+1]
  1211  		want = append(want, t.wantedErrors(full, short)...)
  1212  	}
  1213  
  1214  	for _, we := range want {
  1215  		var errmsgs []string
  1216  		if we.auto {
  1217  			errmsgs, out = partitionStrings("<autogenerated>", out)
  1218  		} else {
  1219  			errmsgs, out = partitionStrings(we.prefix, out)
  1220  		}
  1221  		if len(errmsgs) == 0 {
  1222  			errs = append(errs, fmt.Errorf("%s:%d: missing error %q", we.file, we.lineNum, we.reStr))
  1223  			continue
  1224  		}
  1225  		matched := false
  1226  		n := len(out)
  1227  		for _, errmsg := range errmsgs {
  1228  			// Assume errmsg says "file:line: foo".
  1229  			// Cut leading "file:line: " to avoid accidental matching of file name instead of message.
  1230  			text := errmsg
  1231  			if _, suffix, ok := strings.Cut(text, " "); ok {
  1232  				text = suffix
  1233  			}
  1234  			if we.re.MatchString(text) {
  1235  				matched = true
  1236  			} else {
  1237  				out = append(out, errmsg)
  1238  			}
  1239  		}
  1240  		if !matched {
  1241  			errs = append(errs, fmt.Errorf("%s:%d: no match for %#q in:\n\t%s", we.file, we.lineNum, we.reStr, strings.Join(out[n:], "\n\t")))
  1242  			continue
  1243  		}
  1244  	}
  1245  
  1246  	if len(out) > 0 {
  1247  		// If a test uses -m and instantiates an imported generic function,
  1248  		// the errors will include messages for the instantiated function
  1249  		// with locations in the other package. Filter those out.
  1250  		localOut := make([]string, 0, len(out))
  1251  	outLoop:
  1252  		for _, errLine := range out {
  1253  			for j := 0; j < len(fullshort); j += 2 {
  1254  				full, short := fullshort[j], fullshort[j+1]
  1255  				if strings.HasPrefix(errLine, full+":") || strings.HasPrefix(errLine, short+":") {
  1256  					localOut = append(localOut, errLine)
  1257  					continue outLoop
  1258  				}
  1259  			}
  1260  		}
  1261  		out = localOut
  1262  	}
  1263  
  1264  	if len(out) > 0 {
  1265  		errs = append(errs, fmt.Errorf("Unmatched Errors:"))
  1266  		for _, errLine := range out {
  1267  			errs = append(errs, fmt.Errorf("%s", errLine))
  1268  		}
  1269  	}
  1270  
  1271  	if len(errs) == 0 {
  1272  		return nil
  1273  	}
  1274  	if len(errs) == 1 {
  1275  		return errs[0]
  1276  	}
  1277  	var buf bytes.Buffer
  1278  	fmt.Fprintf(&buf, "\n")
  1279  	for _, err := range errs {
  1280  		fmt.Fprintf(&buf, "%s\n", err.Error())
  1281  	}
  1282  	return errors.New(buf.String())
  1283  }
  1284  
  1285  func (test) updateErrors(out, file string) {
  1286  	base := path.Base(file)
  1287  	// Read in source file.
  1288  	src, err := os.ReadFile(file)
  1289  	if err != nil {
  1290  		fmt.Fprintln(os.Stderr, err)
  1291  		return
  1292  	}
  1293  	lines := strings.Split(string(src), "\n")
  1294  	// Remove old errors.
  1295  	for i := range lines {
  1296  		lines[i], _, _ = strings.Cut(lines[i], " // ERROR ")
  1297  	}
  1298  	// Parse new errors.
  1299  	errors := make(map[int]map[string]bool)
  1300  	tmpRe := regexp.MustCompile(`autotmp_\d+`)
  1301  	fileRe := regexp.MustCompile(`(\.go):\d+:`)
  1302  	for _, errStr := range splitOutput(out, false) {
  1303  		m := fileRe.FindStringSubmatchIndex(errStr)
  1304  		if len(m) != 4 {
  1305  			continue
  1306  		}
  1307  		// The end of the file is the end of the first and only submatch.
  1308  		errFile := errStr[:m[3]]
  1309  		rest := errStr[m[3]+1:]
  1310  		if errFile != file {
  1311  			continue
  1312  		}
  1313  		lineStr, msg, ok := strings.Cut(rest, ":")
  1314  		if !ok {
  1315  			continue
  1316  		}
  1317  		line, err := strconv.Atoi(lineStr)
  1318  		line--
  1319  		if err != nil || line < 0 || line >= len(lines) {
  1320  			continue
  1321  		}
  1322  		msg = strings.ReplaceAll(msg, file, base) // normalize file mentions in error itself
  1323  		msg = strings.TrimLeft(msg, " \t")
  1324  		for _, r := range []string{`\`, `*`, `+`, `?`, `[`, `]`, `(`, `)`} {
  1325  			msg = strings.ReplaceAll(msg, r, `\`+r)
  1326  		}
  1327  		msg = strings.ReplaceAll(msg, `"`, `.`)
  1328  		msg = tmpRe.ReplaceAllLiteralString(msg, `autotmp_[0-9]+`)
  1329  		if errors[line] == nil {
  1330  			errors[line] = make(map[string]bool)
  1331  		}
  1332  		errors[line][msg] = true
  1333  	}
  1334  	// Add new errors.
  1335  	for line, errs := range errors {
  1336  		var sorted []string
  1337  		for e := range errs {
  1338  			sorted = append(sorted, e)
  1339  		}
  1340  		sort.Strings(sorted)
  1341  		lines[line] += " // ERROR"
  1342  		for _, e := range sorted {
  1343  			lines[line] += fmt.Sprintf(` "%s$"`, e)
  1344  		}
  1345  	}
  1346  	// Write new file.
  1347  	err = os.WriteFile(file, []byte(strings.Join(lines, "\n")), 0640)
  1348  	if err != nil {
  1349  		fmt.Fprintln(os.Stderr, err)
  1350  		return
  1351  	}
  1352  	// Polish.
  1353  	exec.Command(goTool, "fmt", file).CombinedOutput()
  1354  }
  1355  
  1356  // matchPrefix reports whether s is of the form ^(.*/)?prefix(:|[),
  1357  // That is, it needs the file name prefix followed by a : or a [,
  1358  // and possibly preceded by a directory name.
  1359  func matchPrefix(s, prefix string) bool {
  1360  	i := strings.Index(s, ":")
  1361  	if i < 0 {
  1362  		return false
  1363  	}
  1364  	j := strings.LastIndex(s[:i], "/")
  1365  	s = s[j+1:]
  1366  	if len(s) <= len(prefix) || s[:len(prefix)] != prefix {
  1367  		return false
  1368  	}
  1369  	switch s[len(prefix)] {
  1370  	case '[', ':':
  1371  		return true
  1372  	}
  1373  	return false
  1374  }
  1375  
  1376  func partitionStrings(prefix string, strs []string) (matched, unmatched []string) {
  1377  	for _, s := range strs {
  1378  		if matchPrefix(s, prefix) {
  1379  			matched = append(matched, s)
  1380  		} else {
  1381  			unmatched = append(unmatched, s)
  1382  		}
  1383  	}
  1384  	return
  1385  }
  1386  
  1387  type wantedError struct {
  1388  	reStr   string
  1389  	re      *regexp.Regexp
  1390  	lineNum int
  1391  	auto    bool // match <autogenerated> line
  1392  	file    string
  1393  	prefix  string
  1394  }
  1395  
  1396  var (
  1397  	errRx       = regexp.MustCompile(`// (?:GC_)?ERROR (.*)`)
  1398  	errAutoRx   = regexp.MustCompile(`// (?:GC_)?ERRORAUTO (.*)`)
  1399  	errQuotesRx = regexp.MustCompile(`"([^"]*)"`)
  1400  	lineRx      = regexp.MustCompile(`LINE(([+-])(\d+))?`)
  1401  )
  1402  
  1403  func (t test) wantedErrors(file, short string) (errs []wantedError) {
  1404  	cache := make(map[string]*regexp.Regexp)
  1405  
  1406  	src, _ := os.ReadFile(file)
  1407  	for i, line := range strings.Split(string(src), "\n") {
  1408  		lineNum := i + 1
  1409  		if strings.Contains(line, "////") {
  1410  			// double comment disables ERROR
  1411  			continue
  1412  		}
  1413  		var auto bool
  1414  		m := errAutoRx.FindStringSubmatch(line)
  1415  		if m != nil {
  1416  			auto = true
  1417  		} else {
  1418  			m = errRx.FindStringSubmatch(line)
  1419  		}
  1420  		if m == nil {
  1421  			continue
  1422  		}
  1423  		all := m[1]
  1424  		mm := errQuotesRx.FindAllStringSubmatch(all, -1)
  1425  		if mm == nil {
  1426  			t.Fatalf("%s:%d: invalid errchk line: %s", t.goFileName(), lineNum, line)
  1427  		}
  1428  		for _, m := range mm {
  1429  			rx := lineRx.ReplaceAllStringFunc(m[1], func(m string) string {
  1430  				n := lineNum
  1431  				if strings.HasPrefix(m, "LINE+") {
  1432  					delta, _ := strconv.Atoi(m[5:])
  1433  					n += delta
  1434  				} else if strings.HasPrefix(m, "LINE-") {
  1435  					delta, _ := strconv.Atoi(m[5:])
  1436  					n -= delta
  1437  				}
  1438  				return fmt.Sprintf("%s:%d", short, n)
  1439  			})
  1440  			re := cache[rx]
  1441  			if re == nil {
  1442  				var err error
  1443  				re, err = regexp.Compile(rx)
  1444  				if err != nil {
  1445  					t.Fatalf("%s:%d: invalid regexp \"%s\" in ERROR line: %v", t.goFileName(), lineNum, rx, err)
  1446  				}
  1447  				cache[rx] = re
  1448  			}
  1449  			prefix := fmt.Sprintf("%s:%d", short, lineNum)
  1450  			errs = append(errs, wantedError{
  1451  				reStr:   rx,
  1452  				re:      re,
  1453  				prefix:  prefix,
  1454  				auto:    auto,
  1455  				lineNum: lineNum,
  1456  				file:    short,
  1457  			})
  1458  		}
  1459  	}
  1460  
  1461  	return
  1462  }
  1463  
  1464  const (
  1465  	// Regexp to match a single opcode check: optionally begin with "-" (to indicate
  1466  	// a negative check) or a positive number (to specify the expected number of
  1467  	// matches), followed by a string literal enclosed in "" or ``. For "",
  1468  	// backslashes must be handled.
  1469  	reMatchCheck = `(-|[1-9]\d*)?(?:\x60[^\x60]*\x60|"(?:[^"\\]|\\.)*")`
  1470  )
  1471  
  1472  var (
  1473  	// Regexp to split a line in code and comment, trimming spaces
  1474  	rxAsmComment = regexp.MustCompile(`^\s*(.*?)\s*(?://\s*(.+)\s*)?$`)
  1475  
  1476  	// Regexp to extract an architecture check: architecture name (or triplet),
  1477  	// followed by semi-colon, followed by a comma-separated list of opcode checks.
  1478  	// Extraneous spaces are ignored.
  1479  	//
  1480  	// An example: arm64/v8.1 : -`ADD` , `SUB`
  1481  	//	"(\w+)" matches "arm64" (architecture name)
  1482  	//	"(/[\w.]+)?" matches "v8.1" (architecture version)
  1483  	//	"(/\w*)?" doesn't match anything here (it's an optional part of the triplet)
  1484  	//	"\s*:\s*" matches " : " (semi-colon)
  1485  	//	"(" starts a capturing group
  1486  	//      first reMatchCheck matches "-`ADD`"
  1487  	//	`(?:" starts a non-capturing group
  1488  	//	"\s*,\s*` matches " , "
  1489  	//	second reMatchCheck matches "`SUB`"
  1490  	//	")*)" closes started groups; "*" means that there might be other elements in the comma-separated list
  1491  	rxAsmPlatform = regexp.MustCompile(`(\w+)(/[\w.]+)?(/\w*)?\s*:\s*(` + reMatchCheck + `(?:\s*,\s*` + reMatchCheck + `)*)`)
  1492  
  1493  	// Regexp to extract a single opcoded check
  1494  	rxAsmCheck = regexp.MustCompile(reMatchCheck)
  1495  
  1496  	// List of all architecture variants. Key is the GOARCH architecture,
  1497  	// value[0] is the variant-changing environment variable, and values[1:]
  1498  	// are the supported variants.
  1499  	archVariants = map[string][]string{
  1500  		"386":     {"GO386", "sse2", "softfloat"},
  1501  		"amd64":   {"GOAMD64", "v1", "v2", "v3", "v4"},
  1502  		"arm":     {"GOARM", "5", "6", "7", "7,softfloat"},
  1503  		"arm64":   {"GOARM64", "v8.0", "v8.1"},
  1504  		"loong64": {},
  1505  		"mips":    {"GOMIPS", "hardfloat", "softfloat"},
  1506  		"mips64":  {"GOMIPS64", "hardfloat", "softfloat"},
  1507  		"ppc64":   {"GOPPC64", "power8", "power9", "power10"},
  1508  		"ppc64le": {"GOPPC64", "power8", "power9", "power10"},
  1509  		"ppc64x":  {}, // A pseudo-arch representing both ppc64 and ppc64le
  1510  		"s390x":   {},
  1511  		"wasm":    {},
  1512  		"riscv64": {"GORISCV64", "rva20u64", "rva22u64", "rva23u64"},
  1513  	}
  1514  )
  1515  
  1516  // wantedAsmOpcode is a single asmcheck check
  1517  type wantedAsmOpcode struct {
  1518  	fileline string         // original source file/line (eg: "/path/foo.go:45")
  1519  	line     int            // original source line
  1520  	opcode   *regexp.Regexp // opcode check to be performed on assembly output
  1521  	expected int            // expected number of matches
  1522  	actual   int            // actual number that matched
  1523  	negative bool           // true if the check is supposed to fail rather than pass
  1524  	found    bool           // true if the opcode check matched at least one in the output
  1525  }
  1526  
  1527  // A build environment triplet separated by slashes (eg: linux/386/sse2).
  1528  // The third field can be empty if the arch does not support variants (eg: "plan9/amd64/")
  1529  type buildEnv string
  1530  
  1531  // Environ returns the environment it represents in cmd.Environ() "key=val" format
  1532  // For instance, "linux/386/sse2".Environ() returns {"GOOS=linux", "GOARCH=386", "GO386=sse2"}
  1533  func (b buildEnv) Environ() []string {
  1534  	fields := strings.Split(string(b), "/")
  1535  	if len(fields) != 3 {
  1536  		panic("invalid buildEnv string: " + string(b))
  1537  	}
  1538  	env := []string{"GOOS=" + fields[0], "GOARCH=" + fields[1]}
  1539  	if fields[2] != "" {
  1540  		env = append(env, archVariants[fields[1]][0]+"="+fields[2])
  1541  	}
  1542  	return env
  1543  }
  1544  
  1545  // asmChecks represents all the asmcheck checks present in a test file
  1546  // The outer map key is the build triplet in which the checks must be performed.
  1547  // The inner map key represent the source file line ("filename.go:1234") at which the
  1548  // checks must be performed.
  1549  type asmChecks map[buildEnv]map[string][]wantedAsmOpcode
  1550  
  1551  // Envs returns all the buildEnv in which at least one check is present
  1552  func (a asmChecks) Envs() []buildEnv {
  1553  	var envs []buildEnv
  1554  	for e := range a {
  1555  		envs = append(envs, e)
  1556  	}
  1557  	sort.Slice(envs, func(i, j int) bool {
  1558  		return string(envs[i]) < string(envs[j])
  1559  	})
  1560  	return envs
  1561  }
  1562  
  1563  func (t test) wantedAsmOpcodes(fn string) asmChecks {
  1564  	ops := make(asmChecks)
  1565  
  1566  	comment := ""
  1567  	src, err := os.ReadFile(fn)
  1568  	if err != nil {
  1569  		t.Fatal(err)
  1570  	}
  1571  	for i, line := range strings.Split(string(src), "\n") {
  1572  		matches := rxAsmComment.FindStringSubmatch(line)
  1573  		code, cmt := matches[1], matches[2]
  1574  
  1575  		// Keep comments pending in the comment variable until
  1576  		// we find a line that contains some code.
  1577  		comment += " " + cmt
  1578  		if code == "" {
  1579  			continue
  1580  		}
  1581  
  1582  		// Parse and extract any architecture check from comments,
  1583  		// made by one architecture name and multiple checks.
  1584  		lnum := fn + ":" + strconv.Itoa(i+1)
  1585  		for _, ac := range rxAsmPlatform.FindAllStringSubmatch(comment, -1) {
  1586  			archspec, allchecks := ac[1:4], ac[4]
  1587  
  1588  			var arch, subarch, os string
  1589  			switch {
  1590  			case archspec[2] != "": // 3 components: "linux/386/sse2"
  1591  				os, arch, subarch = archspec[0], archspec[1][1:], archspec[2][1:]
  1592  			case archspec[1] != "": // 2 components: "386/sse2"
  1593  				os, arch, subarch = "linux", archspec[0], archspec[1][1:]
  1594  			default: // 1 component: "386"
  1595  				os, arch, subarch = "linux", archspec[0], ""
  1596  				if arch == "wasm" {
  1597  					os = "js"
  1598  				}
  1599  			}
  1600  
  1601  			if _, ok := archVariants[arch]; !ok {
  1602  				t.Fatalf("%s:%d: unsupported architecture: %v", t.goFileName(), i+1, arch)
  1603  			}
  1604  
  1605  			// Create the build environments corresponding the above specifiers
  1606  			envs := make([]buildEnv, 0, 4)
  1607  			arches := []string{arch}
  1608  			// ppc64x is a pseudo-arch, generate tests for both endian variants.
  1609  			if arch == "ppc64x" {
  1610  				arches = []string{"ppc64", "ppc64le"}
  1611  			}
  1612  			for _, arch := range arches {
  1613  				if subarch != "" {
  1614  					envs = append(envs, buildEnv(os+"/"+arch+"/"+subarch))
  1615  				} else {
  1616  					subarchs := archVariants[arch]
  1617  					if len(subarchs) == 0 {
  1618  						envs = append(envs, buildEnv(os+"/"+arch+"/"))
  1619  					} else {
  1620  						for _, sa := range archVariants[arch][1:] {
  1621  							envs = append(envs, buildEnv(os+"/"+arch+"/"+sa))
  1622  						}
  1623  					}
  1624  				}
  1625  			}
  1626  
  1627  			for _, m := range rxAsmCheck.FindAllString(allchecks, -1) {
  1628  				negative := false
  1629  				expected := 0
  1630  				if m[0] == '-' {
  1631  					negative = true
  1632  					m = m[1:]
  1633  				} else if '1' <= m[0] && m[0] <= '9' {
  1634  					for '0' <= m[0] && m[0] <= '9' {
  1635  						expected *= 10
  1636  						expected += int(m[0] - '0')
  1637  						m = m[1:]
  1638  					}
  1639  				}
  1640  
  1641  				rxsrc, err := strconv.Unquote(m)
  1642  				if err != nil {
  1643  					t.Fatalf("%s:%d: error unquoting string: %v", t.goFileName(), i+1, err)
  1644  				}
  1645  
  1646  				// Compile the checks as regular expressions. Notice that we
  1647  				// consider checks as matching from the beginning of the actual
  1648  				// assembler source (that is, what is left on each line of the
  1649  				// compile -S output after we strip file/line info) to avoid
  1650  				// trivial bugs such as "ADD" matching "FADD". This
  1651  				// doesn't remove genericity: it's still possible to write
  1652  				// something like "F?ADD", but we make common cases simpler
  1653  				// to get right.
  1654  				oprx, err := regexp.Compile("^" + rxsrc)
  1655  				if err != nil {
  1656  					t.Fatalf("%s:%d: %v", t.goFileName(), i+1, err)
  1657  				}
  1658  
  1659  				for _, env := range envs {
  1660  					if ops[env] == nil {
  1661  						ops[env] = make(map[string][]wantedAsmOpcode)
  1662  					}
  1663  					ops[env][lnum] = append(ops[env][lnum], wantedAsmOpcode{
  1664  						expected: expected,
  1665  						negative: negative,
  1666  						fileline: lnum,
  1667  						line:     i + 1,
  1668  						opcode:   oprx,
  1669  					})
  1670  				}
  1671  			}
  1672  		}
  1673  		comment = ""
  1674  	}
  1675  
  1676  	return ops
  1677  }
  1678  
  1679  func (t test) asmCheck(outStr string, fn string, env buildEnv, fullops map[string][]wantedAsmOpcode) error {
  1680  	// The assembly output contains the concatenated dump of multiple functions.
  1681  	// the first line of each function begins at column 0, while the rest is
  1682  	// indented by a tabulation. These data structures help us index the
  1683  	// output by function.
  1684  	functionMarkers := make([]int, 1)
  1685  	lineFuncMap := make(map[string]int)
  1686  
  1687  	lines := strings.Split(outStr, "\n")
  1688  	rxLine := regexp.MustCompile(fmt.Sprintf(`\((%s:\d+)\)\s+(.*)`, regexp.QuoteMeta(fn)))
  1689  
  1690  	for nl, line := range lines {
  1691  		// Check if this line begins a function
  1692  		if len(line) > 0 && line[0] != '\t' {
  1693  			functionMarkers = append(functionMarkers, nl)
  1694  		}
  1695  
  1696  		// Search if this line contains a assembly opcode (which is prefixed by the
  1697  		// original source file/line in parenthesis)
  1698  		matches := rxLine.FindStringSubmatch(line)
  1699  		if len(matches) == 0 {
  1700  			continue
  1701  		}
  1702  		srcFileLine, asm := matches[1], matches[2]
  1703  
  1704  		// Associate the original file/line information to the current
  1705  		// function in the output; it will be useful to dump it in case
  1706  		// of error.
  1707  		lineFuncMap[srcFileLine] = len(functionMarkers) - 1
  1708  
  1709  		// If there are opcode checks associated to this source file/line,
  1710  		// run the checks.
  1711  		if ops, found := fullops[srcFileLine]; found {
  1712  			for i := range ops {
  1713  				if (!ops[i].found || ops[i].expected > 0) && ops[i].opcode.FindString(asm) != "" {
  1714  					ops[i].actual++
  1715  					ops[i].found = true
  1716  				}
  1717  			}
  1718  		}
  1719  	}
  1720  	functionMarkers = append(functionMarkers, len(lines))
  1721  
  1722  	var failed []wantedAsmOpcode
  1723  	for _, ops := range fullops {
  1724  		for _, o := range ops {
  1725  			// There's a failure if a negative match was found,
  1726  			// or a positive match was not found.
  1727  			if o.negative == o.found {
  1728  				failed = append(failed, o)
  1729  			}
  1730  			if o.expected > 0 && o.expected != o.actual {
  1731  				failed = append(failed, o)
  1732  			}
  1733  		}
  1734  	}
  1735  	if len(failed) == 0 {
  1736  		return nil
  1737  	}
  1738  
  1739  	// At least one asmcheck failed; report them.
  1740  	lastFunction := -1
  1741  	var errbuf bytes.Buffer
  1742  	fmt.Fprintln(&errbuf)
  1743  	sort.Slice(failed, func(i, j int) bool { return failed[i].line < failed[j].line })
  1744  	for _, o := range failed {
  1745  		// Dump the function in which this opcode check was supposed to
  1746  		// pass but failed.
  1747  		funcIdx := lineFuncMap[o.fileline]
  1748  		if funcIdx != 0 && funcIdx != lastFunction {
  1749  			funcLines := lines[functionMarkers[funcIdx]:functionMarkers[funcIdx+1]]
  1750  			t.Log(strings.Join(funcLines, "\n"))
  1751  			lastFunction = funcIdx // avoid printing same function twice
  1752  		}
  1753  
  1754  		if o.negative {
  1755  			fmt.Fprintf(&errbuf, "%s:%d: %s: wrong opcode found: %q\n", t.goFileName(), o.line, env, o.opcode.String())
  1756  		} else if o.expected > 0 {
  1757  			fmt.Fprintf(&errbuf, "%s:%d: %s: wrong number of opcodes: %q\n", t.goFileName(), o.line, env, o.opcode.String())
  1758  		} else {
  1759  			fmt.Fprintf(&errbuf, "%s:%d: %s: opcode not found: %q\n", t.goFileName(), o.line, env, o.opcode.String())
  1760  		}
  1761  	}
  1762  	return errors.New(errbuf.String())
  1763  }
  1764  
  1765  // defaultRunOutputLimit returns the number of runoutput tests that
  1766  // can be executed in parallel.
  1767  func defaultRunOutputLimit() int {
  1768  	const maxArmCPU = 2
  1769  
  1770  	cpu := runtime.NumCPU()
  1771  	if runtime.GOARCH == "arm" && cpu > maxArmCPU {
  1772  		cpu = maxArmCPU
  1773  	}
  1774  	return cpu
  1775  }
  1776  
  1777  func TestShouldTest(t *testing.T) {
  1778  	if *shard != 0 {
  1779  		t.Skipf("nothing to test on shard index %d", *shard)
  1780  	}
  1781  
  1782  	assert := func(ok bool, _ string) {
  1783  		t.Helper()
  1784  		if !ok {
  1785  			t.Error("test case failed")
  1786  		}
  1787  	}
  1788  	assertNot := func(ok bool, _ string) { t.Helper(); assert(!ok, "") }
  1789  
  1790  	// Simple tests.
  1791  	assert(shouldTest("// +build linux", "linux", "arm"))
  1792  	assert(shouldTest("// +build !windows", "linux", "arm"))
  1793  	assertNot(shouldTest("// +build !windows", "windows", "amd64"))
  1794  
  1795  	// A file with no build tags will always be tested.
  1796  	assert(shouldTest("// This is a test.", "os", "arch"))
  1797  
  1798  	// Build tags separated by a space are OR-ed together.
  1799  	assertNot(shouldTest("// +build arm 386", "linux", "amd64"))
  1800  
  1801  	// Build tags separated by a comma are AND-ed together.
  1802  	assertNot(shouldTest("// +build !windows,!plan9", "windows", "amd64"))
  1803  	assertNot(shouldTest("// +build !windows,!plan9", "plan9", "386"))
  1804  
  1805  	// Build tags on multiple lines are AND-ed together.
  1806  	assert(shouldTest("// +build !windows\n// +build amd64", "linux", "amd64"))
  1807  	assertNot(shouldTest("// +build !windows\n// +build amd64", "windows", "amd64"))
  1808  
  1809  	// Test that (!a OR !b) matches anything.
  1810  	assert(shouldTest("// +build !windows !plan9", "windows", "amd64"))
  1811  
  1812  	// Test that //go:build tag match.
  1813  	assert(shouldTest("//go:build go1.4", "linux", "amd64"))
  1814  }
  1815  
  1816  // overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
  1817  func overlayDir(dstRoot, srcRoot string) error {
  1818  	dstRoot = filepath.Clean(dstRoot)
  1819  	if err := os.MkdirAll(dstRoot, 0777); err != nil {
  1820  		return err
  1821  	}
  1822  
  1823  	srcRoot, err := filepath.Abs(srcRoot)
  1824  	if err != nil {
  1825  		return err
  1826  	}
  1827  
  1828  	return filepath.WalkDir(srcRoot, func(srcPath string, d fs.DirEntry, err error) error {
  1829  		if err != nil || srcPath == srcRoot {
  1830  			return err
  1831  		}
  1832  
  1833  		suffix := strings.TrimPrefix(srcPath, srcRoot)
  1834  		for len(suffix) > 0 && suffix[0] == filepath.Separator {
  1835  			suffix = suffix[1:]
  1836  		}
  1837  		dstPath := filepath.Join(dstRoot, suffix)
  1838  
  1839  		var info fs.FileInfo
  1840  		if d.Type()&os.ModeSymlink != 0 {
  1841  			info, err = os.Stat(srcPath)
  1842  		} else {
  1843  			info, err = d.Info()
  1844  		}
  1845  		if err != nil {
  1846  			return err
  1847  		}
  1848  		perm := info.Mode() & os.ModePerm
  1849  
  1850  		// Always copy directories (don't symlink them).
  1851  		// If we add a file in the overlay, we don't want to add it in the original.
  1852  		if info.IsDir() {
  1853  			return os.MkdirAll(dstPath, perm|0200)
  1854  		}
  1855  
  1856  		// If the OS supports symlinks, use them instead of copying bytes.
  1857  		if err := os.Symlink(srcPath, dstPath); err == nil {
  1858  			return nil
  1859  		}
  1860  
  1861  		// Otherwise, copy the bytes.
  1862  		src, err := os.Open(srcPath)
  1863  		if err != nil {
  1864  			return err
  1865  		}
  1866  		defer src.Close()
  1867  
  1868  		dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
  1869  		if err != nil {
  1870  			return err
  1871  		}
  1872  
  1873  		_, err = io.Copy(dst, src)
  1874  		if closeErr := dst.Close(); err == nil {
  1875  			err = closeErr
  1876  		}
  1877  		return err
  1878  	})
  1879  }
  1880  
  1881  // The following sets of files are excluded from testing depending on configuration.
  1882  // The types2Failures(32Bit) files pass with the 1.17 compiler but don't pass with
  1883  // the 1.18 compiler using the new types2 type checker, or pass with sub-optimal
  1884  // error(s).
  1885  
  1886  // List of files that the compiler cannot errorcheck with the new typechecker (types2).
  1887  var types2Failures = setOf(
  1888  	"shift1.go",               // types2 reports two new errors which are probably not right
  1889  	"fixedbugs/issue10700.go", // types2 should give hint about ptr to interface
  1890  	"fixedbugs/issue18331.go", // missing error about misuse of //go:noescape (irgen needs code from noder)
  1891  	"fixedbugs/issue18419.go", // types2 reports no field or method member, but should say unexported
  1892  	"fixedbugs/issue20233.go", // types2 reports two instead of one error (preference: 1.17 compiler)
  1893  	"fixedbugs/issue20245.go", // types2 reports two instead of one error (preference: 1.17 compiler)
  1894  	"fixedbugs/issue31053.go", // types2 reports "unknown field" instead of "cannot refer to unexported field"
  1895  )
  1896  
  1897  var types2Failures32Bit = setOf(
  1898  	"printbig.go",             // large untyped int passed to print (32-bit)
  1899  	"fixedbugs/bug114.go",     // large untyped int passed to println (32-bit)
  1900  	"fixedbugs/issue23305.go", // large untyped int passed to println (32-bit)
  1901  )
  1902  
  1903  // In all of these cases, the 1.17 compiler reports reasonable errors, but either the
  1904  // 1.17 or 1.18 compiler report extra errors, so we can't match correctly on both. We
  1905  // now set the patterns to match correctly on all the 1.18 errors.
  1906  // This list remains here just as a reference and for comparison - these files all pass.
  1907  var _ = setOf(
  1908  	"import1.go",      // types2 reports extra errors
  1909  	"initializerr.go", // types2 reports extra error
  1910  	"typecheck.go",    // types2 reports extra error at function call
  1911  
  1912  	"fixedbugs/bug176.go", // types2 reports all errors (pref: types2)
  1913  	"fixedbugs/bug195.go", // types2 reports slight different errors, and an extra error
  1914  	"fixedbugs/bug412.go", // types2 produces a follow-on error
  1915  
  1916  	"fixedbugs/issue11614.go", // types2 reports an extra error
  1917  	"fixedbugs/issue17038.go", // types2 doesn't report a follow-on error (pref: types2)
  1918  	"fixedbugs/issue23732.go", // types2 reports different (but ok) line numbers
  1919  	"fixedbugs/issue4510.go",  // types2 reports different (but ok) line numbers
  1920  	"fixedbugs/issue7525b.go", // types2 reports init cycle error on different line - ok otherwise
  1921  	"fixedbugs/issue7525c.go", // types2 reports init cycle error on different line - ok otherwise
  1922  	"fixedbugs/issue7525d.go", // types2 reports init cycle error on different line - ok otherwise
  1923  	"fixedbugs/issue7525e.go", // types2 reports init cycle error on different line - ok otherwise
  1924  	"fixedbugs/issue7525.go",  // types2 reports init cycle error on different line - ok otherwise
  1925  )
  1926  
  1927  func setOf(keys ...string) map[string]bool {
  1928  	m := make(map[string]bool, len(keys))
  1929  	for _, key := range keys {
  1930  		m[key] = true
  1931  	}
  1932  	return m
  1933  }
  1934  
  1935  // splitQuoted splits the string s around each instance of one or more consecutive
  1936  // white space characters while taking into account quotes and escaping, and
  1937  // returns an array of substrings of s or an empty list if s contains only white space.
  1938  // Single quotes and double quotes are recognized to prevent splitting within the
  1939  // quoted region, and are removed from the resulting substrings. If a quote in s
  1940  // isn't closed err will be set and r will have the unclosed argument as the
  1941  // last element. The backslash is used for escaping.
  1942  //
  1943  // For example, the following string:
  1944  //
  1945  //	a b:"c d" 'e''f'  "g\""
  1946  //
  1947  // Would be parsed as:
  1948  //
  1949  //	[]string{"a", "b:c d", "ef", `g"`}
  1950  //
  1951  // [copied from src/go/build/build.go]
  1952  func splitQuoted(s string) (r []string, err error) {
  1953  	var args []string
  1954  	arg := make([]rune, len(s))
  1955  	escaped := false
  1956  	quoted := false
  1957  	quote := '\x00'
  1958  	i := 0
  1959  	for _, rune := range s {
  1960  		switch {
  1961  		case escaped:
  1962  			escaped = false
  1963  		case rune == '\\':
  1964  			escaped = true
  1965  			continue
  1966  		case quote != '\x00':
  1967  			if rune == quote {
  1968  				quote = '\x00'
  1969  				continue
  1970  			}
  1971  		case rune == '"' || rune == '\'':
  1972  			quoted = true
  1973  			quote = rune
  1974  			continue
  1975  		case unicode.IsSpace(rune):
  1976  			if quoted || i > 0 {
  1977  				quoted = false
  1978  				args = append(args, string(arg[:i]))
  1979  				i = 0
  1980  			}
  1981  			continue
  1982  		}
  1983  		arg[i] = rune
  1984  		i++
  1985  	}
  1986  	if quoted || i > 0 {
  1987  		args = append(args, string(arg[:i]))
  1988  	}
  1989  	if quote != 0 {
  1990  		err = errors.New("unclosed quote")
  1991  	} else if escaped {
  1992  		err = errors.New("unfinished escaping")
  1993  	}
  1994  	return args, err
  1995  }
  1996  
  1997  // replacePrefix is like strings.ReplaceAll, but only replaces instances of old
  1998  // that are preceded by ' ', '\t', or appear at the beginning of a line.
  1999  //
  2000  // This does the same kind of filename string replacement as cmd/go.
  2001  // Pilfered from src/cmd/go/internal/work/shell.go .
  2002  func replacePrefix(s, old, new string) string {
  2003  	n := strings.Count(s, old)
  2004  	if n == 0 {
  2005  		return s
  2006  	}
  2007  
  2008  	s = strings.ReplaceAll(s, " "+old, " "+new)
  2009  	s = strings.ReplaceAll(s, "\n"+old, "\n"+new)
  2010  	s = strings.ReplaceAll(s, "\n\t"+old, "\n\t"+new)
  2011  	if strings.HasPrefix(s, old) {
  2012  		s = new + s[len(old):]
  2013  	}
  2014  	return s
  2015  }
  2016  

View as plain text