1
0
mirror of https://github.com/golang/go synced 2024-11-22 20:24:47 -07:00

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 <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>
This commit is contained in:
Robert Griesemer 2023-04-27 16:42:26 -07:00 committed by Gopher Robot
parent c9b0d8b61e
commit 69e66a1626
2 changed files with 9 additions and 37 deletions

View 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`},

View 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`},