From 65aa1a4fbe39d4079b3b843095af2f4975443459 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 18 Nov 2013 11:26:49 -0800 Subject: [PATCH] go.tools/go/types: String method for Initializers Plus a couple of minor fixes. R=adonovan CC=golang-dev https://golang.org/cl/28540043 --- go/types/api.go | 14 ++++++++++++++ go/types/api_test.go | 18 ++---------------- go/types/resolver.go | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/go/types/api.go b/go/types/api.go index 6eb10340bd9..0f9277b0afc 100644 --- a/go/types/api.go +++ b/go/types/api.go @@ -23,6 +23,7 @@ package types import ( + "bytes" "fmt" "go/ast" "go/token" @@ -189,6 +190,19 @@ type Initializer struct { Rhs ast.Expr } +func (init *Initializer) String() string { + var buf bytes.Buffer + for i, lhs := range init.Lhs { + if i > 0 { + buf.WriteString(", ") + } + buf.WriteString(lhs.Name()) + } + buf.WriteString(" = ") + WriteExpr(&buf, init.Rhs) + return buf.String() +} + // Check type-checks a package and returns the resulting package object, // the first error if any, and if info != nil, additional type information. // The package is marked as complete if no errors occurred, otherwise it is diff --git a/go/types/api_test.go b/go/types/api_test.go index 9abbfdb07b7..eca697e9069 100644 --- a/go/types/api_test.go +++ b/go/types/api_test.go @@ -7,7 +7,6 @@ package types_test import ( - "bytes" "go/ast" "go/parser" "go/token" @@ -19,7 +18,7 @@ import ( ) func pkgFor(path, source string, info *Info) (*Package, error) { - fset = token.NewFileSet() + fset := token.NewFileSet() f, err := parser.ParseFile(fset, path, source, 0) if err != nil { return nil, err @@ -210,19 +209,6 @@ func TestScopesInfo(t *testing.T) { } } -func initString(init *Initializer) string { - var buf bytes.Buffer - for i, lhs := range init.Lhs { - if i > 0 { - buf.WriteString(", ") - } - buf.WriteString(lhs.Name()) - } - buf.WriteString(" = ") - WriteExpr(&buf, init.Rhs) - return buf.String() -} - func TestInitOrder(t *testing.T) { var tests = []struct { src string @@ -272,7 +258,7 @@ func TestInitOrder(t *testing.T) { // initializers must match for i, want := range test.inits { - got := initString(info.InitOrder[i]) + got := info.InitOrder[i].String() if got != want { t.Errorf("package %s, init %d: got %s; want %s", name, i, got, want) continue diff --git a/go/types/resolver.go b/go/types/resolver.go index 28195c35bb0..66d69f7010a 100644 --- a/go/types/resolver.go +++ b/go/types/resolver.go @@ -147,7 +147,7 @@ func (check *checker) resolveFiles(files []*ast.File) { importer := check.conf.Import if importer == nil { if DefaultImport == nil { - panic("no Config.Import and no DefaultImport") + panic(`no Config.Import or DefaultImport (missing import _ "code.google.com/p/go.tools/go/gcimporter"?)`) } importer = DefaultImport }