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.
8 "cmd/compile/internal/syntax"
13 // langCompat reports an error if the representation of a numeric
14 // literal is not compatible with the current language version.
15 func (check *Checker) langCompat(lit *syntax.BasicLit) {
17 if len(s) <= 2 || check.allowVersion(check.pkg, lit.Pos(), 1, 13) {
21 if strings.Contains(s, "_") {
22 check.versionErrorf(lit, "go1.13", "underscores in numeric literals")
29 if radix == 'b' || radix == 'B' {
30 check.versionErrorf(lit, "go1.13", "binary literals")
33 if radix == 'o' || radix == 'O' {
34 check.versionErrorf(lit, "go1.13", "0o/0O-style octal literals")
37 if lit.Kind != syntax.IntLit && (radix == 'x' || radix == 'X') {
38 check.versionErrorf(lit, "go1.13", "hexadecimal floating-point literals")
42 // allowVersion reports whether the given package
43 // is allowed to use version major.minor.
44 func (check *Checker) allowVersion(pkg *Package, pos syntax.Pos, major, minor int) bool {
45 // We assume that imported packages have all been checked,
46 // so we only have to check for the local package.
51 // If the source file declares its Go version, use that to decide.
52 if check.posVers != nil {
53 if v, ok := check.posVers[base(pos)]; ok && v.major >= 1 {
54 return v.major > major || v.major == major && v.minor >= minor
58 // Otherwise fall back to the version in the checker.
59 ma, mi := check.version.major, check.version.minor
60 return ma == 0 && mi == 0 || ma > major || ma == major && mi >= minor
63 // base finds the underlying PosBase of the source file containing pos,
64 // skipping over intermediate PosBase layers created by //line directives.
65 func base(pos syntax.Pos) *syntax.PosBase {
69 if bb == nil || bb == b {
81 var errVersionSyntax = errors.New("invalid Go version syntax")
83 // parseGoVersion parses a Go version string (such as "go1.12")
84 // and returns the version, or an error. If s is the empty
85 // string, the version is 0.0.
86 func parseGoVersion(s string) (v version, err error) {
90 if !strings.HasPrefix(s, "go") {
91 return version{}, errVersionSyntax
95 for ; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
96 if i >= 10 || i == 0 && s[i] == '0' {
97 return version{}, errVersionSyntax
99 v.major = 10*v.major + int(s[i]) - '0'
101 if i > 0 && i == len(s) {
104 if i == 0 || s[i] != '.' {
105 return version{}, errVersionSyntax
109 // We really should not accept "go1.0",
110 // but we didn't reject it from the start
111 // and there are now programs that use it.
116 for ; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
117 if i >= 10 || i == 0 && s[i] == '0' {
118 return version{}, errVersionSyntax
120 v.minor = 10*v.minor + int(s[i]) - '0'
122 if i > 0 && i == len(s) {
125 return version{}, errVersionSyntax
128 func (v version) equal(u version) bool {
129 return v.major == u.major && v.minor == u.minor
132 func (v version) before(u version) bool {
133 return v.major < u.major || v.major == u.major && v.minor < u.minor
136 func (v version) after(u version) bool {
137 return v.major > u.major || v.major == u.major && v.minor > u.minor