]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: consistent unified IR handling of package unsafe
authorMatthew Dempsky <mdempsky@google.com>
Fri, 29 Apr 2022 17:47:57 +0000 (10:47 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 29 Apr 2022 18:35:07 +0000 (18:35 +0000)
Within the unified IR export format, I was treating package unsafe as
a normal package, but expecting importers to correctly handle
deduplicating it against their respective representation of package
unsafe.

However, the surrounding importer logic differs slightly between
cmd/compile/internal/noder (which unified IR was originally
implemented against) and go/importer (which it was more recently
ported to). In particular, noder initializes its packages map as
`map[string]*types2.Package{"unsafe": types2.Unsafe}`, whereas
go/importer initializes it as just `make(map[string]*types.Package)`.

This CL makes them all consistent. In particular, it:

1. changes noder to initialize packages to an empty map to prevent
further latent issues from the discrepency,

2. adds the same special handling of package unsafe already present in
go/internal/gcimporter's unified IR reader to both of cmd/compile's
implementations, and

3. changes the unified IR writer to treat package unsafe as a builtin
package, to force that readers similarly handle it correctly.

Fixes #52623.

Change-Id: Ibbab9b0a1d2a52d4cc91b56c5df49deedf81295a
Reviewed-on: https://go-review.googlesource.com/c/go/+/403196
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/compile/internal/importer/ureader.go
src/cmd/compile/internal/noder/irgen.go
src/cmd/compile/internal/noder/reader.go
src/cmd/compile/internal/noder/writer.go
src/go/internal/gcimporter/ureader.go

index a22cd2bb5317db0f334852e455c29d499c119e02..b8938cd2d678d2fc187c4ca97f3dfce781be5724 100644 (file)
@@ -148,11 +148,13 @@ func (pr *pkgReader) pkgIdx(idx int) *types2.Package {
 
 func (r *reader) doPkg() *types2.Package {
        path := r.String()
-       if path == "builtin" {
-               return nil // universe
-       }
-       if path == "" {
+       switch path {
+       case "":
                path = r.p.PkgPath()
+       case "builtin":
+               return nil // universe
+       case "unsafe":
+               return types2.Unsafe
        }
 
        if pkg := r.p.imports[path]; pkg != nil {
@@ -371,7 +373,7 @@ func (pr *pkgReader) objIdx(idx int) (*types2.Package, string) {
        tag := pkgbits.CodeObj(rname.Code(pkgbits.SyncCodeObj))
 
        if tag == pkgbits.ObjStub {
-               assert(objPkg == nil || objPkg == types2.Unsafe)
+               base.Assertf(objPkg == nil || objPkg == types2.Unsafe, "unexpected stub package: %v", objPkg)
                return objPkg, objName
        }
 
index 5499ccd4051028d41189c65284d42f2a18a4ddf1..628c0f54fc76f64d7b6c0bf83ff66038e161bba3 100644 (file)
@@ -36,7 +36,7 @@ func checkFiles(noders []*noder) (posMap, *types2.Package, *types2.Info) {
        ctxt := types2.NewContext()
        importer := gcimports{
                ctxt:     ctxt,
-               packages: map[string]*types2.Package{"unsafe": types2.Unsafe},
+               packages: make(map[string]*types2.Package),
        }
        conf := types2.Config{
                Context:               ctxt,
index 1350c22467f98ac9314e07816d0b86ab9ffda8f6..83ebe2477927d649d26b99657372a49605e5be60 100644 (file)
@@ -282,11 +282,13 @@ func (pr *pkgReader) pkgIdx(idx int) *types.Pkg {
 
 func (r *reader) doPkg() *types.Pkg {
        path := r.String()
-       if path == "builtin" {
-               return types.BuiltinPkg
-       }
-       if path == "" {
+       switch path {
+       case "":
                path = r.p.PkgPath()
+       case "builtin":
+               return types.BuiltinPkg
+       case "unsafe":
+               return types.UnsafePkg
        }
 
        name := r.String()
index 0fb162d381b97919f5a733ba14e79f5904166cc2..39f0ad794f0e51371b36ef748dd630dbbc5e1e73 100644 (file)
@@ -214,13 +214,21 @@ func (pw *pkgWriter) pkgIdx(pkg *types2.Package) int {
        w := pw.newWriter(pkgbits.RelocPkg, pkgbits.SyncPkgDef)
        pw.pkgsIdx[pkg] = w.Idx
 
-       if pkg == nil {
-               w.String("builtin")
-       } else {
+       // The universe and package unsafe need to be handled specially by
+       // importers anyway, so we serialize them using just their package
+       // path. This ensures that readers don't confuse them for
+       // user-defined packages.
+       switch pkg {
+       case nil: // universe
+               w.String("builtin") // same package path used by godoc
+       case types2.Unsafe:
+               w.String("unsafe")
+       default:
                var path string
                if pkg != w.p.curpkg {
                        path = pkg.Path()
                }
+               base.Assertf(path != "builtin" && path != "unsafe", "unexpected path for user-defined package: %q", path)
                w.String(path)
                w.String(pkg.Name())
                w.Len(pkg.Height())
index 5260759c4fc1feef2f6935d017ff293696c2e6bc..e27d3e0b4d27c0931c35daa3cff5b158be3a0215 100644 (file)
@@ -184,15 +184,14 @@ func (pr *pkgReader) pkgIdx(idx int) *types.Package {
 
 func (r *reader) doPkg() *types.Package {
        path := r.String()
-       if path == "builtin" {
+       switch path {
+       case "":
+               path = r.p.PkgPath()
+       case "builtin":
                return nil // universe
-       }
-       if path == "unsafe" {
+       case "unsafe":
                return types.Unsafe
        }
-       if path == "" {
-               path = r.p.PkgPath()
-       }
 
        if pkg := r.p.imports[path]; pkg != nil {
                return pkg