about summary refs log tree commit diff
path: root/compiler/rustc_middle/src/ty/context.rs
diff options
context:
space:
mode:
authorStuart Cook <Zalathar@users.noreply.github.com>2025-03-27 15:57:21 +1100
committerGitHub <noreply@github.com>2025-03-27 15:57:21 +1100
commitc45986ae61ed48e24720dd1fef0528873b8e356d (patch)
tree4d9d44ffbdaf1ea2fb8a5b2516a9f2f2c4a85d8d /compiler/rustc_middle/src/ty/context.rs
parent2216f269c53594087dafeb99b435f1c9e3f9b7dc (diff)
parent632ce38c9a93a6ee890aedd99fcb1a61cabd0a46 (diff)
downloadrust-c45986ae61ed48e24720dd1fef0528873b8e356d.tar.gz
rust-c45986ae61ed48e24720dd1fef0528873b8e356d.zip
Rollup merge of #130883 - madsmtm:env-var-query, r=petrochenkov
Add environment variable query

Generally, `rustc` prefers command-line arguments, but in some cases, an environment variable really is the most sensible option. We should make sure that this works properly with the compiler's change-tracking mechanisms, such that changing the relevant environment variable causes a rebuild.

This PR is a first step forwards in doing that.

Part of the work needed to do https://github.com/rust-lang/rust/issues/118204, see https://github.com/rust-lang/rust/pull/129342 for some discussion.

r? ``@petrochenkov``
Diffstat (limited to 'compiler/rustc_middle/src/ty/context.rs')
-rw-r--r--compiler/rustc_middle/src/ty/context.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs
index 834d1f2a4a8..08d4c1f9cf2 100644
--- a/compiler/rustc_middle/src/ty/context.rs
+++ b/compiler/rustc_middle/src/ty/context.rs
@@ -7,6 +7,8 @@ pub mod tls;
 use std::assert_matches::{assert_matches, debug_assert_matches};
 use std::borrow::Borrow;
 use std::cmp::Ordering;
+use std::env::VarError;
+use std::ffi::OsStr;
 use std::hash::{Hash, Hasher};
 use std::marker::PhantomData;
 use std::ops::{Bound, Deref};
@@ -1883,6 +1885,15 @@ impl<'tcx> TyCtxt<'tcx> {
         }
         None
     }
+
+    /// Helper to get a tracked environment variable via. [`TyCtxt::env_var_os`] and converting to
+    /// UTF-8 like [`std::env::var`].
+    pub fn env_var<K: ?Sized + AsRef<OsStr>>(self, key: &'tcx K) -> Result<&'tcx str, VarError> {
+        match self.env_var_os(key.as_ref()) {
+            Some(value) => value.to_str().ok_or_else(|| VarError::NotUnicode(value.to_os_string())),
+            None => Err(VarError::NotPresent),
+        }
+    }
 }
 
 impl<'tcx> TyCtxtAt<'tcx> {