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.
16 // langCompat reports an error if the representation of a numeric
17 // literal is not compatible with the current language version.
18 func (check *Checker) langCompat(lit *ast.BasicLit) {
20 if len(s) <= 2 || check.allowVersion(check.pkg, 1, 13) {
24 if strings.Contains(s, "_") {
25 check.errorf(lit, _UnsupportedFeature, "underscores in numeric literals requires go1.13 or later")
32 if radix == 'b' || radix == 'B' {
33 check.errorf(lit, _UnsupportedFeature, "binary literals requires go1.13 or later")
36 if radix == 'o' || radix == 'O' {
37 check.errorf(lit, _UnsupportedFeature, "0o/0O-style octal literals requires go1.13 or later")
40 if lit.Kind != token.INT && (radix == 'x' || radix == 'X') {
41 check.errorf(lit, _UnsupportedFeature, "hexadecimal floating-point literals requires go1.13 or later")
45 // allowVersion reports whether the given package
46 // is allowed to use version major.minor.
47 func (check *Checker) allowVersion(pkg *Package, major, minor int) bool {
48 // We assume that imported packages have all been checked,
49 // so we only have to check for the local package.
53 ma, mi := check.version.major, check.version.minor
54 return ma == 0 && mi == 0 || ma > major || ma == major && mi >= minor
61 // parseGoVersion parses a Go version string (such as "go1.12")
62 // and returns the version, or an error. If s is the empty
63 // string, the version is 0.0.
64 func parseGoVersion(s string) (v version, err error) {
68 matches := goVersionRx.FindStringSubmatch(s)
70 err = fmt.Errorf(`should be something like "go1.12"`)
73 v.major, err = strconv.Atoi(matches[1])
77 v.minor, err = strconv.Atoi(matches[2])
81 // goVersionRx matches a Go version string, e.g. "go1.12".
82 var goVersionRx = regexp.MustCompile(`^go([1-9]\d*)\.(0|[1-9]\d*)$`)