Skip to content

Commit 2dd6488

Browse files
committed
Make #[cfg(version)] respect RUSTC_OVERRIDE_VERSION_STRING
1 parent 335aa92 commit 2dd6488

File tree

2 files changed

+24
-2
lines changed
  • compiler

2 files changed

+24
-2
lines changed

compiler/rustc_attr_data_structures/src/version.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt::{self, Display};
2+
use std::sync::OnceLock;
23

34
use rustc_macros::{
45
Decodable, Encodable, HashStable_Generic, PrintAttribute, current_rustc_version,
@@ -16,8 +17,29 @@ pub struct RustcVersion {
1617

1718
impl 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+
2143
impl Display for RustcVersion {
2244
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2345
write!(formatter, "{}.{}.{}", self.major, self.minor, self.patch)

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ pub fn eval_condition(
129129

130130
// See https://github.com/rust-lang/rust/issues/64796#issuecomment-640851454 for details
131131
if sess.psess.assume_incomplete_release {
132-
RustcVersion::CURRENT > min_version
132+
RustcVersion::current_configurable() > min_version
133133
} else {
134-
RustcVersion::CURRENT >= min_version
134+
RustcVersion::current_configurable() >= min_version
135135
}
136136
}
137137
MetaItemKind::List(mis) => {

0 commit comments

Comments
 (0)