Skip to content

Make #[cfg(version)] respect RUSTC_OVERRIDE_VERSION_STRING #141413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions compiler/rustc_attr_data_structures/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::{self, Display};
use std::sync::OnceLock;

use rustc_macros::{
Decodable, Encodable, HashStable_Generic, PrintAttribute, current_rustc_version,
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_attr_parsing/src/attributes/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ pub fn eval_condition(

// See https://github.com/rust-lang/rust/issues/64796#issuecomment-640851454 for details
if sess.psess.assume_incomplete_release {
RustcVersion::CURRENT > min_version
RustcVersion::current_overridable() > min_version
} else {
RustcVersion::CURRENT >= min_version
RustcVersion::current_overridable() >= min_version
}
}
MetaItemKind::List(mis) => {
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/feature-gates/cfg-version-expand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//@ run-pass
//@ rustc-env:RUSTC_OVERRIDE_VERSION_STRING=1.50.3

#![feature(cfg_version)]

#[cfg(version("1.49.0"))]
const ON_1_49_0: bool = true;
#[cfg(version("1.50"))]
const ON_1_50_0: bool = true;
#[cfg(not(version("1.51")))]
const ON_1_51_0: bool = false;

// This one uses the wrong syntax, so doesn't eval to true
#[warn(unexpected_cfgs)]
#[cfg(not(version = "1.48.0"))] //~ WARN unexpected `cfg` condition name: `version`
const ON_1_48_0: bool = false;

fn main() {
assert!(!ON_1_48_0);
assert!(ON_1_49_0);
assert!(ON_1_50_0);
assert!(!ON_1_51_0);
assert!(cfg!(version("1.1")));
assert!(cfg!(version("1.49")));
assert!(cfg!(version("1.50.0")));
assert!(cfg!(version("1.50.3")));
assert!(!cfg!(version("1.50.4")));
assert!(!cfg!(version("1.51")));
assert!(!cfg!(version("1.100")));
}
13 changes: 13 additions & 0 deletions tests/ui/feature-gates/cfg-version-expand.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
warning: unexpected `cfg` condition name: `version`
--> $DIR/cfg-version-expand.rs:15:11
|
LL | #[cfg(not(version = "1.48.0"))]
| ^^^^^^^^^^^^^^^^^^
|
= help: expected names are: `FALSE` and `test` and 31 more
= help: to expect this configuration use `--check-cfg=cfg(version, values("1.48.0"))`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default

warning: 1 warning emitted

Loading