]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/types/version.go
go/types, types2: remove local version processing in favor of go/version
[gostls13.git] / src / go / types / version.go
1 // Copyright 2021 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.
4
5 package types
6
7 import (
8         "fmt"
9         "go/ast"
10         "go/token"
11         "go/version"
12         "internal/goversion"
13         "strings"
14 )
15
16 // A goVersion is a Go language version string of the form "go1.%d"
17 // where d is the minor version number. goVersion strings don't
18 // contain release numbers ("go1.20.1" is not a valid goVersion).
19 type goVersion string
20
21 // asGoVersion returns v as a goVersion (e.g., "go1.20.1" becomes "go1.20").
22 // If v is not a valid Go version, the result is the empty string.
23 func asGoVersion(v string) goVersion {
24         return goVersion(version.Lang(v))
25 }
26
27 // isValid reports whether v is a valid Go version.
28 func (v goVersion) isValid() bool {
29         return v != ""
30 }
31
32 // cmp returns -1, 0, or +1 depending on whether x < y, x == y, or x > y,
33 // interpreted as Go versions.
34 func (x goVersion) cmp(y goVersion) int {
35         return version.Compare(string(x), string(y))
36 }
37
38 var (
39         // Go versions that introduced language changes
40         go1_9  = asGoVersion("go1.9")
41         go1_13 = asGoVersion("go1.13")
42         go1_14 = asGoVersion("go1.14")
43         go1_17 = asGoVersion("go1.17")
44         go1_18 = asGoVersion("go1.18")
45         go1_20 = asGoVersion("go1.20")
46         go1_21 = asGoVersion("go1.21")
47
48         // current (deployed) Go version
49         go_current = asGoVersion(fmt.Sprintf("go1.%d", goversion.Version))
50 )
51
52 // langCompat reports an error if the representation of a numeric
53 // literal is not compatible with the current language version.
54 func (check *Checker) langCompat(lit *ast.BasicLit) {
55         s := lit.Value
56         if len(s) <= 2 || check.allowVersion(check.pkg, lit, go1_13) {
57                 return
58         }
59         // len(s) > 2
60         if strings.Contains(s, "_") {
61                 check.versionErrorf(lit, go1_13, "underscores in numeric literals")
62                 return
63         }
64         if s[0] != '0' {
65                 return
66         }
67         radix := s[1]
68         if radix == 'b' || radix == 'B' {
69                 check.versionErrorf(lit, go1_13, "binary literals")
70                 return
71         }
72         if radix == 'o' || radix == 'O' {
73                 check.versionErrorf(lit, go1_13, "0o/0O-style octal literals")
74                 return
75         }
76         if lit.Kind != token.INT && (radix == 'x' || radix == 'X') {
77                 check.versionErrorf(lit, go1_13, "hexadecimal floating-point literals")
78         }
79 }
80
81 // allowVersion reports whether the given package is allowed to use version v.
82 func (check *Checker) allowVersion(pkg *Package, at positioner, v goVersion) bool {
83         // We assume that imported packages have all been checked,
84         // so we only have to check for the local package.
85         if pkg != check.pkg {
86                 return true
87         }
88
89         // If no explicit file version is specified,
90         // fileVersion corresponds to the module version.
91         var fileVersion goVersion
92         if pos := at.Pos(); pos.IsValid() {
93                 // We need version.Lang below because file versions
94                 // can be (unaltered) Config.GoVersion strings that
95                 // may contain dot-release information.
96                 fileVersion = asGoVersion(check.versions[check.fileFor(pos)])
97         }
98         return !fileVersion.isValid() || fileVersion.cmp(v) >= 0
99 }
100
101 // verifyVersionf is like allowVersion but also accepts a format string and arguments
102 // which are used to report a version error if allowVersion returns false. It uses the
103 // current package.
104 func (check *Checker) verifyVersionf(at positioner, v goVersion, format string, args ...interface{}) bool {
105         if !check.allowVersion(check.pkg, at, v) {
106                 check.versionErrorf(at, v, format, args...)
107                 return false
108         }
109         return true
110 }
111
112 // TODO(gri) Consider a more direct (position-independent) mechanism
113 //           to identify which file we're in so that version checks
114 //           work correctly in the absence of correct position info.
115
116 // fileFor returns the *ast.File which contains the position pos.
117 // If there are no files, the result is nil.
118 // The position must be valid.
119 func (check *Checker) fileFor(pos token.Pos) *ast.File {
120         assert(pos.IsValid())
121         // Eval and CheckExpr tests may not have any source files.
122         if len(check.files) == 0 {
123                 return nil
124         }
125         for _, file := range check.files {
126                 if file.FileStart <= pos && pos < file.FileEnd {
127                         return file
128                 }
129         }
130         panic(check.sprintf("file not found for pos = %d (%s)", int(pos), check.fset.Position(pos)))
131 }