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>
diff --git a/go/loader/loader.go b/go/loader/loader.go
index 6671352..19e2bbd 100644
--- a/go/loader/loader.go
+++ b/go/loader/loader.go
@@ -308,7 +308,7 @@
 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 @@
 //    '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 @@
 // 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 @@
 		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()