]> 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 b149ae3908907b1be015b79a92426b122f0cdb51..f5c41033596d32ea377c65925a7b864404b24715 100644 (file)
@@ -34,11 +34,13 @@ import (
        "cmd/compile/internal/syntax"
        "flag"
        "fmt"
+       "internal/buildcfg"
        "internal/testenv"
        "os"
        "path/filepath"
        "reflect"
        "regexp"
+       "runtime"
        "strconv"
        "strings"
        "testing"
@@ -110,6 +112,8 @@ 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 colDelta parameter specifies the tolerance for position
@@ -118,25 +122,25 @@ func parseFlags(src []byte, flags *flag.FlagSet) error {
 //
 // 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)) {
-       if len(filenames) == 0 {
-               t.Fatal("no source files")
+       // 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(srcs[0], 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")
        }
 
+       // 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)
@@ -145,7 +149,8 @@ func testFiles(t *testing.T, filenames []string, srcs [][]byte, colDelta uint, m
                }
        }
 
-       // typecheck and collect typechecker errors
+       // set up typechecker
+       var conf Config
        conf.Trace = manual && testing.Verbose()
        conf.Importer = defaultImporter()
        conf.Error = func(err error) {
@@ -159,12 +164,50 @@ func testFiles(t *testing.T, filenames []string, srcs [][]byte, colDelta uint, m
                errlist = append(errlist, err)
        }
 
+       // apply custom configuration
        for _, opt := range opts {
                opt(&conf)
        }
 
-       conf.Check(pkgName, 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[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
        }
@@ -317,9 +360,40 @@ func TestManual(t *testing.T) {
        }
 }
 
-// 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) {
+       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
 }