1
0
mirror of https://github.com/golang/go synced 2024-11-17 11:04:53 -07:00

all: clean up code with token.IsExported

A handful of packages were reimplementing IsExported, so use
token.IsExported instead. This caused the deps test to fail for net/rpc.
However, net/rpc deals with Go types, and go/token is light and fairly
low-level in terms of Go tooling packages, so that's okay.

While at it, replace all uses of ast.IsExported with token.IsExported.
This is more consistent, and also means that the import graphs are
leaner. A couple of files no longer need to import go/ast, for example.

We can't get rid of cmd/compile/internal/types.IsExported, as the
compiler can only depend on go/token as of Go 1.4. However, gc used
different implementations in a couple of places, so consolidate the use
of types.IsExported there.

Finally, we can't get rid of the copied IsExported implementation in
encoding/gob, as go/token depends on it as part of a test. That test
can't be an external test either, so there's no easy way to break the
import cycle.

Overall, this removes about forty lines of unnecessary code.

Change-Id: I86a475b7614261e6a7b0b153d5ca02b9f64a7b2d
Reviewed-on: https://go-review.googlesource.com/c/go/+/172037
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Daniel Martí 2019-04-15 23:10:50 +09:00 committed by Brad Fitzpatrick
parent b39d0eab90
commit 9b968df177
12 changed files with 26 additions and 66 deletions

View File

