go/loader: don't treat unsafe specially wrt vendoring

The loader treats GOROOT's "unsafe" package specially,
with no source files and a Package of types.Unsafe.

Tested on Go 1.4.1, 1.5, and ~1.6 (tip).

Fixes issue #13882

Change-Id: I86c4e394665d86a50ec3852d6d702f0e9c5d2276
Reviewed-on: https://go-review.googlesource.com/18457
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Alan Donovan 2016-01-08 14:46:14 -05:00
parent e211dfb000
commit ae186f56a2
6 changed files with 31 additions and 23 deletions

View File

@ -83,7 +83,7 @@ func ExampleConfig_FromArgs() {
// created: []
// imported: [errors runtime unicode/utf8]
// initial: [errors runtime unicode/utf8]
// all: [errors runtime unicode/utf8]
// all: [errors runtime unicode/utf8 unsafe]
}
// This example creates and type-checks a single package (without tests)
@ -139,7 +139,7 @@ func ExampleConfig_CreateFromFiles() {
// created: [hello]
// imported: []
// initial: [hello]
// all: [errors fmt hello io math os reflect runtime strconv sync sync/atomic syscall time unicode/utf8]
// all: [errors fmt hello io math os reflect runtime strconv sync sync/atomic syscall time unicode/utf8 unsafe]
// strconv.Files: [atob.go atof.go atoi.go decimal.go extfloat.go ftoa.go isprint.go itoa.go quote.go]
}
@ -167,7 +167,7 @@ func ExampleConfig_Import() {
// created: [strconv_test]
// imported: [errors strconv unicode/utf8]
// initial: [errors strconv strconv_test unicode/utf8]
// all: [bufio bytes errors flag fmt io math math/rand os reflect runtime runtime/pprof sort strconv strconv_test strings sync sync/atomic syscall testing text/tabwriter time unicode unicode/utf8]
// all: [bufio bytes errors flag fmt io math math/rand os reflect runtime runtime/pprof sort strconv strconv_test strings sync sync/atomic syscall testing text/tabwriter time unicode unicode/utf8 unsafe]
// strconv.Files: [atob.go atof.go atoi.go decimal.go extfloat.go ftoa.go isprint.go itoa.go quote.go internal_test.go]
// strconv_test.Files: [atob_test.go atof_test.go atoi_test.go decimal_test.go fp_test.go ftoa_test.go itoa_test.go quote_example_test.go quote_test.go strconv_test.go]
}

View File

@ -87,7 +87,7 @@ func ExampleConfig_FromArgs() {
// created: []
// imported: [errors runtime unicode/utf8]
// initial: [errors runtime unicode/utf8]
// all: [errors runtime unicode/utf8]
// all: [errors runtime unicode/utf8 unsafe]
}
// This example creates and type-checks a single package (without tests)
@ -143,7 +143,7 @@ func ExampleConfig_CreateFromFiles() {
// created: [hello]
// imported: []
// initial: [hello]
// all: [errors fmt hello io math os reflect runtime strconv sync sync/atomic syscall time unicode/utf8]
// all: [errors fmt hello io math os reflect runtime strconv sync sync/atomic syscall time unicode/utf8 unsafe]
// strconv.Files: [atob.go atof.go atoi.go decimal.go doc.go extfloat.go ftoa.go isprint.go itoa.go quote.go]
}
@ -171,7 +171,7 @@ func ExampleConfig_Import() {
// created: [strconv_test]
// imported: [errors strconv unicode/utf8]
// initial: [errors strconv strconv_test unicode/utf8]
// all: [bufio bytes errors flag fmt io log math math/rand os reflect runtime runtime/pprof runtime/trace sort strconv strconv_test strings sync sync/atomic syscall testing text/tabwriter time unicode unicode/utf8]
// all: [bufio bytes errors flag fmt io log math math/rand os reflect runtime runtime/pprof runtime/trace sort strconv strconv_test strings sync sync/atomic syscall testing text/tabwriter time unicode unicode/utf8 unsafe]
// strconv.Files: [atob.go atof.go atoi.go decimal.go doc.go extfloat.go ftoa.go isprint.go itoa.go quote.go internal_test.go]
// strconv_test.Files: [atob_test.go atof_test.go atoi_test.go decimal_test.go example_test.go fp_test.go ftoa_test.go itoa_test.go quote_test.go strconv_test.go]
}

View File

