From: Tim King Date: Fri, 8 Nov 2024 23:07:51 +0000 (-0800) Subject: cmd/compile/internal/importer: minimize Import differences X-Git-Tag: go1.24rc1~390 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c79a486be20b395bdd198be9112e633623665988;p=gostls13.git cmd/compile/internal/importer: minimize Import differences Minimizes the differences with go/internal/gcimporter.Import. Note that the copy in cmd/compile/internal/importer is currently only used in tests. The delta between the two Import functions is now just types vs types2. Change-Id: I5e94d3aa5bbdb78252e47310c95807f63e27ef3d Reviewed-on: https://go-review.googlesource.com/c/go/+/626698 Commit-Queue: Tim King Reviewed-by: Robert Griesemer LUCI-TryBot-Result: Go LUCI --- diff --git a/src/cmd/compile/internal/importer/gcimporter.go b/src/cmd/compile/internal/importer/gcimporter.go index 47a0d7c0bf..fa780d386a 100644 --- a/src/cmd/compile/internal/importer/gcimporter.go +++ b/src/cmd/compile/internal/importer/gcimporter.go @@ -14,6 +14,7 @@ import ( "fmt" "go/build" "internal/pkgbits" + "internal/saferio" "io" "os" "os/exec" @@ -216,23 +217,11 @@ func Import(packages map[string]*types2.Package, path, srcDir string, lookup fun err = fmt.Errorf("import %q: old textual export format no longer supported (recompile package)", path) case "$$B\n": - // TODO(taking): minimize code delta with src/go/internal/gcimporter.Import. - var data []byte - var r io.Reader = buf - if size >= 0 { - r = io.LimitReader(r, int64(size)) - } - data, err = io.ReadAll(r) - if err != nil { - break - } - - if len(data) == 0 { - err = fmt.Errorf("import %q: missing export data", path) - break + var exportFormat byte + if exportFormat, err = buf.ReadByte(); err != nil { + return } - exportFormat := data[0] - s := string(data[1:]) + size-- // The unified export format starts with a 'u'; the indexed export // format starts with an 'i'; and the older binary export format @@ -241,7 +230,18 @@ func Import(packages map[string]*types2.Package, path, srcDir string, lookup fun switch exportFormat { case 'u': // exported strings may contain "\n$$\n" - search backwards + var data []byte + var r io.Reader = buf + if size >= 0 { + if data, err = saferio.ReadData(r, uint64(size)); err != nil { + return + } + } else if data, err = io.ReadAll(r); err != nil { + return + } + s := string(data) s = s[:strings.LastIndex(s, "\n$$\n")] + input := pkgbits.NewPkgDecoder(id, s) pkg = ReadPackage(nil, packages, input) default: diff --git a/src/go/internal/gcimporter/gcimporter.go b/src/go/internal/gcimporter/gcimporter.go index f86b20da8d..a07fd8a6de 100644 --- a/src/go/internal/gcimporter/gcimporter.go +++ b/src/go/internal/gcimporter/gcimporter.go @@ -23,9 +23,6 @@ import ( "sync" ) -// debugging/development support -const debug = false - var exportMap sync.Map // package dir → func() (string, error) // lookupGorootExport returns the location of the export data