Skip to content

Commit 31e0d06

Browse files
sesmith177neild
authored andcommitted
protoc-gen-go: predeclared identifiers in cleanPackageName (#722)
Don't generate identifiers that conflict with predeclared identifiers, prepend with "_" instead. Signed-off-by: Arjun Sreedharan <[email protected]>
1 parent 7011d38 commit 31e0d06

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

protoc-gen-go/generator/generator.go

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,8 @@ func RegisterUniquePackageName(pkg string, f *FileDescriptor) string {
567567
return string(name)
568568
}
569569

570-
var isGoKeyword = map[string]bool{
570+
var isGoKeywordOrPredeclaredIdentifier = map[string]bool{
571+
// Keywords
571572
"break": true,
572573
"case": true,
573574
"chan": true,
@@ -593,12 +594,53 @@ var isGoKeyword = map[string]bool{
593594
"switch": true,
594595
"type": true,
595596
"var": true,
597+
598+
// Predeclared Identifiers
599+
"append": true,
600+
"bool": true,
601+
"byte": true,
602+
"cap": true,
603+
"close": true,
604+
"complex": true,
605+
"complex128": true,
606+
"complex64": true,
607+
"copy": true,
608+
"delete": true,
609+
"error": true,
610+
"false": true,
611+
"float32": true,
612+
"float64": true,
613+
"imag": true,
614+
"int": true,
615+
"int16": true,
616+
"int32": true,
617+
"int64": true,
618+
"int8": true,
619+
"iota": true,
620+
"len": true,
621+
"make": true,
622+
"new": true,
623+
"nil": true,
624+
"panic": true,
625+
"print": true,
626+
"println": true,
627+
"real": true,
628+
"recover": true,
629+
"rune": true,
630+
"string": true,
631+
"true": true,
632+
"uint": true,
633+
"uint16": true,
634+
"uint32": true,
635+
"uint64": true,
636+
"uint8": true,
637+
"uintptr": true,
596638
}
597639

598640
func cleanPackageName(name string) GoPackageName {
599641
name = strings.Map(badToUnderscore, name)
600-
// Identifier must not be keyword: insert _.
601-
if isGoKeyword[name] {
642+
// Identifier must not be keyword or predeclared identifier: insert _.
643+
if isGoKeywordOrPredeclaredIdentifier[name] {
602644
name = "_" + name
603645
}
604646
// Identifier must not begin with digit: insert _.

protoc-gen-go/generator/name_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func TestGoPackageOption(t *testing.T) {
6868
{"foo", "", "foo", true},
6969
{"github.com/golang/bar", "github.com/golang/bar", "bar", true},
7070
{"github.com/golang/bar;baz", "github.com/golang/bar", "baz", true},
71+
{"github.com/golang/string", "github.com/golang/string", "_string", true},
7172
}
7273
for _, tc := range tests {
7374
d := &FileDescriptor{

0 commit comments

Comments
 (0)