@ -87,7 +87,7 @@ func ExampleConfig_FromArgs() {
// created: []
// imported: [errors runtime unicode/utf8]
// initial: [errors runtime unicode/utf8]
// all: [errors runtime runtime/internal/atomic runtime/internal/sys unicode/utf8]
// all: [errors runtime runtime/internal/atomic runtime/internal/sys unicode/utf8 unsafe]
}
// This example creates and type-checks a single package (without tests)
@ -143,7 +143,7 @@ func ExampleConfig_CreateFromFiles() {
// created: [hello]
// imported: []
// initial: [hello]
// all: [errors fmt hello internal/race io math os reflect runtime runtime/internal/atomic runtime/internal/sys strconv sync sync/atomic syscall time unicode/utf8]
// all: [errors fmt hello internal/race io math os reflect runtime runtime/internal/atomic runtime/internal/sys strconv sync sync/atomic syscall time unicode/utf8 unsafe]
// strconv.Files: [atob.go atof.go atoi.go decimal.go doc.go extfloat.go ftoa.go isprint.go itoa.go quote.go]
}
@ -171,7 +171,7 @@ func ExampleConfig_Import() {
// created: [strconv_test]
// imported: [errors strconv unicode/utf8]
// initial: [errors strconv strconv_test unicode/utf8]
// all: [bufio bytes errors flag fmt internal/race io log math math/rand os reflect runtime runtime/debug runtime/internal/atomic runtime/internal/sys runtime/pprof runtime/trace sort strconv strconv_test strings sync sync/atomic syscall testing text/tabwriter time unicode unicode/utf8]
// all: [bufio bytes errors flag fmt internal/race io log math math/rand os reflect runtime runtime/debug runtime/internal/atomic runtime/internal/sys runtime/pprof runtime/trace sort strconv strconv_test strings sync sync/atomic syscall testing text/tabwriter time unicode unicode/utf8 unsafe]
// strconv.Files: [atob.go atof.go atoi.go decimal.go doc.go extfloat.go ftoa.go isprint.go itoa.go quote.go internal_test.go]
// strconv_test.Files: [atob_test.go atof_test.go atoi_test.go decimal_test.go example_test.go fp_test.go ftoa_test.go itoa_test.go quote_test.go strconv_test.go]
}

View File

@ -308,7 +308,7 @@ func (conf *Config) ImportWithTests(path string) { conf.addImport(path, true) }
func (conf *Config) Import(path string) { conf.addImport(path, false) }
func (conf *Config) addImport(path string, tests bool) {
if path == "C" || path == "unsafe" {
if path == "C" {
return // ignore; not a real package
}
if conf.ImportPkgs == nil {
@ -718,6 +718,9 @@ func (conf *Config) build() *build.Context {
// 'x': include external *_test.go source files. (XTestGoFiles)
//
func (conf *Config) parsePackageFiles(bp *build.Package, which rune) ([]*ast.File, []error) {
if bp.Goroot && bp.ImportPath == "unsafe" {
return nil, nil
}
var filenames []string
switch which {
case 'g':
@ -756,11 +759,6 @@ func (conf *Config) parsePackageFiles(bp *build.Package, which rune) ([]*ast.Fil
// Idempotent.
//
func (imp *importer) doImport(from *PackageInfo, to string) (*types.Package, error) {
// Package unsafe is handled specially, and has no PackageInfo.
// (Let's assume there is no "vendor/unsafe" package.)
if to == "unsafe" {
return types.Unsafe, nil
}
if to == "C" {
// This should be unreachable, but ad hoc packages are
// not currently subject to cgo preprocessing.
@ -774,6 +772,12 @@ func (imp *importer) doImport(from *PackageInfo, to string) (*types.Package, err
return nil, err
}
// The standard unsafe package is handled specially,
// and has no PackageInfo.
if bp.Goroot && bp.ImportPath == "unsafe" {
return types.Unsafe, nil
}
// Look for the package in the cache using its canonical path.
path := bp.ImportPath
imp.importedMu.Lock()

View File

@ -306,7 +306,7 @@ func (conf *Config) ImportWithTests(path string) { conf.addImport(path, true) }
func (conf *Config) Import(path string) { conf.addImport(path, false) }
func (conf *Config) addImport(path string, tests bool) {
if path == "C" || path == "unsafe" {
if path == "C" {
return // ignore; not a real package
}
if conf.ImportPkgs == nil {
@ -714,6 +714,9 @@ func (conf *Config) build() *build.Context {
// 'x': include external *_test.go source files. (XTestGoFiles)
//
func (conf *Config) parsePackageFiles(bp *build.Package, which rune) ([]*ast.File, []error) {
if bp.Goroot && bp.ImportPath == "unsafe" {
return nil, nil
}
var filenames []string
switch which {
case 'g':
@ -752,11 +755,6 @@ func (conf *Config) parsePackageFiles(bp *build.Package, which rune) ([]*ast.Fil
// Idempotent.
//
func (imp *importer) doImport(from *PackageInfo, to string) (*types.Package, error) {
// Package unsafe is handled specially, and has no PackageInfo.
// (Let's assume there is no "vendor/unsafe" package.)
if to == "unsafe" {
return types.Unsafe, nil
}
if to == "C" {
// This should be unreachable, but ad hoc packages are
// not currently subject to cgo preprocessing.
@ -770,6 +768,12 @@ func (imp *importer) doImport(from *PackageInfo, to string) (*types.Package, err
return nil, err
}
// The standard unsafe package is handled specially,
// and has no PackageInfo.
if bp.Goroot && bp.ImportPath == "unsafe" {
return types.Unsafe, nil
}
// Look for the package in the cache using its canonical path.
path := bp.ImportPath
imp.importedMu.Lock()

View File

@ -103,8 +103,8 @@ func scanImports(files []*ast.File) map[string]bool {
if err != nil {
continue // quietly ignore the error
}
if path == "C" || path == "unsafe" {
continue // skip pseudo packages
if path == "C" {
continue // skip pseudopackage
}
imports[path] = true
}