]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/compile/internal/types2/check_test.go
go/types, types2: implement Alias proposal (export API)
[gostls13.git] / src / cmd / compile / internal / types2 / check_test.go
index 039b0bde007f175243944d2dfa313e5a95aee390..f5c41033596d32ea377c65925a7b864404b24715 100644 (file)
@@ -4,20 +4,27 @@
 
 // This file implements a typechecker test harness. The packages specified
 // in tests are typechecked. Error messages reported by the typechecker are
-// compared against the error messages expected in the test files.
+// compared against the errors expected in the test files.
 //
-// Expected errors are indicated in the test files by putting a comment
-// of the form /* ERROR "rx" */ immediately following an offending token.
-// The harness will verify that an error matching the regular expression
-// rx is reported at that source position. Consecutive comments may be
-// used to indicate multiple errors for the same token position.
+// Expected errors are indicated in the test files by putting comments
+// of the form /* ERROR pattern */ or /* ERRORx pattern */ (or a similar
+// //-style line comment) immediately following the tokens where errors
+// are reported. There must be exactly one blank before and after the
+// ERROR/ERRORx indicator, and the pattern must be a properly quoted Go
+// string.
 //
-// For instance, the following test file indicates that a "not declared"
+// The harness will verify that each ERROR pattern is a substring of the
+// error reported at that source position, and that each ERRORx pattern
+// is a regular expression matching the respective error.
+// Consecutive comments may be used to indicate multiple errors reported
+// at the same position.
+//
+// For instance, the following test source indicates that an "undeclared"
 // error should be reported for the undeclared variable x:
 //
 //     package p
 //     func f() {
-//             _ = x /* ERROR "not declared" */ + 1
+//             _ = x /* ERROR "undeclared" */ + 1
 //     }
 
 package types2_test
