Skip to content

Commit

Permalink
Rollup merge of rust-lang#118830 - GuillaumeGomez:env-tracked_env, r=…
Browse files Browse the repository at this point in the history
…Nilstrieb

Add support for `--env` on `tracked_env::var`

Follow-up of rust-lang#118368.
Part of Part of rust-lang#80792.

It adds support of the `--env` option for proc-macros through `tracked_env::var`.

r? ``@Nilstrieb``
  • Loading branch information
GuillaumeGomez authored Dec 16, 2023
2 parents 9c6426b + 453ff1e commit 5281ecb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
4 changes: 4 additions & 0 deletions compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ impl server::Types for Rustc<'_, '_> {
}

impl server::FreeFunctions for Rustc<'_, '_> {
fn injected_env_var(&mut self, var: &str) -> Option<String> {
self.ecx.sess.opts.logical_env.get(var).cloned()
}

fn track_env_var(&mut self, var: &str, value: Option<&str>) {
self.sess()
.env_depinfo
Expand Down
1 change: 1 addition & 0 deletions library/proc_macro/src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ macro_rules! with_api {
$m! {
FreeFunctions {
fn drop($self: $S::FreeFunctions);
fn injected_env_var(var: &str) -> Option<String>;
fn track_env_var(var: &str, value: Option<&str>);
fn track_path(path: &str);
fn literal_from_str(s: &str) -> Result<Literal<$S::Span, $S::Symbol>, ()>;
Expand Down
3 changes: 2 additions & 1 deletion library/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,8 @@ pub mod tracked_env {
#[unstable(feature = "proc_macro_tracked_env", issue = "99515")]
pub fn var<K: AsRef<OsStr> + AsRef<str>>(key: K) -> Result<String, VarError> {
let key: &str = key.as_ref();
let value = env::var(key);
let value = crate::bridge::client::FreeFunctions::injected_env_var(key)
.map_or_else(|| env::var(key), Ok);
crate::bridge::client::FreeFunctions::track_env_var(key, value.as_deref().ok());
value
}
Expand Down
4 changes: 4 additions & 0 deletions src/tools/rust-analyzer/crates/proc-macro-srv/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ impl server::Types for RustAnalyzer {
}

impl server::FreeFunctions for RustAnalyzer {
fn injected_env_var(&mut self, _var: &str) -> Option<String> {
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
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/proc-macro/auxiliary/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// force-host
// no-prefer-dynamic

#![crate_type = "proc-macro"]
#![feature(proc_macro_tracked_env)]

extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro::tracked_env::var;

#[proc_macro]
pub fn generate_const(input: TokenStream) -> TokenStream {
let the_const = match var("THE_CONST") {
Ok(x) if x == "12" => {
"const THE_CONST: u32 = 12;"
}
_ => {
"const THE_CONST: u32 = 0;"
}
};
let another = if var("ANOTHER").is_ok() {
"const ANOTHER: u32 = 1;"
} else {
"const ANOTHER: u32 = 2;"
};
format!("{the_const}{another}").parse().unwrap()
}
17 changes: 17 additions & 0 deletions tests/ui/proc-macro/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// aux-build:env.rs
// run-pass
// rustc-env: THE_CONST=1
// compile-flags: -Zunstable-options --env THE_CONST=12 --env ANOTHER=4

#![crate_name = "foo"]

extern crate env;

use env::generate_const;

generate_const!();

fn main() {
assert_eq!(THE_CONST, 12);
assert_eq!(ANOTHER, 1);
}

0 comments on commit 5281ecb

Please sign in to comment.