about summary refs log tree commit diff
path: root/compiler/rustc_feature/src/lib.rs
diff options
context:
space:
mode:
authorMartin Nordholts <martin.nordholts@codetale.se>2025-01-08 11:34:39 +0100
committerMartin Nordholts <martin.nordholts@codetale.se>2025-01-08 11:37:19 +0100
commit54ce831db0471732515697c99999f1cec7da99a1 (patch)
tree8f152b300c56473bd87d9bba339321f2ef25b593 /compiler/rustc_feature/src/lib.rs
parent9c87288a7d2f03625a813df6d3bfe43c09ad4f5a (diff)
downloadrust-54ce831db0471732515697c99999f1cec7da99a1.tar.gz
rust-54ce831db0471732515697c99999f1cec7da99a1.zip
rustc_feature: Avoid unsafe `std::env::set_var()` in `UnstableFeatures` tests
Avoid unsafe `std::env::set_var()` by allowing tests to inject
`std::env::var("RUSTC_BOOTSTRAP")` with a `env_var_rustc_bootstrap`
parameter.
Diffstat (limited to 'compiler/rustc_feature/src/lib.rs')
-rw-r--r--compiler/rustc_feature/src/lib.rs12
1 files changed, 11 insertions, 1 deletions
diff --git a/compiler/rustc_feature/src/lib.rs b/compiler/rustc_feature/src/lib.rs
index 6db512ace1b..0b034a2ae10 100644
--- a/compiler/rustc_feature/src/lib.rs
+++ b/compiler/rustc_feature/src/lib.rs
@@ -68,6 +68,16 @@ impl UnstableFeatures {
     /// If `krate` is [`Some`], then setting `RUSTC_BOOTSTRAP=krate` will enable the nightly
     /// features. Otherwise, only `RUSTC_BOOTSTRAP=1` will work.
     pub fn from_environment(krate: Option<&str>) -> Self {
+        Self::from_environment_value(krate, std::env::var("RUSTC_BOOTSTRAP"))
+    }
+
+    /// Avoid unsafe `std::env::set_var()` by allowing tests to inject
+    /// `std::env::var("RUSTC_BOOTSTRAP")` with the `env_var_rustc_bootstrap`
+    /// arg.
+    fn from_environment_value(
+        krate: Option<&str>,
+        env_var_rustc_bootstrap: Result<String, std::env::VarError>,
+    ) -> Self {
         // `true` if this is a feature-staged build, i.e., on the beta or stable channel.
         let disable_unstable_features =
             option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some_and(|s| s != "0");
@@ -75,7 +85,7 @@ impl UnstableFeatures {
         let is_unstable_crate =
             |var: &str| krate.is_some_and(|name| var.split(',').any(|new_krate| new_krate == name));
 
-        let bootstrap = std::env::var("RUSTC_BOOTSTRAP").ok();
+        let bootstrap = env_var_rustc_bootstrap.ok();
         if let Some(val) = bootstrap.as_deref() {
             match val {
                 val if val == "1" || is_unstable_crate(val) => return UnstableFeatures::Cheat,