summaryrefslogtreecommitdiffhomepage
path: root/tools/go_marshal/gomarshal/generator.go
diff options
context:
space:
mode:
authorRahat Mahmood <rahat@google.com>2020-04-01 00:42:34 -0700
committergVisor bot <gvisor-bot@google.com>2020-04-01 00:43:55 -0700
commit507f997213d4b6778c5da982dd447044b769e7b9 (patch)
tree5da2ec5b38eb2f4fe7baf0978a00947d66754b75 /tools/go_marshal/gomarshal/generator.go
parent840980aeba0b5224b13bcaadf5785ac5305a5230 (diff)
go-marshal: Improve collision detection of import statments.
Previously, the import statement collision detection mechanism aborted go-marshal whenever it detected two imports in any package that has the same local name. Consider this trivial package, defined by the the following two source files: file1.go: package example import ( path/a/to/foo ) ... file2.go: package example import ( another/package/with/final/component/foo ) ... Go-marshal previously couldn't handle generating code for the the above package, even if none of the types marked for marshalling used either of the imported foo packages. This turns out to be too restrictive as we run into this a lot in practice. Examples include "encoding/binary" vs "gvisor/pkg/binary/binary", and "sync" vs "gvisor/pkg/sync/sync". This change allows go-marshal to proceed with marshalling, and only abort if the code generated by go-marshal references any such ambiguous import names. PiperOrigin-RevId: 304131190
Diffstat (limited to 'tools/go_marshal/gomarshal/generator.go')
-rw-r--r--tools/go_marshal/gomarshal/generator.go4
1 files changed, 2 insertions, 2 deletions
diff --git a/tools/go_marshal/gomarshal/generator.go b/tools/go_marshal/gomarshal/generator.go
index 935a36b25..43e668b63 100644
--- a/tools/go_marshal/gomarshal/generator.go
+++ b/tools/go_marshal/gomarshal/generator.go
@@ -326,7 +326,7 @@ func (g *Generator) collectImports(a *ast.File, f *token.FileSet) map[string]imp
// Make sure we have an import that doesn't use any local names that
// would conflict with identifiers in the generated code.
- if len(i.name) == 1 {
+ if len(i.name) == 1 && i.name != "_" {
abortAt(f.Position(spec.Pos()), fmt.Sprintf("Import has a single character local name '%s'; this may conflict with code generated by go_marshal, use a multi-character import alias", i.name))
}
if _, ok := badIdentsMap[i.name]; ok {
@@ -421,7 +421,7 @@ func (g *Generator) Run() error {
// the list of imports we need to copy to the generated code.
for name, _ := range impl.is {
if !g.imports.markUsed(name) {
- panic(fmt.Sprintf("Generated code for '%s' referenced a non-existent import with local name '%s'", impl.typeName(), name))
+ panic(fmt.Sprintf("Generated code for '%s' referenced a non-existent import with local name '%s'. Either go-marshal needs to add an import to the generated file, or a package in an input source file has a package name differ from the final component of its path, which go-marshal doesn't know how to detect; use an import alias to work around this limitation.", impl.typeName(), name))
}
}
ts = append(ts, g.generateOneTestSuite(t))