@ -241,7 +241,7 @@ func (w *Walker) export(pkg *types.Package) {
w.current = pkg w.current = pkg
scope := pkg.Scope() scope := pkg.Scope()
for _, name := range scope.Names() { for _, name := range scope.Names() {
if ast.IsExported(name) { if token.IsExported(name) {
w.emitObj(scope.Lookup(name)) w.emitObj(scope.Lookup(name))
} }
} }

View File

@ -16,8 +16,6 @@ import (
"os" "os"
"reflect" "reflect"
"regexp" "regexp"
"unicode"
"unicode/utf8"
) )
// dump is like fdump but prints to stderr. // dump is like fdump but prints to stderr.
@ -216,7 +214,7 @@ func (p *dumper) dump(x reflect.Value, depth int) {
for i, n := 0, typ.NumField(); i < n; i++ { for i, n := 0, typ.NumField(); i < n; i++ {
// Exclude non-exported fields because their // Exclude non-exported fields because their
// values cannot be accessed via reflection. // values cannot be accessed via reflection.
if name := typ.Field(i).Name; isExported(name) { if name := typ.Field(i).Name; types.IsExported(name) {
if !p.fieldrx.MatchString(name) { if !p.fieldrx.MatchString(name) {
omitted = true omitted = true
continue // field name not selected by filter continue // field name not selected by filter
@ -274,11 +272,6 @@ func isZeroVal(x reflect.Value) bool {
return false return false
} }
func isExported(name string) bool {
ch, _ := utf8.DecodeRuneInString(name)
return unicode.IsUpper(ch)
}
func commonPrefixLen(a, b string) (i int) { func commonPrefixLen(a, b string) (i int) {
for i < len(a) && i < len(b) && a[i] == b[i] { for i < len(a) && i < len(b) && a[i] == b[i] {
i++ i++

View File

@ -206,7 +206,6 @@ import (
"cmd/internal/src" "cmd/internal/src"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"go/ast"
"io" "io"
"math/big" "math/big"
"strings" "strings"
@ -1400,7 +1399,7 @@ func (w *exportWriter) localIdent(s *types.Sym, v int32) {
name = fmt.Sprintf("%s·%d", name, v) name = fmt.Sprintf("%s·%d", name, v)
} }
if !ast.IsExported(name) && s.Pkg != w.currPkg { if !types.IsExported(name) && s.Pkg != w.currPkg {
Fatalf("weird package in name: %v => %v, not %q", s, name, w.currPkg.Path) Fatalf("weird package in name: %v => %v, not %q", s, name, w.currPkg.Path)
} }

View File

@ -49,8 +49,6 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"unicode"
"unicode/utf8"
) )
var ( var (
@ -235,7 +233,7 @@ func parseArgs(args []string) (pkg *build.Package, path, symbol string, more boo
// case letter, it can only be a symbol in the current directory. // case letter, it can only be a symbol in the current directory.
// Kills the problem caused by case-insensitive file systems // Kills the problem caused by case-insensitive file systems
// matching an upper case name as a package name. // matching an upper case name as a package name.
if isUpper(arg) { if token.IsExported(arg) {
pkg, err := build.ImportDir(".", build.ImportComment) pkg, err := build.ImportDir(".", build.ImportComment)
if err == nil { if err == nil {
return pkg, "", arg, false return pkg, "", arg, false
@ -352,19 +350,13 @@ func parseSymbol(str string) (symbol, method string) {
// If the unexported flag (-u) is true, isExported returns true because // If the unexported flag (-u) is true, isExported returns true because
// it means that we treat the name as if it is exported. // it means that we treat the name as if it is exported.
func isExported(name string) bool { func isExported(name string) bool {
return unexported || isUpper(name) return unexported || token.IsExported(name)
}
// isUpper reports whether the name starts with an upper case letter.
func isUpper(name string) bool {
ch, _ := utf8.DecodeRuneInString(name)
return unicode.IsUpper(ch)
} }
// findNextPackage returns the next full file name path that matches the // findNextPackage returns the next full file name path that matches the
// (perhaps partial) package path pkg. The boolean reports if any match was found. // (perhaps partial) package path pkg. The boolean reports if any match was found.
func findNextPackage(pkg string) (string, bool) { func findNextPackage(pkg string) (string, bool) {
if pkg == "" || isUpper(pkg) { // Upper case symbol cannot be a package name. if pkg == "" || token.IsExported(pkg) { // Upper case symbol cannot be a package name.
return "", false return "", false
} }
if filepath.IsAbs(pkg) { if filepath.IsAbs(pkg) {

View File

@ -443,7 +443,7 @@ var pkgDeps = map[string][]string{
}, },
"net/http/httputil": {"L4", "NET", "OS", "context", "net/http", "net/http/internal", "golang.org/x/net/http/httpguts"}, "net/http/httputil": {"L4", "NET", "OS", "context", "net/http", "net/http/internal", "golang.org/x/net/http/httpguts"},
"net/http/pprof": {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"}, "net/http/pprof": {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"},
"net/rpc": {"L4", "NET", "encoding/gob", "html/template", "net/http"}, "net/rpc": {"L4", "NET", "encoding/gob", "html/template", "net/http", "go/token"},
"net/rpc/jsonrpc": {"L4", "NET", "encoding/json", "net/rpc"}, "net/rpc/jsonrpc": {"L4", "NET", "encoding/json", "net/rpc"},
} }

View File

@ -17,7 +17,7 @@ import (
func filterIdentList(list []*ast.Ident) []*ast.Ident { func filterIdentList(list []*ast.Ident) []*ast.Ident {
j := 0 j := 0
for _, x := range list { for _, x := range list {
if ast.IsExported(x.Name) { if token.IsExported(x.Name) {
list[j] = x list[j] = x
j++ j++
} }
@ -59,7 +59,7 @@ func filterExprList(list []ast.Expr, filter Filter, export bool) []ast.Expr {
// and reports whether at least one exported name exists. // and reports whether at least one exported name exists.
func updateIdentList(list []*ast.Ident) (hasExported bool) { func updateIdentList(list []*ast.Ident) (hasExported bool) {
for i, x := range list { for i, x := range list {
if ast.IsExported(x.Name) { if token.IsExported(x.Name) {
hasExported = true hasExported = true
} else { } else {
list[i] = underscore list[i] = underscore
@ -121,7 +121,7 @@ func (r *reader) filterFieldList(parent *namedType, fields *ast.FieldList, ityp
if n := len(field.Names); n == 0 { if n := len(field.Names); n == 0 {
// anonymous field // anonymous field
fname := r.recordAnonymousField(parent, field.Type) fname := r.recordAnonymousField(parent, field.Type)
if ast.IsExported(fname) { if token.IsExported(fname) {
keepField = true keepField = true
} else if ityp != nil && fname == "error" { } else if ityp != nil && fname == "error" {
// possibly the predeclared error interface; keep // possibly the predeclared error interface; keep
@ -199,7 +199,7 @@ func (r *reader) filterSpec(spec ast.Spec) bool {
// always keep imports so we can collect them // always keep imports so we can collect them
return true return true
case *ast.ValueSpec: case *ast.ValueSpec:
s.Values = filterExprList(s.Values, ast.IsExported, true) s.Values = filterExprList(s.Values, token.IsExported, true)
if len(s.Values) > 0 || s.Type == nil && len(s.Values) == 0 { if len(s.Values) > 0 || s.Type == nil && len(s.Values) == 0 {
// If there are values declared on RHS, just replace the unexported // If there are values declared on RHS, just replace the unexported
// identifiers on the LHS with underscore, so that it matches // identifiers on the LHS with underscore, so that it matches
@ -219,7 +219,7 @@ func (r *reader) filterSpec(spec ast.Spec) bool {
} }
} }
case *ast.TypeSpec: case *ast.TypeSpec:
if name := s.Name.Name; ast.IsExported(name) { if name := s.Name.Name; token.IsExported(name) {
r.filterType(r.lookupType(s.Name.Name), s.Type) r.filterType(r.lookupType(s.Name.Name), s.Type)
return true return true
} else if name == "error" { } else if name == "error" {
@ -290,7 +290,7 @@ func (r *reader) filterDecl(decl ast.Decl) bool {
// conflicting method will be filtered here, too - // conflicting method will be filtered here, too -
// thus, removing these methods early will not lead // thus, removing these methods early will not lead
// to the false removal of possible conflicts // to the false removal of possible conflicts
return ast.IsExported(d.Name.Name) return token.IsExported(d.Name.Name)
} }
return false return false
} }

View File

@ -169,7 +169,7 @@ type reader struct {
} }
func (r *reader) isVisible(name string) bool { func (r *reader) isVisible(name string) bool {
return r.mode&AllDecls != 0 || ast.IsExported(name) return r.mode&AllDecls != 0 || token.IsExported(name)
} }
// lookupType returns the base type with the given name. // lookupType returns the base type with the given name.
@ -833,7 +833,7 @@ func sortedFuncs(m methodSet, allMethods bool) []*Func {
switch { switch {
case m.Decl == nil: case m.Decl == nil:
// exclude conflict entry // exclude conflict entry
case allMethods, m.Level == 0, !ast.IsExported(removeStar(m.Orig)): case allMethods, m.Level == 0, !token.IsExported(removeStar(m.Orig)):
// forced inclusion, method not embedded, or method // forced inclusion, method not embedded, or method
// embedded but original receiver type not exported // embedded but original receiver type not exported
list[i] = m list[i] = m

View File

@ -14,8 +14,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"unicode"
"unicode/utf8"
) )
type importer struct { type importer struct {
@ -446,7 +444,7 @@ func (p *importer) typ(parent *types.Package, tname *types.Named) types.Type {
// TODO(gri) replace this with something closer to fieldName // TODO(gri) replace this with something closer to fieldName
pos := p.pos() pos := p.pos()
name := p.string() name := p.string()
if !exported(name) { if !token.IsExported(name) {
p.pkg() p.pkg()
} }
@ -675,7 +673,7 @@ func (p *importer) fieldName(parent *types.Package) (pkg *types.Package, name st
alias = true alias = true
fallthrough fallthrough
default: default:
if !exported(name) { if !token.IsExported(name) {
pkg = p.pkg() pkg = p.pkg()
} }
} }
@ -730,11 +728,6 @@ func (p *importer) param(named bool) (*types.Var, bool) {
return types.NewVar(token.NoPos, pkg, name, t), isddd return types.NewVar(token.NoPos, pkg, name, t), isddd
} }
func exported(name string) bool {
ch, _ := utf8.DecodeRuneInString(name)
return unicode.IsUpper(ch)
}
func (p *importer) value() constant.Value { func (p *importer) value() constant.Value {
switch tag := p.tagOrIndex(); tag { switch tag := p.tagOrIndex(); tag {
case falseTag: case falseTag:

View File

@ -7,7 +7,6 @@ package types
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"go/ast"
"go/constant" "go/constant"
"go/token" "go/token"
) )
@ -59,7 +58,7 @@ type Object interface {
// Id returns name if it is exported, otherwise it // Id returns name if it is exported, otherwise it
// returns the name qualified with the package path. // returns the name qualified with the package path.
func Id(pkg *Package, name string) string { func Id(pkg *Package, name string) string {
if ast.IsExported(name) { if token.IsExported(name) {
return name return name
} }
// unexported names need the package path for differentiation // unexported names need the package path for differentiation
@ -139,7 +138,7 @@ func (obj *object) Type() Type { return obj.typ }
// Exported reports whether the object is exported (starts with a capital letter). // Exported reports whether the object is exported (starts with a capital letter).
// It doesn't take into account whether the object is in a local (function) scope // It doesn't take into account whether the object is in a local (function) scope
// or not. // or not.
func (obj *object) Exported() bool { return ast.IsExported(obj.name) } func (obj *object) Exported() bool { return token.IsExported(obj.name) }
// Id is a wrapper for Id(obj.Pkg(), obj.Name()). // Id is a wrapper for Id(obj.Pkg(), obj.Name()).
func (obj *object) Id() string { return Id(obj.pkg, obj.name) } func (obj *object) Id() string { return Id(obj.pkg, obj.name) }

View File

@ -10,7 +10,7 @@ import (
"compress/gzip" "compress/gzip"
"crypto/rand" "crypto/rand"
"fmt" "fmt"
"go/ast" "go/token"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http/internal" "net/http/internal"
@ -736,7 +736,7 @@ func diff(t *testing.T, prefix string, have, want interface{}) {
} }
for i := 0; i < hv.NumField(); i++ { for i := 0; i < hv.NumField(); i++ {
name := hv.Type().Field(i).Name name := hv.Type().Field(i).Name
if !ast.IsExported(name) { if !token.IsExported(name) {
continue continue
} }
hf := hv.Field(i).Interface() hf := hv.Field(i).Interface()

View File

@ -130,6 +130,7 @@ import (
"bufio" "bufio"
"encoding/gob" "encoding/gob"
"errors" "errors"
"go/token"
"io" "io"
"log" "log"
"net" "net"
@ -137,8 +138,6 @@ import (
"reflect" "reflect"
"strings" "strings"
"sync" "sync"
"unicode"
"unicode/utf8"
) )
const ( const (
@ -202,12 +201,6 @@ func NewServer() *Server {
// DefaultServer is the default instance of *Server. // DefaultServer is the default instance of *Server.
var DefaultServer = NewServer() var DefaultServer = NewServer()
// Is this an exported - upper case - name?
func isExported(name string) bool {
rune, _ := utf8.DecodeRuneInString(name)
return unicode.IsUpper(rune)
}
// Is this type exported or a builtin? // Is this type exported or a builtin?
func isExportedOrBuiltinType(t reflect.Type) bool { func isExportedOrBuiltinType(t reflect.Type) bool {
for t.Kind() == reflect.Ptr { for t.Kind() == reflect.Ptr {
@ -215,7 +208,7 @@ func isExportedOrBuiltinType(t reflect.Type) bool {
} }
// PkgPath will be non-empty even for an exported type, // PkgPath will be non-empty even for an exported type,
// so we need to check the type name as well. // so we need to check the type name as well.
return isExported(t.Name()) || t.PkgPath() == "" return token.IsExported(t.Name()) || t.PkgPath() == ""
} }
// Register publishes in the server the set of methods of the // Register publishes in the server the set of methods of the
@ -251,7 +244,7 @@ func (server *Server) register(rcvr interface{}, name string, useName bool) erro
log.Print(s) log.Print(s)
return errors.New(s) return errors.New(s)
} }
if !isExported(sname) && !useName { if !token.IsExported(sname) && !useName {
s := "rpc.Register: type " + sname + " is not exported" s := "rpc.Register: type " + sname + " is not exported"
log.Print(s) log.Print(s)
return errors.New(s) return errors.New(s)

View File

@ -9,6 +9,7 @@ import (
"encoding/base64" "encoding/base64"
"flag" "flag"
"fmt" "fmt"
"go/token"
"io" "io"
"math" "math"
"math/rand" "math/rand"
@ -22,8 +23,6 @@ import (
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "time"
"unicode"
"unicode/utf8"
"unsafe" "unsafe"
) )
@ -4671,7 +4670,7 @@ func TestStructOfExportRules(t *testing.T) {
if n == "" { if n == "" {
panic("field.Name must not be empty") panic("field.Name must not be empty")
} }
exported := isExported(n) exported := token.IsExported(n)
if exported != test.exported { if exported != test.exported {
t.Errorf("test-%d: got exported=%v want exported=%v", i, exported, test.exported) t.Errorf("test-%d: got exported=%v want exported=%v", i, exported, test.exported)
} }
@ -4679,14 +4678,6 @@ func TestStructOfExportRules(t *testing.T) {
} }
} }
// isExported reports whether name is an exported Go symbol
// (that is, whether it begins with an upper-case letter).
//
func isExported(name string) bool {
ch, _ := utf8.DecodeRuneInString(name)
return unicode.IsUpper(ch)
}
func TestStructOfGC(t *testing.T) { func TestStructOfGC(t *testing.T) {
type T *uintptr type T *uintptr
tt := TypeOf(T(nil)) tt := TypeOf(T(nil))