|
1 | 1 | use std::any::Any; |
2 | | -use std::env::VarError; |
3 | | -use std::ffi::OsString; |
| 2 | +use std::ffi::{OsStr, OsString}; |
4 | 3 | use std::io::{self, BufWriter, Write}; |
5 | 4 | use std::path::{Path, PathBuf}; |
6 | 5 | use std::sync::{Arc, LazyLock}; |
@@ -322,22 +321,29 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) { |
322 | 321 | ) |
323 | 322 | } |
324 | 323 |
|
325 | | -fn env_var(tcx: TyCtxt<'_>, key: Symbol) -> Option<Symbol> { |
326 | | - let var = match env::var(key.as_str()) { |
327 | | - Ok(var) => Some(Symbol::intern(&var)), |
328 | | - Err(VarError::NotPresent) => None, |
329 | | - Err(VarError::NotUnicode(var)) => { |
330 | | - tcx.dcx().emit_err(errors::EnvVarNotUnicode { key, var }); |
331 | | - None |
332 | | - } |
333 | | - }; |
| 324 | +fn env_var_os<'tcx>(tcx: TyCtxt<'tcx>, key: &'tcx OsStr) -> Option<&'tcx OsStr> { |
| 325 | + let value = env::var_os(key); |
| 326 | + |
| 327 | + let value_tcx = value.as_deref().map(|value| { |
| 328 | + let encoded_bytes = tcx.arena.alloc_slice(value.as_encoded_bytes()); |
| 329 | + debug_assert_eq!(value.as_encoded_bytes(), encoded_bytes); |
| 330 | + // SAFETY: The bytes came from `as_encoded_bytes`, and we assume that |
| 331 | + // `alloc_slice` is implemented correctly, and passes the same bytes |
| 332 | + // back (debug asserted above). |
| 333 | + unsafe { OsStr::from_encoded_bytes_unchecked(encoded_bytes) } |
| 334 | + }); |
| 335 | + |
334 | 336 | // Also add the variable to Cargo's dependency tracking |
335 | 337 | // |
336 | 338 | // NOTE: This only works for passes run before `write_dep_info`. See that |
337 | 339 | // for extension points for configuring environment variables to be |
338 | 340 | // properly change-tracked. |
339 | | - tcx.sess.psess.env_depinfo.borrow_mut().insert((key, var)); |
340 | | - var |
| 341 | + tcx.sess.psess.env_depinfo.borrow_mut().insert(( |
| 342 | + Symbol::intern(&key.to_string_lossy()), |
| 343 | + value.and_then(|value| value.to_str()).map(|value| Symbol::intern(&value)), |
| 344 | + )); |
| 345 | + |
| 346 | + value_tcx |
341 | 347 | } |
342 | 348 |
|
343 | 349 | // Returns all the paths that correspond to generated files. |
@@ -707,7 +713,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| { |
707 | 713 | |tcx, _| tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal()); |
708 | 714 | providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1; |
709 | 715 | providers.early_lint_checks = early_lint_checks; |
710 | | - providers.env_var = env_var; |
| 716 | + providers.env_var_os = env_var_os; |
711 | 717 | proc_macro_decls::provide(providers); |
712 | 718 | rustc_const_eval::provide(providers); |
713 | 719 | rustc_middle::hir::provide(providers); |
|
0 commit comments