From f64a0b3f5b6e133eff082481f8cb485357053613 Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Fri, 25 Feb 2022 16:11:53 -0800 Subject: [PATCH 1/6] add --env option --- compiler/rustc_builtin_macros/src/env.rs | 13 +++++++++++-- compiler/rustc_session/src/config.rs | 22 ++++++++++++++++++++++ compiler/rustc_session/src/options.rs | 5 +++++ src/test/ui/extenv/extenv-injected.rs | 8 ++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/extenv/extenv-injected.rs diff --git a/compiler/rustc_builtin_macros/src/env.rs b/compiler/rustc_builtin_macros/src/env.rs index b8828fa671a5d..52e0dfe475924 100644 --- a/compiler/rustc_builtin_macros/src/env.rs +++ b/compiler/rustc_builtin_macros/src/env.rs @@ -21,7 +21,12 @@ 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 injected_value = + cx.sess.opts.injected_env_vars.get(var.as_str()).map(|s| Symbol::intern(s)); + let value = + injected_value.or_else(|| env::var(var.as_str()).ok().as_deref().map(Symbol::intern)); + cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value)); let e = match value { None => { @@ -78,7 +83,11 @@ pub fn expand_env<'cx>( } let sp = cx.with_def_site_ctxt(sp); - let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern); + + let key = var.as_str(); + let injected_value = cx.sess.opts.injected_env_vars.get(key).map(|s| Symbol::intern(s)); + let value = injected_value.or_else(|| env::var(key).ok().as_deref().map(Symbol::intern)); + cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value)); let e = match value { None => { diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 6a8298605a23b..4b111e37ebf9f 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -744,6 +744,7 @@ impl Default for Options { json_future_incompat: false, pretty: None, working_dir: RealFileName::LocalPath(std::env::current_dir().unwrap()), + injected_env_vars: Default::default(), } } } @@ -1429,6 +1430,7 @@ pub fn rustc_optgroups() -> Vec { "Remap source names in all output (compiler messages and output files)", "FROM=TO", ), + opt::multi("", "env", "Inject an environment variable", "VAR=VALUE"), ]); opts } @@ -2197,6 +2199,23 @@ fn parse_remap_path_prefix( mapping } +fn parse_injected_env_vars( + matches: &getopts::Matches, + error_format: ErrorOutputType, +) -> FxHashMap { + let mut vars = FxHashMap::default(); + + for arg in matches.opt_strs("env") { + if let Some((name, val)) = arg.split_once('=') { + vars.insert(name.to_string(), val.to_string()); + } else { + early_error(error_format, &format!("`--env`: specify value for variable `{}`", arg)); + } + } + + vars +} + // JUSTIFICATION: before wrapper fn is available #[cfg_attr(not(bootstrap), allow(rustc::bad_opt_access))] pub fn build_session_options(matches: &getopts::Matches) -> Options { @@ -2470,6 +2489,8 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { RealFileName::LocalPath(path) }; + let injected_env_vars = parse_injected_env_vars(matches, error_format); + Options { assert_incr_state, crate_types, @@ -2506,6 +2527,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { json_future_incompat, pretty, working_dir, + injected_env_vars, } } diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 1827f1c208de7..8a9cb11ad4796 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -10,6 +10,8 @@ use rustc_target::spec::{ RelocModel, RelroLevel, SplitDebuginfo, StackProtector, TargetTriple, TlsModel, }; +use rustc_data_structures::fx::FxHashMap; + use rustc_feature::UnstableFeatures; use rustc_span::edition::Edition; use rustc_span::RealFileName; @@ -210,6 +212,9 @@ top_level_options!( /// The (potentially remapped) working directory working_dir: RealFileName [TRACKED], + + /// Overridden env vars used for `env!` and `option_env!` + injected_env_vars: FxHashMap [UNTRACKED], } ); diff --git a/src/test/ui/extenv/extenv-injected.rs b/src/test/ui/extenv/extenv-injected.rs new file mode 100644 index 0000000000000..00bd55f0d45de --- /dev/null +++ b/src/test/ui/extenv/extenv-injected.rs @@ -0,0 +1,8 @@ +// run-pass +// rustc_env: overridden=no +// compile-flags: -Z unstable-options --env x=y --env overridden=yes + +fn main() { + assert_eq!(env!("x"), "y"); + assert_eq!(env!("overridden"), "yes"); +} From 53a64d2baf080c83ce656b055ac3c2adb905d78b Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Tue, 26 Apr 2022 19:33:44 -0700 Subject: [PATCH 2/6] add support for proc macros --- compiler/rustc_expand/src/proc_macro_server.rs | 4 ++++ library/proc_macro/src/bridge/mod.rs | 1 + library/proc_macro/src/lib.rs | 5 ++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index 7d9a4aed0bf54..b4bad48b2acc0 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -375,6 +375,10 @@ impl server::Types for Rustc<'_, '_> { } impl server::FreeFunctions for Rustc<'_, '_> { + fn injected_env_var(&mut self, var: &str) -> Option { + self.ecx.sess.opts.injected_env_vars.get(var).cloned() + } + fn track_env_var(&mut self, var: &str, value: Option<&str>) { self.sess() .env_depinfo diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index 5cde966bf173d..bd101cbdabf38 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -54,6 +54,7 @@ macro_rules! with_api { $m! { FreeFunctions { fn drop($self: $S::FreeFunctions); + fn injected_env_var(var: &str) -> Option; fn track_env_var(var: &str, value: Option<&str>); fn track_path(path: &str); fn literal_from_str(s: &str) -> Result, ()>; diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 8e478cd7bc8a2..77afc6e478894 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -1505,7 +1505,10 @@ pub mod tracked_env { #[unstable(feature = "proc_macro_tracked_env", issue = "99515")] pub fn var + AsRef>(key: K) -> Result { let key: &str = key.as_ref(); - let value = env::var(key); + let injected_value = crate::bridge::client::FreeFunctions::injected_env_var(key); + let env_value = env::var(key); + + let value = injected_value.map_or_else(env_value, Ok); crate::bridge::client::FreeFunctions::track_env_var(key, value.as_deref().ok()); value } From 34d71d9a5fac3f51501668d80d13d49582ce8180 Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Tue, 26 Apr 2022 20:04:11 -0700 Subject: [PATCH 3/6] add --env to env-dep-info test --- library/proc_macro/src/lib.rs | 2 +- src/test/run-make/env-dep-info/Makefile | 8 ++++++-- src/test/run-make/env-dep-info/macro_def.rs | 2 ++ src/test/run-make/env-dep-info/main.rs | 2 ++ 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 77afc6e478894..4f28b521c3771 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -1508,7 +1508,7 @@ pub mod tracked_env { let injected_value = crate::bridge::client::FreeFunctions::injected_env_var(key); let env_value = env::var(key); - let value = injected_value.map_or_else(env_value, Ok); + let value = injected_value.map_or(env_value, Ok); crate::bridge::client::FreeFunctions::track_env_var(key, value.as_deref().ok()); value } diff --git a/src/test/run-make/env-dep-info/Makefile b/src/test/run-make/env-dep-info/Makefile index 25d9a31c2d6ff..5346bb869fbfa 100644 --- a/src/test/run-make/env-dep-info/Makefile +++ b/src/test/run-make/env-dep-info/Makefile @@ -7,13 +7,17 @@ ADDITIONAL_ARGS := $(RUSTFLAGS) endif all: - EXISTING_ENV=1 EXISTING_OPT_ENV=1 $(RUSTC) --emit dep-info main.rs + EXISTING_ENV=1 EXISTING_OPT_ENV=1 OVERRIDEN_INJECTED_ENV=0 $(RUSTC) --emit dep-info --env INJECTED_ENV=1 --env OVERRIDEN_ENV=1 main.rs $(CGREP) "# env-dep:EXISTING_ENV=1" < $(TMPDIR)/main.d $(CGREP) "# env-dep:EXISTING_OPT_ENV=1" < $(TMPDIR)/main.d $(CGREP) "# env-dep:NONEXISTENT_OPT_ENV" < $(TMPDIR)/main.d $(CGREP) "# env-dep:ESCAPE\nESCAPE\\" < $(TMPDIR)/main.d + $(CGREP) "# env-dep:INJECTED_ENV=1" < $(TMPDIR)/main.d + $(CGREP) "# env-dep:OVERRIDEN_ENV=1" < $(TMPDIR)/main.d # Proc macro $(BARE_RUSTC) $(ADDITIONAL_ARGS) --out-dir $(TMPDIR) macro_def.rs - EXISTING_PROC_MACRO_ENV=1 $(RUSTC) --emit dep-info macro_use.rs + EXISTING_PROC_MACRO_ENV=1 OVERRIDEN_PROC_MACRO_ENV=0 $(RUSTC) --emit dep-info --env INJECTED_PROC_MACRO_ENV=1 --env OVERRIDEN_PROC_MACRO_ENV=1 macro_use.rs $(CGREP) "# env-dep:EXISTING_PROC_MACRO_ENV=1" < $(TMPDIR)/macro_use.d $(CGREP) "# env-dep:NONEXISTENT_PROC_MACEO_ENV" < $(TMPDIR)/macro_use.d + $(CGREP) "# env-dep:INJECTED_PROC_MACRO_ENV=1" < $(TMPDIR)/macro_use.d + $(CGREP) "# env-dep:OVERRIDEN_PROC_MACRO_ENV=1" < $(TMPDIR)/macro_use.d diff --git a/src/test/run-make/env-dep-info/macro_def.rs b/src/test/run-make/env-dep-info/macro_def.rs index e328eae48326d..60ae8c8ffdc14 100644 --- a/src/test/run-make/env-dep-info/macro_def.rs +++ b/src/test/run-make/env-dep-info/macro_def.rs @@ -8,5 +8,7 @@ use proc_macro::*; pub fn access_env_vars(_: TokenStream) -> TokenStream { let _ = tracked_env::var("EXISTING_PROC_MACRO_ENV"); let _ = tracked_env::var("NONEXISTENT_PROC_MACEO_ENV"); + let _ = tracked_env::var("INJECTED_PROC_MACRO_ENV"); + let _ = tracked_env::var("OVERRIDEN_PROC_MACRO_ENV"); TokenStream::new() } diff --git a/src/test/run-make/env-dep-info/main.rs b/src/test/run-make/env-dep-info/main.rs index a25246bac792c..55002293e676d 100644 --- a/src/test/run-make/env-dep-info/main.rs +++ b/src/test/run-make/env-dep-info/main.rs @@ -3,4 +3,6 @@ fn main() { option_env!("EXISTING_OPT_ENV"); option_env!("NONEXISTENT_OPT_ENV"); option_env!("ESCAPE\nESCAPE\\"); + env!("INJECTED_ENV"); + env!("OVERRIDEN_ENV"); } From 5fad8815fb737060d23efd1b22007a3f10e65001 Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Tue, 26 Apr 2022 20:23:01 -0700 Subject: [PATCH 4/6] fix the test --- src/test/run-make/env-dep-info/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/run-make/env-dep-info/Makefile b/src/test/run-make/env-dep-info/Makefile index 5346bb869fbfa..ba05a11aefb03 100644 --- a/src/test/run-make/env-dep-info/Makefile +++ b/src/test/run-make/env-dep-info/Makefile @@ -7,7 +7,7 @@ ADDITIONAL_ARGS := $(RUSTFLAGS) endif all: - EXISTING_ENV=1 EXISTING_OPT_ENV=1 OVERRIDEN_INJECTED_ENV=0 $(RUSTC) --emit dep-info --env INJECTED_ENV=1 --env OVERRIDEN_ENV=1 main.rs + EXISTING_ENV=1 EXISTING_OPT_ENV=1 OVERRIDEN_INJECTED_ENV=0 $(RUSTC) --emit dep-info -Zunstable-options --env INJECTED_ENV=1 --env OVERRIDEN_ENV=1 main.rs $(CGREP) "# env-dep:EXISTING_ENV=1" < $(TMPDIR)/main.d $(CGREP) "# env-dep:EXISTING_OPT_ENV=1" < $(TMPDIR)/main.d $(CGREP) "# env-dep:NONEXISTENT_OPT_ENV" < $(TMPDIR)/main.d @@ -16,7 +16,7 @@ all: $(CGREP) "# env-dep:OVERRIDEN_ENV=1" < $(TMPDIR)/main.d # Proc macro $(BARE_RUSTC) $(ADDITIONAL_ARGS) --out-dir $(TMPDIR) macro_def.rs - EXISTING_PROC_MACRO_ENV=1 OVERRIDEN_PROC_MACRO_ENV=0 $(RUSTC) --emit dep-info --env INJECTED_PROC_MACRO_ENV=1 --env OVERRIDEN_PROC_MACRO_ENV=1 macro_use.rs + EXISTING_PROC_MACRO_ENV=1 OVERRIDEN_PROC_MACRO_ENV=0 $(RUSTC) --emit dep-info -Zunstable-options --env INJECTED_PROC_MACRO_ENV=1 --env OVERRIDEN_PROC_MACRO_ENV=1 macro_use.rs $(CGREP) "# env-dep:EXISTING_PROC_MACRO_ENV=1" < $(TMPDIR)/macro_use.d $(CGREP) "# env-dep:NONEXISTENT_PROC_MACEO_ENV" < $(TMPDIR)/macro_use.d $(CGREP) "# env-dep:INJECTED_PROC_MACRO_ENV=1" < $(TMPDIR)/macro_use.d From 223942d0e79adc796a07c173092bb0bbcf4d13de Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Wed, 27 Apr 2022 21:37:22 -0700 Subject: [PATCH 5/6] fix test --- src/test/run-make/env-dep-info/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/run-make/env-dep-info/Makefile b/src/test/run-make/env-dep-info/Makefile index ba05a11aefb03..881b59241afea 100644 --- a/src/test/run-make/env-dep-info/Makefile +++ b/src/test/run-make/env-dep-info/Makefile @@ -7,7 +7,7 @@ ADDITIONAL_ARGS := $(RUSTFLAGS) endif all: - EXISTING_ENV=1 EXISTING_OPT_ENV=1 OVERRIDEN_INJECTED_ENV=0 $(RUSTC) --emit dep-info -Zunstable-options --env INJECTED_ENV=1 --env OVERRIDEN_ENV=1 main.rs + EXISTING_ENV=1 EXISTING_OPT_ENV=1 OVERRIDEN_ENV=0 $(RUSTC) --emit dep-info -Zunstable-options --env INJECTED_ENV=1 --env OVERRIDEN_ENV=1 main.rs $(CGREP) "# env-dep:EXISTING_ENV=1" < $(TMPDIR)/main.d $(CGREP) "# env-dep:EXISTING_OPT_ENV=1" < $(TMPDIR)/main.d $(CGREP) "# env-dep:NONEXISTENT_OPT_ENV" < $(TMPDIR)/main.d From 8a9edfdee428b0da3650757ff59811488094a816 Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Mon, 1 Aug 2022 01:12:23 -0700 Subject: [PATCH 6/6] add method to rust-analyzer macro server --- .../crates/proc-macro-srv/src/abis/abi_sysroot/ra_server.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/tools/rust-analyzer/crates/proc-macro-srv/src/abis/abi_sysroot/ra_server.rs b/src/tools/rust-analyzer/crates/proc-macro-srv/src/abis/abi_sysroot/ra_server.rs index 46882845a8079..f3ab53f68bbfc 100644 --- a/src/tools/rust-analyzer/crates/proc-macro-srv/src/abis/abi_sysroot/ra_server.rs +++ b/src/tools/rust-analyzer/crates/proc-macro-srv/src/abis/abi_sysroot/ra_server.rs @@ -72,6 +72,10 @@ impl server::Types for RustAnalyzer { } impl server::FreeFunctions for RustAnalyzer { + fn injected_env_var(&mut self, _var: &str) -> Option { + None + } + fn track_env_var(&mut self, _var: &str, _value: Option<&str>) { // FIXME: track env var accesses // https://github.com/rust-lang/rust/pull/71858