11use std:: fmt:: { self , Display } ;
2+ use std:: sync:: OnceLock ;
23
34use rustc_macros:: {
45 Decodable , Encodable , HashStable_Generic , PrintAttribute , current_rustc_version,
@@ -16,8 +17,29 @@ pub struct RustcVersion {
1617
1718impl RustcVersion {
1819 pub const CURRENT : Self = current_rustc_version ! ( ) ;
20+ pub fn current_configurable ( ) -> Self {
21+ * CURRENT_CONFIGURABLE . get_or_init ( || {
22+ if let Ok ( override_var) = std:: env:: var ( "RUSTC_OVERRIDE_VERSION_STRING" )
23+ && let Some ( override_) = Self :: parse_str ( & override_var)
24+ {
25+ override_
26+ } else {
27+ Self :: CURRENT
28+ }
29+ } )
30+ }
31+ fn parse_str ( value : & str ) -> Option < Self > {
32+ // Ignore any suffixes such as "-dev" or "-nightly".
33+ let mut components = value. split ( '-' ) . next ( ) . unwrap ( ) . splitn ( 3 , '.' ) ;
34+ let major = components. next ( ) ?. parse ( ) . ok ( ) ?;
35+ let minor = components. next ( ) ?. parse ( ) . ok ( ) ?;
36+ let patch = components. next ( ) . unwrap_or ( "0" ) . parse ( ) . ok ( ) ?;
37+ Some ( RustcVersion { major, minor, patch } )
38+ }
1939}
2040
41+ static CURRENT_CONFIGURABLE : OnceLock < RustcVersion > = OnceLock :: new ( ) ;
42+
2143impl Display for RustcVersion {
2244 fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2345 write ! ( formatter, "{}.{}.{}" , self . major, self . minor, self . patch)
0 commit comments