diff options
| author | Daniel Martí <mvdan@mvdan.cc> | 2019-04-15 23:10:50 +0900 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2019-04-16 14:43:48 +0000 |
| commit | 9b968df17782f21cc0af14c9d3c0bcf4cf3f911f (patch) | |
| tree | 0240ff2603a12a7f9206b27967b8a60b992110cb /src/cmd | |
| parent | b39d0eab902cb6b90aa99bcf11ca622c00219c7c (diff) | |
| download | go-9b968df17782f21cc0af14c9d3c0bcf4cf3f911f.tar.xz | |
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>
Diffstat (limited to 'src/cmd')
| -rw-r--r-- | src/cmd/api/goapi.go | 2 | ||||
| -rw-r--r-- | src/cmd/compile/internal/gc/dump.go | 9 | ||||
| -rw-r--r-- | src/cmd/compile/internal/gc/iexport.go | 3 | ||||
| -rw-r--r-- | src/cmd/doc/main.go | 14 |
4 files changed, 6 insertions, 22 deletions
diff --git a/src/cmd/api/goapi.go b/src/cmd/api/goapi.go index 1a0242f60c..b728baea1d 100644 --- a/src/cmd/api/goapi.go +++ b/src/cmd/api/goapi.go @@ -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)) } } diff --git a/src/cmd/compile/internal/gc/dump.go b/src/cmd/compile/internal/gc/dump.go index 8de90adf05..29eb1c1e48 100644 --- a/src/cmd/compile/internal/gc/dump.go +++ b/src/cmd/compile/internal/gc/dump.go @@ -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++ diff --git a/src/cmd/compile/internal/gc/iexport.go b/src/cmd/compile/internal/gc/iexport.go index d50d3e9400..93099bfe3d 100644 --- a/src/cmd/compile/internal/gc/iexport.go +++ b/src/cmd/compile/internal/gc/iexport.go @@ -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) } diff --git a/src/cmd/doc/main.go b/src/cmd/doc/main.go index 9b24c5874f..9e3ad0c0e7 100644 --- a/src/cmd/doc/main.go +++ b/src/cmd/doc/main.go @@ -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) { |
