Skip to content

protoc-gen-go: predeclared identifiers in cleanPackageName#722

Merged
neild merged 1 commit intogolang:masterfrom
greenhouse-org:predeclared
Oct 3, 2018
Merged

protoc-gen-go: predeclared identifiers in cleanPackageName#722
neild merged 1 commit intogolang:masterfrom
greenhouse-org:predeclared

Conversation

@sesmith177
Copy link
Copy Markdown
Contributor

Don't generate identifiers that conflict with predeclared identifiers, prepend with "_" instead. This matches the behavior for package names that are Go keywords.

We ran into this issue during envoyproxy/envoy#4556. Code with import statement:

import string "github.com/envoyproxy/data-plane-api/api/string"

was being generated and would not compile.

The list of predeclared indentifiers was taken from: https://golang.org/ref/spec#Predeclared_identifiers

Don't generate identifiers that conflict with predeclared identifiers,
prepend with "_" instead.

Signed-off-by: Arjun Sreedharan <asreedharan@pivotal.io>
@neild neild merged commit 31e0d06 into golang:master Oct 3, 2018
@dsnet
Copy link
Copy Markdown
Member

dsnet commented Oct 3, 2018

While naming a package name as a built-in identifier is not great, isn't this a breaking change?

@neild
Copy link
Copy Markdown
Contributor

neild commented Oct 4, 2018

In the case of a package with a name that conflicts with a predefined identifier, that is imported without a local renaming, and that doesn't conflict with any identifier used in the places it is imported, this could be a breaking change.

Testing the change inside Google's codebase isn't likely to turn up anything broken by this, because our local style is to always rename imported proto packages.

I think generating a local rename that conflicts with a predefined identifier (e.g., import string "...") is obviously wrong. We could keep the previous behavior for determining package names (package string is probably a bad idea, but it'll compile) and apply the new one for local renames only.

@dsnet
Copy link
Copy Markdown
Member

dsnet commented Oct 4, 2018

apply the new one for local renames only.

SGTM.

We could keep the previous behavior for determining package names (package string is probably a bad idea, but it'll compile)

I believe we should keep the existing logic for determining package names.

In most cases, the user should specify the go_package option, where they manually determine what the Go package path and name should be. If they really wanted to name a package the same as a built-in identifier, we should preserve that behavior. I find it odd that we already do magical name cleanups for a string that the user explicitly sets. In my opinion, if they put an invalid package name (perhaps a typo), we should have avoided magically cleaning it up for them, and let the Go compiler complain. I'm not sure adding more magical sanitization logic is a good idea.

Also, I can understand hardcoding the keywords since they cannot be used as identifiers and it is very unlikely that we'll add new keywords even in Go2 (at least the generics proposal seems to make a conscious effort to avoid a new keyword). However, it seems more plausible that new built-in identifiers may be added to Go over time, which would presumably be added to the backlist. The idea that package names can can change over time is concerning.

dsnet added a commit that referenced this pull request Nov 27, 2018
Don't generate identifiers that conflict with predeclared identifiers,
prepend with "_" instead.

Change-Id: I85e29d1861bc225df29949b26f4ae4a740bb4a66
Cherry-Pick: github.com/golang/protobuf@31e0d063dd98c052257e5b69eeb006818133f45c
Original-Author: Sam Smith <sesmith177@gmail.com>
Reviewed-on: https://go-review.googlesource.com/c/151423
Reviewed-by: Damien Neil <dneil@google.com>
@golang golang locked and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants