|
1 | 1 | use std::any::Any;
|
2 |
| -use std::ffi::OsString; |
| 2 | +use std::ffi::{OsStr, OsString}; |
3 | 3 | use std::io::{self, BufWriter, Write};
|
4 | 4 | use std::path::{Path, PathBuf};
|
5 | 5 | use std::sync::{Arc, LazyLock, OnceLock};
|
@@ -361,6 +361,31 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
|
361 | 361 | )
|
362 | 362 | }
|
363 | 363 |
|
| 364 | +fn env_var_os<'tcx>(tcx: TyCtxt<'tcx>, key: &'tcx OsStr) -> Option<&'tcx OsStr> { |
| 365 | + let value = env::var_os(key); |
| 366 | + |
| 367 | + let value_tcx = value.as_ref().map(|value| { |
| 368 | + let encoded_bytes = tcx.arena.alloc_slice(value.as_encoded_bytes()); |
| 369 | + debug_assert_eq!(value.as_encoded_bytes(), encoded_bytes); |
| 370 | + // SAFETY: The bytes came from `as_encoded_bytes`, and we assume that |
| 371 | + // `alloc_slice` is implemented correctly, and passes the same bytes |
| 372 | + // back (debug asserted above). |
| 373 | + unsafe { OsStr::from_encoded_bytes_unchecked(encoded_bytes) } |
| 374 | + }); |
| 375 | + |
| 376 | + // Also add the variable to Cargo's dependency tracking |
| 377 | + // |
| 378 | + // NOTE: This only works for passes run before `write_dep_info`. See that |
| 379 | + // for extension points for configuring environment variables to be |
| 380 | + // properly change-tracked. |
| 381 | + tcx.sess.psess.env_depinfo.borrow_mut().insert(( |
| 382 | + Symbol::intern(&key.to_string_lossy()), |
| 383 | + value.as_ref().and_then(|value| value.to_str()).map(|value| Symbol::intern(&value)), |
| 384 | + )); |
| 385 | + |
| 386 | + value_tcx |
| 387 | +} |
| 388 | + |
364 | 389 | // Returns all the paths that correspond to generated files.
|
365 | 390 | fn generated_output_paths(
|
366 | 391 | tcx: TyCtxt<'_>,
|
@@ -725,6 +750,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
|
725 | 750 | |tcx, _| tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal());
|
726 | 751 | providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1;
|
727 | 752 | providers.early_lint_checks = early_lint_checks;
|
| 753 | + providers.env_var_os = env_var_os; |
728 | 754 | limits::provide(providers);
|
729 | 755 | proc_macro_decls::provide(providers);
|
730 | 756 | rustc_const_eval::provide(providers);
|
|
0 commit comments