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