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"
15 // langCompat reports an error if the representation of a numeric
16 // literal is not compatible with the current language version.
17 func (check *Checker) langCompat(lit *syntax.BasicLit) {
19 if len(s) <= 2 || check.allowVersion(check.pkg, 1, 13) {
23 if strings.Contains(s, "_") {
24 check.versionErrorf(lit, "go1.13", "underscores in numeric literals")
31 if radix == 'b' || radix == 'B' {
32 check.versionErrorf(lit, "go1.13", "binary literals")
35 if radix == 'o' || radix == 'O' {
36 check.versionErrorf(lit, "go1.13", "0o/0O-style octal literals")
39 if lit.Kind != syntax.IntLit && (radix == 'x' || radix == 'X') {
40 check.versionErrorf(lit, "go1.13", "hexadecimal floating-point literals")
44 // allowVersion reports whether the given package
45 // is allowed to use version major.minor.
46 func (check *Checker) allowVersion(pkg *Package, major, minor int) bool {
47 // We assume that imported packages have all been checked,
48 // so we only have to check for the local package.
52 ma, mi := check.version.major, check.version.minor
53 return ma == 0 && mi == 0 || ma > major || ma == major && mi >= minor
60 // parseGoVersion parses a Go version string (such as "go1.12")
61 // and returns the version, or an error. If s is the empty
62 // string, the version is 0.0.
63 func parseGoVersion(s string) (v version, err error) {
67 matches := goVersionRx.FindStringSubmatch(s)
69 err = fmt.Errorf(`should be something like "go1.12"`)
72 v.major, err = strconv.Atoi(matches[1])
76 v.minor, err = strconv.Atoi(matches[2])
80 // goVersionRx matches a Go version string, e.g. "go1.12".
81 var goVersionRx = lazyregexp.New(`^go([1-9][0-9]*)\.(0|[1-9][0-9]*)$`)