about summary refs log tree commit diff
path: root/compiler/rustc_attr_data_structures/src/version.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-05-25 14:46:28 +0000
committerbors <bors@rust-lang.org>2025-05-25 14:46:28 +0000
commit283db70ace62a0ae704a624e43b68c2ee44b87a6 (patch)
tree7e88d24a7189f00dc3fabd256df143c7438ea784 /compiler/rustc_attr_data_structures/src/version.rs
parent88b3b520e852e01970c3f519339ba64ed3e7db6d (diff)
parent43cb506383b0aa98924305804547f461653650f8 (diff)
downloadrust-283db70ace62a0ae704a624e43b68c2ee44b87a6.tar.gz
rust-283db70ace62a0ae704a624e43b68c2ee44b87a6.zip
Auto merge of #141545 - GuillaumeGomez:rollup-myrvuqq, r=GuillaumeGomez
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#141413 (Make #[cfg(version)] respect RUSTC_OVERRIDE_VERSION_STRING)
 - rust-lang/rust#141443 (make teach_help message for cast-before-pass-to-variadic more precise)
 - rust-lang/rust#141508 (bootstrap: clippy: set TESTNAME based on given paths)
 - rust-lang/rust#141512 (Avoid extra path trimming in method not found error)
 - rust-lang/rust#141530 (Added unstable feature doc comments to unstable book)
 - rust-lang/rust#141541 (Random nits)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_attr_data_structures/src/version.rs')
-rw-r--r--compiler/rustc_attr_data_structures/src/version.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/compiler/rustc_attr_data_structures/src/version.rs b/compiler/rustc_attr_data_structures/src/version.rs
index 69b0e041d81..030e9520940 100644
--- a/compiler/rustc_attr_data_structures/src/version.rs
+++ b/compiler/rustc_attr_data_structures/src/version.rs
@@ -1,4 +1,5 @@
 use std::fmt::{self, Display};
+use std::sync::OnceLock;
 
 use rustc_macros::{
     Decodable, Encodable, HashStable_Generic, PrintAttribute, current_rustc_version,
@@ -16,8 +17,29 @@ pub struct RustcVersion {
 
 impl RustcVersion {
     pub const CURRENT: Self = current_rustc_version!();
+    pub fn current_overridable() -> Self {
+        *CURRENT_OVERRIDABLE.get_or_init(|| {
+            if let Ok(override_var) = std::env::var("RUSTC_OVERRIDE_VERSION_STRING")
+                && let Some(override_) = Self::parse_str(&override_var)
+            {
+                override_
+            } else {
+                Self::CURRENT
+            }
+        })
+    }
+    fn parse_str(value: &str) -> Option<Self> {
+        // Ignore any suffixes such as "-dev" or "-nightly".
+        let mut components = value.split('-').next().unwrap().splitn(3, '.');
+        let major = components.next()?.parse().ok()?;
+        let minor = components.next()?.parse().ok()?;
+        let patch = components.next().unwrap_or("0").parse().ok()?;
+        Some(RustcVersion { major, minor, patch })
+    }
 }
 
+static CURRENT_OVERRIDABLE: OnceLock<RustcVersion> = OnceLock::new();
+
 impl Display for RustcVersion {
     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(formatter, "{}.{}.{}", self.major, self.minor, self.patch)