Skip to content

Commit 5e70254

Browse files
committed
Auto merge of rust-lang#118830 - GuillaumeGomez:env-tracked_env, r=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`
2 parents 9022e6a + 453ff1e commit 5e70254

File tree

6 files changed

+56
-1
lines changed

6 files changed

+56
-1
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+4
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ impl server::Types for Rustc<'_, '_> {
426426
}
427427

428428
impl server::FreeFunctions for Rustc<'_, '_> {
429+
fn injected_env_var(&mut self, var: &str) -> Option<String> {
430+
self.ecx.sess.opts.logical_env.get(var).cloned()
431+
}
432+
429433
fn track_env_var(&mut self, var: &str, value: Option<&str>) {
430434
self.sess()
431435
.env_depinfo

library/proc_macro/src/bridge/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ macro_rules! with_api {
5555
$m! {
5656
FreeFunctions {
5757
fn drop($self: $S::FreeFunctions);
58+
fn injected_env_var(var: &str) -> Option<String>;
5859
fn track_env_var(var: &str, value: Option<&str>);
5960
fn track_path(path: &str);
6061
fn literal_from_str(s: &str) -> Result<Literal<$S::Span, $S::Symbol>, ()>;

library/proc_macro/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,8 @@ pub mod tracked_env {
15031503
#[unstable(feature = "proc_macro_tracked_env", issue = "99515")]
15041504
pub fn var<K: AsRef<OsStr> + AsRef<str>>(key: K) -> Result<String, VarError> {
15051505
let key: &str = key.as_ref();
1506-
let value = env::var(key);
1506+
let value = crate::bridge::client::FreeFunctions::injected_env_var(key)
1507+
.map_or_else(|| env::var(key), Ok);
15071508
crate::bridge::client::FreeFunctions::track_env_var(key, value.as_deref().ok());
15081509
value
15091510
}

src/tools/rust-analyzer/crates/proc-macro-srv/src/server.rs

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ impl server::Types for RustAnalyzer {
5858
}
5959

6060
impl server::FreeFunctions for RustAnalyzer {
61+
fn injected_env_var(&mut self, _var: &str) -> Option<String> {
62+
None
63+
}
64+
6165
fn track_env_var(&mut self, _var: &str, _value: Option<&str>) {
6266
// FIXME: track env var accesses
6367
// https://github.com/rust-lang/rust/pull/71858

tests/ui/proc-macro/auxiliary/env.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
#![feature(proc_macro_tracked_env)]
6+
7+
extern crate proc_macro;
8+
9+
use proc_macro::TokenStream;
10+
use proc_macro::tracked_env::var;
11+
12+
#[proc_macro]
13+
pub fn generate_const(input: TokenStream) -> TokenStream {
14+
let the_const = match var("THE_CONST") {
15+
Ok(x) if x == "12" => {
16+
"const THE_CONST: u32 = 12;"
17+
}
18+
_ => {
19+
"const THE_CONST: u32 = 0;"
20+
}
21+
};
22+
let another = if var("ANOTHER").is_ok() {
23+
"const ANOTHER: u32 = 1;"
24+
} else {
25+
"const ANOTHER: u32 = 2;"
26+
};
27+
format!("{the_const}{another}").parse().unwrap()
28+
}

tests/ui/proc-macro/env.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// aux-build:env.rs
2+
// run-pass
3+
// rustc-env: THE_CONST=1
4+
// compile-flags: -Zunstable-options --env THE_CONST=12 --env ANOTHER=4
5+
6+
#![crate_name = "foo"]
7+
8+
extern crate env;
9+
10+
use env::generate_const;
11+
12+
generate_const!();
13+
14+
fn main() {
15+
assert_eq!(THE_CONST, 12);
16+
assert_eq!(ANOTHER, 1);
17+
}

0 commit comments

Comments
 (0)