Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/cmd/compile/internal/types2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2468,8 +2468,8 @@ func TestInstantiateErrors(t *testing.T) {
t.Fatalf("Instantiate(%v, %v) returned nil error, want non-nil", T, test.targs)
}

var argErr *ArgumentError
if !errors.As(err, &argErr) {
argErr, ok := errors.AsType[*ArgumentError](err)
if !ok {
t.Fatalf("Instantiate(%v, %v): error is not an *ArgumentError", T, test.targs)
}

Expand All @@ -2484,8 +2484,8 @@ func TestArgumentErrorUnwrapping(t *testing.T) {
Index: 1,
Err: Error{Msg: "test"},
}
var e Error
if !errors.As(err, &e) {
e, ok := errors.AsType[Error](err)
if !ok {
t.Fatalf("error %v does not wrap types.Error", err)
}
if e.Msg != "test" {
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/go/internal/base/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func sameFile(path1, path2 string) bool {

// ShortPathError rewrites the path in err using base.ShortPath, if err is a wrapped PathError.
func ShortPathError(err error) error {
var pe *fs.PathError
if errors.As(err, &pe) {
if pe, ok := errors.AsType[*fs.PathError](err); ok {
pe.Path = ShortPath(pe.Path)
}
return err
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/go/internal/doc/pkgsite.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func doPkgsite(urlPath, fragment string) error {
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) {
if ee, ok := errors.AsType[*exec.ExitError](err); ok {
// Exit with the same exit status as pkgsite to avoid
// printing of "exit status" error messages.
// Any relevant messages have already been printed
Expand Down
9 changes: 4 additions & 5 deletions src/cmd/go/internal/fmtcmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ func runFmt(ctx context.Context, cmd *base.Command, args []string) {
continue
}
if pkg.Error != nil {
var nogo *load.NoGoError
var embed *load.EmbedError
if (errors.As(pkg.Error, &nogo) || errors.As(pkg.Error, &embed)) && len(pkg.InternalAllGoFiles()) > 0 {
// Skip this error, as we will format
// all files regardless.
if _, ok := errors.AsType[*load.NoGoError](pkg.Error); ok {
// Skip this error, as we will format all files regardless.
} else if _, ok := errors.AsType[*load.EmbedError](pkg.Error); ok && len(pkg.InternalAllGoFiles()) > 0 {
// Skip this error, as we will format all files regardless.
} else {
base.Errorf("%v", pkg.Error)
continue
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ func (p *Package) setLoadPackageDataError(err error, path string, stk *ImportSta

// Replace (possibly wrapped) *build.NoGoError with *load.NoGoError.
// The latter is more specific about the cause.
var nogoErr *build.NoGoError
if errors.As(err, &nogoErr) {
nogoErr, ok := errors.AsType[*build.NoGoError](err)
if ok {
if p.Dir == "" && nogoErr.Dir != "" {
p.Dir = nogoErr.Dir
}
Expand Down
38 changes: 21 additions & 17 deletions src/cmd/go/internal/modget/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -1294,15 +1294,13 @@ func (r *resolver) loadPackages(ctx context.Context, patterns []string, findPack
continue
}

var (
importMissing *modload.ImportMissingError
ambiguous *modload.AmbiguousImportError
)
if !errors.As(err, &importMissing) && !errors.As(err, &ambiguous) {
// The package, which is a dependency of something we care about, has some
// problem that we can't resolve with a version change.
// Leave the error for the final LoadPackages call.
continue
if _, ok := errors.AsType[*modload.ImportMissingError](err); !ok {
if _, ok := errors.AsType[*modload.AmbiguousImportError](err); !ok {
// The package, which is a dependency of something we care about, has some
// problem that we can't resolve with a version change.
// Leave the error for the final LoadPackages call.
continue
}
}

path := pkgPath
Expand Down Expand Up @@ -1674,7 +1672,7 @@ func (r *resolver) checkPackageProblems(ctx context.Context, pkgPatterns []strin
}

base.SetExitStatus(1)
if ambiguousErr := (*modload.AmbiguousImportError)(nil); errors.As(err, &ambiguousErr) {
if ambiguousErr, ok := errors.AsType[*modload.AmbiguousImportError](err); ok {
for _, m := range ambiguousErr.Modules {
relevantMods[m] |= hasPkg
}
Expand Down Expand Up @@ -1717,7 +1715,7 @@ func (r *resolver) checkPackageProblems(ctx context.Context, pkgPatterns []strin
i := i
r.work.Add(func() {
err := modload.CheckRetractions(ctx, retractions[i].m)
if retractErr := (*modload.ModuleRetractedError)(nil); errors.As(err, &retractErr) {
if _, ok := errors.AsType[*modload.ModuleRetractedError](err); ok {
retractions[i].message = err.Error()
}
})
Expand Down Expand Up @@ -1994,8 +1992,8 @@ func (r *resolver) updateBuildList(ctx context.Context, additions []module.Versi
toolchain.SwitchOrFatal(ctx, err)
}

var constraint *modload.ConstraintError
if !errors.As(err, &constraint) {
constraint, ok := errors.AsType[*modload.ConstraintError](err)
if !ok {
base.Fatal(err)
}

Expand Down Expand Up @@ -2066,17 +2064,23 @@ func reqsFromGoMod(f *modfile.File) []module.Version {
// does not exist at the requested version, either because the module does not
// exist at all or because it does not include that specific version.
func isNoSuchModuleVersion(err error) bool {
var noMatch *modload.NoMatchingVersionError
return errors.Is(err, os.ErrNotExist) || errors.As(err, &noMatch)
if errors.Is(err, os.ErrNotExist) {
return true
}
_, ok := errors.AsType[*modload.NoMatchingVersionError](err)
return ok
}

// isNoSuchPackageVersion reports whether err indicates that the requested
// package does not exist at the requested version, either because no module
// that could contain it exists at that version, or because every such module
// that does exist does not actually contain the package.
func isNoSuchPackageVersion(err error) bool {
var noPackage *modload.PackageNotInModuleError
return isNoSuchModuleVersion(err) || errors.As(err, &noPackage)
if isNoSuchModuleVersion(err) {
return true
}
_, ok := errors.AsType[*modload.PackageNotInModuleError](err)
return ok
}

// workspace represents the set of modules in a workspace.
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/modget/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func reportError(q *query, err error) {
// If err already mentions all of the relevant parts of q, just log err to
// reduce stutter. Otherwise, log both q and err.
//
// TODO(bcmills): Use errors.As to unpack these errors instead of parsing
// TODO(bcmills): Use errors.AsType to unpack these errors instead of parsing
// strings with regular expressions.

if !utf8.ValidString(q.pattern) || !utf8.ValidString(q.version) {
Expand Down
22 changes: 10 additions & 12 deletions src/cmd/go/internal/modload/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,10 @@ func addUpdate(ctx context.Context, m *modinfo.ModulePublic) {
}

info, err := Query(ctx, m.Path, "upgrade", m.Version, CheckAllowed)
var noVersionErr *NoMatchingVersionError
if errors.Is(err, ErrDisallowed) ||
if _, ok := errors.AsType[*NoMatchingVersionError](err); ok ||
errors.Is(err, fs.ErrNotExist) ||
errors.As(err, &noVersionErr) {
// Ignore "not found" and "no matching version" errors.
errors.Is(err, ErrDisallowed) {
// Ignore "no matching version" and "not found" errors.
// This means the proxy has no matching version or no versions at all.
//
// Ignore "disallowed" errors. This means the current version is
Expand Down Expand Up @@ -238,10 +237,10 @@ func addRetraction(ctx context.Context, m *modinfo.ModulePublic) {
}

err := CheckRetractions(ctx, module.Version{Path: m.Path, Version: m.Version})
var noVersionErr *NoMatchingVersionError
var retractErr *ModuleRetractedError
if err == nil || errors.Is(err, fs.ErrNotExist) || errors.As(err, &noVersionErr) {
// Ignore "not found" and "no matching version" errors.
if err == nil {
return
} else if _, ok := errors.AsType[*NoMatchingVersionError](err); ok || errors.Is(err, fs.ErrNotExist) {
// Ignore "no matching version" and "not found" errors.
// This means the proxy has no matching version or no versions at all.
//
// We should report other errors though. An attacker that controls the
Expand All @@ -250,7 +249,7 @@ func addRetraction(ctx context.Context, m *modinfo.ModulePublic) {
// hide versions, since the "list" and "latest" endpoints are not
// authenticated.
return
} else if errors.As(err, &retractErr) {
} else if retractErr, ok := errors.AsType[*ModuleRetractedError](err); ok {
if len(retractErr.Rationale) == 0 {
m.Retracted = []string{"retracted by module author"}
} else {
Expand All @@ -265,9 +264,8 @@ func addRetraction(ctx context.Context, m *modinfo.ModulePublic) {
// author. m.Error is set if there's an error loading deprecation information.
func addDeprecation(ctx context.Context, m *modinfo.ModulePublic) {
deprecation, err := CheckDeprecation(ctx, module.Version{Path: m.Path, Version: m.Version})
var noVersionErr *NoMatchingVersionError
if errors.Is(err, fs.ErrNotExist) || errors.As(err, &noVersionErr) {
// Ignore "not found" and "no matching version" errors.
if _, ok := errors.AsType[*NoMatchingVersionError](err); ok || errors.Is(err, fs.ErrNotExist) {
// Ignore "no matching version" and "not found" errors.
// This means the proxy has no matching version or no versions at all.
//
// We should report other errors though. An attacker that controls the
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/modload/buildlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ func tidyPrunedRoots(ctx context.Context, mainModule module.Version, old *Requir
q.Add(func() {
skipModFile := true
_, _, _, _, err := importFromModules(ctx, pkg.path, tidy, nil, skipModFile)
if aie := (*AmbiguousImportError)(nil); errors.As(err, &aie) {
if _, ok := errors.AsType[*AmbiguousImportError](err); ok {
disambiguateRoot.Store(pkg.mod, true)
}
})
Expand Down
6 changes: 4 additions & 2 deletions src/cmd/go/internal/modload/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ func editRequirements(ctx context.Context, rs *Requirements, tryUpgrade, mustSel
// conflict we discover from one or more of the original roots.
mg, upgradedRoots, err := extendGraph(ctx, rootPruning, roots, selectedRoot)
if err != nil {
var tooNew *gover.TooNewError
if mg == nil || errors.As(err, &tooNew) {
if mg == nil {
return orig, false, err
}
if _, ok := errors.AsType[*gover.TooNewError](err); ok {
return orig, false, err
}
// We're about to walk the entire extended module graph, so we will find
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/go/internal/modload/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func importFromModules(ctx context.Context, path string, rs *Requirements, mg *M

root, isLocal, err := fetch(ctx, m)
if err != nil {
if sumErr := (*sumMissingError)(nil); errors.As(err, &sumErr) {
if _, ok := errors.AsType[*sumMissingError](err); ok {
// We are missing a sum needed to fetch a module in the build list.
// We can't verify that the package is unique, and we may not find
// the package at all. Keep checking other modules to decide which
Expand Down Expand Up @@ -549,7 +549,7 @@ func queryImport(ctx context.Context, path string, rs *Requirements) (module.Ver
for _, m := range mods {
root, isLocal, err := fetch(ctx, m)
if err != nil {
if sumErr := (*sumMissingError)(nil); errors.As(err, &sumErr) {
if _, ok := errors.AsType[*sumMissingError](err); ok {
return module.Version{}, &ImportMissingSumError{importPath: path}
}
return module.Version{}, err
Expand Down
6 changes: 2 additions & 4 deletions src/cmd/go/internal/modload/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,11 @@ func listModules(ctx context.Context, rs *Requirements, args []string, mode List
// modinfoError wraps an error to create an error message in
// modinfo.ModuleError with minimal redundancy.
func modinfoError(path, vers string, err error) *modinfo.ModuleError {
var nerr *NoMatchingVersionError
var merr *module.ModuleError
if errors.As(err, &nerr) {
if _, ok := errors.AsType[*NoMatchingVersionError](err); ok {
// NoMatchingVersionError contains the query, so we don't mention the
// query again in ModuleError.
err = &module.ModuleError{Path: path, Err: err}
} else if !errors.As(err, &merr) {
} else if _, ok := errors.AsType[*module.ModuleError](err); !ok {
// If the error does not contain path and version, wrap it in a
// module.ModuleError.
err = &module.ModuleError{Path: path, Version: vers, Err: err}
Expand Down
20 changes: 9 additions & 11 deletions src/cmd/go/internal/modload/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -1304,15 +1304,15 @@ func loadFromRoots(ctx context.Context, params loaderParams) *loader {
}

// Add importer information to checksum errors.
if sumErr := (*ImportMissingSumError)(nil); errors.As(pkg.err, &sumErr) {
if sumErr, ok := errors.AsType[*ImportMissingSumError](pkg.err); ok {
if importer := pkg.stack; importer != nil {
sumErr.importer = importer.path
sumErr.importerVersion = importer.mod.Version
sumErr.importerIsTest = importer.testOf != nil
}
}

if stdErr := (*ImportMissingError)(nil); errors.As(pkg.err, &stdErr) && stdErr.isStd {
if stdErr, ok := errors.AsType[*ImportMissingError](pkg.err); ok && stdErr.isStd {
// Add importer go version information to import errors of standard
// library packages arising from newer releases.
if importer := pkg.stack; importer != nil {
Expand Down Expand Up @@ -1384,7 +1384,7 @@ func (ld *loader) updateRequirements(ctx context.Context) (changed bool, err err
var maxTooNew *gover.TooNewError
for _, pkg := range ld.pkgs {
if pkg.err != nil {
if tooNew := (*gover.TooNewError)(nil); errors.As(pkg.err, &tooNew) {
if tooNew, ok := errors.AsType[*gover.TooNewError](pkg.err); ok {
if maxTooNew == nil || gover.Compare(tooNew.GoVersion, maxTooNew.GoVersion) > 0 {
maxTooNew = tooNew
}
Expand Down Expand Up @@ -1573,7 +1573,7 @@ func (ld *loader) resolveMissingImports(ctx context.Context) (modAddedBy map[mod
// we should only add the missing import once.
continue
}
if !errors.As(pkg.err, new(*ImportMissingError)) {
if _, ok := errors.AsType[*ImportMissingError](pkg.err); !ok {
// Leave other errors for Import or load.Packages to report.
continue
}
Expand All @@ -1584,8 +1584,7 @@ func (ld *loader) resolveMissingImports(ctx context.Context) (modAddedBy map[mod
var err error
mod, err = queryImport(ctx, pkg.path, ld.requirements)
if err != nil {
var ime *ImportMissingError
if errors.As(err, &ime) {
if ime, ok := errors.AsType[*ImportMissingError](err); ok {
for curstack := pkg.stack; curstack != nil; curstack = curstack.stack {
if MainModules.Contains(curstack.mod.Path) {
ime.ImportingMainModule = curstack.mod
Expand Down Expand Up @@ -1625,7 +1624,7 @@ func (ld *loader) resolveMissingImports(ctx context.Context) (modAddedBy map[mod
maxTooNewPkg *loadPkg
)
for _, pm := range pkgMods {
if tooNew := (*gover.TooNewError)(nil); errors.As(pm.pkg.err, &tooNew) {
if tooNew, ok := errors.AsType[*gover.TooNewError](pm.pkg.err); ok {
if maxTooNew == nil || gover.Compare(tooNew.GoVersion, maxTooNew.GoVersion) > 0 {
maxTooNew = tooNew
maxTooNewPkg = pm.pkg
Expand Down Expand Up @@ -1771,8 +1770,7 @@ func (ld *loader) preloadRootModules(ctx context.Context, rootPkgs []string) (ch
// full module graph.
m, _, _, _, err := importFromModules(ctx, path, ld.requirements, nil, ld.skipImportModFiles)
if err != nil {
var missing *ImportMissingError
if errors.As(err, &missing) && ld.ResolveMissingImports {
if _, ok := errors.AsType[*ImportMissingError](err); ok && ld.ResolveMissingImports {
// This package isn't provided by any selected module.
// If we can find it, it will be a new root dependency.
m, err = queryImport(ctx, path, ld.requirements)
Expand Down Expand Up @@ -2196,14 +2194,14 @@ func (ld *loader) checkTidyCompatibility(ctx context.Context, rs *Requirements,
// module that previously provided the package to a version that no
// longer does, or to a version for which the module source code (but
// not the go.mod file in isolation) has a checksum error.
if missing := (*ImportMissingError)(nil); errors.As(mismatch.err, &missing) {
if _, ok := errors.AsType[*ImportMissingError](mismatch.err); ok {
selected := module.Version{
Path: pkg.mod.Path,
Version: mg.Selected(pkg.mod.Path),
}
ld.error(fmt.Errorf("%s loaded from %v,\n\tbut go %s would fail to locate it in %s", pkg.stackText(), pkg.mod, compatVersion, selected))
} else {
if ambiguous := (*AmbiguousImportError)(nil); errors.As(mismatch.err, &ambiguous) {
if _, ok := errors.AsType[*AmbiguousImportError](mismatch.err); ok {
// TODO: Is this check needed?
}
ld.error(fmt.Errorf("%s loaded from %v,\n\tbut go %s would fail to locate it:\n\t%v", pkg.stackText(), pkg.mod, compatVersion, mismatch.err))
Expand Down
10 changes: 6 additions & 4 deletions src/cmd/go/internal/modload/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ func ReadModFile(gomod string, fix modfile.VersionFixer) (data []byte, f *modfil
}

func shortPathErrorList(err error) error {
var el modfile.ErrorList
if errors.As(err, &el) {
if el, ok := errors.AsType[modfile.ErrorList](err); ok {
for i := range el {
el[i].Filename = base.ShortPath(el[i].Filename)
}
Expand Down Expand Up @@ -175,12 +174,15 @@ func (e *excludedError) Is(err error) bool { return err == ErrDisallowed }
// its author.
func CheckRetractions(ctx context.Context, m module.Version) (err error) {
defer func() {
if retractErr := (*ModuleRetractedError)(nil); err == nil || errors.As(err, &retractErr) {
if err == nil {
return
}
if _, ok := errors.AsType[*ModuleRetractedError](err); ok {
return
}
// Attribute the error to the version being checked, not the version from
// which the retractions were to be loaded.
if mErr := (*module.ModuleError)(nil); errors.As(err, &mErr) {
if mErr, ok := errors.AsType[*module.ModuleError](err); ok {
err = mErr.Err
}
err = &retractionLoadingError{m: m, err: err}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/modload/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ func queryPrefixModules(ctx context.Context, candidateModules []string, queryMod
if notExistErr == nil {
notExistErr = rErr
}
} else if iv := (*module.InvalidVersionError)(nil); errors.As(rErr, &iv) {
} else if _, ok := errors.AsType[*module.InvalidVersionError](rErr); ok {
if invalidVersion == nil {
invalidVersion = rErr
}
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/go/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1735,8 +1735,7 @@ func (r *runTestActor) Act(b *work.Builder, ctx context.Context, a *work.Action)
} else if errors.Is(err, exec.ErrWaitDelay) {
fmt.Fprintf(cmd.Stdout, "*** Test I/O incomplete %v after exiting.\n", cmd.WaitDelay)
}
var ee *exec.ExitError
if len(out) == 0 || !errors.As(err, &ee) || !ee.Exited() {
if ee, ok := errors.AsType[*exec.ExitError](err); !ok || !ee.Exited() || len(out) == 0 {
// If there was no test output, print the exit status so that the reason
// for failure is clear.
fmt.Fprintf(cmd.Stdout, "%s\n", err)
Expand Down
Loading