Source file src/go/types/check_test.go

     1  // Copyright 2011 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 implements a typechecker test harness. The packages specified
     6  // in tests are typechecked. Error messages reported by the typechecker are
     7  // compared against the errors expected in the test files.
     8  //
     9  // Expected errors are indicated in the test files by putting comments
    10  // of the form /* ERROR pattern */ or /* ERRORx pattern */ (or a similar
    11  // //-style line comment) immediately following the tokens where errors
    12  // are reported. There must be exactly one blank before and after the
    13  // ERROR/ERRORx indicator, and the pattern must be a properly quoted Go
    14  // string.
    15  //
    16  // The harness will verify that each ERROR pattern is a substring of the
    17  // error reported at that source position, and that each ERRORx pattern
    18  // is a regular expression matching the respective error.
    19  // Consecutive comments may be used to indicate multiple errors reported
    20  // at the same position.
    21  //
    22  // For instance, the following test source indicates that an "undeclared"
    23  // error should be reported for the undeclared variable x:
    24  //
    25  //	package p
    26  //	func f() {
    27  //		_ = x /* ERROR "undeclared" */ + 1
    28  //	}
    29  
    30  package types_test
    31  
    32  import (
    33  	"bytes"
    34  	"flag"
    35  	"fmt"
    36  	"go/ast"
    37  	"go/importer"
    38  	"go/parser"
    39  	"go/scanner"
    40  	"go/token"
    41  	"internal/buildcfg"
    42  	"internal/testenv"
    43  	"internal/types/errors"
    44  	"os"
    45  	"path/filepath"
    46  	"reflect"
    47  	"regexp"
    48  	"runtime"
    49  	"strconv"
    50  	"strings"
    51  	"testing"
    52  
    53  	. "go/types"
    54  )
    55  
    56  var (
    57  	haltOnError  = flag.Bool("halt", false, "halt on error")
    58  	verifyErrors = flag.Bool("verify", false, "verify errors (rather than list them) in TestManual")
    59  )
    60  
    61  var fset = token.NewFileSet()
    62  
    63  func parseFiles(t *testing.T, filenames []string, srcs [][]byte, mode parser.Mode) ([]*ast.File, []error) {
    64  	var files []*ast.File
    65  	var errlist []error
    66  	for i, filename := range filenames {
    67  		file, err := parser.ParseFile(fset, filename, srcs[i], mode)
    68  		if file == nil {
    69  			t.Fatalf("%s: %s", filename, err)
    70  		}
    71  		files = append(files, file)
    72  		if err != nil {
    73  			if list, _ := err.(scanner.ErrorList); len(list) > 0 {
    74  				for _, err := range list {
    75  					errlist = append(errlist, err)
    76  				}
    77  			} else {
    78  				errlist = append(errlist, err)
    79  			}
    80  		}
    81  	}
    82  	return files, errlist
    83  }
    84  
    85  func unpackError(fset *token.FileSet, err error) (token.Position, string) {
    86  	switch err := err.(type) {
    87  	case *scanner.Error:
    88  		return err.Pos, err.Msg
    89  	case Error:
    90  		return fset.Position(err.Pos), err.Msg
    91  	}
    92  	panic("unreachable")
    93  }
    94  
    95  // absDiff returns the absolute difference between x and y.
    96  func absDiff(x, y int) int {
    97  	if x < y {
    98  		return y - x
    99  	}
   100  	return x - y
   101  }
   102  
   103  // parseFlags parses flags from the first line of the given source if the line
   104  // starts with "//" (line comment) followed by "-" (possibly with spaces
   105  // between). Otherwise the line is ignored.
   106  func parseFlags(src []byte, flags *flag.FlagSet) error {
   107  	// we must have a line comment that starts with a "-"
   108  	const prefix = "//"
   109  	if !bytes.HasPrefix(src, []byte(prefix)) {
   110  		return nil // first line is not a line comment
   111  	}
   112  	src = src[len(prefix):]
   113  	if i := bytes.Index(src, []byte("-")); i < 0 || len(bytes.TrimSpace(src[:i])) != 0 {
   114  		return nil // comment doesn't start with a "-"
   115  	}
   116  	end := bytes.Index(src, []byte("\n"))
   117  	const maxLen = 256
   118  	if end < 0 || end > maxLen {
   119  		return fmt.Errorf("flags comment line too long")
   120  	}
   121  
   122  	return flags.Parse(strings.Fields(string(src[:end])))
   123  }
   124  
   125  // testFiles type-checks the package consisting of the given files, and
   126  // compares the resulting errors with the ERROR annotations in the source.
   127  // Except for manual tests, each package is type-checked twice, once without
   128  // use of Alias types, and once with Alias types.
   129  //
   130  // The srcs slice contains the file content for the files named in the
   131  // filenames slice. The colDelta parameter specifies the tolerance for position
   132  // mismatch when comparing errors. The manual parameter specifies whether this
   133  // is a 'manual' test.
   134  //
   135  // If provided, opts may be used to mutate the Config before type-checking.
   136  func testFiles(t *testing.T, filenames []string, srcs [][]byte, manual bool, opts ...func(*Config)) {
   137  	// Alias types are enabled by default
   138  	testFilesImpl(t, filenames, srcs, manual, opts...)
   139  	if !manual {
   140  		t.Setenv("GODEBUG", "gotypesalias=0")
   141  		testFilesImpl(t, filenames, srcs, manual, opts...)
   142  	}
   143  }
   144  
   145  func testFilesImpl(t *testing.T, filenames []string, srcs [][]byte, manual bool, opts ...func(*Config)) {
   146  	if len(filenames) == 0 {
   147  		t.Fatal("no source files")
   148  	}
   149  
   150  	// parse files
   151  	files, errlist := parseFiles(t, filenames, srcs, parser.AllErrors)
   152  	pkgName := "<no package>"
   153  	if len(files) > 0 {
   154  		pkgName = files[0].Name.Name
   155  	}
   156  	listErrors := manual && !*verifyErrors
   157  	if listErrors && len(errlist) > 0 {
   158  		t.Errorf("--- %s:", pkgName)
   159  		for _, err := range errlist {
   160  			t.Error(err)
   161  		}
   162  	}
   163  
   164  	// set up typechecker
   165  	var conf Config
   166  	*boolFieldAddr(&conf, "_Trace") = manual && testing.Verbose()
   167  	conf.Importer = importer.Default()
   168  	conf.Error = func(err error) {
   169  		if *haltOnError {
   170  			defer panic(err)
   171  		}
   172  		if listErrors {
   173  			t.Error(err)
   174  			return
   175  		}
   176  		// Ignore secondary error messages starting with "\t";
   177  		// they are clarifying messages for a primary error.
   178  		if !strings.Contains(err.Error(), ": \t") {
   179  			errlist = append(errlist, err)
   180  		}
   181  	}
   182  
   183  	// apply custom configuration
   184  	for _, opt := range opts {
   185  		opt(&conf)
   186  	}
   187  
   188  	// apply flag setting (overrides custom configuration)
   189  	var goexperiment, gotypesalias string
   190  	flags := flag.NewFlagSet("", flag.PanicOnError)
   191  	flags.StringVar(&conf.GoVersion, "lang", "", "")
   192  	flags.StringVar(&goexperiment, "goexperiment", "", "")
   193  	flags.BoolVar(&conf.FakeImportC, "fakeImportC", false, "")
   194  	flags.StringVar(&gotypesalias, "gotypesalias", "", "")
   195  	if err := parseFlags(srcs[0], flags); err != nil {
   196  		t.Fatal(err)
   197  	}
   198  
   199  	if goexperiment != "" {
   200  		revert := setGOEXPERIMENT(goexperiment)
   201  		defer revert()
   202  	}
   203  
   204  	// By default, gotypesalias is not set.
   205  	if gotypesalias != "" {
   206  		t.Setenv("GODEBUG", "gotypesalias="+gotypesalias)
   207  	}
   208  
   209  	// Provide Config.Info with all maps so that info recording is tested.
   210  	info := Info{
   211  		Types:        make(map[ast.Expr]TypeAndValue),
   212  		Instances:    make(map[*ast.Ident]Instance),
   213  		Defs:         make(map[*ast.Ident]Object),
   214  		Uses:         make(map[*ast.Ident]Object),
   215  		Implicits:    make(map[ast.Node]Object),
   216  		Selections:   make(map[*ast.SelectorExpr]*Selection),
   217  		Scopes:       make(map[ast.Node]*Scope),
   218  		FileVersions: make(map[*ast.File]string),
   219  	}
   220  
   221  	// typecheck
   222  	conf.Check(pkgName, fset, files, &info)
   223  	if listErrors {
   224  		return
   225  	}
   226  
   227  	// collect expected errors
   228  	errmap := make(map[string]map[int][]comment)
   229  	for i, filename := range filenames {
   230  		if m := commentMap(srcs[i], regexp.MustCompile("^ ERRORx? ")); len(m) > 0 {
   231  			errmap[filename] = m
   232  		}
   233  	}
   234  
   235  	// match against found errors
   236  	var indices []int // list indices of matching errors, reused for each error
   237  	for _, err := range errlist {
   238  		gotPos, gotMsg := unpackError(fset, err)
   239  
   240  		// find list of errors for the respective error line
   241  		filename := gotPos.Filename
   242  		filemap := errmap[filename]
   243  		line := gotPos.Line
   244  		var errList []comment
   245  		if filemap != nil {
   246  			errList = filemap[line]
   247  		}
   248  
   249  		// At least one of the errors in errList should match the current error.
   250  		indices = indices[:0]
   251  		for i, want := range errList {
   252  			pattern, substr := strings.CutPrefix(want.text, " ERROR ")
   253  			if !substr {
   254  				var found bool
   255  				pattern, found = strings.CutPrefix(want.text, " ERRORx ")
   256  				if !found {
   257  					panic("unreachable")
   258  				}
   259  			}
   260  			unquoted, err := strconv.Unquote(strings.TrimSpace(pattern))
   261  			if err != nil {
   262  				t.Errorf("%s:%d:%d: invalid ERROR pattern (cannot unquote %s)", filename, line, want.col, pattern)
   263  				continue
   264  			}
   265  			if substr {
   266  				if !strings.Contains(gotMsg, unquoted) {
   267  					continue
   268  				}
   269  			} else {
   270  				rx, err := regexp.Compile(unquoted)
   271  				if err != nil {
   272  					t.Errorf("%s:%d:%d: %v", filename, line, want.col, err)
   273  					continue
   274  				}
   275  				if !rx.MatchString(gotMsg) {
   276  					continue
   277  				}
   278  			}
   279  			indices = append(indices, i)
   280  		}
   281  		if len(indices) == 0 {
   282  			t.Errorf("%s: no error expected: %q", gotPos, gotMsg)
   283  			continue
   284  		}
   285  		// len(indices) > 0
   286  
   287  		// If there are multiple matching errors, select the one with the closest column position.
   288  		index := -1 // index of matching error
   289  		var delta int
   290  		for _, i := range indices {
   291  			if d := absDiff(gotPos.Column, errList[i].col); index < 0 || d < delta {
   292  				index, delta = i, d
   293  			}
   294  		}
   295  
   296  		// The closest column position must be within expected colDelta.
   297  		const colDelta = 0 // go/types errors are positioned correctly
   298  		if delta > colDelta {
   299  			t.Errorf("%s: got col = %d; want %d", gotPos, gotPos.Column, errList[index].col)
   300  		}
   301  
   302  		// eliminate from errList
   303  		if n := len(errList) - 1; n > 0 {
   304  			// not the last entry - slide entries down (don't reorder)
   305  			copy(errList[index:], errList[index+1:])
   306  			filemap[line] = errList[:n]
   307  		} else {
   308  			// last entry - remove errList from filemap
   309  			delete(filemap, line)
   310  		}
   311  
   312  		// if filemap is empty, eliminate from errmap
   313  		if len(filemap) == 0 {
   314  			delete(errmap, filename)
   315  		}
   316  	}
   317  
   318  	// there should be no expected errors left
   319  	if len(errmap) > 0 {
   320  		t.Errorf("--- %s: unreported errors:", pkgName)
   321  		for filename, filemap := range errmap {
   322  			for line, errList := range filemap {
   323  				for _, err := range errList {
   324  					t.Errorf("%s:%d:%d: %s", filename, line, err.col, err.text)
   325  				}
   326  			}
   327  		}
   328  	}
   329  }
   330  
   331  func readCode(err Error) errors.Code {
   332  	v := reflect.ValueOf(err)
   333  	return errors.Code(v.FieldByName("go116code").Int())
   334  }
   335  
   336  // boolFieldAddr(conf, name) returns the address of the boolean field conf.<name>.
   337  // For accessing unexported fields.
   338  func boolFieldAddr(conf *Config, name string) *bool {
   339  	v := reflect.Indirect(reflect.ValueOf(conf))
   340  	return (*bool)(v.FieldByName(name).Addr().UnsafePointer())
   341  }
   342  
   343  // stringFieldAddr(conf, name) returns the address of the string field conf.<name>.
   344  // For accessing unexported fields.
   345  func stringFieldAddr(conf *Config, name string) *string {
   346  	v := reflect.Indirect(reflect.ValueOf(conf))
   347  	return (*string)(v.FieldByName(name).Addr().UnsafePointer())
   348  }
   349  
   350  // setGOEXPERIMENT overwrites the existing buildcfg.Experiment with a new one
   351  // based on the provided goexperiment string. Calling the result function
   352  // (typically via defer), reverts buildcfg.Experiment to the prior value.
   353  // For testing use, only.
   354  func setGOEXPERIMENT(goexperiment string) func() {
   355  	exp, err := buildcfg.ParseGOEXPERIMENT(runtime.GOOS, runtime.GOARCH, goexperiment)
   356  	if err != nil {
   357  		panic(err)
   358  	}
   359  	old := buildcfg.Experiment
   360  	buildcfg.Experiment = *exp
   361  	return func() { buildcfg.Experiment = old }
   362  }
   363  
   364  // TestManual is for manual testing of a package - either provided
   365  // as a list of filenames belonging to the package, or a directory
   366  // name containing the package files - after the test arguments
   367  // (and a separating "--"). For instance, to test the package made
   368  // of the files foo.go and bar.go, use:
   369  //
   370  //	go test -run Manual -- foo.go bar.go
   371  //
   372  // If no source arguments are provided, the file testdata/manual.go
   373  // is used instead.
   374  // Provide the -verify flag to verify errors against ERROR comments
   375  // in the input files rather than having a list of errors reported.
   376  // The accepted Go language version can be controlled with the -lang
   377  // flag.
   378  func TestManual(t *testing.T) {
   379  	testenv.MustHaveGoBuild(t)
   380  
   381  	filenames := flag.Args()
   382  	if len(filenames) == 0 {
   383  		filenames = []string{filepath.FromSlash("testdata/manual.go")}
   384  	}
   385  
   386  	info, err := os.Stat(filenames[0])
   387  	if err != nil {
   388  		t.Fatalf("TestManual: %v", err)
   389  	}
   390  
   391  	DefPredeclaredTestFuncs()
   392  	if info.IsDir() {
   393  		if len(filenames) > 1 {
   394  			t.Fatal("TestManual: must have only one directory argument")
   395  		}
   396  		testDir(t, filenames[0], true)
   397  	} else {
   398  		testPkg(t, filenames, true)
   399  	}
   400  }
   401  
   402  func TestLongConstants(t *testing.T) {
   403  	format := `package longconst; const _ = %s /* ERROR "constant overflow" */; const _ = %s // ERROR "excessively long constant"`
   404  	src := fmt.Sprintf(format, strings.Repeat("1", 9999), strings.Repeat("1", 10001))
   405  	testFiles(t, []string{"longconst.go"}, [][]byte{[]byte(src)}, false)
   406  }
   407  
   408  func withSizes(sizes Sizes) func(*Config) {
   409  	return func(cfg *Config) {
   410  		cfg.Sizes = sizes
   411  	}
   412  }
   413  
   414  // TestIndexRepresentability tests that constant index operands must
   415  // be representable as int even if they already have a type that can
   416  // represent larger values.
   417  func TestIndexRepresentability(t *testing.T) {
   418  	const src = `package index; var s []byte; var _ = s[int64 /* ERRORx "int64\\(1\\) << 40 \\(.*\\) overflows int" */ (1) << 40]`
   419  	testFiles(t, []string{"index.go"}, [][]byte{[]byte(src)}, false, withSizes(&StdSizes{4, 4}))
   420  }
   421  
   422  func TestIssue47243_TypedRHS(t *testing.T) {
   423  	// The RHS of the shift expression below overflows uint on 32bit platforms,
   424  	// but this is OK as it is explicitly typed.
   425  	const src = `package issue47243; var a uint64; var _ = a << uint64(4294967296)` // uint64(1<<32)
   426  	testFiles(t, []string{"p.go"}, [][]byte{[]byte(src)}, false, withSizes(&StdSizes{4, 4}))
   427  }
   428  
   429  func TestCheck(t *testing.T) {
   430  	old := buildcfg.Experiment.RangeFunc
   431  	defer func() {
   432  		buildcfg.Experiment.RangeFunc = old
   433  	}()
   434  	buildcfg.Experiment.RangeFunc = true
   435  
   436  	DefPredeclaredTestFuncs()
   437  	testDirFiles(t, "../../internal/types/testdata/check", false)
   438  }
   439  func TestSpec(t *testing.T)      { testDirFiles(t, "../../internal/types/testdata/spec", false) }
   440  func TestExamples(t *testing.T)  { testDirFiles(t, "../../internal/types/testdata/examples", false) }
   441  func TestFixedbugs(t *testing.T) { testDirFiles(t, "../../internal/types/testdata/fixedbugs", false) }
   442  func TestLocal(t *testing.T)     { testDirFiles(t, "testdata/local", false) }
   443  
   444  func testDirFiles(t *testing.T, dir string, manual bool) {
   445  	testenv.MustHaveGoBuild(t)
   446  	dir = filepath.FromSlash(dir)
   447  
   448  	fis, err := os.ReadDir(dir)
   449  	if err != nil {
   450  		t.Error(err)
   451  		return
   452  	}
   453  
   454  	for _, fi := range fis {
   455  		path := filepath.Join(dir, fi.Name())
   456  
   457  		// If fi is a directory, its files make up a single package.
   458  		if fi.IsDir() {
   459  			testDir(t, path, manual)
   460  		} else {
   461  			t.Run(filepath.Base(path), func(t *testing.T) {
   462  				testPkg(t, []string{path}, manual)
   463  			})
   464  		}
   465  	}
   466  }
   467  
   468  func testDir(t *testing.T, dir string, manual bool) {
   469  	testenv.MustHaveGoBuild(t)
   470  
   471  	fis, err := os.ReadDir(dir)
   472  	if err != nil {
   473  		t.Error(err)
   474  		return
   475  	}
   476  
   477  	var filenames []string
   478  	for _, fi := range fis {
   479  		filenames = append(filenames, filepath.Join(dir, fi.Name()))
   480  	}
   481  
   482  	t.Run(filepath.Base(dir), func(t *testing.T) {
   483  		testPkg(t, filenames, manual)
   484  	})
   485  }
   486  
   487  func testPkg(t *testing.T, filenames []string, manual bool) {
   488  	srcs := make([][]byte, len(filenames))
   489  	for i, filename := range filenames {
   490  		src, err := os.ReadFile(filename)
   491  		if err != nil {
   492  			t.Fatalf("could not read %s: %v", filename, err)
   493  		}
   494  		srcs[i] = src
   495  	}
   496  	testFiles(t, filenames, srcs, manual)
   497  }
   498  

View as plain text