diff options
| author | bors <bors@rust-lang.org> | 2023-12-10 21:48:53 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-12-10 21:48:53 +0000 |
| commit | d86d65bbc19b928387f68427fcc3a0da498d8a19 (patch) | |
| tree | 4cb4a17ba1d40e64b5a126b044742176c49eace9 /compiler/rustc_builtin_macros/src | |
| parent | ec4176167b964debee77015d0c67720639f9d673 (diff) | |
| parent | dc2f77aad617400b68ba607ca59bbf0121000b1f (diff) | |
Auto merge of #118368 - GuillaumeGomez:env-flag, r=Nilstrieb
Implement `--env` compiler flag (without `tracked_env` support)
Part of https://github.com/rust-lang/rust/issues/80792.
Implementation of https://github.com/rust-lang/compiler-team/issues/653.
Not an implementation of https://github.com/rust-lang/rfcs/pull/2794.
It adds the `--env` compiler flag option which allows to set environment values used by `env!` and `option_env!`.
Important to note: When trying to retrieve an environment variable value, it will first look into the ones defined with `--env`, and if there isn't one, then only it will look into the environment variables. So if you use `--env PATH=a`, then `env!("PATH")` will return `"a"` and not the actual `PATH` value.
As mentioned in the title, `tracked_env` support is not added here. I'll do it in a follow-up PR.
r? rust-lang/compiler
Diffstat (limited to 'compiler/rustc_builtin_macros/src')
| -rw-r--r-- | compiler/rustc_builtin_macros/src/env.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/compiler/rustc_builtin_macros/src/env.rs b/compiler/rustc_builtin_macros/src/env.rs index 8c2fa6ee95f..d772642b88b 100644 --- a/compiler/rustc_builtin_macros/src/env.rs +++ b/compiler/rustc_builtin_macros/src/env.rs @@ -13,6 +13,16 @@ use thin_vec::thin_vec; use crate::errors; +fn lookup_env<'cx>(cx: &'cx ExtCtxt<'_>, var: Symbol) -> Option<Symbol> { + let var = var.as_str(); + if let Some(value) = cx.sess.opts.logical_env.get(var) { + return Some(Symbol::intern(value)); + } + // If the environment variable was not defined with the `--env` option, we try to retrieve it + // from rustc's environment. + env::var(var).ok().as_deref().map(Symbol::intern) +} + pub fn expand_option_env<'cx>( cx: &'cx mut ExtCtxt<'_>, sp: Span, @@ -23,7 +33,7 @@ pub fn expand_option_env<'cx>( }; let sp = cx.with_def_site_ctxt(sp); - let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern); + let value = lookup_env(cx, var); cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value)); let e = match value { None => { @@ -77,7 +87,7 @@ pub fn expand_env<'cx>( }; let span = cx.with_def_site_ctxt(sp); - let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern); + let value = lookup_env(cx, var); cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value)); let e = match value { None => { |
