From 936ce874ace0bed17eac20dfa21fe04a45d30603 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 1 Jun 2023 14:11:27 -0400 Subject: [PATCH] cmd/go: add gover.Max and gover.MaxToolchain I've inlined these by hand enough times now. For #57001. Change-Id: Ia274bd444f12b07ce14eeb2b5a66546880db8f77 Reviewed-on: https://go-review.googlesource.com/c/go/+/499982 Run-TryBot: Russ Cox Reviewed-by: Bryan Mills TryBot-Result: Gopher Robot Auto-Submit: Russ Cox --- src/cmd/go/internal/gover/gover.go | 20 ++++++++++++++++++++ src/cmd/go/internal/modget/get.go | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/internal/gover/gover.go b/src/cmd/go/internal/gover/gover.go index fbea612227..247717125b 100644 --- a/src/cmd/go/internal/gover/gover.go +++ b/src/cmd/go/internal/gover/gover.go @@ -55,6 +55,26 @@ func Compare(x, y string) int { return 0 } +// Max returns the maximum of x and y interpreted as toolchain versions, +// compared using Compare. +// If x and y compare equal, Max returns x. +func Max(x, y string) string { + if Compare(x, y) < 0 { + return y + } + return x +} + +// Toolchain returns the maximum of x and y interpreted as toolchain names, +// compared using Compare(FromToolchain(x), FromToolchain(y)). +// If x and y compare equal, Max returns x. +func ToolchainMax(x, y string) string { + if Compare(FromToolchain(x), FromToolchain(y)) < 0 { + return y + } + return x +} + // IsLang reports whether v denotes the overall Go language version // and not a specific release. Starting with the Go 1.21 release, "1.x" denotes // the overall language version; the first release is "1.x.0". diff --git a/src/cmd/go/internal/modget/get.go b/src/cmd/go/internal/modget/get.go index 0cf3c1dc84..8a8b8dea22 100644 --- a/src/cmd/go/internal/modget/get.go +++ b/src/cmd/go/internal/modget/get.go @@ -1233,8 +1233,8 @@ func (r *resolver) resolveQueries(ctx context.Context, queries []*query) (change goVers := "" for _, q := range queries { for _, cs := range q.candidates { - if e := (*gover.TooNewError)(nil); errors.As(cs.err, &e) && gover.Compare(goVers, e.GoVersion) < 0 { - goVers = e.GoVersion + if e := (*gover.TooNewError)(nil); errors.As(cs.err, &e) { + goVers = gover.Max(goVers, e.GoVersion) } } } -- 2.50.0