rustc_attr_data_structures/
version.rs

1use std::fmt::{self, Display};
2use std::sync::OnceLock;
3
4use rustc_macros::{
5    Decodable, Encodable, HashStable_Generic, PrintAttribute, current_rustc_version,
6};
7
8use crate::PrintAttribute;
9
10#[derive(Encodable, Decodable, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
11#[derive(HashStable_Generic, PrintAttribute)]
12pub struct RustcVersion {
13    pub major: u16,
14    pub minor: u16,
15    pub patch: u16,
16}
17
18impl RustcVersion {
19    pub const CURRENT: Self = current_rustc_version!();
20    pub fn current_overridable() -> Self {
21        *CURRENT_OVERRIDABLE.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    }
39}
40
41static CURRENT_OVERRIDABLE: OnceLock<RustcVersion> = OnceLock::new();
42
43impl Display for RustcVersion {
44    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
45        write!(formatter, "{}.{}.{}", self.major, self.minor, self.patch)
46    }
47}