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

View as plain text