Source file src/cmd/compile/internal/types2/stdlib_test.go

     1  // Copyright 2013 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  // This file tests types2.Check by using it to
     6  // typecheck the standard library and tests.
     7  
     8  package types2_test
     9  
    10  import (
    11  	"bytes"
    12  	"cmd/compile/internal/syntax"
    13  	"errors"
    14  	"fmt"
    15  	"go/build"
    16  	"internal/testenv"
    17  	"os"
    18  	"path/filepath"
    19  	"runtime"
    20  	"slices"
    21  	"strings"
    22  	"sync"
    23  	"testing"
    24  	"time"
    25  
    26  	. "cmd/compile/internal/types2"
    27  )
    28  
    29  var stdLibImporter = defaultImporter()
    30  
    31  func TestStdlib(t *testing.T) {
    32  	if testing.Short() {
    33  		t.Skip("skipping in short mode")
    34  	}
    35  
    36  	testenv.MustHaveGoBuild(t)
    37  
    38  	// Collect non-test files.
    39  	dirFiles := make(map[string][]string)
    40  	root := filepath.Join(testenv.GOROOT(t), "src")
    41  	walkPkgDirs(root, func(dir string, filenames []string) {
    42  		dirFiles[dir] = filenames
    43  	}, t.Error)
    44  
    45  	c := &stdlibChecker{
    46  		dirFiles: dirFiles,
    47  		pkgs:     make(map[string]*futurePackage),
    48  	}
    49  
    50  	start := time.Now()
    51  
    52  	// Though we read files while parsing, type-checking is otherwise CPU bound.
    53  	//
    54  	// This doesn't achieve great CPU utilization as many packages may block
    55  	// waiting for a common import, but in combination with the non-deterministic
    56  	// map iteration below this should provide decent coverage of concurrent
    57  	// type-checking (see golang/go#47729).
    58  	cpulimit := make(chan struct{}, runtime.GOMAXPROCS(0))
    59  	var wg sync.WaitGroup
    60  
    61  	for dir := range dirFiles {
    62  		dir := dir
    63  
    64  		cpulimit <- struct{}{}
    65  		wg.Add(1)
    66  		go func() {
    67  			defer func() {
    68  				wg.Done()
    69  				<-cpulimit
    70  			}()
    71  
    72  			_, err := c.getDirPackage(dir)
    73  			if err != nil {
    74  				t.Errorf("error checking %s: %v", dir, err)
    75  			}
    76  		}()
    77  	}
    78  
    79  	wg.Wait()
    80  
    81  	if testing.Verbose() {
    82  		fmt.Println(len(dirFiles), "packages typechecked in", time.Since(start))
    83  	}
    84  }
    85  
    86  // stdlibChecker implements concurrent type-checking of the packages defined by
    87  // dirFiles, which must define a closed set of packages (such as GOROOT/src).
    88  type stdlibChecker struct {
    89  	dirFiles map[string][]string // non-test files per directory; must be pre-populated
    90  
    91  	mu   sync.Mutex
    92  	pkgs map[string]*futurePackage // future cache of type-checking results
    93  }
    94  
    95  // A futurePackage is a future result of type-checking.
    96  type futurePackage struct {
    97  	done chan struct{} // guards pkg and err
    98  	pkg  *Package
    99  	err  error
   100  }
   101  
   102  func (c *stdlibChecker) Import(path string) (*Package, error) {
   103  	panic("unimplemented: use ImportFrom")
   104  }
   105  
   106  func (c *stdlibChecker) ImportFrom(path, dir string, _ ImportMode) (*Package, error) {
   107  	if path == "unsafe" {
   108  		// unsafe cannot be type checked normally.
   109  		return Unsafe, nil
   110  	}
   111  
   112  	p, err := build.Default.Import(path, dir, build.FindOnly)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	pkg, err := c.getDirPackage(p.Dir)
   118  	if pkg != nil {
   119  		// As long as pkg is non-nil, avoid redundant errors related to failed
   120  		// imports. TestStdlib will collect errors once for each package.
   121  		return pkg, nil
   122  	}
   123  	return nil, err
   124  }
   125  
   126  // getDirPackage gets the package defined in dir from the future cache.
   127  //
   128  // If this is the first goroutine requesting the package, getDirPackage
   129  // type-checks.
   130  func (c *stdlibChecker) getDirPackage(dir string) (*Package, error) {
   131  	c.mu.Lock()
   132  	fut, ok := c.pkgs[dir]
   133  	if !ok {
   134  		// First request for this package dir; type check.
   135  		fut = &futurePackage{
   136  			done: make(chan struct{}),
   137  		}
   138  		c.pkgs[dir] = fut
   139  		files, ok := c.dirFiles[dir]
   140  		c.mu.Unlock()
   141  		if !ok {
   142  			fut.err = fmt.Errorf("no files for %s", dir)
   143  		} else {
   144  			// Using dir as the package path here may be inconsistent with the behavior
   145  			// of a normal importer, but is sufficient as dir is by construction unique
   146  			// to this package.
   147  			fut.pkg, fut.err = typecheckFiles(dir, files, c)
   148  		}
   149  		close(fut.done)
   150  	} else {
   151  		// Otherwise, await the result.
   152  		c.mu.Unlock()
   153  		<-fut.done
   154  	}
   155  	return fut.pkg, fut.err
   156  }
   157  
   158  // firstComment returns the contents of the first non-empty comment in
   159  // the given file, "skip", or the empty string. No matter the present
   160  // comments, if any of them contains a build tag, the result is always
   161  // "skip". Only comments within the first 4K of the file are considered.
   162  // TODO(gri) should only read until we see "package" token.
   163  func firstComment(filename string) (first string) {
   164  	f, err := os.Open(filename)
   165  	if err != nil {
   166  		return ""
   167  	}
   168  	defer f.Close()
   169  
   170  	// read at most 4KB
   171  	var buf [4 << 10]byte
   172  	n, _ := f.Read(buf[:])
   173  	src := bytes.NewBuffer(buf[:n])
   174  
   175  	// TODO(gri) we need a better way to terminate CommentsDo
   176  	defer func() {
   177  		if p := recover(); p != nil {
   178  			if s, ok := p.(string); ok {
   179  				first = s
   180  			}
   181  		}
   182  	}()
   183  
   184  	syntax.CommentsDo(src, func(_, _ uint, text string) {
   185  		if text[0] != '/' {
   186  			return // not a comment
   187  		}
   188  
   189  		// extract comment text
   190  		if text[1] == '*' {
   191  			text = text[:len(text)-2]
   192  		}
   193  		text = strings.TrimSpace(text[2:])
   194  
   195  		if strings.HasPrefix(text, "go:build ") {
   196  			panic("skip")
   197  		}
   198  		if first == "" {
   199  			first = text // text may be "" but that's ok
   200  		}
   201  		// continue as we may still see build tags
   202  	})
   203  
   204  	return
   205  }
   206  
   207  func testTestDir(t *testing.T, path string, ignore ...string) {
   208  	files, err := os.ReadDir(path)
   209  	if err != nil {
   210  		// cmd/distpack deletes GOROOT/test, so skip the test if it isn't present.
   211  		// cmd/distpack also requires GOROOT/VERSION to exist, so use that to
   212  		// suppress false-positive skips.
   213  		if _, err := os.Stat(filepath.Join(testenv.GOROOT(t), "test")); os.IsNotExist(err) {
   214  			if _, err := os.Stat(filepath.Join(testenv.GOROOT(t), "VERSION")); err == nil {
   215  				t.Skipf("skipping: GOROOT/test not present")
   216  			}
   217  		}
   218  		t.Fatal(err)
   219  	}
   220  
   221  	excluded := make(map[string]bool)
   222  	for _, filename := range ignore {
   223  		excluded[filename] = true
   224  	}
   225  
   226  	for _, f := range files {
   227  		// filter directory contents
   228  		if f.IsDir() || !strings.HasSuffix(f.Name(), ".go") || excluded[f.Name()] {
   229  			continue
   230  		}
   231  
   232  		// get per-file instructions
   233  		expectErrors := false
   234  		filename := filepath.Join(path, f.Name())
   235  		goVersion := ""
   236  		if comment := firstComment(filename); comment != "" {
   237  			if strings.Contains(comment, "-goexperiment") {
   238  				continue // ignore this file
   239  			}
   240  			fields := strings.Fields(comment)
   241  			switch fields[0] {
   242  			case "skip", "compiledir":
   243  				continue // ignore this file
   244  			case "errorcheck":
   245  				expectErrors = true
   246  				for _, arg := range fields[1:] {
   247  					if arg == "-0" || arg == "-+" || arg == "-std" {
   248  						// Marked explicitly as not expecting errors (-0),
   249  						// or marked as compiling runtime/stdlib, which is only done
   250  						// to trigger runtime/stdlib-only error output.
   251  						// In both cases, the code should typecheck.
   252  						expectErrors = false
   253  						break
   254  					}
   255  					const prefix = "-lang="
   256  					if strings.HasPrefix(arg, prefix) {
   257  						goVersion = arg[len(prefix):]
   258  					}
   259  				}
   260  			}
   261  		}
   262  
   263  		// parse and type-check file
   264  		if testing.Verbose() {
   265  			fmt.Println("\t", filename)
   266  		}
   267  		file, err := syntax.ParseFile(filename, nil, nil, 0)
   268  		if err == nil {
   269  			conf := Config{
   270  				GoVersion: goVersion,
   271  				Importer:  stdLibImporter,
   272  			}
   273  			_, err = conf.Check(filename, []*syntax.File{file}, nil)
   274  		}
   275  
   276  		if expectErrors {
   277  			if err == nil {
   278  				t.Errorf("expected errors but found none in %s", filename)
   279  			}
   280  		} else {
   281  			if err != nil {
   282  				t.Error(err)
   283  			}
   284  		}
   285  	}
   286  }
   287  
   288  func TestStdTest(t *testing.T) {
   289  	testenv.MustHaveGoBuild(t)
   290  
   291  	if testing.Short() && testenv.Builder() == "" {
   292  		t.Skip("skipping in short mode")
   293  	}
   294  
   295  	testTestDir(t, filepath.Join(testenv.GOROOT(t), "test"),
   296  		"cmplxdivide.go", // also needs file cmplxdivide1.go - ignore
   297  		"directive.go",   // tests compiler rejection of bad directive placement - ignore
   298  		"directive2.go",  // tests compiler rejection of bad directive placement - ignore
   299  		"embedfunc.go",   // tests //go:embed
   300  		"embedvers.go",   // tests //go:embed
   301  		"linkname2.go",   // types2 doesn't check validity of //go:xxx directives
   302  		"linkname3.go",   // types2 doesn't check validity of //go:xxx directives
   303  	)
   304  }
   305  
   306  func TestStdFixed(t *testing.T) {
   307  	testenv.MustHaveGoBuild(t)
   308  
   309  	if testing.Short() && testenv.Builder() == "" {
   310  		t.Skip("skipping in short mode")
   311  	}
   312  
   313  	testTestDir(t, filepath.Join(testenv.GOROOT(t), "test", "fixedbugs"),
   314  		"bug248.go", "bug302.go", "bug369.go", // complex test instructions - ignore
   315  		"bug398.go",      // types2 doesn't check for anonymous interface cycles (go.dev/issue/56103)
   316  		"issue6889.go",   // gc-specific test
   317  		"issue11362.go",  // canonical import path check
   318  		"issue16369.go",  // types2 handles this correctly - not an issue
   319  		"issue18459.go",  // types2 doesn't check validity of //go:xxx directives
   320  		"issue18882.go",  // types2 doesn't check validity of //go:xxx directives
   321  		"issue20027.go",  // types2 does not have constraints on channel element size
   322  		"issue20529.go",  // types2 does not have constraints on stack size
   323  		"issue22200.go",  // types2 does not have constraints on stack size
   324  		"issue22200b.go", // types2 does not have constraints on stack size
   325  		"issue25507.go",  // types2 does not have constraints on stack size
   326  		"issue20780.go",  // types2 does not have constraints on stack size
   327  		"issue42058a.go", // types2 does not have constraints on channel element size
   328  		"issue42058b.go", // types2 does not have constraints on channel element size
   329  		"issue48097.go",  // go/types doesn't check validity of //go:xxx directives, and non-init bodyless function
   330  		"issue48230.go",  // go/types doesn't check validity of //go:xxx directives
   331  		"issue49767.go",  // go/types does not have constraints on channel element size
   332  		"issue49814.go",  // go/types does not have constraints on array size
   333  		"issue56103.go",  // anonymous interface cycles; will be a type checker error in 1.22
   334  		"issue52697.go",  // types2 does not have constraints on stack size
   335  
   336  		// These tests requires runtime/cgo.Incomplete, which is only available on some platforms.
   337  		// However, types2 does not know about build constraints.
   338  		"bug514.go",
   339  		"issue40954.go",
   340  		"issue42032.go",
   341  		"issue42076.go",
   342  		"issue46903.go",
   343  		"issue51733.go",
   344  		"notinheap2.go",
   345  		"notinheap3.go",
   346  	)
   347  }
   348  
   349  func TestStdKen(t *testing.T) {
   350  	testenv.MustHaveGoBuild(t)
   351  
   352  	testTestDir(t, filepath.Join(testenv.GOROOT(t), "test", "ken"))
   353  }
   354  
   355  // Package paths of excluded packages.
   356  var excluded = map[string]bool{
   357  	"builtin":                       true,
   358  	"cmd/compile/internal/ssa/_gen": true,
   359  }
   360  
   361  // printPackageMu synchronizes the printing of type-checked package files in
   362  // the typecheckFiles function.
   363  //
   364  // Without synchronization, package files may be interleaved during concurrent
   365  // type-checking.
   366  var printPackageMu sync.Mutex
   367  
   368  // typecheckFiles typechecks the given package files.
   369  func typecheckFiles(path string, filenames []string, importer Importer) (*Package, error) {
   370  	// Parse package files.
   371  	var files []*syntax.File
   372  	for _, filename := range filenames {
   373  		var errs []error
   374  		errh := func(err error) { errs = append(errs, err) }
   375  		file, err := syntax.ParseFile(filename, errh, nil, 0)
   376  		if err != nil {
   377  			return nil, errors.Join(errs...)
   378  		}
   379  
   380  		files = append(files, file)
   381  	}
   382  
   383  	if testing.Verbose() {
   384  		printPackageMu.Lock()
   385  		fmt.Println("package", files[0].PkgName.Value)
   386  		for _, filename := range filenames {
   387  			fmt.Println("\t", filename)
   388  		}
   389  		printPackageMu.Unlock()
   390  	}
   391  
   392  	// Typecheck package files.
   393  	var errs []error
   394  	conf := Config{
   395  		Error: func(err error) {
   396  			errs = append(errs, err)
   397  		},
   398  		Importer:    importer,
   399  		EnableAlias: true,
   400  	}
   401  	info := Info{Uses: make(map[*syntax.Name]Object)}
   402  	pkg, _ := conf.Check(path, files, &info)
   403  	err := errors.Join(errs...)
   404  	if err != nil {
   405  		return pkg, err
   406  	}
   407  
   408  	// Perform checks of API invariants.
   409  
   410  	// All Objects have a package, except predeclared ones.
   411  	errorError := Universe.Lookup("error").Type().Underlying().(*Interface).ExplicitMethod(0) // (error).Error
   412  	for id, obj := range info.Uses {
   413  		predeclared := obj == Universe.Lookup(obj.Name()) || obj == errorError
   414  		if predeclared == (obj.Pkg() != nil) {
   415  			posn := id.Pos()
   416  			if predeclared {
   417  				return nil, fmt.Errorf("%s: predeclared object with package: %s", posn, obj)
   418  			} else {
   419  				return nil, fmt.Errorf("%s: user-defined object without package: %s", posn, obj)
   420  			}
   421  		}
   422  	}
   423  
   424  	return pkg, nil
   425  }
   426  
   427  // pkgFilenames returns the list of package filenames for the given directory.
   428  func pkgFilenames(dir string, includeTest bool) ([]string, error) {
   429  	ctxt := build.Default
   430  	ctxt.CgoEnabled = false
   431  	pkg, err := ctxt.ImportDir(dir, 0)
   432  	if err != nil {
   433  		if _, nogo := err.(*build.NoGoError); nogo {
   434  			return nil, nil // no *.go files, not an error
   435  		}
   436  		return nil, err
   437  	}
   438  	if excluded[pkg.ImportPath] {
   439  		return nil, nil
   440  	}
   441  	if slices.Contains(strings.Split(pkg.ImportPath, "/"), "_asm") {
   442  		// Submodules where not all dependencies are available.
   443  		// See go.dev/issue/46027.
   444  		return nil, nil
   445  	}
   446  	var filenames []string
   447  	for _, name := range pkg.GoFiles {
   448  		filenames = append(filenames, filepath.Join(pkg.Dir, name))
   449  	}
   450  	if includeTest {
   451  		for _, name := range pkg.TestGoFiles {
   452  			filenames = append(filenames, filepath.Join(pkg.Dir, name))
   453  		}
   454  	}
   455  	return filenames, nil
   456  }
   457  
   458  func walkPkgDirs(dir string, pkgh func(dir string, filenames []string), errh func(args ...interface{})) {
   459  	w := walker{pkgh, errh}
   460  	w.walk(dir)
   461  }
   462  
   463  type walker struct {
   464  	pkgh func(dir string, filenames []string)
   465  	errh func(args ...any)
   466  }
   467  
   468  func (w *walker) walk(dir string) {
   469  	files, err := os.ReadDir(dir)
   470  	if err != nil {
   471  		w.errh(err)
   472  		return
   473  	}
   474  
   475  	// apply pkgh to the files in directory dir
   476  
   477  	// Don't get test files as these packages are imported.
   478  	pkgFiles, err := pkgFilenames(dir, false)
   479  	if err != nil {
   480  		w.errh(err)
   481  		return
   482  	}
   483  	if pkgFiles != nil {
   484  		w.pkgh(dir, pkgFiles)
   485  	}
   486  
   487  	// traverse subdirectories, but don't walk into testdata
   488  	for _, f := range files {
   489  		if f.IsDir() && f.Name() != "testdata" {
   490  			w.walk(filepath.Join(dir, f.Name()))
   491  		}
   492  	}
   493  }
   494  

View as plain text