mirror of
https://github.com/golang/go
synced 2024-11-19 03:44:40 -07:00
go.tools/go/types: String method for Initializers
Plus a couple of minor fixes. R=adonovan CC=golang-dev https://golang.org/cl/28540043
This commit is contained in:
parent
a284a61701
commit
65aa1a4fbe
@ -23,6 +23,7 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/token"
|
"go/token"
|
||||||
@ -189,6 +190,19 @@ type Initializer struct {
|
|||||||
Rhs ast.Expr
|
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,
|
// Check type-checks a package and returns the resulting package object,
|
||||||
// the first error if any, and if info != nil, additional type information.
|
// 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
|
// The package is marked as complete if no errors occurred, otherwise it is
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
package types_test
|
package types_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
@ -19,7 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func pkgFor(path, source string, info *Info) (*Package, error) {
|
func pkgFor(path, source string, info *Info) (*Package, error) {
|
||||||
fset = token.NewFileSet()
|
fset := token.NewFileSet()
|
||||||
f, err := parser.ParseFile(fset, path, source, 0)
|
f, err := parser.ParseFile(fset, path, source, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
func TestInitOrder(t *testing.T) {
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
src string
|
src string
|
||||||
@ -272,7 +258,7 @@ func TestInitOrder(t *testing.T) {
|
|||||||
|
|
||||||
// initializers must match
|
// initializers must match
|
||||||
for i, want := range test.inits {
|
for i, want := range test.inits {
|
||||||
got := initString(info.InitOrder[i])
|
got := info.InitOrder[i].String()
|
||||||
if got != want {
|
if got != want {
|
||||||
t.Errorf("package %s, init %d: got %s; want %s", name, i, got, want)
|
t.Errorf("package %s, init %d: got %s; want %s", name, i, got, want)
|
||||||
continue
|
continue
|
||||||
|
@ -147,7 +147,7 @@ func (check *checker) resolveFiles(files []*ast.File) {
|
|||||||
importer := check.conf.Import
|
importer := check.conf.Import
|
||||||
if importer == nil {
|
if importer == nil {
|
||||||
if DefaultImport == 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
|
importer = DefaultImport
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user