diff --git a/protogen/names.go b/protogen/names.go index e291805f..a1bddf7b 100644 --- a/protogen/names.go +++ b/protogen/names.go @@ -83,48 +83,6 @@ func cleanGoName(s string, mustExport bool) string { return s } -var isGoPredeclaredIdentifier = map[string]bool{ - "append": true, - "bool": true, - "byte": true, - "cap": true, - "close": true, - "complex": true, - "complex128": true, - "complex64": true, - "copy": true, - "delete": true, - "error": true, - "false": true, - "float32": true, - "float64": true, - "imag": true, - "int": true, - "int16": true, - "int32": true, - "int64": true, - "int8": true, - "iota": true, - "len": true, - "make": true, - "new": true, - "nil": true, - "panic": true, - "print": true, - "println": true, - "real": true, - "recover": true, - "rune": true, - "string": true, - "true": true, - "uint": true, - "uint16": true, - "uint32": true, - "uint64": true, - "uint8": true, - "uintptr": true, -} - // baseName returns the last path element of the name, with the last dotted suffix removed. func baseName(name string) string { // First, find the last element diff --git a/protogen/protogen.go b/protogen/protogen.go index 9c279b83..eb89cfa5 100644 --- a/protogen/protogen.go +++ b/protogen/protogen.go @@ -19,6 +19,7 @@ import ( "go/parser" "go/printer" "go/token" + "go/types" "io/ioutil" "os" "path" @@ -851,6 +852,12 @@ func (gen *Plugin) NewGeneratedFile(filename string, goImportPath GoImportPath) manualImports: make(map[GoImportPath]bool), annotations: make(map[string][]Location), } + + // All predeclared identifiers in Go are already used. + for _, s := range types.Universe.Names() { + g.usedPackageNames[GoPackageName(s)] = true + } + gen.genFiles = append(gen.genFiles, g) return g } @@ -906,7 +913,7 @@ func (g *GeneratedFile) QualifiedGoIdent(ident GoIdent) string { return string(packageName) + "." + ident.GoName } packageName := cleanPackageName(baseName(string(ident.GoImportPath))) - for i, orig := 1, packageName; g.usedPackageNames[packageName] || isGoPredeclaredIdentifier[string(packageName)]; i++ { + for i, orig := 1, packageName; g.usedPackageNames[packageName]; i++ { packageName = orig + GoPackageName(strconv.Itoa(i)) } g.packageNames[ident.GoImportPath] = packageName