]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: remove invalid documentation and assertion on package names
authorRobert Griesemer <gri@golang.org>
Fri, 5 May 2017 20:57:22 +0000 (13:57 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 5 May 2017 23:03:50 +0000 (23:03 +0000)
NewPackage required through documentation that the package name not
be blank (which wasn't true since each time we check a new package
we create one with a blank name (api.go:350). NewPackage also asserted
that a package name not be "_". While it is invalid for a package name
to be "_", one could conceivably create a package named "_" through
export data manipulation. Furthermore, it is ok to import a package
with package path "_" as long as the package itself is not named "_".

- removed misleading documentation
- removed unnecessary assertion
- added safety checks when we actually do the import

Fixes #20231.

Change-Id: I1eb1ab7b5e3130283db715374770cf05d749d159
Reviewed-on: https://go-review.googlesource.com/42852
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/types/package.go
src/go/types/resolver.go

index 9828909dfa0d1d71dc470c01448db51531704c43..cd202a0ed9f789c558c69c60ef6ed0ecbab966a9 100644 (file)
@@ -19,13 +19,9 @@ type Package struct {
        fake     bool // scope lookup errors are silently dropped if package is fake (internal use only)
 }
 
-// NewPackage returns a new Package for the given package path and name;
-// the name must not be the blank identifier.
+// NewPackage returns a new Package for the given package path and name.
 // The package is not complete and contains no explicit imports.
 func NewPackage(path, name string) *Package {
-       if name == "_" {
-               panic("invalid package name _")
-       }
        scope := NewScope(Universe, token.NoPos, token.NoPos, fmt.Sprintf("package %q", path))
        return &Package{path: path, name: name, scope: scope}
 }
index 04389916f94da8b682e7d21cdc86cced22225e64..05603b3442d00db23436bad759e4d0cf9e7400e5 100644 (file)
@@ -157,6 +157,12 @@ func (check *Checker) importPackage(pos token.Pos, path, dir string) *Package {
                                err = fmt.Errorf("Config.Importer.Import(%s) returned nil but no error", path)
                        }
                }
+               // make sure we have a valid package name
+               // (errors here can only happen through manipulation of packages after creation)
+               if err == nil && imp != nil && (imp.name == "_" || imp.name == "") {
+                       err = fmt.Errorf("invalid package name: %q", imp.name)
+                       imp = nil // create fake package below
+               }
                if err != nil {
                        check.errorf(pos, "could not import %s (%s)", path, err)
                        if imp == nil {