rustc_ast/attr/
version.rs

1use std::fmt::{self, Display};
2use std::sync::OnceLock;
3
4use rustc_macros::{BlobDecodable, Encodable, HashStable_Generic, current_rustc_version};
5
6#[derive(Encodable, BlobDecodable, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[derive(HashStable_Generic)]
8pub struct RustcVersion {
9    pub major: u16,
10    pub minor: u16,
11    pub patch: u16,
12}
13
14impl RustcVersion {
15    pub const CURRENT: Self = current_rustc_version!();
16    pub fn current_overridable() -> Self {
17        *CURRENT_OVERRIDABLE.get_or_init(|| {
18            if let Ok(override_var) = std::env::var("RUSTC_OVERRIDE_VERSION_STRING")
19                && let Some(override_) = Self::parse_str(&override_var)
20            {
21                override_
22            } else {
23                Self::CURRENT
24            }
25        })
26    }
27    fn parse_str(value: &str) -> Option<Self> {
28        // Ignore any suffixes such as "-dev" or "-nightly".
29        let mut components = value.split('-').next().unwrap().splitn(3, '.');
30        let major = components.next()?.parse().ok()?;
31        let minor = components.next()?.parse().ok()?;
32        let patch = components.next().unwrap_or("0").parse().ok()?;
33        Some(RustcVersion { major, minor, patch })
34    }
35}
36
37static CURRENT_OVERRIDABLE: OnceLock<RustcVersion> = OnceLock::new();
38
39impl Display for RustcVersion {
40    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
41        write!(formatter, "{}.{}.{}", self.major, self.minor, self.patch)
42    }
43}