]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/load: refactor setErrorPos to PackageError.setPos
authorJay Conrod <jayconrod@google.com>
Wed, 13 Jan 2021 21:21:16 +0000 (16:21 -0500)
committerJay Conrod <jayconrod@google.com>
Thu, 14 Jan 2021 16:50:30 +0000 (16:50 +0000)
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>
src/cmd/go/internal/load/pkg.go

index cffc8fcefa5b9925ba253d8256e4f2fa55275a6e..9cea76d7383ba5e2f02e6634afbc57f3a6556a06 100644 (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)
                        }
                }
        }