From: Robert Griesemer Date: Thu, 27 Apr 2023 23:42:26 +0000 (-0700) Subject: go/types, types2: remove parse (we only need mustParse for tests) X-Git-Tag: go1.21rc1~748 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=69e66a1626eb585cd3e7261f8192590a931a4874;p=gostls13.git go/types, types2: remove parse (we only need mustParse for tests) While at it, also simplify mustTypecheck again as it can just use typecheck. Change-Id: I6cb07b1078d9a39e0f22851028fdd4442127f2f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/490015 Run-TryBot: Robert Griesemer Reviewed-by: Robert Griesemer Reviewed-by: Robert Findley Auto-Submit: Robert Griesemer TryBot-Result: Gopher Robot --- diff --git a/src/cmd/compile/internal/types2/api_test.go b/src/cmd/compile/internal/types2/api_test.go index 0e76a73699..a13f43111c 100644 --- a/src/cmd/compile/internal/types2/api_test.go +++ b/src/cmd/compile/internal/types2/api_test.go @@ -21,13 +21,8 @@ import ( // nopos indicates an unknown position var nopos syntax.Pos -func parse(src string) (*syntax.File, error) { - errh := func(error) {} // dummy error handler so that parsing continues in presence of errors - return syntax.Parse(syntax.NewFileBase(pkgName(src)), strings.NewReader(src), errh, nil, 0) -} - func mustParse(src string) *syntax.File { - f, err := parse(src) + f, err := syntax.Parse(syntax.NewFileBase(pkgName(src)), strings.NewReader(src), nil, nil, 0) if err != nil { panic(err) // so we don't need to pass *testing.T } @@ -35,10 +30,7 @@ func mustParse(src string) *syntax.File { } func typecheck(src string, conf *Config, info *Info) (*Package, error) { - f, err := parse(src) - if f == nil { // ignore errors unless f is nil - return nil, err - } + f := mustParse(src) if conf == nil { conf = &Config{ Error: func(err error) {}, // collect all errors @@ -49,13 +41,7 @@ func typecheck(src string, conf *Config, info *Info) (*Package, error) { } func mustTypecheck(src string, conf *Config, info *Info) *Package { - f := mustParse(src) - if conf == nil { - conf = &Config{ - Importer: defaultImporter(), - } - } - pkg, err := conf.Check(f.PkgName.Value, []*syntax.File{f}, info) + pkg, err := typecheck(src, conf, info) if err != nil { panic(err) // so we don't need to pass *testing.T } @@ -339,7 +325,7 @@ func TestTypesInfo(t *testing.T) { {`package issue47243_i; var x int32; var _ = 1 << (2 << x)`, `(2 << x)`, `untyped int`}, {`package issue47243_j; var x int32; var _ = 1 << (2 << x)`, `2`, `untyped int`}, - // tests for broken code that doesn't parse or type-check + // tests for broken code that doesn't type-check {brokenPkg + `x0; func _() { var x struct {f string}; x.f := 0 }`, `x.f`, `string`}, {brokenPkg + `x1; func _() { var z string; type x struct {f string}; y := &x{q: z}}`, `z`, `string`}, {brokenPkg + `x2; func _() { var a, b string; type x struct {f string}; z := &x{f: a, f: b,}}`, `b`, `string`}, diff --git a/src/go/types/api_test.go b/src/go/types/api_test.go index 825f30585b..ae1a7e50a7 100644 --- a/src/go/types/api_test.go +++ b/src/go/types/api_test.go @@ -24,12 +24,8 @@ import ( // nopos indicates an unknown position var nopos token.Pos -func parse(fset *token.FileSet, src string) (*ast.File, error) { - return parser.ParseFile(fset, pkgName(src), src, 0) -} - func mustParse(fset *token.FileSet, src string) *ast.File { - f, err := parse(fset, src) + f, err := parser.ParseFile(fset, pkgName(src), src, 0) if err != nil { panic(err) // so we don't need to pass *testing.T } @@ -38,10 +34,7 @@ func mustParse(fset *token.FileSet, src string) *ast.File { func typecheck(src string, conf *Config, info *Info) (*Package, error) { fset := token.NewFileSet() - f, err := parse(fset, src) - if f == nil { // ignore errors unless f is nil - return nil, err - } + f := mustParse(fset, src) if conf == nil { conf = &Config{ Error: func(err error) {}, // collect all errors @@ -52,14 +45,7 @@ func typecheck(src string, conf *Config, info *Info) (*Package, error) { } func mustTypecheck(src string, conf *Config, info *Info) *Package { - fset := token.NewFileSet() - f := mustParse(fset, src) - if conf == nil { - conf = &Config{ - Importer: importer.Default(), - } - } - pkg, err := conf.Check(f.Name.Name, fset, []*ast.File{f}, info) + pkg, err := typecheck(src, conf, info) if err != nil { panic(err) // so we don't need to pass *testing.T } @@ -339,10 +325,10 @@ func TestTypesInfo(t *testing.T) { {`package issue47243_i; var x int32; var _ = 1 << (2 << x)`, `(2 << x)`, `untyped int`}, {`package issue47243_j; var x int32; var _ = 1 << (2 << x)`, `2`, `untyped int`}, - // tests for broken code that doesn't parse or type-check + // tests for broken code that doesn't type-check {broken + `x0; func _() { var x struct {f string}; x.f := 0 }`, `x.f`, `string`}, {broken + `x1; func _() { var z string; type x struct {f string}; y := &x{q: z}}`, `z`, `string`}, - {broken + `x2; func _() { var a, b string; type x struct {f string}; z := &x{f: a; f: b;}}`, `b`, `string`}, + {broken + `x2; func _() { var a, b string; type x struct {f string}; z := &x{f: a, f: b,}}`, `b`, `string`}, {broken + `x3; var x = panic("");`, `panic`, `func(interface{})`}, {`package x4; func _() { panic("") }`, `panic`, `func(interface{})`}, {broken + `x5; func _() { var x map[string][...]int; x = map[string][...]int{"": {1,2,3}} }`, `x`, `map[string]invalid type`},