@@ -27,11 +34,14 @@ import (
        "cmd/compile/internal/syntax"
        "flag"
        "fmt"
+       "internal/buildcfg"
        "internal/testenv"
        "os"
        "path/filepath"
+       "reflect"
        "regexp"
-       "sort"
+       "runtime"
+       "strconv"
        "strings"
        "testing"
 
@@ -41,15 +51,16 @@ import (
 var (
        haltOnError  = flag.Bool("halt", false, "halt on error")
        verifyErrors = flag.Bool("verify", false, "verify errors (rather than list them) in TestManual")
-       goVersion    = flag.String("lang", "", "Go language version (e.g. \"go1.12\")")
 )
 
-func parseFiles(t *testing.T, filenames []string, mode syntax.Mode) ([]*syntax.File, []error) {
+func parseFiles(t *testing.T, filenames []string, srcs [][]byte, mode syntax.Mode) ([]*syntax.File, []error) {
        var files []*syntax.File
        var errlist []error
        errh := func(err error) { errlist = append(errlist, err) }
-       for _, filename := range filenames {
-               file, err := syntax.ParseFile(filename, errh, nil, mode)
+       for i, filename := range filenames {
+               base := syntax.NewFileBase(filename)
+               r := bytes.NewReader(srcs[i])
+               file, err := syntax.Parse(base, r, errh, nil, mode)
                if file == nil {
                        t.Fatalf("%s: %s", filename, err)
                }
@@ -58,53 +69,29 @@ func parseFiles(t *testing.T, filenames []string, mode syntax.Mode) ([]*syntax.F
        return files, errlist
 }
 
-func unpackError(err error) syntax.Error {
+func unpackError(err error) (syntax.Pos, string) {
        switch err := err.(type) {
        case syntax.Error:
-               return err
+               return err.Pos, err.Msg
        case Error:
-               return syntax.Error{Pos: err.Pos, Msg: err.Msg}
+               return err.Pos, err.Msg
        default:
-               return syntax.Error{Msg: err.Error()}
+               return nopos, err.Error()
        }
 }
 
-// delta returns the absolute difference between x and y.
-func delta(x, y uint) uint {
-       switch {
-       case x < y:
+// absDiff returns the absolute difference between x and y.
+func absDiff(x, y uint) uint {
+       if x < y {
                return y - x
-       case x > y:
-               return x - y
-       default:
-               return 0
        }
+       return x - y
 }
 
-// Note: parseFlags is identical to the version in go/types which is
-//       why it has a src argument even though here it is always nil.
-
-// parseFlags parses flags from the first line of the given source
-// (from src if present, or by reading from the file) if the line
-// starts with "//" (line comment) followed by "-" (possibly with
-// spaces between). Otherwise the line is ignored.
-func parseFlags(filename string, src []byte, flags *flag.FlagSet) error {
-       // If there is no src, read from the file.
-       const maxLen = 256
-       if len(src) == 0 {
-               f, err := os.Open(filename)
-               if err != nil {
-                       return err
-               }
-
-               var buf [maxLen]byte
-               n, err := f.Read(buf[:])
-               if err != nil {
-                       return err
-               }
-               src = buf[:n]
-       }
-
+// parseFlags parses flags from the first line of the given source if the line
+// starts with "//" (line comment) followed by "-" (possibly with spaces
+// between). Otherwise the line is ignored.
+func parseFlags(src []byte, flags *flag.FlagSet) error {
        // we must have a line comment that starts with a "-"
        const prefix = "//"
        if !bytes.HasPrefix(src, []byte(prefix)) {
@@ -115,6 +102,7 @@ func parseFlags(filename string, src []byte, flags *flag.FlagSet) error {
                return nil // comment doesn't start with a "-"
        }
        end := bytes.Index(src, []byte("\n"))
+       const maxLen = 256
        if end < 0 || end > maxLen {
                return fmt.Errorf("flags comment line too long")
        }
@@ -122,26 +110,37 @@ func parseFlags(filename string, src []byte, flags *flag.FlagSet) error {
        return flags.Parse(strings.Fields(string(src[:end])))
 }
 
-func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
-       if len(filenames) == 0 {
-               t.Fatal("no source files")
+// testFiles type-checks the package consisting of the given files, and
+// compares the resulting errors with the ERROR annotations in the source.
+// Except for manual tests, each package is type-checked twice, once without
+// use of Alias types, and once with Alias types.
+//
+// The srcs slice contains the file content for the files named in the
+// filenames slice. The colDelta parameter specifies the tolerance for position
+// mismatch when comparing errors. The manual parameter specifies whether this
+// is a 'manual' test.
+//
+// If provided, opts may be used to mutate the Config before type-checking.
+func testFiles(t *testing.T, filenames []string, srcs [][]byte, colDelta uint, manual bool, opts ...func(*Config)) {
+       // Alias types are disabled by default
+       testFilesImpl(t, filenames, srcs, colDelta, manual, opts...)
+       if !manual {
+               t.Setenv("GODEBUG", "gotypesalias=1")
+               testFilesImpl(t, filenames, srcs, colDelta, manual, opts...)
        }
+}
 
-       var conf Config
-       flags := flag.NewFlagSet("", flag.PanicOnError)
-       flags.StringVar(&conf.GoVersion, "lang", "", "")
-       flags.BoolVar(&conf.FakeImportC, "fakeImportC", false, "")
-       if err := parseFlags(filenames[0], nil, flags); err != nil {
-               t.Fatal(err)
+func testFilesImpl(t *testing.T, filenames []string, srcs [][]byte, colDelta uint, manual bool, opts ...func(*Config)) {
+       if len(filenames) == 0 {
+               t.Fatal("no source files")
        }
 
-       files, errlist := parseFiles(t, filenames, 0)
-
+       // parse files
+       files, errlist := parseFiles(t, filenames, srcs, 0)
        pkgName := "<no package>"
        if len(files) > 0 {
                pkgName = files[0].PkgName.Value
        }
-
        listErrors := manual && !*verifyErrors
        if listErrors && len(errlist) > 0 {
                t.Errorf("--- %s:", pkgName)
@@ -150,7 +149,8 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
                }
        }
 
-       // typecheck and collect typechecker errors
+       // set up typechecker
+       var conf Config
        conf.Trace = manual && testing.Verbose()
        conf.Importer = defaultImporter()
        conf.Error = func(err error) {
@@ -163,78 +163,136 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
                }
                errlist = append(errlist, err)
        }
-       conf.Check(pkgName, files, nil)
 
+       // apply custom configuration
+       for _, opt := range opts {
+               opt(&conf)
+       }
+
+       // apply flag setting (overrides custom configuration)
+       var goexperiment, gotypesalias string
+       flags := flag.NewFlagSet("", flag.PanicOnError)
+       flags.StringVar(&conf.GoVersion, "lang", "", "")
+       flags.StringVar(&goexperiment, "goexperiment", "", "")
+       flags.BoolVar(&conf.FakeImportC, "fakeImportC", false, "")
+       flags.StringVar(&gotypesalias, "gotypesalias", "", "")
+       if err := parseFlags(srcs[0], flags); err != nil {
+               t.Fatal(err)
+       }
+
+       exp, err := buildcfg.ParseGOEXPERIMENT(runtime.GOOS, runtime.GOARCH, goexperiment)
+       if err != nil {
+               t.Fatal(err)
+       }
+       old := buildcfg.Experiment
+       defer func() {
+               buildcfg.Experiment = old
+       }()
+       buildcfg.Experiment = *exp
+
+       // By default, gotypesalias is not set.
+       if gotypesalias != "" {
+               t.Setenv("GODEBUG", "gotypesalias="+gotypesalias)
+       }
+
+       // Provide Config.Info with all maps so that info recording is tested.
+       info := Info{
+               Types:      make(map[syntax.Expr]TypeAndValue),
+               Instances:  make(map[*syntax.Name]Instance),
+               Defs:       make(map[*syntax.Name]Object),
+               Uses:       make(map[*syntax.Name]Object),
+               Implicits:  make(map[syntax.Node]Object),
+               Selections: make(map[*syntax.SelectorExpr]*Selection),
+               Scopes:     make(map[syntax.Node]*Scope),
+       }
+
+       // typecheck
+       conf.Check(pkgName, files, &info)
        if listErrors {
                return
        }
 
-       // sort errlist in source order
-       sort.Slice(errlist, func(i, j int) bool {
-               pi := unpackError(errlist[i]).Pos
-               pj := unpackError(errlist[j]).Pos
-               return pi.Cmp(pj) < 0
-       })
-
        // collect expected errors
        errmap := make(map[string]map[uint][]syntax.Error)
-       for _, filename := range filenames {
-               f, err := os.Open(filename)
-               if err != nil {
-                       t.Error(err)
-                       continue
-               }
-               if m := syntax.ErrorMap(f); len(m) > 0 {
+       for i, filename := range filenames {
+               if m := syntax.CommentMap(bytes.NewReader(srcs[i]), regexp.MustCompile("^ ERRORx? ")); len(m) > 0 {
                        errmap[filename] = m
                }
-               f.Close()
        }
 
        // match against found errors
+       var indices []int // list indices of matching errors, reused for each error
        for _, err := range errlist {
-               got := unpackError(err)
+               gotPos, gotMsg := unpackError(err)
 
                // find list of errors for the respective error line
-               filename := got.Pos.Base().Filename()
+               filename := gotPos.Base().Filename()
                filemap := errmap[filename]
-               line := got.Pos.Line()
-               var list []syntax.Error
+               line := gotPos.Line()
+               var errList []syntax.Error
                if filemap != nil {
-                       list = filemap[line]
+                       errList = filemap[line]
                }
-               // list may be nil
 
-               // one of errors in list should match the current error
-               index := -1 // list index of matching message, if any
-               for i, want := range list {
-                       rx, err := regexp.Compile(want.Msg)
+               // At least one of the errors in errList should match the current error.
+               indices = indices[:0]
+               for i, want := range errList {
+                       pattern, substr := strings.CutPrefix(want.Msg, " ERROR ")
+                       if !substr {
+                               var found bool
+                               pattern, found = strings.CutPrefix(want.Msg, " ERRORx ")
+                               if !found {
+                                       panic("unreachable")
+                               }
+                       }
+                       pattern, err := strconv.Unquote(strings.TrimSpace(pattern))
                        if err != nil {
                                t.Errorf("%s:%d:%d: %v", filename, line, want.Pos.Col(), err)
                                continue
                        }
-                       if rx.MatchString(got.Msg) {
-                               index = i
-                               break
+                       if substr {
+                               if !strings.Contains(gotMsg, pattern) {
+                                       continue
+                               }
+                       } else {
+                               rx, err := regexp.Compile(pattern)
+                               if err != nil {
+                                       t.Errorf("%s:%d:%d: %v", filename, line, want.Pos.Col(), err)
+                                       continue
+                               }
+                               if !rx.MatchString(gotMsg) {
+                                       continue
+                               }
                        }
+                       indices = append(indices, i)
                }
-               if index < 0 {
-                       t.Errorf("%s: no error expected: %q", got.Pos, got.Msg)
+               if len(indices) == 0 {
+                       t.Errorf("%s: no error expected: %q", gotPos, gotMsg)
                        continue
                }
+               // len(indices) > 0
+
+               // If there are multiple matching errors, select the one with the closest column position.
+               index := -1 // index of matching error
+               var delta uint
+               for _, i := range indices {
+                       if d := absDiff(gotPos.Col(), errList[i].Pos.Col()); index < 0 || d < delta {
+                               index, delta = i, d
+                       }
+               }
 
-               // column position must be within expected colDelta
-               want := list[index]
-               if delta(got.Pos.Col(), want.Pos.Col()) > colDelta {
-                       t.Errorf("%s: got col = %d; want %d", got.Pos, got.Pos.Col(), want.Pos.Col())
+               // The closest column position must be within expected colDelta.
+               if delta > colDelta {
+                       t.Errorf("%s: got col = %d; want %d", gotPos, gotPos.Col(), errList[index].Pos.Col())
                }
 
-               // eliminate from list
-               if n := len(list) - 1; n > 0 {
+               // eliminate from errList
+               if n := len(errList) - 1; n > 0 {
                        // not the last entry - slide entries down (don't reorder)
-                       copy(list[index:], list[index+1:])
-                       filemap[line] = list[:n]
+                       copy(errList[index:], errList[index+1:])
+                       filemap[line] = errList[:n]
                } else {
-                       // last entry - remove list from filemap
+                       // last entry - remove errList from filemap
                        delete(filemap, line)
                }
 
@@ -248,8 +306,8 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
        if len(errmap) > 0 {
                t.Errorf("--- %s: unreported errors:", pkgName)
                for filename, filemap := range errmap {
-                       for line, list := range filemap {
-                               for _, err := range list {
+                       for line, errList := range filemap {
+                               for _, err := range errList {
                                        t.Errorf("%s:%d:%d: %s", filename, line, err.Pos.Col(), err.Msg)
                                }
                        }
@@ -257,6 +315,13 @@ func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
        }
 }
 
+// boolFieldAddr(conf, name) returns the address of the boolean field conf.<name>.
+// For accessing unexported fields.
+func boolFieldAddr(conf *Config, name string) *bool {
+       v := reflect.Indirect(reflect.ValueOf(conf))
+       return (*bool)(v.FieldByName(name).Addr().UnsafePointer())
+}
+
 // TestManual is for manual testing of a package - either provided
 // as a list of filenames belonging to the package, or a directory
 // name containing the package files - after the test arguments
@@ -291,16 +356,55 @@ func TestManual(t *testing.T) {
                }
                testDir(t, filenames[0], 0, true)
        } else {
-               testFiles(t, filenames, 0, true)
+               testPkg(t, filenames, 0, true)
        }
 }
 
-// TODO(gri) go/types has extra TestLongConstants and TestIndexRepresentability tests
+func TestLongConstants(t *testing.T) {
+       format := `package longconst; const _ = %s /* ERROR "constant overflow" */; const _ = %s // ERROR "excessively long constant"`
+       src := fmt.Sprintf(format, strings.Repeat("1", 9999), strings.Repeat("1", 10001))
+       testFiles(t, []string{"longconst.go"}, [][]byte{[]byte(src)}, 0, false)
+}
+
+func withSizes(sizes Sizes) func(*Config) {
+       return func(cfg *Config) {
+               cfg.Sizes = sizes
+       }
+}
+
+// TestIndexRepresentability tests that constant index operands must
+// be representable as int even if they already have a type that can
+// represent larger values.
+func TestIndexRepresentability(t *testing.T) {
+       const src = `package index; var s []byte; var _ = s[int64 /* ERRORx "int64\\(1\\) << 40 \\(.*\\) overflows int" */ (1) << 40]`
+       testFiles(t, []string{"index.go"}, [][]byte{[]byte(src)}, 0, false, withSizes(&StdSizes{4, 4}))
+}
+
+func TestIssue47243_TypedRHS(t *testing.T) {
+       // The RHS of the shift expression below overflows uint on 32bit platforms,
+       // but this is OK as it is explicitly typed.
+       const src = `package issue47243; var a uint64; var _ = a << uint64(4294967296)` // uint64(1<<32)
+       testFiles(t, []string{"p.go"}, [][]byte{[]byte(src)}, 0, false, withSizes(&StdSizes{4, 4}))
+}
 
-func TestCheck(t *testing.T)     { DefPredeclaredTestFuncs(); testDirFiles(t, "testdata/check", 55, false) } // TODO(gri) narrow column tolerance
-func TestSpec(t *testing.T)      { testDirFiles(t, "../../../../go/types/testdata/spec", 0, false) }
-func TestExamples(t *testing.T)  { testDirFiles(t, "testdata/examples", 45, false) }
-func TestFixedbugs(t *testing.T) { testDirFiles(t, "testdata/fixedbugs", 0, false) }
+func TestCheck(t *testing.T) {
+       old := buildcfg.Experiment.RangeFunc
+       defer func() {
+               buildcfg.Experiment.RangeFunc = old
+       }()
+       buildcfg.Experiment.RangeFunc = true
+
+       DefPredeclaredTestFuncs()
+       testDirFiles(t, "../../../../internal/types/testdata/check", 50, false) // TODO(gri) narrow column tolerance
+}
+func TestSpec(t *testing.T) { testDirFiles(t, "../../../../internal/types/testdata/spec", 0, false) }
+func TestExamples(t *testing.T) {
+       testDirFiles(t, "../../../../internal/types/testdata/examples", 125, false)
+} // TODO(gri) narrow column tolerance
+func TestFixedbugs(t *testing.T) {
+       testDirFiles(t, "../../../../internal/types/testdata/fixedbugs", 100, false)
+}                            // TODO(gri) narrow column tolerance
+func TestLocal(t *testing.T) { testDirFiles(t, "testdata/local", 0, false) }
 
 func testDirFiles(t *testing.T, dir string, colDelta uint, manual bool) {
        testenv.MustHaveGoBuild(t)
@@ -320,7 +424,7 @@ func testDirFiles(t *testing.T, dir string, colDelta uint, manual bool) {
                        testDir(t, path, colDelta, manual)
                } else {
                        t.Run(filepath.Base(path), func(t *testing.T) {
-                               testFiles(t, []string{path}, colDelta, manual)
+                               testPkg(t, []string{path}, colDelta, manual)
                        })
                }
        }
@@ -339,6 +443,18 @@ func testDir(t *testing.T, dir string, colDelta uint, manual bool) {
        }
 
        t.Run(filepath.Base(dir), func(t *testing.T) {
-               testFiles(t, filenames, colDelta, manual)
+               testPkg(t, filenames, colDelta, manual)
        })
 }
+
+func testPkg(t *testing.T, filenames []string, colDelta uint, manual bool) {
+       srcs := make([][]byte, len(filenames))
+       for i, filename := range filenames {
+               src, err := os.ReadFile(filename)
+               if err != nil {
+                       t.Fatalf("could not read %s: %v", filename, err)
+               }
+               srcs[i] = src
+       }
+       testFiles(t, filenames, srcs, colDelta, manual)
+}