Source file src/cmd/compile/internal/types2/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 types2
     6  
     7  import (
     8  	"fmt"
     9  	"go/version"
    10  	"internal/goversion"
    11  )
    12  
    13  // A goVersion is a Go language version string of the form "go1.%d"
    14  // where d is the minor version number. goVersion strings don't
    15  // contain release numbers ("go1.20.1" is not a valid goVersion).
    16  type goVersion string
    17  
    18  // asGoVersion returns v as a goVersion (e.g., "go1.20.1" becomes "go1.20").
    19  // If v is not a valid Go version, the result is the empty string.
    20  func asGoVersion(v string) goVersion {
    21  	return goVersion(version.Lang(v))
    22  }
    23  
    24  // isValid reports whether v is a valid Go version.
    25  func (v goVersion) isValid() bool {
    26  	return v != ""
    27  }
    28  
    29  // cmp returns -1, 0, or +1 depending on whether x < y, x == y, or x > y,
    30  // interpreted as Go versions.
    31  func (x goVersion) cmp(y goVersion) int {
    32  	return version.Compare(string(x), string(y))
    33  }
    34  
    35  var (
    36  	// Go versions that introduced language changes
    37  	go1_9  = asGoVersion("go1.9")
    38  	go1_13 = asGoVersion("go1.13")
    39  	go1_14 = asGoVersion("go1.14")
    40  	go1_17 = asGoVersion("go1.17")
    41  	go1_18 = asGoVersion("go1.18")
    42  	go1_20 = asGoVersion("go1.20")
    43  	go1_21 = asGoVersion("go1.21")
    44  	go1_22 = asGoVersion("go1.22")
    45  	go1_23 = asGoVersion("go1.23")
    46  
    47  	// current (deployed) Go version
    48  	go_current = asGoVersion(fmt.Sprintf("go1.%d", goversion.Version))
    49  )
    50  
    51  // allowVersion reports whether the current package at the given position
    52  // is allowed to use version v. If the position is unknown, the specified
    53  // module version (Config.GoVersion) is used. If that version is invalid,
    54  // allowVersion returns true.
    55  func (check *Checker) allowVersion(at poser, v goVersion) bool {
    56  	fileVersion := check.conf.GoVersion
    57  	if pos := at.Pos(); pos.IsKnown() {
    58  		fileVersion = check.versions[pos.FileBase()]
    59  	}
    60  
    61  	// We need asGoVersion (which calls version.Lang) below
    62  	// because fileVersion may be the (unaltered) Config.GoVersion
    63  	// string which may contain dot-release information.
    64  	version := asGoVersion(fileVersion)
    65  
    66  	return !version.isValid() || version.cmp(v) >= 0
    67  }
    68  
    69  // verifyVersionf is like allowVersion but also accepts a format string and arguments
    70  // which are used to report a version error if allowVersion returns false.
    71  func (check *Checker) verifyVersionf(at poser, v goVersion, format string, args ...interface{}) bool {
    72  	if !check.allowVersion(at, v) {
    73  		check.versionErrorf(at, v, format, args...)
    74  		return false
    75  	}
    76  	return true
    77  }
    78  

View as plain text