diff options
author | Jay Conrod <jayconrod@google.com> | 2021-01-13 16:21:16 -0500 |
---|---|---|
committer | Jay Conrod <jayconrod@google.com> | 2021-01-14 16:50:30 +0000 |
commit | c73232d08f84e110707627a23ceae14d2b534889 (patch) | |
tree | 0d9be92a593acc0b9dd1c0dec85ea25b4b783a81 /src/cmd/go/internal/load/pkg.go | |
parent | 6aa28d3e06d0757995c54a22d2f2a1f1b396774f (diff) | |
download | go-c73232d08f84e110707627a23ceae14d2b534889.tar.gz go-c73232d08f84e110707627a23ceae14d2b534889.zip |
cmd/go/internal/load: refactor setErrorPos to PackageError.setPos
Renamed setErrorPos to setPos, made it a method of PackageError,
and removed its Package parameter and return value. This makes it
more clear that setPos modifies PackageError and does not create a new
Package.
Change-Id: I26c58d3d456c7c18a5c2598e1e8e158b1e6b4b36
Reviewed-on: https://go-review.googlesource.com/c/go/+/283637
Trust: Jay Conrod <jayconrod@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/go/internal/load/pkg.go')
-rw-r--r-- | src/cmd/go/internal/load/pkg.go | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index cffc8fcefa..9cea76d738 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -304,7 +304,7 @@ func (p *Package) setLoadPackageDataError(err error, path string, stk *ImportSta } if path != stk.Top() { - p = setErrorPos(p, importPos) + p.Error.setPos(importPos) } } @@ -447,6 +447,15 @@ func (p *PackageError) MarshalJSON() ([]byte, error) { return json.Marshal(perr) } +func (p *PackageError) setPos(posList []token.Position) { + if len(posList) == 0 { + return + } + pos := posList[0] + pos.Filename = base.ShortPath(pos.Filename) + p.Pos = pos.String() +} + // ImportPathError is a type of error that prevents a package from being loaded // for a given import path. When such a package is loaded, a *Package is // returned with Err wrapping an ImportPathError: the error is attached to @@ -695,17 +704,19 @@ func loadImport(ctx context.Context, pre *preload, path, srcDir string, parent * Err: ImportErrorf(path, "non-canonical import path %q: should be %q", path, pathpkg.Clean(path)), } p.Incomplete = true - setErrorPos(p, importPos) + p.Error.setPos(importPos) } } // Checked on every import because the rules depend on the code doing the importing. if perr := disallowInternal(srcDir, parent, parentPath, p, stk); perr != p { - return setErrorPos(perr, importPos) + perr.Error.setPos(importPos) + return perr } if mode&ResolveImport != 0 { if perr := disallowVendor(srcDir, path, parentPath, p, stk); perr != p { - return setErrorPos(perr, importPos) + perr.Error.setPos(importPos) + return perr } } @@ -715,7 +726,8 @@ func loadImport(ctx context.Context, pre *preload, path, srcDir string, parent * ImportStack: stk.Copy(), Err: ImportErrorf(path, "import %q is a program, not an importable package", path), } - return setErrorPos(&perr, importPos) + perr.Error.setPos(importPos) + return &perr } if p.Internal.Local && parent != nil && !parent.Internal.Local { @@ -730,21 +742,13 @@ func loadImport(ctx context.Context, pre *preload, path, srcDir string, parent * ImportStack: stk.Copy(), Err: err, } - return setErrorPos(&perr, importPos) + perr.Error.setPos(importPos) + return &perr } return p } -func setErrorPos(p *Package, importPos []token.Position) *Package { - if len(importPos) > 0 { - pos := importPos[0] - pos.Filename = base.ShortPath(pos.Filename) - p.Error.Pos = pos.String() - } - return p -} - // loadPackageData loads information needed to construct a *Package. The result // is cached, and later calls to loadPackageData for the same package will return // the same data. @@ -1649,7 +1653,7 @@ func (p *Package) load(ctx context.Context, path string, stk *ImportStack, impor // must be either in an explicit command-line argument, // or on the importer side (indicated by a non-empty importPos). if path != stk.Top() && len(importPos) > 0 { - p = setErrorPos(p, importPos) + p.Error.setPos(importPos) } } } |