1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:14:46 -07:00

go.tools/go/types: package name must not be blank

Fixes golang/go#8077.

LGTM=adonovan
R=golang-codereviews, adonovan
CC=golang-codereviews
https://golang.org/cl/91640043
This commit is contained in:
Robert Griesemer 2014-05-22 13:45:29 -07:00
parent 2afc128b30
commit 8df7a779db
4 changed files with 16 additions and 2 deletions

View File

@ -162,7 +162,11 @@ func (check *checker) initFiles(files []*ast.File) {
for i, file := range files {
switch name := file.Name.Name; pkg.name {
case "":
pkg.name = name
if name != "_" {
pkg.name = name
} else {
check.errorf(file.Name.Pos(), "invalid package name _")
}
fallthrough
case name:

View File

@ -79,6 +79,7 @@ var tests = [][]string{
{"testdata/gotos.src"},
{"testdata/labels.src"},
{"testdata/issues.src"},
{"testdata/blank.src"},
}
var fset = token.NewFileSet()

View File

@ -16,9 +16,13 @@ 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.
// NewPackage returns a new Package for the given package path and name;
// the name must not be the blank identifier.
// 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, fmt.Sprintf("package %q", path))
return &Package{path: path, name: name, scope: scope}
}

5
go/types/testdata/blank.src vendored Normal file
View File

@ -0,0 +1,5 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package _ /* ERROR invalid package name */