go/loader: don't create types.Package for "unsafe"

https://golang.org/cl/37694 is a backwards-incompatible change to the
go/types API that causes "unsafe" to be included among the results of
(*types.Package).Imports().  Client packages such as go/loader and
go/ssa now need to recognize the special unsafe package and its
unusual *Bultin members and not create a types.Package for it.

Change-Id: I902725d3053cb887246978efdb5ec5dcdc0e4818
Reviewed-on: https://go-review.googlesource.com/37710
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Alan Donovan 2017-03-02 16:31:04 -05:00
parent 1dce95f761
commit 7a49e427c8
2 changed files with 16 additions and 2 deletions

View File

@ -1009,6 +1009,10 @@ func (imp *importer) addFiles(info *PackageInfo, files []*ast.File, cycleCheck b
time.Since(imp.start), info.Pkg.Path(), len(files))
}
if info.Pkg == types.Unsafe && len(files) > 0 {
panic(`addFiles("unsafe") not permitted`)
}
// Ignore the returned (first) error since we
// already collect them all in the PackageInfo.
info.checker.Files(files)
@ -1025,7 +1029,12 @@ func (imp *importer) addFiles(info *PackageInfo, files []*ast.File, cycleCheck b
}
func (imp *importer) newPackageInfo(path, dir string) *PackageInfo {
pkg := types.NewPackage(path, "")
var pkg *types.Package
if path == "unsafe" {
pkg = types.Unsafe
} else {
pkg = types.NewPackage(path, "")
}
info := &PackageInfo{
Pkg: pkg,
Info: types.Info{

View File

@ -49,6 +49,11 @@ func NewProgram(fset *token.FileSet, mode BuilderMode) *Program {
func memberFromObject(pkg *Package, obj types.Object, syntax ast.Node) {
name := obj.Name()
switch obj := obj.(type) {
case *types.Builtin:
if pkg.Pkg != types.Unsafe {
panic("unexpected builtin object: " + obj.String())
}
case *types.TypeName:
pkg.Members[name] = &Type{
object: obj,
@ -187,7 +192,7 @@ func (prog *Program) CreatePackage(pkg *types.Package, files []*ast.File, info *
}
}
} else {
// GC-compiled binary package.
// GC-compiled binary package (or "unsafe")
// No code.
// No position information.
scope := p.Pkg.Scope()