1
0
mirror of https://github.com/golang/go synced 2024-11-22 19:34:59 -07:00

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>
This commit is contained in:
Jay Conrod 2021-01-13 16:21:16 -05:00
parent 6aa28d3e06
commit c73232d08f

View File

@ -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)
}
}
}