]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/go/types/check_test.go
go/types, types2: add FileVersions map to test Info
[gostls13.git] / src / go / types / check_test.go
index 73ac80235cce010933f582608f37f189ab55d13e..fc9723a67fa5cf4569bb51e55dead7c0d0ed2d6a 100644 (file)
@@ -38,12 +38,14 @@ import (
        "go/parser"
        "go/scanner"
        "go/token"
+       "internal/buildcfg"
        "internal/testenv"
        "internal/types/errors"
        "os"
        "path/filepath"
        "reflect"
        "regexp"
+       "runtime"
        "strconv"
        "strings"
        "testing"
@@ -122,32 +124,35 @@ func parseFlags(src []byte, flags *flag.FlagSet) error {
 
 // 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 manual parameter specifies whether this is a 'manual'
-// test.
+// 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, manual bool, opts ...func(*Config)) {
-       if len(filenames) == 0 {
-               t.Fatal("no source files")
+       // Alias types are disabled by default
+       testFilesImpl(t, filenames, srcs, manual, opts...)
+       if !manual {
+               t.Setenv("GODEBUG", "gotypesalias=1")
+               testFilesImpl(t, filenames, srcs, manual, opts...)
        }
+}
 
-       var conf Config
-       flags := flag.NewFlagSet("", flag.PanicOnError)
-       flags.StringVar(&conf.GoVersion, "lang", "", "")
-       flags.BoolVar(&conf.FakeImportC, "fakeImportC", false, "")
-       if err := parseFlags(srcs[0], flags); err != nil {
-               t.Fatal(err)
+func testFilesImpl(t *testing.T, filenames []string, srcs [][]byte, manual bool, opts ...func(*Config)) {
+       if len(filenames) == 0 {
+               t.Fatal("no source files")
        }
 
+       // parse files
        files, errlist := parseFiles(t, filenames, srcs, parser.AllErrors)
-
        pkgName := "<no package>"
        if len(files) > 0 {
                pkgName = files[0].Name.Name
        }
-
        listErrors := manual && !*verifyErrors
        if listErrors && len(errlist) > 0 {
                t.Errorf("--- %s:", pkgName)
@@ -156,7 +161,8 @@ func testFiles(t *testing.T, filenames []string, srcs [][]byte, manual bool, opt
                }
        }
 
-       // typecheck and collect typechecker errors
+       // set up typechecker
+       var conf Config
        *boolFieldAddr(&conf, "_Trace") = manual && testing.Verbose()
        conf.Importer = importer.Default()
        conf.Error = func(err error) {
@@ -174,12 +180,51 @@ func testFiles(t *testing.T, filenames []string, srcs [][]byte, manual bool, opt
                }
        }
 
+       // apply custom configuration
        for _, opt := range opts {
                opt(&conf)
        }
 
-       conf.Check(pkgName, fset, files, nil)
+       // 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[ast.Expr]TypeAndValue),
+               Instances:    make(map[*ast.Ident]Instance),
+               Defs:         make(map[*ast.Ident]Object),
+               Uses:         make(map[*ast.Ident]Object),
+               Implicits:    make(map[ast.Node]Object),
+               Selections:   make(map[*ast.SelectorExpr]*Selection),
+               Scopes:       make(map[ast.Node]*Scope),
+               FileVersions: make(map[*ast.File]string),
+       }
 
+       // typecheck
+       conf.Check(pkgName, fset, files, &info)
        if listErrors {
                return
        }
@@ -300,6 +345,13 @@ func boolFieldAddr(conf *Config, name string) *bool {
        return (*bool)(v.FieldByName(name).Addr().UnsafePointer())
 }
 
+// stringFieldAddr(conf, name) returns the address of the string field conf.<name>.
+// For accessing unexported fields.
+func stringFieldAddr(conf *Config, name string) *string {
+       v := reflect.Indirect(reflect.ValueOf(conf))
+       return (*string)(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
@@ -366,6 +418,12 @@ func TestIssue47243_TypedRHS(t *testing.T) {
 }
 
 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", false)
 }