Skip to content

Commit

Permalink
Add TyCtx::env_var
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 27, 2024
1 parent f2becdf commit 9ccebf6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_errors/src/diagnostic_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ impl IntoDiagArg for std::ffi::CString {
}
}

impl IntoDiagArg for std::ffi::OsString {
fn into_diag_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
}
}

impl IntoDiagArg for rustc_data_structures::small_c_str::SmallCStr {
fn into_diag_arg(self) -> DiagArgValue {
DiagArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_interface/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ interface_cant_emit_mir =
interface_emoji_identifier =
identifiers cannot contain emoji: `{$ident}`
interface_env_var_not_unicode =
cannot read environment variable "{$key}" with value "{$var}", since it contains non-unicode data
interface_error_writing_dependencies =
error writing dependencies to `{$path}`: {$error}
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_interface/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ffi::OsString;
use std::io;
use std::path::Path;

Expand All @@ -21,6 +22,13 @@ pub struct EmojiIdentifier {
pub ident: Symbol,
}

#[derive(Diagnostic)]
#[diag(interface_env_var_not_unicode)]
pub struct EnvVarNotUnicode {
pub key: Symbol,
pub var: OsString,
}

#[derive(Diagnostic)]
#[diag(interface_mixed_bin_crate)]
pub struct MixedBinCrate;
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::any::Any;
use std::env::VarError;
use std::ffi::OsString;
use std::io::{self, BufWriter, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -336,6 +337,20 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
)
}

fn env_var(tcx: TyCtxt<'_>, key: Symbol) -> Option<Symbol> {
let var = match std::env::var(key.as_str()) {
Ok(var) => Some(Symbol::intern(&var)),
Err(VarError::NotPresent) => None,
Err(VarError::NotUnicode(var)) => {
tcx.dcx().emit_err(errors::EnvVarNotUnicode { key, var });
None
}
};
// Also add the variable to Cargo's dependency tracking
tcx.sess.psess.env_depinfo.borrow_mut().insert((key, var));
var
}

// Returns all the paths that correspond to generated files.
fn generated_output_paths(
tcx: TyCtxt<'_>,
Expand Down Expand Up @@ -623,6 +638,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
|tcx, _| tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal());
providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1;
providers.early_lint_checks = early_lint_checks;
providers.env_var = env_var;
proc_macro_decls::provide(providers);
rustc_const_eval::provide(providers);
rustc_middle::hir::provide(providers);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/query/erase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ trivial! {
Option<rustc_span::def_id::DefId>,
Option<rustc_span::def_id::LocalDefId>,
Option<rustc_span::Span>,
Option<rustc_span::Symbol>,
Option<rustc_target::abi::FieldIdx>,
Option<rustc_target::spec::PanicStrategy>,
Option<usize>,
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ rustc_queries! {
desc { "perform lints prior to macro expansion" }
}

/// Useful for the implementation of `std::env!`, `proc-macro`s change
/// detection and target-dependent compiler flags.
///
/// Will emit an error and return `None` if the variable is not UTF-8.
query env_var(key: Symbol) -> Option<Symbol> {
// Environment variables are global state
eval_always
desc { "get the value of an environment variable" }
}

query resolutions(_: ()) -> &'tcx ty::ResolverGlobalCtxt {
no_hash
desc { "getting the resolver outputs" }
Expand Down

0 comments on commit 9ccebf6

Please sign in to comment.