diff --git a/go/types/check.go b/go/types/check.go index 378e44c30d..480c4b16f5 100644 --- a/go/types/check.go +++ b/go/types/check.go @@ -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: diff --git a/go/types/check_test.go b/go/types/check_test.go index 619e1f1e64..cfea1ac994 100644 --- a/go/types/check_test.go +++ b/go/types/check_test.go @@ -79,6 +79,7 @@ var tests = [][]string{ {"testdata/gotos.src"}, {"testdata/labels.src"}, {"testdata/issues.src"}, + {"testdata/blank.src"}, } var fset = token.NewFileSet() diff --git a/go/types/package.go b/go/types/package.go index c29dc252ac..366ca3948d 100644 --- a/go/types/package.go +++ b/go/types/package.go @@ -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} } diff --git a/go/types/testdata/blank.src b/go/types/testdata/blank.src new file mode 100644 index 0000000000..6a2507f482 --- /dev/null +++ b/go/types/testdata/blank.src @@ -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 */