From 3d169bd3f4cdc2dc3dd09eadbbc17c19214d69f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Mon, 3 Aug 2020 13:57:04 +0300 Subject: Add track_env_var to the proc macro server --- .../proc_macro_srv/src/proc_macro/bridge/client.rs | 1 + crates/proc_macro_srv/src/proc_macro/bridge/mod.rs | 4 ++++ .../proc_macro_srv/src/proc_macro/bridge/server.rs | 2 ++ crates/proc_macro_srv/src/proc_macro/mod.rs | 22 ++++++++++++++++++++++ crates/proc_macro_srv/src/rustc_server.rs | 10 ++++++++++ xtask/src/install.rs | 2 +- 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/crates/proc_macro_srv/src/proc_macro/bridge/client.rs b/crates/proc_macro_srv/src/proc_macro/bridge/client.rs index cb4b3bdb0..55d6330cc 100644 --- a/crates/proc_macro_srv/src/proc_macro/bridge/client.rs +++ b/crates/proc_macro_srv/src/proc_macro/bridge/client.rs @@ -160,6 +160,7 @@ macro_rules! define_handles { } define_handles! { 'owned: + FreeFunctions, TokenStream, TokenStreamBuilder, TokenStreamIter, diff --git a/crates/proc_macro_srv/src/proc_macro/bridge/mod.rs b/crates/proc_macro_srv/src/proc_macro/bridge/mod.rs index aeb05aad4..b97886eb9 100644 --- a/crates/proc_macro_srv/src/proc_macro/bridge/mod.rs +++ b/crates/proc_macro_srv/src/proc_macro/bridge/mod.rs @@ -57,6 +57,10 @@ use std::thread; macro_rules! with_api { ($S:ident, $self:ident, $m:ident) => { $m! { + FreeFunctions { + fn drop($self: $S::FreeFunctions); + fn track_env_var(var: &str, value: Option<&str>); + }, TokenStream { fn drop($self: $S::TokenStream); fn clone($self: &$S::TokenStream) -> $S::TokenStream; diff --git a/crates/proc_macro_srv/src/proc_macro/bridge/server.rs b/crates/proc_macro_srv/src/proc_macro/bridge/server.rs index 45d41ac02..3acb239af 100644 --- a/crates/proc_macro_srv/src/proc_macro/bridge/server.rs +++ b/crates/proc_macro_srv/src/proc_macro/bridge/server.rs @@ -11,6 +11,8 @@ use super::client::HandleStore; /// Declare an associated item of one of the traits below, optionally /// adjusting it (i.e., adding bounds to types and default bodies to methods). macro_rules! associated_item { + (type FreeFunctions) => + (type FreeFunctions: 'static;); (type TokenStream) => (type TokenStream: 'static + Clone;); (type TokenStreamBuilder) => diff --git a/crates/proc_macro_srv/src/proc_macro/mod.rs b/crates/proc_macro_srv/src/proc_macro/mod.rs index ee0dc9722..fc6e7344f 100644 --- a/crates/proc_macro_srv/src/proc_macro/mod.rs +++ b/crates/proc_macro_srv/src/proc_macro/mod.rs @@ -924,3 +924,25 @@ impl fmt::Debug for Literal { self.0.fmt(f) } } + +pub mod tracked_env { + use std::env::{self, VarError}; + use std::ffi::OsStr; + + /// Retrieve an environment variable and add it to build dependency info. + /// Build system executing the compiler will know that the variable was accessed during + /// compilation, and will be able to rerun the build when the value of that variable changes. + /// Besides the dependency tracking this function should be equivalent to `env::var` from the + /// standard library, except that the argument must be UTF-8. + pub fn var + AsRef>(key: K) -> Result { + use std::ops::Deref; + + let key: &str = key.as_ref(); + let value = env::var(key); + super::bridge::client::FreeFunctions::track_env_var( + key, + value.as_ref().map(|t| t.deref()).ok(), + ); + value + } +} diff --git a/crates/proc_macro_srv/src/rustc_server.rs b/crates/proc_macro_srv/src/rustc_server.rs index 7d1695c86..c5fe3591e 100644 --- a/crates/proc_macro_srv/src/rustc_server.rs +++ b/crates/proc_macro_srv/src/rustc_server.rs @@ -242,6 +242,8 @@ impl TokenStreamBuilder { } } +pub struct FreeFunctions; + #[derive(Clone)] pub struct TokenStreamIter { trees: IntoIter, @@ -254,6 +256,7 @@ pub struct Rustc { } impl server::Types for Rustc { + type FreeFunctions = FreeFunctions; type TokenStream = TokenStream; type TokenStreamBuilder = TokenStreamBuilder; type TokenStreamIter = TokenStreamIter; @@ -267,6 +270,13 @@ impl server::Types for Rustc { type MultiSpan = Vec; } +impl server::FreeFunctions for Rustc { + fn track_env_var(&mut self, _var: &str, _value: Option<&str>) { + // FIXME: track env var accesses + // https://github.com/rust-lang/rust/pull/71858 + } +} + impl server::TokenStream for Rustc { fn new(&mut self) -> Self::TokenStream { Self::TokenStream::new() diff --git a/xtask/src/install.rs b/xtask/src/install.rs index d829790d7..fcc4f05e4 100644 --- a/xtask/src/install.rs +++ b/xtask/src/install.rs @@ -7,7 +7,7 @@ use anyhow::{bail, format_err, Context, Result}; use crate::not_bash::{pushd, run}; // Latest stable, feel free to send a PR if this lags behind. -const REQUIRED_RUST_VERSION: u32 = 46; +const REQUIRED_RUST_VERSION: u32 = 47; pub struct InstallCmd { pub client: Option, -- cgit v1.2.3