Skip to content

Commit 9ccebf6

Browse files
committed
Add TyCtx::env_var
1 parent f2becdf commit 9ccebf6

File tree

6 files changed

+44
-0
lines changed

6 files changed

+44
-0
lines changed

compiler/rustc_errors/src/diagnostic_impls.rs

+6
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ impl IntoDiagArg for std::ffi::CString {
242242
}
243243
}
244244

245+
impl IntoDiagArg for std::ffi::OsString {
246+
fn into_diag_arg(self) -> DiagArgValue {
247+
DiagArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
248+
}
249+
}
250+
245251
impl IntoDiagArg for rustc_data_structures::small_c_str::SmallCStr {
246252
fn into_diag_arg(self) -> DiagArgValue {
247253
DiagArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))

compiler/rustc_interface/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ interface_cant_emit_mir =
44
interface_emoji_identifier =
55
identifiers cannot contain emoji: `{$ident}`
66
7+
interface_env_var_not_unicode =
8+
cannot read environment variable "{$key}" with value "{$var}", since it contains non-unicode data
9+
710
interface_error_writing_dependencies =
811
error writing dependencies to `{$path}`: {$error}
912

compiler/rustc_interface/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::ffi::OsString;
12
use std::io;
23
use std::path::Path;
34

@@ -21,6 +22,13 @@ pub struct EmojiIdentifier {
2122
pub ident: Symbol,
2223
}
2324

25+
#[derive(Diagnostic)]
26+
#[diag(interface_env_var_not_unicode)]
27+
pub struct EnvVarNotUnicode {
28+
pub key: Symbol,
29+
pub var: OsString,
30+
}
31+
2432
#[derive(Diagnostic)]
2533
#[diag(interface_mixed_bin_crate)]
2634
pub struct MixedBinCrate;

compiler/rustc_interface/src/passes.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::any::Any;
2+
use std::env::VarError;
23
use std::ffi::OsString;
34
use std::io::{self, BufWriter, Write};
45
use std::path::{Path, PathBuf};
@@ -336,6 +337,20 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
336337
)
337338
}
338339

340+
fn env_var(tcx: TyCtxt<'_>, key: Symbol) -> Option<Symbol> {
341+
let var = match std::env::var(key.as_str()) {
342+
Ok(var) => Some(Symbol::intern(&var)),
343+
Err(VarError::NotPresent) => None,
344+
Err(VarError::NotUnicode(var)) => {
345+
tcx.dcx().emit_err(errors::EnvVarNotUnicode { key, var });
346+
None
347+
}
348+
};
349+
// Also add the variable to Cargo's dependency tracking
350+
tcx.sess.psess.env_depinfo.borrow_mut().insert((key, var));
351+
var
352+
}
353+
339354
// Returns all the paths that correspond to generated files.
340355
fn generated_output_paths(
341356
tcx: TyCtxt<'_>,
@@ -623,6 +638,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
623638
|tcx, _| tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal());
624639
providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1;
625640
providers.early_lint_checks = early_lint_checks;
641+
providers.env_var = env_var;
626642
proc_macro_decls::provide(providers);
627643
rustc_const_eval::provide(providers);
628644
rustc_middle::hir::provide(providers);

compiler/rustc_middle/src/query/erase.rs

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ trivial! {
253253
Option<rustc_span::def_id::DefId>,
254254
Option<rustc_span::def_id::LocalDefId>,
255255
Option<rustc_span::Span>,
256+
Option<rustc_span::Symbol>,
256257
Option<rustc_target::abi::FieldIdx>,
257258
Option<rustc_target::spec::PanicStrategy>,
258259
Option<usize>,

compiler/rustc_middle/src/query/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ rustc_queries! {
118118
desc { "perform lints prior to macro expansion" }
119119
}
120120

121+
/// Useful for the implementation of `std::env!`, `proc-macro`s change
122+
/// detection and target-dependent compiler flags.
123+
///
124+
/// Will emit an error and return `None` if the variable is not UTF-8.
125+
query env_var(key: Symbol) -> Option<Symbol> {
126+
// Environment variables are global state
127+
eval_always
128+
desc { "get the value of an environment variable" }
129+
}
130+
121131
query resolutions(_: ()) -> &'tcx ty::ResolverGlobalCtxt {
122132
no_hash
123133
desc { "getting the resolver outputs" }

0 commit comments

Comments
 (0)