1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 // GoVersion returns the minimum Go version implied by a given build expression.
13 // If the expression can be satisfied without any Go version tags, GoVersion returns an empty string.
17 // GoVersion(linux && go1.22) = "go1.22"
18 // GoVersion((linux && go1.22) || (windows && go1.20)) = "go1.20" => go1.20
19 // GoVersion(linux) = ""
20 // GoVersion(linux || (windows && go1.22)) = ""
21 // GoVersion(!go1.22) = ""
23 // GoVersion assumes that any tag or negated tag may independently be true,
24 // so that its analysis can be purely structural, without SAT solving.
25 // “Impossible” subexpressions may therefore affect the result.
29 // GoVersion((linux && !linux && go1.20) || go1.21) = "go1.20"
30 func GoVersion(x Expr) string {
31 v := minVersion(x, +1)
38 return "go1." + strconv.Itoa(v)
41 // minVersion returns the minimum Go major version (9 for go1.9)
42 // implied by expression z, or if sign < 0, by expression !z.
43 func minVersion(z Expr, sign int) int {
44 switch z := z.(type) {
52 return op(minVersion(z.X, sign), minVersion(z.Y, sign))
58 return op(minVersion(z.X, sign), minVersion(z.Y, sign))
60 return minVersion(z.X, -sign)
63 // !foo implies nothing
69 _, v, _ := stringsCut(z.Tag, "go1.")
70 n, err := strconv.Atoi(v)
79 // TODO: Delete, replace calls with strings.Cut once Go bootstrap toolchain is bumped.
80 func stringsCut(s, sep string) (before, after string, found bool) {
81 if i := strings.Index(s, sep); i >= 0 {
82 return s[:i], s[i+len(sep):], true
87 // andVersion returns the minimum Go version
88 // implied by the AND of two minimum Go versions,
89 // which is the max of the versions.
90 func andVersion(x, y int) int {
97 // orVersion returns the minimum Go version
98 // implied by the OR of two minimum Go versions,
99 // which is the min of the versions.
100 func orVersion(x, y int) int {