]> Cypherpunks repositories - gostls13.git/commitdiff
all: clean up code with token.IsExported
authorDaniel Martí <mvdan@mvdan.cc>
Mon, 15 Apr 2019 14:10:50 +0000 (23:10 +0900)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 16 Apr 2019 14:43:48 +0000 (14:43 +0000)
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>
12 files changed:
src/cmd/api/goapi.go
src/cmd/compile/internal/gc/dump.go
src/cmd/compile/internal/gc/iexport.go
src/cmd/doc/main.go
src/go/build/deps_test.go
src/go/doc/exports.go
src/go/doc/reader.go
src/go/internal/gcimporter/bimport.go
src/go/types/object.go
src/net/http/response_test.go
src/net/rpc/server.go
src/reflect/all_test.go

index 1a0242f60c6df5cdf77588899b5647830183267d..b728baea1d70770a8e890e011534f7a022c386e9 100644 (file)
@@ -241,7 +241,7 @@ func (w *Walker) export(pkg *types.Package) {
        w.current = pkg
        scope := pkg.Scope()
        for _, name := range scope.Names() {
-               if ast.IsExported(name) {
+               if token.IsExported(name) {
                        w.emitObj(scope.Lookup(name))
                }
        }
index 8de90adf05714ec7c8dcf1647dae897b93aa588c..29eb1c1e48b6cf9f7b165c2ce6331b74352ace4e 100644 (file)
@@ -16,8 +16,6 @@ import (
        "os"
        "reflect"
        "regexp"
-       "unicode"
-       "unicode/utf8"
 )
 
 // 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++ {
                        // Exclude non-exported fields because their
                        // 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) {
                                        omitted = true
                                        continue // field name not selected by filter
@@ -274,11 +272,6 @@ func isZeroVal(x reflect.Value) bool {
        return false
 }
 
-func isExported(name string) bool {
-       ch, _ := utf8.DecodeRuneInString(name)
-       return unicode.IsUpper(ch)
-}
-
 func commonPrefixLen(a, b string) (i int) {
        for i < len(a) && i < len(b) && a[i] == b[i] {
                i++
index d50d3e94007b71bd0dfbe2e24ffb18cc463457a8..93099bfe3d1c64bce116fc8c86894432b46ec114 100644 (file)
@@ -206,7 +206,6 @@ import (
        "cmd/internal/src"
        "encoding/binary"
        "fmt"
-       "go/ast"
        "io"
        "math/big"
        "strings"
@@ -1400,7 +1399,7 @@ func (w *exportWriter) localIdent(s *types.Sym, v int32) {
                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)
        }
 
index 9b24c5874f013640392b1d54524743e3386cc9ec..9e3ad0c0e789594242925a0acc4413db8e86e873 100644 (file)
@@ -49,8 +49,6 @@ import (
        "path"
        "path/filepath"
        "strings"
-       "unicode"
-       "unicode/utf8"
 )
 
 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.
        // Kills the problem caused by case-insensitive file systems
        // matching an upper case name as a package name.
-       if isUpper(arg) {
+       if token.IsExported(arg) {
                pkg, err := build.ImportDir(".", build.ImportComment)
                if err == nil {
                        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
 // it means that we treat the name as if it is exported.
 func isExported(name string) bool {
-       return unexported || isUpper(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)
+       return unexported || token.IsExported(name)
 }
 
 // findNextPackage returns the next full file name path that matches the
 // (perhaps partial) package path pkg. The boolean reports if any match was found.
 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
        }
        if filepath.IsAbs(pkg) {
index 853a7e64c828cc515914751da180187b1a3c4d15..c81d313b72aab9153a9464513a475faa75096d56 100644 (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/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"},
 }
 
index 5f99bf7772c175e8376833337605e6578ed4b709..819c030c9bf51c9f7f36f50a20688df98bd04c14 100644 (file)
@@ -17,7 +17,7 @@ import (
 func filterIdentList(list []*ast.Ident) []*ast.Ident {
        j := 0
        for _, x := range list {
-               if ast.IsExported(x.Name) {
+               if token.IsExported(x.Name) {
                        list[j] = x
                        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.
 func updateIdentList(list []*ast.Ident) (hasExported bool) {
        for i, x := range list {
-               if ast.IsExported(x.Name) {
+               if token.IsExported(x.Name) {
                        hasExported = true
                } else {
                        list[i] = underscore
@@ -121,7 +121,7 @@ func (r *reader) filterFieldList(parent *namedType, fields *ast.FieldList, ityp
                if n := len(field.Names); n == 0 {
                        // anonymous field
                        fname := r.recordAnonymousField(parent, field.Type)
-                       if ast.IsExported(fname) {
+                       if token.IsExported(fname) {
                                keepField = true
                        } else if ityp != nil && fname == "error" {
                                // 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
                return true
        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 there are values declared on RHS, just replace the unexported
                        // 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:
-               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)
                        return true
                } else if name == "error" {
@@ -290,7 +290,7 @@ func (r *reader) filterDecl(decl ast.Decl) bool {
                // conflicting method will be filtered here, too -
                // thus, removing these methods early will not lead
                // to the false removal of possible conflicts
-               return ast.IsExported(d.Name.Name)
+               return token.IsExported(d.Name.Name)
        }
        return false
 }
index 49d2af771a09cccdbb834befd2028a2f730e4e3c..c277b35e89420acfe14bd95ee5a6af7393e91ded 100644 (file)
@@ -169,7 +169,7 @@ type reader struct {
 }
 
 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.
@@ -833,7 +833,7 @@ func sortedFuncs(m methodSet, allMethods bool) []*Func {
                switch {
                case m.Decl == nil:
                        // 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
                        // embedded but original receiver type not exported
                        list[i] = m
index 4e3023b906e031db38a111053922a9c68b559950..cf03632aa201bd923cc4f43d454b13c212c5cd57 100644 (file)
@@ -14,8 +14,6 @@ import (
        "strconv"
        "strings"
        "sync"
-       "unicode"
-       "unicode/utf8"
 )
 
 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
                        pos := p.pos()
                        name := p.string()
-                       if !exported(name) {
+                       if !token.IsExported(name) {
                                p.pkg()
                        }
 
@@ -675,7 +673,7 @@ func (p *importer) fieldName(parent *types.Package) (pkg *types.Package, name st
                alias = true
                fallthrough
        default:
-               if !exported(name) {
+               if !token.IsExported(name) {
                        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
 }
 
-func exported(name string) bool {
-       ch, _ := utf8.DecodeRuneInString(name)
-       return unicode.IsUpper(ch)
-}
-
 func (p *importer) value() constant.Value {
        switch tag := p.tagOrIndex(); tag {
        case falseTag:
index cf773238a0df6e023c0d1933763737597b854ec4..374b24d1acfa03e47da92ba7dd49f9e5297db13c 100644 (file)
@@ -7,7 +7,6 @@ package types
 import (
        "bytes"
        "fmt"
-       "go/ast"
        "go/constant"
        "go/token"
 )
@@ -59,7 +58,7 @@ type Object interface {
 // Id returns name if it is exported, otherwise it
 // returns the name qualified with the package path.
 func Id(pkg *Package, name string) string {
-       if ast.IsExported(name) {
+       if token.IsExported(name) {
                return name
        }
        // 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).
 // It doesn't take into account whether the object is in a local (function) scope
 // 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()).
 func (obj *object) Id() string { return Id(obj.pkg, obj.name) }
index c46f13f7988334e7697c9bcc13d6cf815693aee1..ee7f0d0b708ffc6233a740b289f15fe4fbeb7a9d 100644 (file)
@@ -10,7 +10,7 @@ import (
        "compress/gzip"
        "crypto/rand"
        "fmt"
-       "go/ast"
+       "go/token"
        "io"
        "io/ioutil"
        "net/http/internal"
@@ -736,7 +736,7 @@ func diff(t *testing.T, prefix string, have, want interface{}) {
        }
        for i := 0; i < hv.NumField(); i++ {
                name := hv.Type().Field(i).Name
-               if !ast.IsExported(name) {
+               if !token.IsExported(name) {
                        continue
                }
                hf := hv.Field(i).Interface()
index 7bb6476ffab596c6c297281912a6dafb40c3856e..9cb928240f1c4de331eee28f3b82953ccaf68b47 100644 (file)
@@ -130,6 +130,7 @@ import (
        "bufio"
        "encoding/gob"
        "errors"
+       "go/token"
        "io"
        "log"
        "net"
@@ -137,8 +138,6 @@ import (
        "reflect"
        "strings"
        "sync"
-       "unicode"
-       "unicode/utf8"
 )
 
 const (
@@ -202,12 +201,6 @@ func NewServer() *Server {
 // DefaultServer is the default instance of *Server.
 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?
 func isExportedOrBuiltinType(t reflect.Type) bool {
        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,
        // 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
@@ -251,7 +244,7 @@ func (server *Server) register(rcvr interface{}, name string, useName bool) erro
                log.Print(s)
                return errors.New(s)
        }
-       if !isExported(sname) && !useName {
+       if !token.IsExported(sname) && !useName {
                s := "rpc.Register: type " + sname + " is not exported"
                log.Print(s)
                return errors.New(s)
index cbf0f5a93fe049296f189c89f7c38974c174d329..964d8c6e951e09bfd64ff349e2413cffce672683 100644 (file)
@@ -9,6 +9,7 @@ import (
        "encoding/base64"
        "flag"
        "fmt"
+       "go/token"
        "io"
        "math"
        "math/rand"
@@ -22,8 +23,6 @@ import (
        "sync/atomic"
        "testing"
        "time"
-       "unicode"
-       "unicode/utf8"
        "unsafe"
 )
 
@@ -4671,7 +4670,7 @@ func TestStructOfExportRules(t *testing.T) {
                        if n == "" {
                                panic("field.Name must not be empty")
                        }
-                       exported := isExported(n)
+                       exported := token.IsExported(n)
                        if 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) {
        type T *uintptr
        tt := TypeOf(T(nil))