1
2
3
4
5 package types2
6
7 import (
8 "cmd/compile/internal/syntax"
9 "fmt"
10 "go/version"
11 "internal/goversion"
12 )
13
14
15
16
17 type goVersion string
18
19
20
21 func asGoVersion(v string) goVersion {
22 return goVersion(version.Lang(v))
23 }
24
25
26 func (v goVersion) isValid() bool {
27 return v != ""
28 }
29
30
31
32 func (x goVersion) cmp(y goVersion) int {
33 return version.Compare(string(x), string(y))
34 }
35
36 var (
37
38 go1_9 = asGoVersion("go1.9")
39 go1_13 = asGoVersion("go1.13")
40 go1_14 = asGoVersion("go1.14")
41 go1_17 = asGoVersion("go1.17")
42 go1_18 = asGoVersion("go1.18")
43 go1_20 = asGoVersion("go1.20")
44 go1_21 = asGoVersion("go1.21")
45 go1_22 = asGoVersion("go1.22")
46 go1_23 = asGoVersion("go1.23")
47
48
49 go_current = asGoVersion(fmt.Sprintf("go1.%d", goversion.Version))
50 )
51
52
53
54
55
56 func (check *Checker) allowVersion(at poser, v goVersion) bool {
57 fileVersion := check.conf.GoVersion
58 if pos := at.Pos(); pos.IsKnown() {
59 fileVersion = check.versions[base(pos)]
60 }
61
62
63
64
65 version := asGoVersion(fileVersion)
66
67 return !version.isValid() || version.cmp(v) >= 0
68 }
69
70
71
72 func (check *Checker) verifyVersionf(at poser, v goVersion, format string, args ...interface{}) bool {
73 if !check.allowVersion(at, v) {
74 check.versionErrorf(at, v, format, args...)
75 return false
76 }
77 return true
78 }
79
80
81
82
83 func base(pos syntax.Pos) *syntax.PosBase {
84 assert(pos.IsKnown())
85 b := pos.Base()
86 for {
87 bb := b.Pos().Base()
88 if bb == nil || bb == b {
89 break
90 }
91 b = bb
92 }
93 return b
94 }
95
View as plain text