From 2eeaba4172522783f30440bf6e11f7f865116024 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Fri, 5 May 2017 13:57:22 -0700 Subject: [PATCH] go/types: remove invalid documentation and assertion on package names 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 TryBot-Result: Gobot Gobot Reviewed-by: Alan Donovan --- src/go/types/package.go | 6 +----- src/go/types/resolver.go | 6 ++++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/go/types/package.go b/src/go/types/package.go index 9828909dfa..cd202a0ed9 100644 --- a/src/go/types/package.go +++ b/src/go/types/package.go @@ -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} } diff --git a/src/go/types/resolver.go b/src/go/types/resolver.go index 04389916f9..05603b3442 100644 --- a/src/go/types/resolver.go +++ b/src/go/types/resolver.go @@ -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 { -- 2.50.0