Skip to content

Commit c73232d

Browse files
author
Jay Conrod
committed
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>
1 parent 6aa28d3 commit c73232d

File tree

1 file changed

+20
-16
lines changed
  • src/cmd/go/internal/load

1 file changed

+20
-16
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func (p *Package) setLoadPackageDataError(err error, path string, stk *ImportSta
304304
}
305305

306306
if path != stk.Top() {
307-
p = setErrorPos(p, importPos)
307+
p.Error.setPos(importPos)
308308
}
309309
}
310310

@@ -447,6 +447,15 @@ func (p *PackageError) MarshalJSON() ([]byte, error) {
447447
return json.Marshal(perr)
448448
}
449449

450+
func (p *PackageError) setPos(posList []token.Position) {
451+
if len(posList) == 0 {
452+
return
453+
}
454+
pos := posList[0]
455+
pos.Filename = base.ShortPath(pos.Filename)
456+
p.Pos = pos.String()
457+
}
458+
450459
// ImportPathError is a type of error that prevents a package from being loaded
451460
// for a given import path. When such a package is loaded, a *Package is
452461
// 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 *
695704
Err: ImportErrorf(path, "non-canonical import path %q: should be %q", path, pathpkg.Clean(path)),
696705
}
697706
p.Incomplete = true
698-
setErrorPos(p, importPos)
707+
p.Error.setPos(importPos)
699708
}
700709
}
701710

702711
// Checked on every import because the rules depend on the code doing the importing.
703712
if perr := disallowInternal(srcDir, parent, parentPath, p, stk); perr != p {
704-
return setErrorPos(perr, importPos)
713+
perr.Error.setPos(importPos)
714+
return perr
705715
}
706716
if mode&ResolveImport != 0 {
707717
if perr := disallowVendor(srcDir, path, parentPath, p, stk); perr != p {
708-
return setErrorPos(perr, importPos)
718+
perr.Error.setPos(importPos)
719+
return perr
709720
}
710721
}
711722

@@ -715,7 +726,8 @@ func loadImport(ctx context.Context, pre *preload, path, srcDir string, parent *
715726
ImportStack: stk.Copy(),
716727
Err: ImportErrorf(path, "import %q is a program, not an importable package", path),
717728
}
718-
return setErrorPos(&perr, importPos)
729+
perr.Error.setPos(importPos)
730+
return &perr
719731
}
720732

721733
if p.Internal.Local && parent != nil && !parent.Internal.Local {
@@ -730,21 +742,13 @@ func loadImport(ctx context.Context, pre *preload, path, srcDir string, parent *
730742
ImportStack: stk.Copy(),
731743
Err: err,
732744
}
733-
return setErrorPos(&perr, importPos)
745+
perr.Error.setPos(importPos)
746+
return &perr
734747
}
735748

736749
return p
737750
}
738751

739-
func setErrorPos(p *Package, importPos []token.Position) *Package {
740-
if len(importPos) > 0 {
741-
pos := importPos[0]
742-
pos.Filename = base.ShortPath(pos.Filename)
743-
p.Error.Pos = pos.String()
744-
}
745-
return p
746-
}
747-
748752
// loadPackageData loads information needed to construct a *Package. The result
749753
// is cached, and later calls to loadPackageData for the same package will return
750754
// the same data.
@@ -1649,7 +1653,7 @@ func (p *Package) load(ctx context.Context, path string, stk *ImportStack, impor
16491653
// must be either in an explicit command-line argument,
16501654
// or on the importer side (indicated by a non-empty importPos).
16511655
if path != stk.Top() && len(importPos) > 0 {
1652-
p = setErrorPos(p, importPos)
1656+
p.Error.setPos(importPos)
16531657
}
16541658
}
16551659
}

0 commit comments

Comments
 (0)