]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types, types2: remove parse (we only need mustParse for tests)
authorRobert Griesemer <gri@golang.org>
Thu, 27 Apr 2023 23:42:26 +0000 (16:42 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 28 Apr 2023 17:58:13 +0000 (17:58 +0000)
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 <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/compile/internal/types2/api_test.go
src/go/types/api_test.go

index 0e76a73699f4f4673c67cc44e9a152c669356c4e..a13f43111c4c0edbc3ac81aa5176ee318abb432b 100644 (file)
@@ -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`},
index 825f30585b1597a991b59ecc2cf6c85a4272d219..ae1a7e50a7b29d8b271e67a977cfa77d20e6204f 100644 (file)
@@ -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`},