Skip to content

Commit fdaca73

Browse files
committed
Make #[cfg(version)] respect RUSTC_OVERRIDE_VERSION_STRING
1 parent 2eef478 commit fdaca73

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-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_overridable() -> 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_overridable() > min_version
133133
} else {
134-
RustcVersion::CURRENT >= min_version
134+
RustcVersion::current_overridable() >= min_version
135135
}
136136
}
137137
MetaItemKind::List(mis) => {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//@ run-pass
2+
//@ rustc-env:RUSTC_OVERRIDE_VERSION_STRING=1.50.3
3+
4+
#![feature(cfg_version)]
5+
6+
#[cfg(version("1.49.0"))]
7+
const ON_1_49_0: bool = true;
8+
#[cfg(version("1.50"))]
9+
const ON_1_50_0: bool = true;
10+
#[cfg(not(version("1.51")))]
11+
const ON_1_51_0: bool = false;
12+
13+
// This one uses the wrong syntax, so doesn't eval to true
14+
#[warn(unexpected_cfgs)]
15+
#[cfg(not(version = "1.48.0"))] //~ WARN unexpected `cfg` condition name: `version`
16+
const ON_1_48_0: bool = false;
17+
18+
fn main() {
19+
assert!(!ON_1_48_0);
20+
assert!(ON_1_49_0);
21+
assert!(ON_1_50_0);
22+
assert!(!ON_1_51_0);
23+
assert!(cfg!(version("1.1")));
24+
assert!(cfg!(version("1.49")));
25+
assert!(cfg!(version("1.50.0")));
26+
assert!(cfg!(version("1.50.3")));
27+
assert!(!cfg!(version("1.50.4")));
28+
assert!(!cfg!(version("1.51")));
29+
assert!(!cfg!(version("1.100")));
30+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
warning: unexpected `cfg` condition name: `version`
2+
--> $DIR/cfg-version-expand.rs:15:11
3+
|
4+
LL | #[cfg(not(version = "1.48.0"))]
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: expected names are: `FALSE` and `test` and 31 more
8+
= help: to expect this configuration use `--check-cfg=cfg(version, values("1.48.0"))`
9+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
10+
= note: `#[warn(unexpected_cfgs)]` on by default
11+
12+
warning: 1 warning emitted
13+

0 commit comments

Comments
 (0)